import java.io.FileOutputStream;\r
import java.io.IOException;\r
import java.io.InputStream;\r
+import java.net.MalformedURLException;\r
import java.net.URL;\r
import java.net.URLConnection;\r
import java.net.URLStreamHandler;\r
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
* 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
\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
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
\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
+ String customConfig = System\r
+ .getProperty(CatalinaActivator.ARGEO_SERVER_TOMCAT_CONFIG);\r
+ try {\r
+ xmlConfiguration = new URL(customConfig);\r
+ } catch (MalformedURLException e) {\r
+ // within this bundle\r
+ // typically 'default-server-ssl.xml'\r
+ xmlConfiguration = bundle.getResource(customConfig);\r
+ }\r
+ } else {\r
+ // fragment\r
+ xmlConfiguration = bundle.getResource(XML_CONF_LOCATION);\r
+ }\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
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
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
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
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
+ return bundleContext.registerService(\r
+ URLStreamHandlerService.class.getName(),\r
+ new AbstractURLStreamHandlerService() {\r
\r
+ private final static String EMPTY_STRING = "";\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