hackage
[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             sl.setHttpServer(hs);
31             Host.init();
32             Enumeration e = Host.enumerateHosts();
33             while(e.hasMoreElements()) {
34                 Host host = (Host)e.nextElement();
35                 Log.warn("", "scanning host: " + host.hostname + " at " + host.path);
36                 try {
37                     createContext(host, host.path, 0);
38                 } catch (Exception ex) {
39                     Log.warn(Jetty.class, ex);
40                 }
41             }
42             hs.start();
43         } catch (Exception e) { Log.error(Main.class, e); }
44         return instance;
45     }
46
47     private static void createContext(Host host, String path, int depth) throws Exception {
48         Log.warn(host, "scanning " + path);
49         if (!new File(path+"/WEB-INF/web.xml").exists()) {
50             if (depth >= 4) return;
51             for(String s : new File(path).list()) {
52                 if (new File(path+"/"+s).isDirectory()) {
53                     try {
54                         createContext(host, path+"/"+s, depth+1);
55                     } catch (Exception ex) {
56                         Log.warn(Jetty.class, ex);
57                     }
58                 }
59             }
60             return;
61         }
62         WebApplicationContext context = new WebApplicationContext(path);
63         String webPath = path.substring(host.path.length());
64         if (webPath.endsWith("/_")) webPath = webPath.substring(0, webPath.length()-1);
65         if (webPath.endsWith("/_/")) webPath = webPath.substring(0, webPath.length()-2);
66         System.out.println("mapping " + webPath + " as " + path);
67         context.setContextPath(webPath);
68         hs.addContext(host.hostname, context);
69         //context.getServletHandler().getHttpContext().setParentClassLoader(Jetty.class.getClassLoader());   // ???
70         context.setClassLoaderJava2Compliant(true);
71         context.setClassLoader(new TreeClassLoader(new File(path+"/WEB-INF"), host));
72         //context.setParentClassLoader(Jetty.class.getClassLoader());
73         context.setResourceBase(path+"/");
74         
75         ServletHolder sh = context.addServlet("jsp", "*.jsp", "org.apache.jasper.servlet.JspServlet");
76         sh.setInitParameter("fork", "false");
77         sh.setInitParameter("mappedfile", "true");
78         sh.setInitParameter("keepgenerated", "false");
79         sh.setInitOrder(0);
80
81         context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
82         ServletHolder def = context.addServlet("default", "/", "org.mortbay.jetty.servlet.Default");
83         def.setInitParameter("acceptRanges", "true");
84         def.setInitParameter("dirAllowed", "true");
85         def.setInitParameter("putAllowed", "false");
86         def.setInitParameter("delAllowed", "false");
87         def.setInitParameter("redirectWelcome", "false");
88         def.setInitParameter("minGzipLength", "8192");
89         def.setInitOrder(0);
90         
91         context.setDefaultsDescriptor(null);
92         context.addHandler(new ResourceHandler());
93         context.addHandler(new NotFoundHandler());
94         context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
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 }