tons of changes
[org.ibex.jinetd.git] / src / org / ibex / jetty / Jetty.java
diff --git a/src/org/ibex/jetty/Jetty.java b/src/org/ibex/jetty/Jetty.java
new file mode 100644 (file)
index 0000000..b5fa5fe
--- /dev/null
@@ -0,0 +1,130 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.jetty;
+import org.mortbay.jetty.servlet.*;
+import org.mortbay.jetty.*;
+import org.mortbay.http.handler.*;
+import org.mortbay.http.*;
+import org.ibex.util.*;
+import org.ibex.jinetd.*;
+import org.ibex.io.*;
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+public class Jetty {
+
+    private static Jetty instance = null;
+    private static Server hs = null;
+    private static SocketListener sl = null;
+
+    public static synchronized Jetty instance() {
+        if (instance != null) return instance;
+        hs = new Server();
+        sl = new SocketListener();
+        hs.addListener(sl);
+        instance = new Jetty();
+        try {
+            ClassLoader cc = Thread.currentThread().getContextClassLoader();
+            Thread.currentThread().setContextClassLoader(Jetty.class.getClassLoader());
+            sl.setHttpServer(hs);
+            addContexts(Root.root + "/host", null);
+            hs.start();
+            Thread.currentThread().setContextClassLoader(cc);
+        } catch (Exception e) { Log.error(Main.class, e); }
+        return instance;
+    }
+
+    private static void addContexts(String path, String host) {
+        try {
+            File webinf = new File(path + "/WEB-INF");
+            if (webinf.exists()) {
+                String pad = "";
+                while(pad.length() + host.length() < 30) pad += " ";
+                try {
+                    InetAddress.getByName(host);
+                    Log.info(Main.class, pad + host + " => " + path);
+                    WebApplicationContext context = hs.addWebApplication(host, "", path);
+                    context.getServletHandler().getHttpContext().setParentClassLoader(Jetty.class.getClassLoader());
+                    context.setClassLoaderJava2Compliant(true);
+                    context.setClassLoader(new TreeClassLoader(webinf, Jetty.class.getClassLoader()));
+                    context.setParentClassLoader(Jetty.class.getClassLoader());
+                    context.setResourceBase(path+"/");
+
+                    ServletHolder sh = context.addServlet("jsp", "*.jsp", "org.apache.jasper.servlet.JspServlet");
+                    sh.setInitParameter("fork", "false");
+                    sh.setInitParameter("mappedfile", "true");
+                    sh.setInitParameter("keepgenerated", "false");
+                    sh.setInitOrder(0);
+
+                    context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
+                    ServletHolder def = context.addServlet("default", "/", "org.mortbay.jetty.servlet.Default");
+                    def.setInitParameter("acceptRanges", "true");
+                    def.setInitParameter("dirAllowed", "true");
+                    def.setInitParameter("putAllowed", "false");
+                    def.setInitParameter("delAllowed", "false");
+                    def.setInitParameter("redirectWelcome", "false");
+                    def.setInitParameter("minGzipLength", "8192");
+                    def.setInitOrder(0);
+
+                    context.setDefaultsDescriptor(null);
+                    context.addHandler(new ResourceHandler());
+                    context.addHandler(new NotFoundHandler());
+                    context.setWelcomeFiles(new String[] { "index.jsp", "index.html", "index.xt", "index.txt" });
+
+                } catch (UnknownHostException e) {
+                    Log.warn(Main.class, pad + host + " => " + e.getClass().getName());
+                }
+                //return;
+            }
+            File f = new File(path);
+            if (!f.isDirectory()) return;
+            String[] list = f.list();
+            for(int i=0; i<list.length; i++) {
+                if (list[i].indexOf('.') != -1) continue;
+                if (!list[i].toLowerCase().equals(list[i])) continue;
+                addContexts(path + File.separatorChar + list[i], (host == null ? list[i] : (list[i] + "." + host)));
+            }
+        } catch (Exception e) {
+            Log.warn(Main.class, e);
+        }
+    }
+
+    public void accept(Connection conn) {
+        try {
+            try { sl.handleConnection(conn.getSocket()); } finally { conn.close(); }
+        } catch (Exception e) {
+            Log.error(this, e);
+            if (e instanceof javax.servlet.ServletException) Log.error(this, ((javax.servlet.ServletException)e).getRootCause());
+        }
+    }
+
+
+    // Logging //////////////////////////////////////////////////////////////////////////////
+
+    static { System.setProperty("org.apache.commons.logging.Log", "org.ibex.jetty.Jetty$LogAdapter"); }
+    public static class LogAdapter implements org.apache.commons.logging.Log {
+        public LogAdapter(String s) { }
+        public void trace(Object o)              { /* Log.debug("[jetty]", o); */ }
+        public void trace(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
+        public void debug(Object o)              { /* Log.debug("[jetty]", o); */ }
+        public void debug(Object o, Throwable t) { /* Log.debug("[jetty]", o); Log.debug("[jetty]", t); */ }
+        public void info(Object o)               { /* Log.info("[jetty]", o); */ }
+        public void info(Object o, Throwable t)  { /* Log.info("[jetty]", o); Log.info("[jetty]", t); */ }
+        public void warn(Object o)               { Log.warn("[jetty]", o); }
+        public void warn(Object o, Throwable t)  { Log.warn("[jetty]", o); Log.warn("[jetty]", t); }
+        public void error(Object o)              { Log.error("[jetty]", o); }
+        public void error(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
+        public void fatal(Object o)              { Log.error("[jetty]", o); }
+        public void fatal(Object o, Throwable t) { Log.error("[jetty]", o); Log.error("[jetty]", t); }
+        public boolean isTraceEnabled()          { return false; }
+        public boolean isDebugEnabled()          { return false; }
+        public boolean isInfoEnabled()           { return false; }
+        public boolean isWarnEnabled()           { return true; }
+        public boolean isErrorEnabled()          { return true; }
+        public boolean isFatalEnabled()          { return true; }
+    }
+
+}