X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=server%2Fruntime%2Forg.argeo.server.catalina.start%2Fsrc%2Fmain%2Fjava%2Forg%2Fspringframework%2Fosgi%2Fweb%2Ftomcat%2Finternal%2FActivator.java;h=17ed5e996e1bafe308c3548f8f5ecaed8ec6711d;hb=4646d292392fec25320bd23df142513766e4c53d;hp=70284fe18fde732ea9ad8b3681901ee729509269;hpb=15d66f3c17c025e40f833a7164aa34fe69ae9fad;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.catalina.start/src/main/java/org/springframework/osgi/web/tomcat/internal/Activator.java b/server/runtime/org.argeo.server.catalina.start/src/main/java/org/springframework/osgi/web/tomcat/internal/Activator.java index 70284fe18..17ed5e996 100644 --- a/server/runtime/org.argeo.server.catalina.start/src/main/java/org/springframework/osgi/web/tomcat/internal/Activator.java +++ b/server/runtime/org.argeo.server.catalina.start/src/main/java/org/springframework/osgi/web/tomcat/internal/Activator.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; @@ -36,6 +37,7 @@ import org.apache.catalina.util.ServerInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.naming.resources.DirContextURLStreamHandler; +import org.argeo.catalina.start.CatalinaActivator; import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -49,9 +51,10 @@ import org.osgi.service.url.URLStreamHandlerService; * Simple activator for starting Apache Tomcat Catalina container inside OSGi * using Tomcat's XML configuration files. * - *

This activator looks initially for a conf/server.xml file - * falling back to conf/default-server.xml. This allows the - * default configuration to be tweaked through fragments for example. + *

+ * This activator looks initially for a conf/server.xml file + * falling back to conf/default-server.xml. This allows the default + * configuration to be tweaked through fragments for example. * * @author Costin Leau */ @@ -74,7 +77,6 @@ public class Activator implements BundleActivator { private Thread startupThread; - public void start(BundleContext context) throws Exception { this.bundleContext = context; // do the initialization on a different thread @@ -99,22 +101,22 @@ public class Activator implements BundleActivator { Connector[] connectors = server.findConnectors(); for (int i = 0; i < connectors.length; i++) { Connector conn = connectors[i]; - log.info("Succesfully started " + ServerInfo.getServerInfo() + " @ " + conn.getDomain() + ":" - + conn.getPort()); + log.info("Succesfully started " + + ServerInfo.getServerInfo() + " @ " + + conn.getDomain() + ":" + conn.getPort()); } // register URL service urlRegistration = registerTomcatJNDIUrlService(); // publish server as an OSGi service registration = publishServerAsAService(server); - log.info("Published " + ServerInfo.getServerInfo() + " as an OSGi service"); - } - catch (Exception ex) { + log.info("Published " + ServerInfo.getServerInfo() + + " as an OSGi service"); + } catch (Exception ex) { String msg = "Cannot start " + ServerInfo.getServerInfo(); log.error(msg, ex); throw new RuntimeException(msg, ex); - } - finally { + } finally { current.setContextClassLoader(old); } } @@ -137,29 +139,41 @@ public class Activator implements BundleActivator { try { current.setContextClassLoader(cl); - //reset CCL + // reset CCL // current.setContextClassLoader(null); log.info("Stopping " + ServerInfo.getServerInfo() + " ..."); server.stop(); log.info("Succesfully stopped " + ServerInfo.getServerInfo()); - } - catch (Exception ex) { + } catch (Exception ex) { log.error("Cannot stop " + ServerInfo.getServerInfo(), ex); throw ex; - } - finally { + } finally { current.setContextClassLoader(old); } } - private StandardService createCatalinaServer(Bundle bundle) throws Exception { - // first try to use the XML file - URL xmlConfiguration = bundle.getResource(XML_CONF_LOCATION); + private StandardService createCatalinaServer(Bundle bundle) + throws Exception { + URL xmlConfiguration = null; + + if (System.getProperty(CatalinaActivator.ARGEO_SERVER_TOMCAT_CONFIG) != null) { + String customConfig = System + .getProperty(CatalinaActivator.ARGEO_SERVER_TOMCAT_CONFIG); + try { + xmlConfiguration = new URL(customConfig); + } catch (MalformedURLException e) { + // within this bundle + // typically 'default-server-ssl.xml' + xmlConfiguration = bundle.getResource(customConfig); + } + } else { + // fragment + xmlConfiguration = bundle.getResource(XML_CONF_LOCATION); + } if (xmlConfiguration != null) { log.info("Using custom XML configuration " + xmlConfiguration); - } - else { + } else { xmlConfiguration = bundle.getResource(DEFAULT_XML_CONF_LOCATION); if (xmlConfiguration == null) log.error("No XML configuration found; bailing out..."); @@ -170,20 +184,25 @@ public class Activator implements BundleActivator { return createServerFromXML(xmlConfiguration); } - private StandardService createServerFromXML(URL xmlConfiguration) throws IOException { + private StandardService createServerFromXML(URL xmlConfiguration) + throws IOException { OsgiCatalina catalina = new OsgiCatalina(); catalina.setAwait(false); catalina.setUseShutdownHook(false); catalina.setName("Catalina"); - catalina.setParentClassLoader(Thread.currentThread().getContextClassLoader()); + catalina.setParentClassLoader(Thread.currentThread() + .getContextClassLoader()); - // copy the URL file to a local temporary file (since Catalina doesn't use URL unfortunately) + // copy the URL file to a local temporary file (since Catalina doesn't + // use URL unfortunately) File configTempFile = File.createTempFile("dm.catalina", ".cfg.xml"); configTempFile.deleteOnExit(); // copy URL to temporary file - copyURLToFile(xmlConfiguration.openStream(), new FileOutputStream(configTempFile)); - log.debug("Copied configuration " + xmlConfiguration + " to temporary file " + configTempFile); + copyURLToFile(xmlConfiguration.openStream(), new FileOutputStream( + configTempFile)); + log.debug("Copied configuration " + xmlConfiguration + + " to temporary file " + configTempFile); catalina.setConfigFile(configTempFile.getAbsolutePath()); @@ -202,20 +221,17 @@ public class Activator implements BundleActivator { while ((bytesRead = inStream.read(buf)) >= 0) { outStream.write(buf, 0, bytesRead); } - } - catch (IOException ex) { - throw (RuntimeException) new IllegalStateException("Cannot copy URL to file").initCause(ex); - } - finally { + } catch (IOException ex) { + throw (RuntimeException) new IllegalStateException( + "Cannot copy URL to file").initCause(ex); + } finally { try { inStream.close(); - } - catch (IOException ignore) { + } catch (IOException ignore) { } try { outStream.close(); - } - catch (IOException ignore) { + } catch (IOException ignore) { } } } @@ -226,14 +242,17 @@ public class Activator implements BundleActivator { props.put(Constants.SERVICE_VENDOR, "Spring Dynamic Modules"); props.put(Constants.SERVICE_DESCRIPTION, ServerInfo.getServerInfo()); props.put(Constants.BUNDLE_VERSION, ServerInfo.getServerNumber()); - props.put(Constants.BUNDLE_NAME, bundleContext.getBundle().getSymbolicName()); + props.put(Constants.BUNDLE_NAME, bundleContext.getBundle() + .getSymbolicName()); // spring-dm specific property props.put("org.springframework.osgi.bean.name", "tomcat-server"); - // publish just the interfaces and the major classes (server/handlerWrapper) - String[] classes = new String[] { StandardService.class.getName(), Service.class.getName(), - MBeanRegistration.class.getName(), Lifecycle.class.getName() }; + // publish just the interfaces and the major classes + // (server/handlerWrapper) + String[] classes = new String[] { StandardService.class.getName(), + Service.class.getName(), MBeanRegistration.class.getName(), + Lifecycle.class.getName() }; return bundleContext.registerService(classes, server, props); } @@ -243,15 +262,17 @@ public class Activator implements BundleActivator { properties.put(URLConstants.URL_HANDLER_PROTOCOL, "jndi"); final URLStreamHandler handler = new DirContextURLStreamHandler(); - return bundleContext.registerService(URLStreamHandlerService.class.getName(), - new AbstractURLStreamHandlerService() { - - private final static String EMPTY_STRING = ""; + return bundleContext.registerService( + URLStreamHandlerService.class.getName(), + new AbstractURLStreamHandlerService() { + private final static String EMPTY_STRING = ""; - public URLConnection openConnection(URL u) throws IOException { - return new URL(u, EMPTY_STRING, handler).openConnection(); - } - }, properties); + public URLConnection openConnection(URL u) + throws IOException { + return new URL(u, EMPTY_STRING, handler) + .openConnection(); + } + }, properties); } } \ No newline at end of file