Added an optional server-context name fetching operation.
[jsvc.git] / src / dolda / jsvc / j2ee / TomcatContext.java
CommitLineData
762009ab
FT
1package dolda.jsvc.j2ee;
2
3import dolda.jsvc.*;
4import dolda.jsvc.util.*;
5import javax.servlet.*;
6import java.lang.reflect.*;
7import java.util.*;
8import java.io.*;
9
10public class TomcatContext extends J2eeContext {
11 private final String name;
12
13 TomcatContext(ServletConfig sc) {
14 super(sc);
15 ServletContext ctx = j2eeconfig().getServletContext();
16 Class<?> cclass = ctx.getClass();
17 try {
18 Method cpm = cclass.getMethod("getContextPath");
19 name = Misc.stripslashes((String)cpm.invoke(ctx), true, true);
20 } catch(NoSuchMethodException e) {
21 throw(new RuntimeException("Could not fetch context path from Tomcat", e));
22 } catch(IllegalAccessException e) {
23 throw(new RuntimeException("Could not fetch context path from Tomcat", e));
24 } catch(InvocationTargetException e) {
25 throw(new RuntimeException("Could not fetch context path from Tomcat", e));
26 }
27 readconfig();
28 }
29
30 private static void loadprops(Properties props, File pfile) {
31 if(!pfile.exists())
32 return;
33 try {
34 InputStream in = new FileInputStream(pfile);
35 try {
36 props.load(in);
37 } finally {
38 in.close();
39 }
40 } catch(IOException e) {
41 throw(new RuntimeException(e));
42 }
43 }
44
45 private void readconfig() {
46 String basename = System.getProperty("catalina.base");
47 File base = new File(basename);
48 config.put("jsvc.storage", "file:" + new File(new File(base, "work"), "jsvc").getPath());
49 File cdir = new File(base, "conf");
50 loadprops(config, new File(cdir, "jsvc.properties"));
51 }
52
53 public static boolean tomcatp(ServletConfig sc) {
54 ServletContext ctx = sc.getServletContext();
55 if(ctx.getClass().getName().equals("org.apache.catalina.core.ApplicationContextFacade"))
56 return(true);
57 return(false);
58 }
59
60 public String name() {
61 return(name);
62 }
63}