cba21a9b8854b8993e4189d4fef56e0a9e8b994f
[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                 try {
36                     createContext(host);
37                 } catch (Exception ex) {
38                     Log.warn(Jetty.class, ex);
39                 }
40             }
41             hs.start();
42         } catch (Exception e) { Log.error(Main.class, e); }
43         return instance;
44     }
45
46     private static void createContext(Host host) throws Exception {
47         WebApplicationContext context = new WebApplicationContext(host.path);
48         context.setContextPath("");
49         hs.addContext(host.hostname, context);
50         //context.getServletHandler().getHttpContext().setParentClassLoader(Jetty.class.getClassLoader());   // ???
51         context.setClassLoaderJava2Compliant(true);
52         context.setClassLoader(host);
53         //context.setParentClassLoader(Jetty.class.getClassLoader());
54         context.setResourceBase(host.path+"/");
55         
56         ServletHolder sh = context.addServlet("jsp", "*.jsp", "org.apache.jasper.servlet.JspServlet");
57         sh.setInitParameter("fork", "false");
58         sh.setInitParameter("mappedfile", "true");
59         sh.setInitParameter("keepgenerated", "false");
60         sh.setInitOrder(0);
61
62         context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
63         ServletHolder def = context.addServlet("default", "/", "org.mortbay.jetty.servlet.Default");
64         def.setInitParameter("acceptRanges", "true");
65         def.setInitParameter("dirAllowed", "true");
66         def.setInitParameter("putAllowed", "false");
67         def.setInitParameter("delAllowed", "false");
68         def.setInitParameter("redirectWelcome", "false");
69         def.setInitParameter("minGzipLength", "8192");
70         def.setInitOrder(0);
71         
72         context.setDefaultsDescriptor(null);
73         context.addHandler(new ResourceHandler());
74         context.addHandler(new NotFoundHandler());
75         context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
76     }
77
78     public void accept(Connection conn) {
79         try {
80             try { sl.handleConnection(conn.getSocket()); } finally { conn.close(); }
81         } catch (Exception e) {
82             Log.error(this, e);
83             if (e instanceof javax.servlet.ServletException) Log.error(this, ((javax.servlet.ServletException)e).getRootCause());
84         }
85     }
86
87
88     // Logging //////////////////////////////////////////////////////////////////////////////
89
90     static { System.setProperty("org.apache.commons.logging.Log", "org.ibex.jetty.Jetty$LogAdapter"); }
91     public static class LogAdapter implements org.apache.commons.logging.Log {
92         public LogAdapter(String s) { }
93         public void trace(Object o)              { /* Log.debug("[jetty]", o); */ }
94         public void trace(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
95         public void debug(Object o)              { /* Log.debug("[jetty]", o); */ }
96         public void debug(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
97         public void info(Object o)               { /* Log.info("[jetty]", o); */ }
98         public void info(Object o, Throwable t)  { /* Log.info("[jetty]", o); Log.info("[jetty]", t); */ }
99         public void warn(Object o)               { Log.warn("[jetty]", o); }
100         public void warn(Object o, Throwable t)  { Log.warn("[jetty]", o); Log.warn("[jetty]", t); }
101         public void error(Object o)              { Log.error("[jetty]", o); }
102         public void error(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
103         public void fatal(Object o)              { Log.error("[jetty]", o); }
104         public void fatal(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
105         public boolean isTraceEnabled()          { return false; }
106         public boolean isDebugEnabled()          { return false; }
107         public boolean isInfoEnabled()           { return false; }
108         public boolean isWarnEnabled()           { return true; }
109         public boolean isErrorEnabled()          { return true; }
110         public boolean isFatalEnabled()          { return true; }
111     }
112
113 }