Commit | Line | Data |
---|---|---|
762009ab FT |
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.*; | |
5027c00a | 9 | import java.util.logging.*; |
762009ab FT |
10 | |
11 | public 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 | } |