Unnamed patch
[org.ibex.jetty.git] / src / org / ibex / jetty / Jetty.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.jetty;
6 import org.mortbay.jetty.servlet.*;
7 import org.mortbay.jetty.*;
8 import org.mortbay.http.handler.*;
9 import org.mortbay.http.*;
10 import org.ibex.util.*;
11 import org.ibex.jinetd.*;
12 import org.ibex.io.*;
13 import java.io.*;
14 import java.net.*;
15 import java.util.*;
16
17 public class Jetty {
18
19     private static Jetty instance = null;
20     private static Server hs = null;
21     private static SocketListener sl = null;
22
23     public static synchronized Jetty instance() {
24         if (instance != null) return instance;
25         hs = new Server();
26         sl = new SocketListener();
27         hs.addListener(sl);
28         instance = new Jetty();
29         try {
30             ClassLoader cc = Thread.currentThread().getContextClassLoader();
31             Thread.currentThread().setContextClassLoader(Jetty.class.getClassLoader());
32             sl.setHttpServer(hs);
33             addContexts(org.ibex.jinetd.Main.ROOT + "/host", null);
34             hs.start();
35             Thread.currentThread().setContextClassLoader(cc);
36         } catch (Exception e) { Log.error(Main.class, e); }
37         return instance;
38     }
39
40     private static void addContexts(String path, String host) {
41         try {
42             File webinf = new File(path + "/WEB-INF");
43             if (webinf.exists()) {
44                 String pad = "";
45                 while(pad.length() + host.length() < 30) pad += " ";
46                 try {
47                     InetAddress.getByName(host);
48                     Log.info(Main.class, pad + host + " => " + path);
49                     WebApplicationContext context = new WebApplicationContext(path);
50                     context.setContextPath("");
51                     hs.addContext(host, context);
52                     context.getServletHandler().getHttpContext().setParentClassLoader(Jetty.class.getClassLoader());
53                     context.setClassLoaderJava2Compliant(true);
54                     context.setClassLoader(new TreeClassLoader(webinf, Jetty.class.getClassLoader()));
55                     context.setParentClassLoader(Jetty.class.getClassLoader());
56                     context.setResourceBase(path+"/");
57
58                     ServletHolder sh = context.addServlet("jsp", "*.jsp", "org.apache.jasper.servlet.JspServlet");
59                     sh.setInitParameter("fork", "false");
60                     sh.setInitParameter("mappedfile", "true");
61                     sh.setInitParameter("keepgenerated", "false");
62                     sh.setInitOrder(0);
63
64                     context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
65                     ServletHolder def = context.addServlet("default", "/", "org.mortbay.jetty.servlet.Default");
66                     def.setInitParameter("acceptRanges", "true");
67                     def.setInitParameter("dirAllowed", "true");
68                     def.setInitParameter("putAllowed", "false");
69                     def.setInitParameter("delAllowed", "false");
70                     def.setInitParameter("redirectWelcome", "false");
71                     def.setInitParameter("minGzipLength", "8192");
72                     def.setInitOrder(0);
73
74                     context.setDefaultsDescriptor(null);
75                     context.addHandler(new ResourceHandler());
76                     context.addHandler(new NotFoundHandler());
77                     context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
78
79                 } catch (UnknownHostException e) {
80                     Log.warn(Main.class, pad + host + " => " + e.getClass().getName());
81                 }
82                 //return;
83             }
84             File f = new File(path);
85             if (!f.isDirectory()) return;
86             String[] list = f.list();
87             for(int i=0; i<list.length; i++) {
88                 if (list[i].indexOf('.') != -1) continue;
89                 if (!list[i].toLowerCase().equals(list[i])) continue;
90                 addContexts(path + File.separatorChar + list[i], (host == null ? list[i] : (list[i] + "." + host)));
91             }
92         } catch (Exception e) {
93             Log.warn(Main.class, e);
94         }
95     }
96
97     public void accept(Connection conn) {
98         try {
99             try { sl.handleConnection(conn.getSocket()); } finally { conn.close(); }
100         } catch (Exception e) {
101             Log.error(this, e);
102             if (e instanceof javax.servlet.ServletException) Log.error(this, ((javax.servlet.ServletException)e).getRootCause());
103         }
104     }
105
106
107     // Logging //////////////////////////////////////////////////////////////////////////////
108
109     static { System.setProperty("org.apache.commons.logging.Log", "org.ibex.jetty.Jetty$LogAdapter"); }
110     public static class LogAdapter implements org.apache.commons.logging.Log {
111         public LogAdapter(String s) { }
112         public void trace(Object o)              { /* Log.debug("[jetty]", o); */ }
113         public void trace(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
114         public void debug(Object o)              { /* Log.debug("[jetty]", o); */ }
115         public void debug(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
116         public void info(Object o)               { /* Log.info("[jetty]", o); */ }
117         public void info(Object o, Throwable t)  { /* Log.info("[jetty]", o); Log.info("[jetty]", t); */ }
118         public void warn(Object o)               { Log.warn("[jetty]", o); }
119         public void warn(Object o, Throwable t)  { Log.warn("[jetty]", o); Log.warn("[jetty]", t); }
120         public void error(Object o)              { Log.error("[jetty]", o); }
121         public void error(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
122         public void fatal(Object o)              { Log.error("[jetty]", o); }
123         public void fatal(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
124         public boolean isTraceEnabled()          { return false; }
125         public boolean isDebugEnabled()          { return false; }
126         public boolean isInfoEnabled()           { return false; }
127         public boolean isWarnEnabled()           { return true; }
128         public boolean isErrorEnabled()          { return true; }
129         public boolean isFatalEnabled()          { return true; }
130     }
131
132 }