--- /dev/null
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.argeo.dep.osgi</groupId>
+ <artifactId>org.argeo.dep.osgi.catalina.start</artifactId>
+ <name>Catalina starter provided by Spring DM</name>
+ <version>1.0.0.0009</version>
+ <packaging>jar</packaging>
+ <properties></properties>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifestFile>META-INF/MANIFEST.MF</manifestFile>
+ </archive>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.eclipse.osgi</groupId>
+ <artifactId>org.eclipse.osgi</artifactId>
+ <version>3.5.1.R35x_v20090827</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.catalina</groupId>
+ <artifactId>com.springsource.org.apache.catalina</artifactId>
+ <version>6.0.24</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>com.springsource.org.apache.commons.logging</artifactId>
+ <version>1.1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>com.springsource.org.apache.commons.io</artifactId>
+ <version>1.4.0</version>
+ </dependency>
+ </dependencies>
+ <repositories>
+ <repository>
+ <id>com.springsource.repository.bundles.external</id>
+ <name>
+ SpringSource Enterprise Bundle Repository - External
+ Bundle
+ Releases
+ </name>
+ <url>
+ http://repository.springsource.com/maven/bundles/external
+ </url>
+ <releases>
+ <enabled>true</enabled>
+ <updatePolicy>daily</updatePolicy>
+ <checksumPolicy>fail</checksumPolicy>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ <updatePolicy>never</updatePolicy>
+ <checksumPolicy>fail</checksumPolicy>
+ </snapshots>
+ </repository>
+ </repositories>
+ <distributionManagement>
+ <repository>
+ <uniqueVersion>false</uniqueVersion>
+ <id>argeo-restricted</id>
+ <name>Argeo FOSS Repository</name>
+ <url>file:///srv/maven/www/argeo</url>
+ </repository>
+ <snapshotRepository>
+ <uniqueVersion>true</uniqueVersion>
+ <id>argeo-snapshots-restricted</id>
+ <name>Argeo FOSS Snapshots Repository</name>
+ <url>file:///srv/maven/www/argeo-snapshots</url>
+ </snapshotRepository>
+ </distributionManagement>
+</project>
--- /dev/null
+package org.argeo.dep.osgi.catalina.start;
+
+import java.io.File;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Properties;
+
+import org.apache.commons.io.IOUtils;
+import org.osgi.framework.BundleContext;
+import org.springframework.osgi.web.tomcat.internal.Activator;
+
+/** Starts Catalina (hacked from Spring OSGi 1.0) */
+public class CatalinaActivator extends Activator {
+ private final static String ARGEO_OSGI_DATA_DIR = "argeo.osgi.data.dir";
+
+ public void start(BundleContext context) throws Exception {
+ if (!System.getProperties().containsKey(ARGEO_OSGI_DATA_DIR)) {
+ String osgiInstanceArea = System.getProperty("osgi.instance.area");
+ String osgiInstanceAreaDefault = System
+ .getProperty("osgi.instance.area.default");
+ String tempDir = System.getProperty("java.io.tmpdir");
+
+ File dataDir = null;
+ if (osgiInstanceArea != null) {
+ // within OSGi with -data specified
+ osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
+ dataDir = new File(osgiInstanceArea);
+ } else if (osgiInstanceAreaDefault != null) {
+ // within OSGi without -data specified
+ osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
+ dataDir = new File(osgiInstanceAreaDefault);
+ } else {// outside OSGi
+ dataDir = new File(tempDir + File.separator + "osgiData");
+ }
+
+ System.setProperty(ARGEO_OSGI_DATA_DIR, dataDir.getAbsolutePath());
+ }
+
+ // Load config properties and put them in system properties so that they
+ // can be used in tomcat conf
+ Properties confProps = new Properties();
+ URL propsUrl = context.getBundle().getResource("tomcat.properties");
+ if (propsUrl != null) {
+ InputStream in = null;
+ try {
+ in = propsUrl.openStream();
+ confProps.load(in);
+ } catch (Exception e) {
+ throw new RuntimeException("Cannot read catalina properties.",
+ e);
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+
+ for (Object key : confProps.keySet()) {
+ // System properties have priority
+ if (!System.getProperties().containsKey(key)) {
+ System.setProperty(key.toString(),
+ confProps.getProperty(key.toString()));
+ }
+ }
+ }
+
+ // calling Catalina.setCatalinaHome(String) or
+ // Catalina.setCatalinaBase(String) does the same
+ if (System.getProperty("catalina.home") == null)
+ System.setProperty("catalina.home",
+ System.getProperty(ARGEO_OSGI_DATA_DIR) + "/tomcat");
+ if (System.getProperty("catalina.base") == null)
+ System.setProperty("catalina.base",
+ System.getProperty(ARGEO_OSGI_DATA_DIR) + "/tomcat");
+
+ // Call Spring starter
+ super.start(context);
+ }
+
+ @Override
+ public void stop(BundleContext context) throws Exception {
+ super.stop(context);
+ }
+
+ protected String removeFilePrefix(String url) {
+ if (url.startsWith("file:"))
+ return url.substring("file:".length());
+ else if (url.startsWith("reference:file:"))
+ return url.substring("reference:file:".length());
+ else
+ return url;
+ }
+
+}
--- /dev/null
+/*\r
+ * Copyright 2006-2008 the original author or authors.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.springframework.osgi.web.tomcat.internal;\r
+\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.URL;\r
+import java.net.URLConnection;\r
+import java.net.URLStreamHandler;\r
+import java.util.Properties;\r
+\r
+import javax.management.MBeanRegistration;\r
+\r
+import org.apache.catalina.Lifecycle;\r
+import org.apache.catalina.Server;\r
+import org.apache.catalina.Service;\r
+import org.apache.catalina.connector.Connector;\r
+import org.apache.catalina.core.StandardService;\r
+import org.apache.catalina.util.ServerInfo;\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.apache.naming.resources.DirContextURLStreamHandler;\r
+import org.osgi.framework.Bundle;\r
+import org.osgi.framework.BundleActivator;\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.Constants;\r
+import org.osgi.framework.ServiceRegistration;\r
+import org.osgi.service.url.AbstractURLStreamHandlerService;\r
+import org.osgi.service.url.URLConstants;\r
+import org.osgi.service.url.URLStreamHandlerService;\r
+\r
+/**\r
+ * 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
+ * \r
+ * @author Costin Leau\r
+ */\r
+public class Activator implements BundleActivator {\r
+\r
+ /** logger */\r
+ private static final Log log = LogFactory.getLog(Activator.class);\r
+\r
+ /** default XML configuration */\r
+ private static final String DEFAULT_XML_CONF_LOCATION = "conf/default-server.xml";\r
+\r
+ /** user-configurable XML configuration */\r
+ private static final String XML_CONF_LOCATION = "conf/server.xml";\r
+\r
+ private BundleContext bundleContext;\r
+\r
+ private StandardService server;\r
+\r
+ private ServiceRegistration registration, urlRegistration;\r
+\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
+ // so the activator finishes fast\r
+ startupThread = new Thread(new Runnable() {\r
+\r
+ public void run() {\r
+ log.info("Starting " + ServerInfo.getServerInfo() + " ...");\r
+\r
+ // default startup procedure\r
+ ClassLoader cl = Activator.class.getClassLoader();\r
+ Thread current = Thread.currentThread();\r
+ ClassLoader old = current.getContextClassLoader();\r
+\r
+ try {\r
+ current.setContextClassLoader(cl);\r
+\r
+ server = createCatalinaServer(bundleContext.getBundle());\r
+\r
+ server.start();\r
+\r
+ 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
+ }\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
+ String msg = "Cannot start " + ServerInfo.getServerInfo();\r
+ log.error(msg, ex);\r
+ throw new RuntimeException(msg, ex);\r
+ }\r
+ finally {\r
+ current.setContextClassLoader(old);\r
+ }\r
+ }\r
+ }, "Tomcat Catalina Start Thread");\r
+\r
+ startupThread.start();\r
+ }\r
+\r
+ public void stop(BundleContext context) throws Exception {\r
+ // unpublish service first\r
+ registration.unregister();\r
+ urlRegistration.unregister();\r
+\r
+ log.info("Unpublished " + ServerInfo.getServerInfo() + " OSGi service");\r
+\r
+ // default startup procedure\r
+ ClassLoader cl = Activator.class.getClassLoader();\r
+ Thread current = Thread.currentThread();\r
+ ClassLoader old = current.getContextClassLoader();\r
+\r
+ try {\r
+ current.setContextClassLoader(cl);\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
+ log.error("Cannot stop " + ServerInfo.getServerInfo(), ex);\r
+ throw ex;\r
+ }\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
+\r
+ if (xmlConfiguration != null) {\r
+ log.info("Using custom XML configuration " + xmlConfiguration);\r
+ }\r
+ else {\r
+ xmlConfiguration = bundle.getResource(DEFAULT_XML_CONF_LOCATION);\r
+ if (xmlConfiguration == null)\r
+ log.error("No XML configuration found; bailing out...");\r
+ else\r
+ log.info("Using default XML configuration " + xmlConfiguration);\r
+ }\r
+\r
+ return createServerFromXML(xmlConfiguration);\r
+ }\r
+\r
+ private StandardService createServerFromXML(URL xmlConfiguration) 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
+\r
+ // copy the URL file to a local temporary file (since Catalina doesn't 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
+\r
+ catalina.setConfigFile(configTempFile.getAbsolutePath());\r
+\r
+ catalina.load();\r
+\r
+ Server server = catalina.getServer();\r
+\r
+ return (StandardService) server.findServices()[0];\r
+ }\r
+\r
+ private void copyURLToFile(InputStream inStream, FileOutputStream outStream) {\r
+\r
+ int bytesRead;\r
+ byte[] buf = new byte[4096];\r
+ try {\r
+ 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
+ try {\r
+ inStream.close();\r
+ }\r
+ catch (IOException ignore) {\r
+ }\r
+ try {\r
+ outStream.close();\r
+ }\r
+ catch (IOException ignore) {\r
+ }\r
+ }\r
+ }\r
+\r
+ private ServiceRegistration publishServerAsAService(StandardService server) {\r
+ Properties props = new Properties();\r
+ // put some extra properties to easily identify the service\r
+ 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
+\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
+\r
+ return bundleContext.registerService(classes, server, props);\r
+ }\r
+\r
+ private ServiceRegistration registerTomcatJNDIUrlService() {\r
+ Properties properties = new Properties();\r
+ 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
+\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
+ }\r
+}
\ No newline at end of file