Fixed message typo.
[jsvc.git] / src / dolda / jsvc / j2ee / TomcatContext.java
1 package dolda.jsvc.j2ee;
2
3 import dolda.jsvc.*;
4 import dolda.jsvc.util.*;
5 import javax.servlet.*;
6 import java.lang.reflect.*;
7 import java.util.*;
8 import java.io.*;
9 import java.util.logging.*;
10
11 public class TomcatContext extends J2eeContext {
12     private final String name;
13     private static final Logger logger = Logger.getLogger("dolda.jsvc.context");
14     
15     TomcatContext(ServletConfig sc) {
16         super(sc);
17         ServletContext ctx = j2eeconfig().getServletContext();
18         Class<?> cclass = ctx.getClass();
19         String name;
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));
29         } catch(SecurityException e) {
30             logger.log(Level.WARNING, "no permissions to fetch context name from Tomcat", e);
31             name = null;
32         }
33         this.name = name;
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() {
53         File base;
54         try {
55             String basename = System.getProperty("catalina.base");
56             base = new File(basename);
57         } catch(SecurityException e) {
58             logger.log(Level.WARNING, "no permissions to fetch Tomcat base directory while reading configuration", e);
59             return;
60         }
61         config.put("jsvc.storage", "file:" + new File(new File(base, "work"), "jsvc").getPath());
62         File cdir = new File(base, "conf");
63         try {
64             loadprops(config, new File(cdir, "jsvc.properties"));
65         } catch(SecurityException e) {
66             logger.log(Level.WARNING, "no permssions to read from Tomcat conf directory while reading configuration", e);
67         }
68     }
69     
70     public static boolean tomcatp(ServletConfig sc) {
71         ServletContext ctx = sc.getServletContext();
72         if(ctx.getClass().getName().equals("org.apache.catalina.core.ApplicationContextFacade"))
73             return(true);
74         return(false);
75     }
76     
77     public String name() {
78         return(name);
79     }
80 }