Made per-context storage roots a Tomcat-specific function.
[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.*;
5027c00a 9import java.util.logging.*;
762009ab
FT
10
11public class TomcatContext extends J2eeContext {
12 private final String name;
5027c00a 13 private static final Logger logger = Logger.getLogger("dolda.jsvc.context");
762009ab
FT
14
15 TomcatContext(ServletConfig sc) {
16 super(sc);
17 ServletContext ctx = j2eeconfig().getServletContext();
18 Class<?> cclass = ctx.getClass();
5027c00a 19 String name;
762009ab
FT
20 try {
21 Method cpm = cclass.getMethod("getContextPath");
22 name = Misc.stripslashes((String)cpm.invoke(ctx), true, true);
23 } catch(NoSuchMethodException e) {
24 throw(new RuntimeException("Could not fetch context path from Tomcat", e));
25 } catch(IllegalAccessException e) {
26 throw(new RuntimeException("Could not fetch context path from Tomcat", e));
27 } catch(InvocationTargetException e) {
28 throw(new RuntimeException("Could not fetch context path from Tomcat", e));
5027c00a
FT
29 } catch(SecurityException e) {
30 logger.log(Level.WARNING, "no permissions to fetch context name from Tomcat", e);
31 name = null;
762009ab 32 }
5027c00a 33 this.name = name;
762009ab
FT
34 readconfig();
35 }
36
37 private static void loadprops(Properties props, File pfile) {
38 if(!pfile.exists())
39 return;
40 try {
41 InputStream in = new FileInputStream(pfile);
42 try {
43 props.load(in);
44 } finally {
45 in.close();
46 }
47 } catch(IOException e) {
48 throw(new RuntimeException(e));
49 }
50 }
51
52 private void readconfig() {
5027c00a
FT
53 File base;
54 try {
55 String basename = System.getProperty("catalina.base");
56 base = new File(basename);
57 } catch(SecurityException e) {
f94215d0 58 logger.log(Level.WARNING, "no permissions to fetch Tomcat base directory while reading configuration", e);
5027c00a
FT
59 return;
60 }
67fe85aa
FT
61 File sroot = new File(new File(base, "work"), "jsvc");
62 if(name != null)
63 sroot = new File(sroot, name());
64 sysconfig.put("jsvc.storage", "file:" + sroot.getPath());
762009ab 65 File cdir = new File(base, "conf");
5027c00a 66 try {
6a0cb6cb 67 loadprops(sysconfig, new File(cdir, "jsvc.properties"));
5027c00a
FT
68 } catch(SecurityException e) {
69 logger.log(Level.WARNING, "no permssions to read from Tomcat conf directory while reading configuration", e);
70 }
762009ab
FT
71 }
72
73 public static boolean tomcatp(ServletConfig sc) {
74 ServletContext ctx = sc.getServletContext();
75 if(ctx.getClass().getName().equals("org.apache.catalina.core.ApplicationContextFacade"))
76 return(true);
77 return(false);
78 }
79
80 public String name() {
81 return(name);
82 }
83}