Only supported on Tomcat so far.
public interface ServerContext {
public long starttime();
public String config(String key);
+ public String name();
}
import dolda.jsvc.*;
import dolda.jsvc.util.*;
import javax.servlet.*;
-import javax.servlet.http.*;
import java.util.*;
import java.io.*;
-public class J2eeContext implements ServerContext {
+public abstract class J2eeContext implements ServerContext {
private final ServletConfig sc;
private final long ctime;
- private final Properties config;
+ protected final Properties config;
- J2eeContext(ServletConfig sc) {
+ protected J2eeContext(ServletConfig sc) {
this.sc = sc;
this.ctime = System.currentTimeMillis();
- config = readconfig(sc);
+ config = new Properties();
}
- private static void loadprops(Properties props, File pfile) {
- if(!pfile.exists())
- return;
- try {
- InputStream in = new FileInputStream(pfile);
- try {
- props.load(in);
- } finally {
- in.close();
- }
- } catch(IOException e) {
- throw(new RuntimeException(e));
- }
- }
-
- private static Properties readconfig(ServletConfig sc) {
- /* This only works on Tomcat now, but that's because I've no
- * idea how other J2EE servers work. I don't even know if they
- * _have_ any reasonable place to put configuration and
- * storage. */
- Properties cfg = new Properties();
- String basename = System.getProperty("catalina.base");
- if(basename != null) {
- File base = new File(basename);
- cfg.put("jsvc.storage", new File(new File(base, "work"), "jsvc").getPath());
- File cdir = new File(base, "conf");
- loadprops(cfg, new File(cdir, "jsvc.properties"));
- }
- return(cfg);
+ static J2eeContext create(ServletConfig sc) {
+ if(TomcatContext.tomcatp(sc))
+ return(new TomcatContext(sc));
+ return(new StandardContext(sc));
}
public long starttime() {
} catch(ClassNotFoundException e) {
throw(new ServletException("Invalid JSvc bootstrapper specified", e));
}
- tg = new ThreadContext(null, "JSvc service", new J2eeContext(cfg), bc);
+ tg = new ThreadContext(null, "JSvc service", J2eeContext.create(cfg), bc);
}
public void destroy() {
--- /dev/null
+package dolda.jsvc.j2ee;
+
+import dolda.jsvc.*;
+import javax.servlet.*;
+
+public class StandardContext extends J2eeContext {
+ StandardContext(ServletConfig cfg) {
+ super(cfg);
+ }
+
+ public String name() {
+ return(null);
+ }
+}
--- /dev/null
+package dolda.jsvc.j2ee;
+
+import dolda.jsvc.*;
+import dolda.jsvc.util.*;
+import javax.servlet.*;
+import java.lang.reflect.*;
+import java.util.*;
+import java.io.*;
+
+public class TomcatContext extends J2eeContext {
+ private final String name;
+
+ TomcatContext(ServletConfig sc) {
+ super(sc);
+ ServletContext ctx = j2eeconfig().getServletContext();
+ Class<?> cclass = ctx.getClass();
+ try {
+ Method cpm = cclass.getMethod("getContextPath");
+ name = Misc.stripslashes((String)cpm.invoke(ctx), true, true);
+ } catch(NoSuchMethodException e) {
+ throw(new RuntimeException("Could not fetch context path from Tomcat", e));
+ } catch(IllegalAccessException e) {
+ throw(new RuntimeException("Could not fetch context path from Tomcat", e));
+ } catch(InvocationTargetException e) {
+ throw(new RuntimeException("Could not fetch context path from Tomcat", e));
+ }
+ readconfig();
+ }
+
+ private static void loadprops(Properties props, File pfile) {
+ if(!pfile.exists())
+ return;
+ try {
+ InputStream in = new FileInputStream(pfile);
+ try {
+ props.load(in);
+ } finally {
+ in.close();
+ }
+ } catch(IOException e) {
+ throw(new RuntimeException(e));
+ }
+ }
+
+ private void readconfig() {
+ String basename = System.getProperty("catalina.base");
+ File base = new File(basename);
+ config.put("jsvc.storage", "file:" + new File(new File(base, "work"), "jsvc").getPath());
+ File cdir = new File(base, "conf");
+ loadprops(config, new File(cdir, "jsvc.properties"));
+ }
+
+ public static boolean tomcatp(ServletConfig sc) {
+ ServletContext ctx = sc.getServletContext();
+ if(ctx.getClass().getName().equals("org.apache.catalina.core.ApplicationContextFacade"))
+ return(true);
+ return(false);
+ }
+
+ public String name() {
+ return(name);
+ }
+}