hackage
[org.ibex.jetty.git] / src / org / ibex / jetty / Jetty.java
index 3fc4fc5..d84a6fb 100644 (file)
@@ -27,71 +27,71 @@ public class Jetty {
         hs.addListener(sl);
         instance = new Jetty();
         try {
-            ClassLoader cc = Thread.currentThread().getContextClassLoader();
-            Thread.currentThread().setContextClassLoader(Jetty.class.getClassLoader());
             sl.setHttpServer(hs);
-            addContexts(org.ibex.jinetd.Main.ROOT + "/host", null);
+            Host.init();
+            Enumeration e = Host.enumerateHosts();
+            while(e.hasMoreElements()) {
+                Host host = (Host)e.nextElement();
+                Log.warn("", "scanning host: " + host.hostname + " at " + host.path);
+                try {
+                    createContext(host, host.path, 0);
+                } catch (Exception ex) {
+                    Log.warn(Jetty.class, ex);
+                }
+            }
             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 = new WebApplicationContext(path);
-                   context.setContextPath("");
-                    hs.addContext(host, context);
-                    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());
+    private static void createContext(Host host, String path, int depth) throws Exception {
+        Log.warn(host, "scanning " + path);
+        if (!new File(path+"/WEB-INF/web.xml").exists()) {
+            if (depth >= 4) return;
+            for(String s : new File(path).list()) {
+                if (new File(path+"/"+s).isDirectory()) {
+                    try {
+                        createContext(host, path+"/"+s, depth+1);
+                    } catch (Exception ex) {
+                        Log.warn(Jetty.class, ex);
+                    }
                 }
-                //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);
+            return;
         }
+        WebApplicationContext context = new WebApplicationContext(path);
+        String webPath = path.substring(host.path.length());
+        if (webPath.endsWith("/_")) webPath = webPath.substring(0, webPath.length()-1);
+        if (webPath.endsWith("/_/")) webPath = webPath.substring(0, webPath.length()-2);
+        System.out.println("mapping " + webPath + " as " + path);
+        context.setContextPath(webPath);
+        hs.addContext(host.hostname, context);
+        //context.getServletHandler().getHttpContext().setParentClassLoader(Jetty.class.getClassLoader());   // ???
+        context.setClassLoaderJava2Compliant(true);
+        context.setClassLoader(new TreeClassLoader(new File(path+"/WEB-INF"), host));
+        //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" });
     }
 
     public void accept(Connection conn) {