X-Git-Url: http://dolda2000.com/gitweb/?p=jsvc.git;a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Futil%2FJarContext.java;fp=src%2Fdolda%2Fjsvc%2Futil%2FJarContext.java;h=cb16d37c187fb3b58b630ad8af43a47ba78156cc;hp=0000000000000000000000000000000000000000;hb=13e578b10b388cc0dea88e05b79265c21416e3a0;hpb=b560fc1c45ee31c6d509781b53d5934121990189 diff --git a/src/dolda/jsvc/util/JarContext.java b/src/dolda/jsvc/util/JarContext.java new file mode 100644 index 0000000..cb16d37 --- /dev/null +++ b/src/dolda/jsvc/util/JarContext.java @@ -0,0 +1,93 @@ +package dolda.jsvc.util; + +import java.io.*; +import java.util.*; +import java.net.*; +import dolda.jsvc.*; + +public class JarContext implements ServerContext { + private final long ctime; + private final String name; + public final ClassLoader loader; + protected final Properties sysconfig, libconfig; + + private static String mangle(File f) { + String ret = f.getName(); + int p = ret.lastIndexOf('.'); + if(p > 0) + ret = ret.substring(0, p); + for(f = f.getParentFile(); f != null; f = f.getParentFile()) + ret = f.getName() + "/" + ret; + return(ret); + } + + private void loadconfig() { + try { + InputStream pi = loader.getResourceAsStream("jsvc.properties"); + if(pi != null) { + try { + libconfig.load(pi); + } finally { + pi.close(); + } + } + } catch(IOException e) { + throw(new Error(e)); + } + } + + public Class findboot() { + String clnm = libconfig("jsvc.bootstrap", null); + if(clnm == null) + return(null); + Class bc; + try { + bc = loader.loadClass(clnm); + } catch(ClassNotFoundException e) { + return(null); + } + return(bc); + } + + public JarContext(ClassLoader cl, String name) { + this.ctime = System.currentTimeMillis(); + this.name = name; + this.loader = cl; + sysconfig = new Properties(); + libconfig = new Properties(); + + loadconfig(); + } + + private static URL makingmewanttokilljavac(File jar) { + try { + return(jar.toURI().toURL()); + } catch(MalformedURLException e) { + throw(new RuntimeException(e)); + } + } + + public JarContext(File jar) { + this(new URLClassLoader(new URL[] {makingmewanttokilljavac(jar)}, JarContext.class.getClassLoader()), mangle(jar)); + } + + public long starttime() { + return(ctime); + } + + public String name() { + return(name); + } + + public String sysconfig(String key, String def) { + return(sysconfig.getProperty(key, def)); + } + + public String libconfig(String key, String def) { + return(libconfig.getProperty(key, def)); + } + + public RequestThread worker(Responder root, Request req, ThreadGroup tg, String name) { + return(new RequestThread(root, req, tg, name)); + } +}