After discussion with original author Hugues, decided to remove this implementation in favour of using only JettyServerServiceTracker.

This commit is contained in:
Jan Bartel 2012-05-10 14:54:42 +02:00
parent ea557759b3
commit 9773113a47
3 changed files with 14 additions and 262 deletions

View File

@ -35,22 +35,17 @@ import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.BundleTracker; import org.osgi.util.tracker.BundleTracker;
/** /**
* Experiment: bootstrap jetty's complete distrib from an OSGi bundle. Progress: * Bootstrap jetty and publish a default Server instance as an OSGi service.
*
* Listen for other Server instances to be published as services and support them as deployment targets.
*
* Listen for Bundles to be activated, and deploy those that represent webapps to one of the known Server instances.
*
* <ol> * <ol>
* <li>basic servlet [ok]</li> * <li>basic servlet [ok]</li>
* <li>basic jetty.xml [ok]</li> * <li>basic jetty.xml [ok]</li>
* <li>basic jetty.xml and jetty-plus.xml [ok]</li> * <li>basic jetty.xml and jetty-plus.xml [ok]</li>
* <li>basic jsp [ok with modifications] * <li>basic jsp [ok]</li>
* <ul>
* <li>Needed to modify the headers of jdt.core-3.1.1 so that its dependency on
* eclipse.runtime, eclipse.resources and eclipse.text are optional. Also we
* should depend on the latest jdt.core from eclipse-3.5 not from eclipse-3.1.1
* although that will require actual changes to jasper as some internal APIs of
* jdt.core have changed.</li>
* <li>Modifications to org.mortbay.jetty.jsp-2.1-glassfish: made all imports to
* ant, xalan and sun packages optional.</li>
* </ul>
* </li>
* <li>jsp with tag-libs [ok]</li> * <li>jsp with tag-libs [ok]</li>
* <li>test-jndi with atomikos and derby inside ${jetty.home}/lib/ext [ok]</li> * <li>test-jndi with atomikos and derby inside ${jetty.home}/lib/ext [ok]</li>
* </ul> * </ul>
@ -77,7 +72,6 @@ public class JettyBootstrapActivator implements BundleActivator
private BundleContext _bundleContext; private BundleContext _bundleContext;
// private ServiceRegistration _jettyServerFactoryService;
private JettyServerServiceTracker _jettyServerServiceTracker; private JettyServerServiceTracker _jettyServerServiceTracker;
/** /**
@ -97,35 +91,24 @@ public class JettyBootstrapActivator implements BundleActivator
// should activate. // should activate.
_packageAdminServiceTracker = new PackageAdminServiceTracker(context); _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
// track Server instances that we should support as deployment targets
_jettyServerServiceTracker = new JettyServerServiceTracker(); _jettyServerServiceTracker = new JettyServerServiceTracker();
context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")"); context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")");
// Register the Jetty Server Factory as a ManagedServiceFactory: // track ContextHandler class instances and deploy them to one of the known Servers
// Properties jettyServerMgdFactoryServiceProps = new Properties();
// jettyServerMgdFactoryServiceProps.put("pid",
// OSGiWebappConstants.MANAGED_JETTY_SERVER_FACTORY_PID);
// _jettyServerFactoryService = context.registerService(
// ManagedServiceFactory.class.getName(), new
// JettyServersManagedFactory(),
// jettyServerMgdFactoryServiceProps);
_jettyContextHandlerTracker = new JettyContextHandlerServiceTracker(_jettyServerServiceTracker); _jettyContextHandlerTracker = new JettyContextHandlerServiceTracker(_jettyServerServiceTracker);
// the tracker in charge of the actual deployment
// and that will configure and start the jetty server.
context.addServiceListener(_jettyContextHandlerTracker, "(objectclass=" + ContextHandler.class.getName() + ")"); context.addServiceListener(_jettyContextHandlerTracker, "(objectclass=" + ContextHandler.class.getName() + ")");
// Create a default jetty instance right now. // Create a default jetty instance right now.
DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context); DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
// now ready to support the Extender pattern: // track Bundles and deploy those that represent webapps to one of the known Servers
_webBundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, new WebBundleTrackerCustomizer()); _webBundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, new WebBundleTrackerCustomizer());
_webBundleTracker.open(); _webBundleTracker.open();
} }
/* /**
* (non-Javadoc) * Stop the activator.
* *
* @see * @see
* org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
@ -173,22 +156,6 @@ public class JettyBootstrapActivator implements BundleActivator
_registeredServer = null; _registeredServer = null;
} }
} }
// if (_jettyServerFactoryService != null)
// {
// try
// {
// _jettyServerFactoryService.unregister();
// }
// catch (IllegalArgumentException ill)
// {
// // already unregistered.
// }
// finally
// {
// _jettyServerFactoryService = null;
// }
// }
} }
finally finally
{ {
@ -289,7 +256,7 @@ public class JettyBootstrapActivator implements BundleActivator
/** /**
* Since org.eclipse.jetty.osgi.boot does not have a lazy activation policy * Since org.eclipse.jetty.osgi.boot does not have a lazy activation policy
* when one fo the static methods to register a webapp is called we should * when one of the static methods to register a webapp is called we should
* make sure that the bundle is started. * make sure that the bundle is started.
*/ */
private static void checkBundleActivated() private static void checkBundleActivated()

View File

@ -1,215 +0,0 @@
// ========================================================================
// Copyright (c) 2009 Intalio, Inc.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// Contributors:
// Hugues Malphettes - initial API and implementation
// ========================================================================
package org.eclipse.jetty.osgi.boot.internal.serverfactory;
import java.net.URL;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.StringTokenizer;
import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
import org.eclipse.jetty.server.Server;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
/**
* Manages the deployment of jetty server instances. Not sure this is bringing
* much compared to the JettyServerServiceTracker.
*
* @author hmalphettes
*/
public class JettyServersManagedFactory implements ManagedServiceFactory, IManagedJettyServerRegistry
{
/**
* key to configure the server according to a jetty home folder. the value
* is the corresponding java.io.File
*/
public static final String JETTY_HOME = "jettyhome";
/** key to configure the server according to a jetty.xml file */
public static final String JETTY_CONFIG_XML = "jettyxml";
/**
* invoke jetty-factory class. the value of this property is the instance of
* that class to call back.
*/
public static final String JETTY_FACTORY = "jettyfactory";
/**
* default property in jetty.xml that is used as the value of the http port.
*/
public static final String JETTY_HTTP_PORT = "jetty.http.port";
/**
* default property in jetty.xml that is used as the value of the https
* port.
*/
public static final String JETTY_HTTPS_PORT = "jetty.http.port";
/**
* Servers indexed by PIDs. PIDs are generated by the ConfigurationAdmin
* service.
*/
private Map<String, ServerInstanceWrapper> _serversIndexedByPID = new HashMap<String, ServerInstanceWrapper>();
/**
* PID -> {@link OSGiWebappConstants#MANAGED_JETTY_SERVER_NAME}
*/
private Map<String, String> _serversNameIndexedByPID = new HashMap<String, String>();
/**
* {@link OSGiWebappConstants#MANAGED_JETTY_SERVER_NAME} -> PID
*/
private Map<String, String> _serversPIDIndexedByName = new HashMap<String, String>();
/**
* Return a descriptive name of this factory.
*
* @return the name for the factory, which might be localized
*/
public String getName()
{
return getClass().getName();
}
public void updated(String pid, Dictionary properties) throws ConfigurationException
{
ServerInstanceWrapper serverInstanceWrapper = getServerByPID(pid);
deleted(pid);
// do we need to collect the currently deployed http services and
// webapps
// to be able to re-deploy them later?
// probably not. simply restart and see the various service trackers
// do everything that is needed.
String name = (String) properties.get(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME);
if (name == null) { throw new ConfigurationException(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, "The name of the server is mandatory"); }
serverInstanceWrapper = new ServerInstanceWrapper(name);
_serversIndexedByPID.put(pid, serverInstanceWrapper);
_serversNameIndexedByPID.put(pid, name);
_serversPIDIndexedByName.put(name, pid);
try
{
serverInstanceWrapper.start(new Server(), properties);
}
catch (Exception e)
{
throw new ConfigurationException(null, "Error starting jetty server instance", e);
}
}
public synchronized void deleted(String pid)
{
ServerInstanceWrapper server = (ServerInstanceWrapper) _serversIndexedByPID.remove(pid);
String name = _serversNameIndexedByPID.remove(pid);
if (name != null)
{
_serversPIDIndexedByName.remove(name);
}
else
{
// something incorrect going on.
}
if (server != null)
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public synchronized ServerInstanceWrapper getServerByPID(String pid)
{
return _serversIndexedByPID.get(pid);
}
/**
* @param managedServerName The server name
* @return the corresponding jetty server wrapped with its deployment
* properties.
*/
public ServerInstanceWrapper getServerInstanceWrapper(String managedServerName)
{
String pid = _serversPIDIndexedByName.get(managedServerName);
return pid != null ? _serversIndexedByPID.get(pid) : null;
}
/**
* Helper method to create and configure a new Jetty Server via the
* ManagedServiceFactory
*
* @param contributor
* @param serverName
* @param urlsToJettyXml
* @throws Exception
*/
public static void createNewServer(Bundle contributor, String serverName, String urlsToJettyXml) throws Exception
{
ServiceReference configurationAdminReference = contributor.getBundleContext().getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin confAdmin = (ConfigurationAdmin) contributor.getBundleContext().getService(configurationAdminReference);
Configuration configuration = confAdmin.createFactoryConfiguration(OSGiServerConstants.MANAGED_JETTY_SERVER_FACTORY_PID, contributor.getLocation());
Dictionary properties = new Hashtable();
properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, serverName);
StringBuilder actualBundleUrls = new StringBuilder();
StringTokenizer tokenizer = new StringTokenizer(urlsToJettyXml, ",", false);
while (tokenizer.hasMoreTokens())
{
if (actualBundleUrls.length() != 0)
{
actualBundleUrls.append(",");
}
String token = tokenizer.nextToken();
if (token.indexOf(':') != -1)
{
// a complete url. no change needed:
actualBundleUrls.append(token);
}
else if (token.startsWith("/"))
{
// url relative to the contributor bundle:
URL url = contributor.getEntry(token);
if (url == null)
{
actualBundleUrls.append(token);
}
else
{
actualBundleUrls.append(url.toString());
}
}
}
properties.put(OSGiServerConstants.MANAGED_JETTY_XML_CONFIG_URLS, actualBundleUrls.toString());
configuration.update(properties);
}
}

View File

@ -46,7 +46,7 @@ import org.xml.sax.SAXParseException;
/** /**
* ServerInstanceWrapper * ServerInstanceWrapper
* *
* Configures a jetty Server instance. Can also be used from the ManagedServiceFactory * Configures and starts a jetty Server instance.
*/ */
public class ServerInstanceWrapper public class ServerInstanceWrapper
{ {