Added initial SCGI server and a handler for serving JARs from the filesystem.
[jsvc.git] / src / dolda / jsvc / util / JarContext.java
CommitLineData
13e578b1
FT
1package dolda.jsvc.util;
2
3import java.io.*;
4import java.util.*;
5import java.net.*;
6import dolda.jsvc.*;
7
8public class JarContext implements ServerContext {
9 private final long ctime;
10 private final String name;
11 public final ClassLoader loader;
12 protected final Properties sysconfig, libconfig;
13
14 private static String mangle(File f) {
15 String ret = f.getName();
16 int p = ret.lastIndexOf('.');
17 if(p > 0)
18 ret = ret.substring(0, p);
19 for(f = f.getParentFile(); f != null; f = f.getParentFile())
20 ret = f.getName() + "/" + ret;
21 return(ret);
22 }
23
24 private void loadconfig() {
25 try {
26 InputStream pi = loader.getResourceAsStream("jsvc.properties");
27 if(pi != null) {
28 try {
29 libconfig.load(pi);
30 } finally {
31 pi.close();
32 }
33 }
34 } catch(IOException e) {
35 throw(new Error(e));
36 }
37 }
38
39 public Class<?> findboot() {
40 String clnm = libconfig("jsvc.bootstrap", null);
41 if(clnm == null)
42 return(null);
43 Class<?> bc;
44 try {
45 bc = loader.loadClass(clnm);
46 } catch(ClassNotFoundException e) {
47 return(null);
48 }
49 return(bc);
50 }
51
52 public JarContext(ClassLoader cl, String name) {
53 this.ctime = System.currentTimeMillis();
54 this.name = name;
55 this.loader = cl;
56 sysconfig = new Properties();
57 libconfig = new Properties();
58
59 loadconfig();
60 }
61
62 private static URL makingmewanttokilljavac(File jar) {
63 try {
64 return(jar.toURI().toURL());
65 } catch(MalformedURLException e) {
66 throw(new RuntimeException(e));
67 }
68 }
69
70 public JarContext(File jar) {
71 this(new URLClassLoader(new URL[] {makingmewanttokilljavac(jar)}, JarContext.class.getClassLoader()), mangle(jar));
72 }
73
74 public long starttime() {
75 return(ctime);
76 }
77
78 public String name() {
79 return(name);
80 }
81
82 public String sysconfig(String key, String def) {
83 return(sysconfig.getProperty(key, def));
84 }
85
86 public String libconfig(String key, String def) {
87 return(libconfig.getProperty(key, def));
88 }
89
90 public RequestThread worker(Responder root, Request req, ThreadGroup tg, String name) {
91 return(new RequestThread(root, req, tg, name));
92 }
93}