| 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 | 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()); |
| 65 | File cdir = new File(base, "conf"); |
| 66 | try { |
| 67 | loadprops(sysconfig, new File(cdir, "jsvc.properties")); |
| 68 | } catch(SecurityException e) { |
| 69 | logger.log(Level.WARNING, "no permssions to read from Tomcat conf directory while reading configuration", e); |
| 70 | } |
| 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 | } |