Make Tomcat much easier to configure.
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.catalina.start / src / main / java / org / springframework / osgi / web / tomcat / internal / Activator.java
index 70284fe18fde732ea9ad8b3681901ee729509269..9481f3158282293b52325071f5b96813aaa06aaf 100644 (file)
@@ -36,6 +36,7 @@ import org.apache.catalina.util.ServerInfo;
 import org.apache.commons.logging.Log;\r
 import org.apache.commons.logging.LogFactory;\r
 import org.apache.naming.resources.DirContextURLStreamHandler;\r
+import org.argeo.catalina.start.CatalinaActivator;\r
 import org.osgi.framework.Bundle;\r
 import org.osgi.framework.BundleActivator;\r
 import org.osgi.framework.BundleContext;\r
@@ -49,9 +50,10 @@ import org.osgi.service.url.URLStreamHandlerService;
  * Simple activator for starting Apache Tomcat Catalina container inside OSGi\r
  * using Tomcat's XML configuration files.\r
  * \r
- * <p/> This activator looks initially for a <code>conf/server.xml</code> file\r
- * falling back to <code>conf/default-server.xml</code>. This allows the\r
- * default configuration to be tweaked through fragments for example.\r
+ * <p/>\r
+ * This activator looks initially for a <code>conf/server.xml</code> file\r
+ * falling back to <code>conf/default-server.xml</code>. This allows the default\r
+ * configuration to be tweaked through fragments for example.\r
  * \r
  * @author Costin Leau\r
  */\r
@@ -74,7 +76,6 @@ public class Activator implements BundleActivator {
 \r
        private Thread startupThread;\r
 \r
-\r
        public void start(BundleContext context) throws Exception {\r
                this.bundleContext = context;\r
                // do the initialization on a different thread\r
@@ -99,22 +100,22 @@ public class Activator implements BundleActivator {
                                        Connector[] connectors = server.findConnectors();\r
                                        for (int i = 0; i < connectors.length; i++) {\r
                                                Connector conn = connectors[i];\r
-                                               log.info("Succesfully started " + ServerInfo.getServerInfo() + " @ " + conn.getDomain() + ":"\r
-                                                               + conn.getPort());\r
+                                               log.info("Succesfully started "\r
+                                                               + ServerInfo.getServerInfo() + " @ "\r
+                                                               + conn.getDomain() + ":" + conn.getPort());\r
                                        }\r
 \r
                                        // register URL service\r
                                        urlRegistration = registerTomcatJNDIUrlService();\r
                                        // publish server as an OSGi service\r
                                        registration = publishServerAsAService(server);\r
-                                       log.info("Published " + ServerInfo.getServerInfo() + " as an OSGi service");\r
-                               }\r
-                               catch (Exception ex) {\r
+                                       log.info("Published " + ServerInfo.getServerInfo()\r
+                                                       + " as an OSGi service");\r
+                               catch (Exception ex) {\r
                                        String msg = "Cannot start " + ServerInfo.getServerInfo();\r
                                        log.error(msg, ex);\r
                                        throw new RuntimeException(msg, ex);\r
-                               }\r
-                               finally {\r
+                               } finally {\r
                                        current.setContextClassLoader(old);\r
                                }\r
                        }\r
@@ -137,29 +138,33 @@ public class Activator implements BundleActivator {
 \r
                try {\r
                        current.setContextClassLoader(cl);\r
-                       //reset CCL \r
+                       // reset CCL\r
                        // current.setContextClassLoader(null);\r
                        log.info("Stopping " + ServerInfo.getServerInfo() + " ...");\r
                        server.stop();\r
                        log.info("Succesfully stopped " + ServerInfo.getServerInfo());\r
-               }\r
-               catch (Exception ex) {\r
+               } catch (Exception ex) {\r
                        log.error("Cannot stop " + ServerInfo.getServerInfo(), ex);\r
                        throw ex;\r
-               }\r
-               finally {\r
+               } finally {\r
                        current.setContextClassLoader(old);\r
                }\r
        }\r
 \r
-       private StandardService createCatalinaServer(Bundle bundle) throws Exception {\r
-               // first try to use the XML file\r
-               URL xmlConfiguration = bundle.getResource(XML_CONF_LOCATION);\r
+       private StandardService createCatalinaServer(Bundle bundle)\r
+                       throws Exception {\r
+               URL xmlConfiguration = null;\r
+\r
+               if (System.getProperty(CatalinaActivator.ARGEO_SERVER_TOMCAT_CONFIG) != null)\r
+                       xmlConfiguration = new URL(\r
+                                       System.getProperty(CatalinaActivator.ARGEO_SERVER_TOMCAT_CONFIG));\r
+               else\r
+                       // fragment\r
+                       xmlConfiguration = bundle.getResource(XML_CONF_LOCATION);\r
 \r
                if (xmlConfiguration != null) {\r
                        log.info("Using custom XML configuration " + xmlConfiguration);\r
-               }\r
-               else {\r
+               } else {\r
                        xmlConfiguration = bundle.getResource(DEFAULT_XML_CONF_LOCATION);\r
                        if (xmlConfiguration == null)\r
                                log.error("No XML configuration found; bailing out...");\r
@@ -170,20 +175,25 @@ public class Activator implements BundleActivator {
                return createServerFromXML(xmlConfiguration);\r
        }\r
 \r
-       private StandardService createServerFromXML(URL xmlConfiguration) throws IOException {\r
+       private StandardService createServerFromXML(URL xmlConfiguration)\r
+                       throws IOException {\r
                OsgiCatalina catalina = new OsgiCatalina();\r
                catalina.setAwait(false);\r
                catalina.setUseShutdownHook(false);\r
                catalina.setName("Catalina");\r
-               catalina.setParentClassLoader(Thread.currentThread().getContextClassLoader());\r
+               catalina.setParentClassLoader(Thread.currentThread()\r
+                               .getContextClassLoader());\r
 \r
-               // copy the URL file to a local temporary file (since Catalina doesn't use URL unfortunately)\r
+               // copy the URL file to a local temporary file (since Catalina doesn't\r
+               // use URL unfortunately)\r
                File configTempFile = File.createTempFile("dm.catalina", ".cfg.xml");\r
                configTempFile.deleteOnExit();\r
 \r
                // copy URL to temporary file\r
-               copyURLToFile(xmlConfiguration.openStream(), new FileOutputStream(configTempFile));\r
-               log.debug("Copied configuration " + xmlConfiguration + " to temporary file " + configTempFile);\r
+               copyURLToFile(xmlConfiguration.openStream(), new FileOutputStream(\r
+                               configTempFile));\r
+               log.debug("Copied configuration " + xmlConfiguration\r
+                               + " to temporary file " + configTempFile);\r
 \r
                catalina.setConfigFile(configTempFile.getAbsolutePath());\r
 \r
@@ -202,20 +212,17 @@ public class Activator implements BundleActivator {
                        while ((bytesRead = inStream.read(buf)) >= 0) {\r
                                outStream.write(buf, 0, bytesRead);\r
                        }\r
-               }\r
-               catch (IOException ex) {\r
-                       throw (RuntimeException) new IllegalStateException("Cannot copy URL to file").initCause(ex);\r
-               }\r
-               finally {\r
+               } catch (IOException ex) {\r
+                       throw (RuntimeException) new IllegalStateException(\r
+                                       "Cannot copy URL to file").initCause(ex);\r
+               } finally {\r
                        try {\r
                                inStream.close();\r
-                       }\r
-                       catch (IOException ignore) {\r
+                       } catch (IOException ignore) {\r
                        }\r
                        try {\r
                                outStream.close();\r
-                       }\r
-                       catch (IOException ignore) {\r
+                       } catch (IOException ignore) {\r
                        }\r
                }\r
        }\r
@@ -226,14 +233,17 @@ public class Activator implements BundleActivator {
                props.put(Constants.SERVICE_VENDOR, "Spring Dynamic Modules");\r
                props.put(Constants.SERVICE_DESCRIPTION, ServerInfo.getServerInfo());\r
                props.put(Constants.BUNDLE_VERSION, ServerInfo.getServerNumber());\r
-               props.put(Constants.BUNDLE_NAME, bundleContext.getBundle().getSymbolicName());\r
+               props.put(Constants.BUNDLE_NAME, bundleContext.getBundle()\r
+                               .getSymbolicName());\r
 \r
                // spring-dm specific property\r
                props.put("org.springframework.osgi.bean.name", "tomcat-server");\r
 \r
-               // publish just the interfaces and the major classes (server/handlerWrapper)\r
-               String[] classes = new String[] { StandardService.class.getName(), Service.class.getName(),\r
-                       MBeanRegistration.class.getName(), Lifecycle.class.getName() };\r
+               // publish just the interfaces and the major classes\r
+               // (server/handlerWrapper)\r
+               String[] classes = new String[] { StandardService.class.getName(),\r
+                               Service.class.getName(), MBeanRegistration.class.getName(),\r
+                               Lifecycle.class.getName() };\r
 \r
                return bundleContext.registerService(classes, server, props);\r
        }\r
@@ -243,15 +253,17 @@ public class Activator implements BundleActivator {
                properties.put(URLConstants.URL_HANDLER_PROTOCOL, "jndi");\r
                final URLStreamHandler handler = new DirContextURLStreamHandler();\r
 \r
-               return bundleContext.registerService(URLStreamHandlerService.class.getName(),\r
-                       new AbstractURLStreamHandlerService() {\r
+               return bundleContext.registerService(\r
+                               URLStreamHandlerService.class.getName(),\r
+                               new AbstractURLStreamHandlerService() {\r
 \r
-                               private final static String EMPTY_STRING = "";\r
+                                       private final static String EMPTY_STRING = "";\r
 \r
-\r
-                               public URLConnection openConnection(URL u) throws IOException {\r
-                                       return new URL(u, EMPTY_STRING, handler).openConnection();\r
-                               }\r
-                       }, properties);\r
+                                       public URLConnection openConnection(URL u)\r
+                                                       throws IOException {\r
+                                               return new URL(u, EMPTY_STRING, handler)\r
+                                                               .openConnection();\r
+                                       }\r
+                               }, properties);\r
        }\r
 }
\ No newline at end of file