From: Fredrik Tolf Date: Thu, 15 Oct 2009 23:07:40 +0000 (+0200) Subject: Added an optional server-context name fetching operation. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=762009abaf3acc38003c9635115c721c602bdaa5;p=jsvc.git Added an optional server-context name fetching operation. Only supported on Tomcat so far. --- diff --git a/src/dolda/jsvc/ServerContext.java b/src/dolda/jsvc/ServerContext.java index 81cb4cd..acb27d6 100644 --- a/src/dolda/jsvc/ServerContext.java +++ b/src/dolda/jsvc/ServerContext.java @@ -3,4 +3,5 @@ package dolda.jsvc; public interface ServerContext { public long starttime(); public String config(String key); + public String name(); } diff --git a/src/dolda/jsvc/j2ee/J2eeContext.java b/src/dolda/jsvc/j2ee/J2eeContext.java index 5049d26..bf1917c 100644 --- a/src/dolda/jsvc/j2ee/J2eeContext.java +++ b/src/dolda/jsvc/j2ee/J2eeContext.java @@ -3,50 +3,24 @@ package dolda.jsvc.j2ee; 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() { diff --git a/src/dolda/jsvc/j2ee/Servlet.java b/src/dolda/jsvc/j2ee/Servlet.java index 14ace40..2b149c9 100644 --- a/src/dolda/jsvc/j2ee/Servlet.java +++ b/src/dolda/jsvc/j2ee/Servlet.java @@ -31,7 +31,7 @@ public class Servlet extends HttpServlet { } 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() { diff --git a/src/dolda/jsvc/j2ee/StandardContext.java b/src/dolda/jsvc/j2ee/StandardContext.java new file mode 100644 index 0000000..e7e3f6d --- /dev/null +++ b/src/dolda/jsvc/j2ee/StandardContext.java @@ -0,0 +1,14 @@ +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); + } +} diff --git a/src/dolda/jsvc/j2ee/TomcatContext.java b/src/dolda/jsvc/j2ee/TomcatContext.java new file mode 100644 index 0000000..5511820 --- /dev/null +++ b/src/dolda/jsvc/j2ee/TomcatContext.java @@ -0,0 +1,63 @@ +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); + } +}