From: adam Date: Fri, 18 Mar 2005 09:11:22 +0000 (+0000) Subject: tons of changes X-Git-Url: http://git.megacz.com/?p=org.ibex.jinetd.git;a=commitdiff_plain;h=df13dadbee1a679518ccebbf417a11ec17c77aec tons of changes darcs-hash:20050318091122-5007d-b1a8d1b6ece6e776b6a865fff8fb89a2e2245b0f.gz --- diff --git a/src/org/ibex/jetty/Jetty.java b/src/org/ibex/jetty/Jetty.java new file mode 100644 index 0000000..b5fa5fe --- /dev/null +++ b/src/org/ibex/jetty/Jetty.java @@ -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 "+ (p.bindTo+":"+p.port)); p.spawn(bindTo, port); return p; } void spawn(InetAddress bindTo, int port) { new PortThread(bindTo, port).start(); } - private Port(String path, InetAddress bindTo, int port) { - super(path); - this.bindTo = bindTo; - this.port = port; - } + private Port(String path, InetAddress bindTo, int port, ThreadPool tp) { + super(path); this.bindTo = bindTo; this.port = port; this.tp = tp; } public void changed(Watched w) { //Log.warn(this, "Port: noticed change in " + w); super.changed(w); } - - Class isListener(String name) throws ClassNotFoundException { - final ClassLoader cl = getClassLoader(); - final Class c = cl.loadClass(name); - if (c == null) return null; - if (Listener.class.isAssignableFrom(c) && c != Listener.class) return c; - return null; - } - - Class findListener() throws Exception { - getClassLoader(); - String[] list = list(); - for(int i=0; i " + c.getName()); Log.clearnotes(); - final ClassLoader cl = getClassLoader(); Thread.currentThread().setContextClassLoader(cl); Listener l = (Listener)c.newInstance(); l.accept(conn); @@ -113,7 +59,7 @@ public class Port extends Loader { } finally { conn.close(); } - } }).start(); + } }); } private class PortThread extends Thread { diff --git a/src/org/ibex/jinetd/Root.java b/src/org/ibex/jinetd/Root.java index d89b04c..49bcf39 100644 --- a/src/org/ibex/jinetd/Root.java +++ b/src/org/ibex/jinetd/Root.java @@ -11,9 +11,12 @@ import java.net.*; public class Root extends Loader { public static String root = System.getProperty("jinetd.root", null); + private final Host host; private final Watched port; + private static final ThreadPool tp = new ThreadPool(10, 100); + public Root(String path) { super(path); host = new Host(path + File.separatorChar + "host", null); @@ -56,7 +59,8 @@ public class Root extends Loader { try { return Port.newPort(this.path + File.separatorChar + part, ipaddr == null ? null : InetAddress.getByName(ipaddr), - portnum.equals("*") ? 0 : Integer.parseInt(portnum)); + portnum.equals("*") ? 0 : Integer.parseInt(portnum), + tp); } catch (UnknownHostException e) { Log.warn(this, "can't resolve host for port directory: " + part); } catch (NumberFormatException e) { Log.warn(this, "invalid port directory: " + part); } catch (Exception e) { Log.warn(this, "error instantiating Port: " + part); diff --git a/src/org/ibex/jinetd/TreeClassLoader.java b/src/org/ibex/jinetd/TreeClassLoader.java new file mode 100644 index 0000000..e9f901f --- /dev/null +++ b/src/org/ibex/jinetd/TreeClassLoader.java @@ -0,0 +1,115 @@ +// 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.jinetd; +import org.ibex.io.*; +import org.ibex.util.*; +import java.io.*; +import java.util.*; +import java.text.*; +import java.net.*; +import java.util.zip.*; + +public class TreeClassLoader extends URLClassLoader { + + private final File root; + private final File lib; + private Hashtable cache = new Hashtable(); + + public TreeClassLoader(File root, ClassLoader parent) { + super(new URL[] { }, parent); + this.root = root; + this.lib = new File(root.getAbsolutePath() + File.separatorChar + "lib"); + } + + // Classloading ////////////////////////////////////////////////////////////////////////////// + + public URL[] getURLs() { + try { + Vec v = new Vec(); + if (getParent() != null && getParent() instanceof URLClassLoader) { + URL[] parentUrls = ((URLClassLoader)getParent()).getURLs(); + for(int i=0; i