1 package org
.argeo
.catalina
.start
;
4 import java
.io
.InputStream
;
6 import java
.util
.Properties
;
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
;
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);
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";
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");
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");
42 System
.setProperty(ARGEO_OSGI_DATA_DIR
, dataDir
.getAbsolutePath());
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;
52 in
= propsUrl
.openStream();
54 } catch (Exception e
) {
55 throw new RuntimeException("Cannot read catalina properties.",
58 IOUtils
.closeQuietly(in
);
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()));
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");
79 // Make sure directories are created
80 File catalinaDir
= new File(System
.getProperty("catalina.home"));
81 if (!catalinaDir
.exists()) {
83 if (log
.isDebugEnabled())
84 log
.debug("Created Tomcat directory " + catalinaDir
);
87 // Call Spring starter
92 public void stop(BundleContext context
) throws Exception
{
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());