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.
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.*;
19 private static Jetty instance = null;
20 private static Server hs = null;
21 private static SocketListener sl = null;
23 public static synchronized Jetty instance() {
24 if (instance != null) return instance;
26 sl = new SocketListener();
28 instance = new Jetty();
30 ClassLoader cc = Thread.currentThread().getContextClassLoader();
31 Thread.currentThread().setContextClassLoader(Jetty.class.getClassLoader());
33 addContexts(Root.root + "/host", null);
35 Thread.currentThread().setContextClassLoader(cc);
36 } catch (Exception e) { Log.error(Main.class, e); }
40 private static void addContexts(String path, String host) {
42 File webinf = new File(path + "/WEB-INF");
43 if (webinf.exists()) {
45 while(pad.length() + host.length() < 30) pad += " ";
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+"/");
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");
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");
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" });
77 } catch (UnknownHostException e) {
78 Log.warn(Main.class, pad + host + " => " + e.getClass().getName());
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)));
90 } catch (Exception e) {
91 Log.warn(Main.class, e);
95 public void accept(Connection conn) {
97 try { sl.handleConnection(conn.getSocket()); } finally { conn.close(); }
98 } catch (Exception e) {
100 if (e instanceof javax.servlet.ServletException) Log.error(this, ((javax.servlet.ServletException)e).getRootCause());
105 // Logging //////////////////////////////////////////////////////////////////////////////
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; }