tons of changes
[org.ibex.jinetd.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(Root.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 = hs.addWebApplication(host, "", path);
50                     context.getServletHandler().getHttpContext().setParentClassLoader(Jetty.class.getClassLoader());
51                     context.setClassLoaderJava2Compliant(true);
52                     context.setClassLoader(new TreeClassLoader(webinf, Jetty.class.getClassLoader()));
53                     context.setParentClassLoader(Jetty.class.getClassLoader());
54                     context.setResourceBase(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                 } catch (UnknownHostException e) {
78                     Log.warn(Main.class, pad + host + " => " + e.getClass().getName());
79                 }
80                 //return;
81             }
82             File f = new File(path);
83             if (!f.isDirectory()) return;
84             String[] list = f.list();
85             for(int i=0; i<list.length; i++) {
86                 if (list[i].indexOf('.') != -1) continue;
87                 if (!list[i].toLowerCase().equals(list[i])) continue;
88                 addContexts(path + File.separatorChar + list[i], (host == null ? list[i] : (list[i] + "." + host)));
89             }
90         } catch (Exception e) {
91             Log.warn(Main.class, e);
92         }
93     }
94
95     public void accept(Connection conn) {
96         try {
97             try { sl.handleConnection(conn.getSocket()); } finally { conn.close(); }
98         } catch (Exception e) {
99             Log.error(this, e);
100             if (e instanceof javax.servlet.ServletException) Log.error(this, ((javax.servlet.ServletException)e).getRootCause());
101         }
102     }
103
104
105     // Logging //////////////////////////////////////////////////////////////////////////////
106
107     static { System.setProperty("org.apache.commons.logging.Log", "org.ibex.jetty.Jetty$LogAdapter"); }
108     public static class LogAdapter implements org.apache.commons.logging.Log {
109         public LogAdapter(String s) { }
110         public void trace(Object o)              { /* Log.debug("[jetty]", o); */ }
111         public void trace(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
112         public void debug(Object o)              { /* Log.debug("[jetty]", o); */ }
113         public void debug(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
114         public void info(Object o)               { /* Log.info("[jetty]", o); */ }
115         public void info(Object o, Throwable t)  { /* Log.info("[jetty]", o); Log.info("[jetty]", t); */ }
116         public void warn(Object o)               { Log.warn("[jetty]", o); }
117         public void warn(Object o, Throwable t)  { Log.warn("[jetty]", o); Log.warn("[jetty]", t); }
118         public void error(Object o)              { Log.error("[jetty]", o); }
119         public void error(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
120         public void fatal(Object o)              { Log.error("[jetty]", o); }
121         public void fatal(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
122         public boolean isTraceEnabled()          { return false; }
123         public boolean isDebugEnabled()          { return false; }
124         public boolean isInfoEnabled()           { return false; }
125         public boolean isWarnEnabled()           { return true; }
126         public boolean isErrorEnabled()          { return true; }
127         public boolean isFatalEnabled()          { return true; }
128     }
129
130 }