]> git.argeo.org Git - lgpl/argeo-commons.git/blob - start/CatalinaActivator.java
Prepare next development cycle
[lgpl/argeo-commons.git] / start / CatalinaActivator.java
1 package org.argeo.catalina.start;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.Properties;
7
8 import org.apache.commons.io.IOUtils;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.osgi.framework.BundleContext;
12 import org.springframework.osgi.web.tomcat.internal.Activator;
13
14 /** Starts Catalina (hacked from Spring OSGi 1.0) */
15 public class CatalinaActivator extends Activator {
16 private final static Log log = LogFactory.getLog(CatalinaActivator.class);
17
18 private final static String ARGEO_OSGI_DATA_DIR = "argeo.osgi.data.dir";
19 /** System properties used to override Tomcat XML config URL */
20 public final static String ARGEO_SERVER_TOMCAT_CONFIG = "argeo.server.tomcat.config";
21
22 public void start(BundleContext context) throws Exception {
23 if (!System.getProperties().containsKey(ARGEO_OSGI_DATA_DIR)) {
24 String osgiInstanceArea = System.getProperty("osgi.instance.area");
25 String osgiInstanceAreaDefault = System
26 .getProperty("osgi.instance.area.default");
27 String tempDir = System.getProperty("java.io.tmpdir");
28
29 File dataDir = null;
30 if (osgiInstanceArea != null) {
31 // within OSGi with -data specified
32 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
33 dataDir = new File(osgiInstanceArea);
34 } else if (osgiInstanceAreaDefault != null) {
35 // within OSGi without -data specified
36 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
37 dataDir = new File(osgiInstanceAreaDefault);
38 } else {// outside OSGi
39 dataDir = new File(tempDir + File.separator + "osgiData");
40 }
41
42 System.setProperty(ARGEO_OSGI_DATA_DIR, dataDir.getAbsolutePath());
43 }
44
45 // Load config properties and put them in system properties so that they
46 // can be used in tomcat conf
47 Properties confProps = new Properties();
48 URL propsUrl = context.getBundle().getResource("tomcat.properties");
49 if (propsUrl != null) {
50 InputStream in = null;
51 try {
52 in = propsUrl.openStream();
53 confProps.load(in);
54 } catch (Exception e) {
55 throw new RuntimeException("Cannot read catalina properties.",
56 e);
57 } finally {
58 IOUtils.closeQuietly(in);
59 }
60
61 for (Object key : confProps.keySet()) {
62 // System properties have priority
63 if (!System.getProperties().containsKey(key)) {
64 System.setProperty(key.toString(),
65 confProps.getProperty(key.toString()));
66 }
67 }
68 }
69
70 // calling Catalina.setCatalinaHome(String) or
71 // Catalina.setCatalinaBase(String) does the same
72 if (System.getProperty("catalina.home") == null)
73 System.setProperty("catalina.home",
74 System.getProperty(ARGEO_OSGI_DATA_DIR) + "/tomcat");
75 if (System.getProperty("catalina.base") == null)
76 System.setProperty("catalina.base",
77 System.getProperty(ARGEO_OSGI_DATA_DIR) + "/tomcat");
78
79 // Make sure directories are created
80 File catalinaDir = new File(System.getProperty("catalina.home"));
81 if (!catalinaDir.exists()) {
82 catalinaDir.mkdirs();
83 if (log.isDebugEnabled())
84 log.debug("Created Tomcat directory " + catalinaDir);
85 }
86
87 // Call Spring starter
88 super.start(context);
89 }
90
91 @Override
92 public void stop(BundleContext context) throws Exception {
93 super.stop(context);
94 }
95
96 protected String removeFilePrefix(String url) {
97 if (url.startsWith("file:"))
98 return url.substring("file:".length());
99 else if (url.startsWith("reference:file:"))
100 return url.substring("reference:file:".length());
101 else
102 return url;
103 }
104
105 }