diff options
2 files changed, 120 insertions, 91 deletions
diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/JettyBootstrapActivator.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/JettyBootstrapActivator.java index 8e50e9c318..43f19f255f 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/JettyBootstrapActivator.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/JettyBootstrapActivator.java @@ -32,6 +32,7 @@ import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.util.tracker.BundleTracker; +import org.osgi.util.tracker.ServiceTracker; /** * JettyBootstrapActivator @@ -56,14 +57,12 @@ public class JettyBootstrapActivator implements BundleActivator private ServiceRegistration _registeredServer; - private ServiceWatcher _jettyContextHandlerTracker; - + private ServiceTracker _contextHandlerTracker; + private PackageAdminServiceTracker _packageAdminServiceTracker; private BundleTracker _webBundleTracker; - private BundleContext _bundleContext; - private JettyServerServiceTracker _jettyServerServiceTracker; @@ -79,8 +78,8 @@ public class JettyBootstrapActivator implements BundleActivator */ public void start(final BundleContext context) throws Exception { + try { INSTANCE = this; - _bundleContext = context; // track other bundles and fragments attached to this bundle that we // should activate. @@ -90,12 +89,12 @@ public class JettyBootstrapActivator implements BundleActivator _jettyServerServiceTracker = new JettyServerServiceTracker(); context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")"); - // track ContextHandler class instances and deploy them to one of the known Servers - _jettyContextHandlerTracker = new ServiceWatcher(); - context.addServiceListener(_jettyContextHandlerTracker, "(objectclass=" + ContextHandler.class.getName() + ")"); - // Create a default jetty instance right now. Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context); + + // track ContextHandler class instances and deploy them to one of the known Servers + _contextHandlerTracker = new ServiceTracker(context, context.createFilter("(objectclass=" + ContextHandler.class.getName() + ")"), new ServiceWatcher()); + _contextHandlerTracker.open(); //Create a bundle tracker to help deploy webapps and ContextHandlers BundleWatcher bundleTrackerCustomizer = new BundleWatcher(); @@ -103,6 +102,7 @@ public class JettyBootstrapActivator implements BundleActivator _webBundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, bundleTrackerCustomizer); bundleTrackerCustomizer.setBundleTracker(_webBundleTracker); bundleTrackerCustomizer.open(); + } catch (Exception e) { e.printStackTrace();} } @@ -123,10 +123,10 @@ public class JettyBootstrapActivator implements BundleActivator _webBundleTracker.close(); _webBundleTracker = null; } - if (_jettyContextHandlerTracker != null) + if (_contextHandlerTracker != null) { - context.removeServiceListener(_jettyContextHandlerTracker); - _jettyContextHandlerTracker = null; + _contextHandlerTracker.close(); + _contextHandlerTracker = null; } if (_jettyServerServiceTracker != null) { diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/ServiceWatcher.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/ServiceWatcher.java index 9b4763fd1e..0cd7d8b4fa 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/ServiceWatcher.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/ServiceWatcher.java @@ -33,10 +33,9 @@ import org.eclipse.jetty.util.log.Logger; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; /** * ServiceWatcher @@ -47,9 +46,9 @@ import org.osgi.util.tracker.ServiceTracker; * * ContextHandlers and WebApps can also be deployed into jetty without creating them as * osgi services. Instead, they can be deployed via manifest headers inside bundles. See - * {@link WebBundleTrackerCustomizer}. + * {@link BundleWatcher}. */ -public class ServiceWatcher implements ServiceListener +public class ServiceWatcher implements ServiceTrackerCustomizer { private static Logger LOG = Log.getLogger(ServiceWatcher.class); @@ -104,97 +103,127 @@ public class ServiceWatcher implements ServiceListener return candidates; } + + + /* ------------------------------------------------------------ */ - /** - * Receives notification that a service has had a lifecycle change. + /** + * A Service that is a ContextHandler is detected. + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference) + */ + @Override + public Object addingService(ServiceReference reference) + { + BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext(); + ContextHandler contextHandler = (ContextHandler) context.getService(reference); + return addService(context, contextHandler, reference); + } + + + /* ------------------------------------------------------------ */ + /** + * A Service that is a ContextHandler has been modified. We + * undeploy and then redeploy the ContextHandler. * - * @param ev The <code>ServiceEvent</code> object. + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object) */ + @Override + public void modifiedService(ServiceReference reference, Object service) + { + BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext(); + ContextHandler contextHandler = (ContextHandler) context.getService(reference); + removeService (context, contextHandler, reference); + addService (context, contextHandler, reference); + } + + + /* ------------------------------------------------------------ */ /** - * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent) + * A Service that is a ContextHandler is being removed. + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object) */ - public void serviceChanged(ServiceEvent ev) + @Override + public void removedService(ServiceReference reference, Object service) { - ServiceReference sr = ev.getServiceReference(); - switch (ev.getType()) + BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext(); + ContextHandler contextHandler = (ContextHandler) context.getService(reference); + removeService (context, contextHandler, reference); + } + + + + /* ------------------------------------------------------------ */ + /** Deploy ContextHandler that is a Service. + * + * @param reference + * @return + */ + public Object addService (BundleContext context, ContextHandler contextHandler, ServiceReference reference) + { + if (contextHandler.getServer() != null) + { + // is configured elsewhere. + return context.getService(reference); + } + String watermark = (String)reference.getProperty(OSGiWebappConstants.WATERMARK); + if (watermark != null && !"".equals(watermark)) + return context.getService(reference); //one of our deployers just registered the context as an OSGi service, so we can ignore it + + //Get a jetty deployer targetted to the named server instance, or the default one if not named + String serverName = (String)reference.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); + Map<ServiceReference, ServiceProvider> candidates = getDeployers(serverName); + if (candidates != null) { - case ServiceEvent.MODIFIED: - case ServiceEvent.UNREGISTERING: + boolean added = false; + Iterator<Entry<ServiceReference, ServiceProvider>> itor = candidates.entrySet().iterator(); + while (!added && itor.hasNext()) { - BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext(); - ContextHandler contextHandler = (ContextHandler) context.getService(sr); - - //if this was not a service that another of our deployers may have deployed (in which case they will undeploy it) - String watermark = (String)sr.getProperty(OSGiWebappConstants.WATERMARK); - - //Get a jetty deployer targetted to the named server instance, or the default one if not named - //The individual deployer will decide if it can remove the context or not - String serverName = (String)sr.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); - Map<ServiceReference, ServiceProvider> candidates = getDeployers(serverName); - if (candidates != null) + Entry<ServiceReference, ServiceProvider> e = itor.next(); + try { - boolean removed = false; - Iterator<Entry<ServiceReference, ServiceProvider>> itor = candidates.entrySet().iterator(); - while (!removed && itor.hasNext()) - { - Entry<ServiceReference, ServiceProvider> e = itor.next(); - try - { - removed = e.getValue().serviceRemoved(sr, contextHandler); - } - catch (Exception x) - { - LOG.warn("Error undeploying service representing jetty context ", x); - } - } + added = e.getValue().serviceAdded(reference, contextHandler); + if (added && LOG.isDebugEnabled()) + LOG.debug("Provider "+e.getValue()+" deployed "+contextHandler); + } + catch (Exception x) + { + LOG.warn("Error deploying service representing jetty context", x); } } - if (ev.getType() == ServiceEvent.UNREGISTERING) - { - break; - } - else - { - // modified, meaning: we reload it. now that we stopped it; - // we can register it. - } - case ServiceEvent.REGISTERED: + } + return context.getService(reference); + } + + + + + /* ------------------------------------------------------------ */ + /** + * Undeploy a ContextHandler that is a Service. + * + * @param reference + */ + public void removeService (BundleContext context, ContextHandler contextHandler, ServiceReference reference) + { + //Get a jetty deployer targetted to the named server instance, or the default one if not named + //The individual deployer will decide if it can remove the context or not + String serverName = (String)reference.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); + Map<ServiceReference, ServiceProvider> candidates = getDeployers(serverName); + if (candidates != null) + { + boolean removed = false; + Iterator<Entry<ServiceReference, ServiceProvider>> itor = candidates.entrySet().iterator(); + while (!removed && itor.hasNext()) { - Bundle contributor = sr.getBundle(); - BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext(); - ContextHandler contextHandler = (ContextHandler) context.getService(sr); - if (contextHandler.getServer() != null) + Entry<ServiceReference, ServiceProvider> e = itor.next(); + try { - // is configured elsewhere. - return; + removed = e.getValue().serviceRemoved(reference, contextHandler); } - String watermark = (String)sr.getProperty(OSGiWebappConstants.WATERMARK); - if (watermark != null && !"".equals(watermark)) - return; //one of our deployers just registered the context as an OSGi service, so we can ignore it - - //Get a jetty deployer targetted to the named server instance, or the default one if not named - String serverName = (String)sr.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); - Map<ServiceReference, ServiceProvider> candidates = getDeployers(serverName); - if (candidates != null) + catch (Exception x) { - boolean added = false; - Iterator<Entry<ServiceReference, ServiceProvider>> itor = candidates.entrySet().iterator(); - while (!added && itor.hasNext()) - { - Entry<ServiceReference, ServiceProvider> e = itor.next(); - try - { - added = e.getValue().serviceAdded(sr, contextHandler); - if (added && LOG.isDebugEnabled()) - LOG.debug("Provider "+e.getValue()+" deployed "+contextHandler); - } - catch (Exception x) - { - LOG.warn("Error deploying service representing jetty context", x); - } - } + LOG.warn("Error undeploying service representing jetty context ", x); } - break; } } } |