From 2f1d2dac729d1d08258fe67ecc7a8af22be221ec Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 27 Jul 2016 17:46:41 +1000 Subject: [PATCH 1/2] Fixed Debug NPE #754 --- .../java/org/eclipse/jetty/server/CachedContentFactory.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/CachedContentFactory.java b/jetty-server/src/main/java/org/eclipse/jetty/server/CachedContentFactory.java index 36561e453fa..42ba4c8584f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/CachedContentFactory.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/CachedContentFactory.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; @@ -55,6 +56,7 @@ import org.eclipse.jetty.util.resource.ResourceFactory; public class CachedContentFactory implements HttpContent.ContentFactory { private static final Logger LOG = Log.getLogger(CachedContentFactory.class); + private final static Map NO_PRECOMPRESSED = Collections.unmodifiableMap(Collections.emptyMap()); private final ConcurrentMap _cache; private final AtomicInteger _cachedSize; @@ -456,7 +458,7 @@ public class CachedContentFactory implements HttpContent.ContentFactory } else { - _precompressed = null; + _precompressed = NO_PRECOMPRESSED; } } @@ -678,7 +680,7 @@ public class CachedContentFactory implements HttpContent.ContentFactory @Override public String toString() { - return String.format("CachedContent@%x{r=%s,e=%b,lm=%s,ct=%s,c=%b}",hashCode(),_resource,_resource.exists(),_lastModified,_contentType,!_precompressed.isEmpty()); + return String.format("CachedContent@%x{r=%s,e=%b,lm=%s,ct=%s,c=%d}",hashCode(),_resource,_resource.exists(),_lastModified,_contentType,_precompressed.size()); } /* ------------------------------------------------------------ */ From 37fa778ea03aba953712348426393136e2314658 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Wed, 27 Jul 2016 20:12:10 +1000 Subject: [PATCH 2/2] Issue #213 --- .../osgi/boot/BundleContextProvider.java | 66 ++++ .../jetty/osgi/boot/BundleWebAppProvider.java | 64 ++++ .../osgi/boot/JettyBootstrapActivator.java | 49 +-- .../osgi/boot/ServiceContextProvider.java | 66 ++++ .../osgi/boot/ServiceWebAppProvider.java | 66 ++++ .../JettyServerServiceTracker.java | 154 +++------ .../boot/internal/webapp/BundleWatcher.java | 307 ------------------ .../boot/internal/webapp/ServiceWatcher.java | 223 ------------- .../eclipse/jetty/osgi/boot/utils/Util.java | 32 +- jetty-osgi/pom.xml | 1 + .../main/java/com/acme/osgi/Activator.java | 9 +- jetty-osgi/test-jetty-osgi-server/pom.xml | 84 +++++ .../main/java/com/acme/osgi/Activator.java | 68 ++++ .../src/main/resources/index.html | 6 + jetty-osgi/test-jetty-osgi-webapp/pom.xml | 6 + .../main/java/com/acme/osgi/Activator.java | 39 +-- .../src/main/resources/webappA/index.html | 6 + .../src/main/resources/webappB/index.html | 6 + jetty-osgi/test-jetty-osgi/pom.xml | 7 +- .../TestJettyOSGiBootWebAppAsService.java | 35 +- 20 files changed, 588 insertions(+), 706 deletions(-) delete mode 100644 jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/BundleWatcher.java delete mode 100644 jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/ServiceWatcher.java create mode 100644 jetty-osgi/test-jetty-osgi-server/pom.xml create mode 100644 jetty-osgi/test-jetty-osgi-server/src/main/java/com/acme/osgi/Activator.java create mode 100644 jetty-osgi/test-jetty-osgi-server/src/main/resources/index.html create mode 100644 jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappA/index.html create mode 100644 jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappB/index.html diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleContextProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleContextProvider.java index 6a6d71385b1..353f7a3c881 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleContextProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleContextProvider.java @@ -27,11 +27,15 @@ import java.util.Map; import org.eclipse.jetty.deploy.App; import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper; +import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceRegistration; +import org.osgi.util.tracker.BundleTracker; /** * BundleContextProvider @@ -47,6 +51,62 @@ public class BundleContextProvider extends AbstractContextProvider implements Bu private Map> _bundleMap = new HashMap>(); private ServiceRegistration _serviceRegForBundles; + + private BundleTracker _tracker; + + + public class ContextBundleTracker extends BundleTracker + { + protected String _managedServerName; + + public ContextBundleTracker (BundleContext bundleContext, String managedServerName) + { + super (bundleContext, Bundle.ACTIVE | Bundle.STOPPING,null); + _managedServerName = managedServerName; + } + + /** + * @see org.osgi.util.tracker.BundleTracker#addingBundle(org.osgi.framework.Bundle, org.osgi.framework.BundleEvent) + */ + @Override + public Object addingBundle(Bundle bundle, BundleEvent event) + { + try + { + String serverName = (String)bundle.getHeaders().get(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); + if ((StringUtil.isBlank(serverName) && _managedServerName.equals(OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME)) + || (!StringUtil.isBlank(serverName) && (serverName.equals(_managedServerName)))) + { + if (bundleAdded (bundle)) + return bundle; + } + } + catch (Exception e) + { + LOG.warn(e); + } + return null; + } + + + + /** + * @see org.osgi.util.tracker.BundleTracker#removedBundle(org.osgi.framework.Bundle, org.osgi.framework.BundleEvent, java.lang.Object) + */ + @Override + public void removedBundle(Bundle bundle, BundleEvent event, Object object) + { + try + { + bundleRemoved(bundle); + } + catch (Exception e) + { + LOG.warn(e); + } + } + + } /* ------------------------------------------------------------ */ public BundleContextProvider(ServerInstanceWrapper wrapper) @@ -59,6 +119,10 @@ public class BundleContextProvider extends AbstractContextProvider implements Bu @Override protected void doStart() throws Exception { + //Track bundles that are ContextHandlers that should be deployed + _tracker = new ContextBundleTracker(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), getServerInstanceWrapper().getManagedServerName()); + _tracker.open(); + //register as an osgi service for deploying contexts defined in a bundle, advertising the name of the jetty Server instance we are related to Dictionary properties = new Hashtable(); properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName()); @@ -70,6 +134,8 @@ public class BundleContextProvider extends AbstractContextProvider implements Bu @Override protected void doStop() throws Exception { + _tracker.close(); + //unregister ourselves if (_serviceRegForBundles != null) { diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java index 8d60de063c3..43bf2c4733c 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java @@ -26,11 +26,15 @@ import java.util.Map; import org.eclipse.jetty.deploy.App; import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper; import org.eclipse.jetty.osgi.boot.utils.Util; +import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceRegistration; +import org.osgi.util.tracker.BundleTracker; /** * BundleWebAppProvider @@ -48,6 +52,62 @@ public class BundleWebAppProvider extends AbstractWebAppProvider implements Bund private ServiceRegistration _serviceRegForBundles; + private WebAppTracker _webappTracker; + + + public class WebAppTracker extends BundleTracker + { + protected String _managedServerName; + + public WebAppTracker (BundleContext bundleContext, String managedServerName) + { + super (bundleContext, Bundle.ACTIVE | Bundle.STOPPING,null); + _managedServerName = managedServerName; + } + + /** + * @see org.osgi.util.tracker.BundleTracker#addingBundle(org.osgi.framework.Bundle, org.osgi.framework.BundleEvent) + */ + @Override + public Object addingBundle(Bundle bundle, BundleEvent event) + { + try + { + String serverName = (String)bundle.getHeaders().get(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); + if ((StringUtil.isBlank(serverName) && _managedServerName.equals(OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME)) + || (!StringUtil.isBlank(serverName) && (serverName.equals(_managedServerName)))) + { + if (bundleAdded (bundle)) + return bundle; + } + } + catch (Exception e) + { + LOG.warn(e); + } + return null; + } + + + + /** + * @see org.osgi.util.tracker.BundleTracker#removedBundle(org.osgi.framework.Bundle, org.osgi.framework.BundleEvent, java.lang.Object) + */ + @Override + public void removedBundle(Bundle bundle, BundleEvent event, Object object) + { + try + { + bundleRemoved(bundle); + } + catch (Exception e) + { + LOG.warn(e); + } + } + + + } /* ------------------------------------------------------------ */ public BundleWebAppProvider (ServerInstanceWrapper wrapper) @@ -61,6 +121,8 @@ public class BundleWebAppProvider extends AbstractWebAppProvider implements Bund */ protected void doStart() throws Exception { + _webappTracker = new WebAppTracker(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), getServerInstanceWrapper().getManagedServerName()); + _webappTracker.open(); //register as an osgi service for deploying bundles, advertising the name of the jetty Server instance we are related to Dictionary properties = new Hashtable(); properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName()); @@ -75,6 +137,8 @@ public class BundleWebAppProvider extends AbstractWebAppProvider implements Bund @Override protected void doStop() throws Exception { + _webappTracker.close(); + //unregister ourselves if (_serviceRegForBundles != null) { 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 a41904f6df7..969a5769a0e 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 @@ -20,18 +20,13 @@ package org.eclipse.jetty.osgi.boot; import org.eclipse.jetty.osgi.boot.internal.serverfactory.DefaultJettyAtJettyHomeHelper; import org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker; -import org.eclipse.jetty.osgi.boot.internal.webapp.BundleWatcher; -import org.eclipse.jetty.osgi.boot.internal.webapp.ServiceWatcher; import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import org.osgi.framework.Bundle; 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; /** @@ -55,14 +50,10 @@ public class JettyBootstrapActivator implements BundleActivator } private ServiceRegistration _registeredServer; - - private ServiceTracker _contextHandlerTracker; private PackageAdminServiceTracker _packageAdminServiceTracker; - private BundleTracker _webBundleTracker; - - private JettyServerServiceTracker _jettyServerServiceTracker; + private ServiceTracker _jettyServerServiceTracker; @@ -77,7 +68,6 @@ public class JettyBootstrapActivator implements BundleActivator */ public void start(final BundleContext context) throws Exception { - try { INSTANCE = this; // track other bundles and fragments attached to this bundle that we @@ -85,27 +75,15 @@ public class JettyBootstrapActivator implements BundleActivator _packageAdminServiceTracker = new PackageAdminServiceTracker(context); // track jetty Server instances that we should support as deployment targets - _jettyServerServiceTracker = new JettyServerServiceTracker(); - context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")"); - + _jettyServerServiceTracker = new ServiceTracker(context, context.createFilter("(objectclass=" + Server.class.getName() + ")"), new JettyServerServiceTracker()); + _jettyServerServiceTracker.open(); + // 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(); - bundleTrackerCustomizer.setWaitForDefaultServer(defaultServer != null); - _webBundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, bundleTrackerCustomizer); - bundleTrackerCustomizer.setBundleTracker(_webBundleTracker); - bundleTrackerCustomizer.open(); - } catch (Exception e) { e.printStackTrace();} } - - - + + + /* ------------------------------------------------------------ */ /** * Stop the activator. @@ -117,20 +95,9 @@ public class JettyBootstrapActivator implements BundleActivator { try { - if (_webBundleTracker != null) - { - _webBundleTracker.close(); - _webBundleTracker = null; - } - if (_contextHandlerTracker != null) - { - _contextHandlerTracker.close(); - _contextHandlerTracker = null; - } if (_jettyServerServiceTracker != null) { - _jettyServerServiceTracker.stop(); - context.removeServiceListener(_jettyServerServiceTracker); + _jettyServerServiceTracker.close(); _jettyServerServiceTracker = null; } if (_packageAdminServiceTracker != null) diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceContextProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceContextProvider.java index d8059fbb428..3b37748fb5e 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceContextProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceContextProvider.java @@ -27,14 +27,19 @@ import org.eclipse.jetty.deploy.App; import org.eclipse.jetty.deploy.AppProvider; import org.eclipse.jetty.deploy.DeploymentManager; import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper; +import org.eclipse.jetty.osgi.boot.utils.Util; import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; +import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; +import org.osgi.util.tracker.ServiceTracker; /** * ServiceContextProvider @@ -51,6 +56,55 @@ public class ServiceContextProvider extends AbstractContextProvider implements S private ServiceRegistration _serviceRegForServices; + ServiceTracker _tracker; + + + /** + * ContextTracker + * + * + */ + public class ContextTracker extends ServiceTracker + { + + public ContextTracker (BundleContext bundleContext, Filter filter) + { + super(bundleContext, filter, null); + } + + /** + * @see org.osgi.util.tracker.ServiceTracker#addingService(org.osgi.framework.ServiceReference) + */ + @Override + public Object addingService(ServiceReference reference) + { + ContextHandler h = (ContextHandler)context.getService(reference); + serviceAdded (reference, h); + return h; + } + + /** + * @see org.osgi.util.tracker.ServiceTracker#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void modifiedService(ServiceReference reference, Object service) + { + removedService(reference,service); + addingService(reference); + } + + /** + * @see org.osgi.util.tracker.ServiceTracker#removedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void removedService(ServiceReference reference, Object service) + { + context.ungetService(reference); + serviceRemoved(reference, (ContextHandler)service); + } + } + + /** * ServiceApp @@ -162,6 +216,15 @@ public class ServiceContextProvider extends AbstractContextProvider implements S @Override protected void doStart() throws Exception { + + BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + + //Start a tracker to find webapps that are osgi services that are targetted to my server name + _tracker = new ContextTracker (bundleContext, + Util.createFilter(bundleContext, ContextHandler.class.getName(), getServerInstanceWrapper().getManagedServerName())); + _tracker.open(); + + //register as an osgi service for deploying contexts defined in a bundle, advertising the name of the jetty Server instance we are related to Dictionary properties = new Hashtable(); properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName()); @@ -175,6 +238,9 @@ public class ServiceContextProvider extends AbstractContextProvider implements S @Override protected void doStop() throws Exception { + if (_tracker != null) + _tracker.close(); + //unregister ourselves if (_serviceRegForServices != null) { diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java index d99b1893ee2..583d6ddafe8 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java @@ -27,14 +27,19 @@ import org.eclipse.jetty.deploy.App; import org.eclipse.jetty.deploy.AppProvider; import org.eclipse.jetty.deploy.DeploymentManager; import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper; +import org.eclipse.jetty.osgi.boot.utils.Util; import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.webapp.WebAppContext; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; +import org.osgi.util.tracker.ServiceTracker; /** * ServiceWebAppProvider @@ -52,6 +57,58 @@ public class ServiceWebAppProvider extends AbstractWebAppProvider implements Ser private ServiceRegistration _serviceRegForServices; + private ServiceTracker webappTracker; + + + /** + * WebAppTracker + * + * + */ + public class WebAppTracker extends ServiceTracker + { + /** + * @param bundleContext the osgi context + * @param filter the osgi filter for the tracker + */ + public WebAppTracker (BundleContext bundleContext, Filter filter) + { + super(bundleContext, filter, null); + } + + /** + * @see org.osgi.util.tracker.ServiceTracker#addingService(org.osgi.framework.ServiceReference) + */ + @Override + public Object addingService(ServiceReference reference) + { + WebAppContext wac = (WebAppContext)context.getService(reference); + serviceAdded (reference, wac); + return wac; + } + + /** + * @see org.osgi.util.tracker.ServiceTracker#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void modifiedService(ServiceReference reference, Object service) + { + removedService(reference,service); + addingService(reference); + } + + /** + * @see org.osgi.util.tracker.ServiceTracker#removedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void removedService(ServiceReference reference, Object service) + { + serviceRemoved(reference, (WebAppContext)service); + context.ungetService(reference); + } + } + + /** * ServiceApp * @@ -207,6 +264,13 @@ public class ServiceWebAppProvider extends AbstractWebAppProvider implements Ser */ protected void doStart() throws Exception { + BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + + //Start a tracker to find webapps that are osgi services that are targetted to my server name + webappTracker = new WebAppTracker (bundleContext, + Util.createFilter(bundleContext, WebAppContext.class.getName(), getServerInstanceWrapper().getManagedServerName())); + webappTracker.open(); + //register as an osgi service for deploying bundles, advertising the name of the jetty Server instance we are related to Dictionary properties = new Hashtable(); properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName()); @@ -223,6 +287,8 @@ public class ServiceWebAppProvider extends AbstractWebAppProvider implements Ser @Override protected void doStop() throws Exception { + webappTracker.close(); + //unregister ourselves if (_serviceRegForServices != null) { diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/JettyServerServiceTracker.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/JettyServerServiceTracker.java index b7f334337f4..b6f05a5b6d2 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/JettyServerServiceTracker.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/JettyServerServiceTracker.java @@ -18,8 +18,6 @@ package org.eclipse.jetty.osgi.boot.internal.serverfactory; -import java.util.HashMap; -import java.util.Map; import java.util.Properties; import org.eclipse.jetty.osgi.boot.OSGiServerConstants; @@ -27,9 +25,8 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.osgi.framework.Bundle; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTrackerCustomizer; /** * JettyServerServiceTracker @@ -38,116 +35,71 @@ import org.osgi.framework.ServiceReference; * webapps or ContextHandlers discovered from the OSGi environment. * */ -public class JettyServerServiceTracker implements ServiceListener +public class JettyServerServiceTracker implements ServiceTrackerCustomizer { private static Logger LOG = Log.getLogger(JettyServerServiceTracker.class.getName()); - /** The context-handler to deactivate indexed by ServerInstanceWrapper */ - private Map _indexByServiceReference = new HashMap(); - - /** - * Stops each one of the registered servers. + /** + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference) */ - public void stop() + @Override + public Object addingService(ServiceReference sr) { - for (ServerInstanceWrapper wrapper : _indexByServiceReference.values()) + Bundle contributor = sr.getBundle(); + Server server = (Server) contributor.getBundleContext().getService(sr); + String name = (String) sr.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); + if (name == null) { throw new IllegalArgumentException("The property " + OSGiServerConstants.MANAGED_JETTY_SERVER_NAME + " is mandatory"); } + if (LOG.isDebugEnabled()) LOG.debug("Adding Server {}", name); + ServerInstanceWrapper wrapper = new ServerInstanceWrapper(name); + Properties props = new Properties(); + for (String key : sr.getPropertyKeys()) + { + Object value = sr.getProperty(key); + props.put(key, value); + } + try + { + wrapper.start(server, props); + LOG.info("Started Server {}", name); + return wrapper; + } + catch (Exception e) + { + LOG.warn(e); + return sr.getBundle().getBundleContext().getService(sr); + } + } + + /** + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void modifiedService(ServiceReference reference, Object service) + { + removedService(reference, service); + addingService(reference); + } + + /** + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void removedService(ServiceReference reference, Object service) + { + if (service instanceof ServerInstanceWrapper) { try { + ServerInstanceWrapper wrapper = (ServerInstanceWrapper)service; wrapper.stop(); + LOG.info("Stopped Server {}",wrapper.getManagedServerName()); } - catch (Throwable t) + catch (Exception e) { - LOG.warn(t); + LOG.warn(e); } } - } - - /** - * Receives notification that a service has had a lifecycle change. - * - * @param ev The ServiceEvent object. - */ - public void serviceChanged(ServiceEvent ev) - { - ServiceReference sr = ev.getServiceReference(); - switch (ev.getType()) - { - case ServiceEvent.MODIFIED: - case ServiceEvent.UNREGISTERING: - { - ServerInstanceWrapper instance = unregisterInIndex(ev.getServiceReference()); - if (instance != null) - { - try - { - instance.stop(); - } - catch (Exception e) - { - LOG.warn(e); - } - } - - if (ev.getType() == ServiceEvent.UNREGISTERING) - { - break; - } - else - { - // modified, meaning: we reload it. now that we stopped it; - // we can register it. - } - } - case ServiceEvent.REGISTERED: - { - try - { - Bundle contributor = sr.getBundle(); - Server server = (Server) contributor.getBundleContext().getService(sr); - ServerInstanceWrapper wrapper = registerInIndex(server, sr); - Properties props = new Properties(); - for (String key : sr.getPropertyKeys()) - { - Object value = sr.getProperty(key); - props.put(key, value); - } - wrapper.start(server, props); - } - catch (Exception e) - { - LOG.warn(e); - } - break; - } - } - } - - private ServerInstanceWrapper registerInIndex(Server server, ServiceReference sr) - { - String name = (String) sr.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); - if (name == null) { throw new IllegalArgumentException("The property " + OSGiServerConstants.MANAGED_JETTY_SERVER_NAME + " is mandatory"); } - ServerInstanceWrapper wrapper = new ServerInstanceWrapper(name); - _indexByServiceReference.put(sr, wrapper); - return wrapper; - } - - /** - * Returns the ContextHandler to stop. - * - * @param reg - * @return the ContextHandler to stop. - */ - private ServerInstanceWrapper unregisterInIndex(ServiceReference sr) - { - ServerInstanceWrapper handler = _indexByServiceReference.remove(sr); - if (handler == null) - { - LOG.warn("Unknown Jetty Server ServiceReference: ", sr); - return null; - } - - return handler; + } } diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/BundleWatcher.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/BundleWatcher.java deleted file mode 100644 index 87ce60ae41b..00000000000 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/BundleWatcher.java +++ /dev/null @@ -1,307 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// 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. -// ======================================================================== -// - -package org.eclipse.jetty.osgi.boot.internal.webapp; - - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; - -import org.eclipse.jetty.osgi.boot.BundleProvider; -import org.eclipse.jetty.osgi.boot.OSGiServerConstants; -import org.eclipse.jetty.osgi.boot.utils.TldBundleDiscoverer; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleEvent; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.BundleTracker; -import org.osgi.util.tracker.BundleTrackerCustomizer; -import org.osgi.util.tracker.ServiceTracker; - -/** - * BundleWatcher - * - * Tracks the installation and removal of Bundles in the OSGi environment. Any bundles - * that are added are passed to the set of Jetty DeploymentManager providers to see if - * the bundle should be deployed as a webapp or ContextHandler into Jetty. - */ -public class BundleWatcher implements BundleTrackerCustomizer -{ - private static final Logger LOG = Log.getLogger(BundleWatcher.class); - - public static final Collection JSP_REGISTRATION_HELPERS = new ArrayList(); - - - public static final String FILTER = "(objectclass=" + BundleProvider.class.getName() + ")"; - private ServiceTracker _serviceTracker; - private BundleTracker _bundleTracker; - private boolean _waitForDefaultServer = true; - private boolean _defaultServerReady = false; - private Bundle _bundle = null; - - - - /* ------------------------------------------------------------ */ - public BundleWatcher() throws Exception - { - _bundle = FrameworkUtil.getBundle(this.getClass()); - //Track all BundleProviders (Jetty DeploymentManager Providers that can deploy bundles) - _serviceTracker = new ServiceTracker(_bundle.getBundleContext(), FrameworkUtil.createFilter(FILTER),null); - _serviceTracker.open(); - } - - - /* ------------------------------------------------------------ */ - public boolean isWaitForDefaultServer() - { - return _waitForDefaultServer; - } - - - /* ------------------------------------------------------------ */ - public void setWaitForDefaultServer(boolean waitForDefaultServer) - { - _waitForDefaultServer = waitForDefaultServer; - } - - - /* ------------------------------------------------------------ */ - public void setBundleTracker (BundleTracker bundleTracker) - { - _bundleTracker = bundleTracker; - } - - - /* ------------------------------------------------------------ */ - public void open () throws Exception - { - if (_waitForDefaultServer && !_defaultServerReady) - { - String filter = "(&(objectclass=" + BundleProvider.class.getName() + ")"+ - "("+OSGiServerConstants.MANAGED_JETTY_SERVER_NAME+"="+OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME+"))"; - - ServiceTracker defaultServerTracker = new ServiceTracker(_bundle.getBundleContext(), - FrameworkUtil.createFilter(filter),null) - { - public Object addingService(ServiceReference reference) - { - try - { - Object object = super.addingService(reference); - LOG.debug("Default Jetty Server registered {}", reference); - _defaultServerReady = true; - openBundleTracker(); - return object; - } - catch (Exception e) - { - throw new IllegalStateException(e); - } - } - }; - defaultServerTracker.open(); - } - else - openBundleTracker(); - } - - /* ------------------------------------------------------------ */ - public Map getDeployers(String managedServerName) - { - if (managedServerName == null) - managedServerName = OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME; - - Map candidates = new HashMap(); - - ServiceReference[] references = _serviceTracker.getServiceReferences(); - if (references != null) - { - for (ServiceReference ref:references) - { - String name = (String)ref.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); - if (managedServerName.equalsIgnoreCase(name)) - { - BundleProvider candidate = (BundleProvider)_serviceTracker.getService(ref); - if (candidate != null) - candidates.put(ref, candidate); - } - } - } - return candidates; - } - - /* ------------------------------------------------------------ */ - /** - * A bundle is being added to the BundleTracker. - * - *

- * This method is called before a bundle which matched the search parameters - * of the BundleTracker is added to the - * BundleTracker. This method should return the object to be - * tracked for the specified Bundle. The returned object is - * stored in the BundleTracker and is available from the - * {@link BundleTracker#getObject(Bundle) getObject} method. - * - * @param bundle The Bundle being added to the - * BundleTracker. - * @param event The bundle event which caused this customizer method to be - * called or null if there is no bundle event - * associated with the call to this method. - * @return The object to be tracked for the specified Bundle - * object or null if the specified Bundle - * object should not be tracked. - */ - public Object addingBundle(Bundle bundle, BundleEvent event) - { - if (bundle.getState() == Bundle.ACTIVE) - { - register(bundle); - } - else if (bundle.getState() == Bundle.STOPPING) - { - unregister(bundle); - } - else - { - // we should not be called in that state as - // we are registered only for ACTIVE and STOPPING - } - return null; - } - - - /* ------------------------------------------------------------ */ - /** - * A bundle tracked by the BundleTracker has been modified. - * - *

- * This method is called when a bundle being tracked by the - * BundleTracker has had its state modified. - * - * @param bundle The Bundle whose state has been modified. - * @param event The bundle event which caused this customizer method to be - * called or null if there is no bundle event - * associated with the call to this method. - * @param object The tracked object for the specified bundle. - */ - public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) - { - if (bundle.getState() == Bundle.STOPPING || bundle.getState() == Bundle.ACTIVE) - { - unregister(bundle); - } - if (bundle.getState() == Bundle.ACTIVE) - { - register(bundle); - } - } - - - /* ------------------------------------------------------------ */ - /** - * A bundle tracked by the BundleTracker has been removed. - * - *

- * This method is called after a bundle is no longer being tracked by the - * BundleTracker. - * - * @param bundle The Bundle that has been removed. - * @param event The bundle event which caused this customizer method to be - * called or null if there is no bundle event - * associated with the call to this method. - * @param object The tracked object for the specified bundle. - */ - public void removedBundle(Bundle bundle, BundleEvent event, Object object) - { - unregister(bundle); - } - - - protected void openBundleTracker() - { - _bundleTracker.open(); - } - - /* ------------------------------------------------------------ */ - /** - * @param bundle - * @return true if this bundle can be deployed into Jetty - */ - private boolean register(Bundle bundle) - { - if (bundle == null) - return false; - - //It might be a bundle that is deployable by Jetty. - //Use any named Server instance provided, defaulting to the default Server instance if none supplied - boolean deployed = false; - String serverName = (String)bundle.getHeaders().get(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); - Map candidates = getDeployers(serverName); - if (candidates != null) - { - Iterator> itor = candidates.entrySet().iterator(); - while (!deployed && itor.hasNext()) - { - Entry e = itor.next(); - try - { - deployed = e.getValue().bundleAdded(bundle); - } - catch (Exception x) - { - LOG.warn("Error deploying bundle for jetty context", x); - } - } - } - - return deployed; - } - - /* ------------------------------------------------------------ */ - /** - * @param bundle - */ - private void unregister(Bundle bundle) - { - boolean undeployed = false; - String serverName = (String)bundle.getHeaders().get(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); - Map candidates = getDeployers(serverName); - if (candidates != null) - { - Iterator> itor = candidates.entrySet().iterator(); - while (!undeployed && itor.hasNext()) - { - Entry e = itor.next(); - try - { - undeployed = e.getValue().bundleRemoved(bundle); - } - catch (Exception x) - { - LOG.warn("Error undeploying Bundle representing jetty deployable ", x); - } - } - } - } -} 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 deleted file mode 100644 index 023a03aeb20..00000000000 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/ServiceWatcher.java +++ /dev/null @@ -1,223 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// 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. -// ======================================================================== -// - -package org.eclipse.jetty.osgi.boot.internal.webapp; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; - -import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator; -import org.eclipse.jetty.osgi.boot.OSGiServerConstants; -import org.eclipse.jetty.osgi.boot.OSGiWebappConstants; -import org.eclipse.jetty.osgi.boot.ServiceProvider; -import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.util.log.Log; -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.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; -import org.osgi.util.tracker.ServiceTrackerCustomizer; - -/** - * ServiceWatcher - * - * When a {@link ContextHandler} is activated as an osgi service we find a jetty deployer - * for it. The ContextHandler could be either a WebAppContext or any other derivative of - * ContextHandler. - * - * 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 BundleWatcher}. - */ -public class ServiceWatcher implements ServiceTrackerCustomizer -{ - private static Logger LOG = Log.getLogger(ServiceWatcher.class); - - public static final String FILTER = "(objectclass=" + ServiceProvider.class.getName() + ")"; - - //track all instances of deployers of webapps as bundles - ServiceTracker _serviceTracker; - - - /* ------------------------------------------------------------ */ - public ServiceWatcher() throws Exception - { - //track all instances of deployers of webapps - Bundle myBundle = FrameworkUtil.getBundle(this.getClass()); - _serviceTracker = new ServiceTracker(myBundle.getBundleContext(), FrameworkUtil.createFilter(FILTER),null); - _serviceTracker.open(); - } - - - - /* ------------------------------------------------------------ */ - public Map getDeployers(String managedServerName) - { - if (managedServerName == null) - managedServerName = OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME; - - Map candidates = new HashMap(); - - ServiceReference[] references = _serviceTracker.getServiceReferences(); - if (references != null) - { - for (ServiceReference ref:references) - { - String name = (String)ref.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME); - if (managedServerName.equalsIgnoreCase(name)) - { - ServiceProvider candidate = (ServiceProvider)_serviceTracker.getService(ref); - if (candidate != null) - candidates.put(ref, candidate); - } - } - } - return candidates; - } - - - - - /* ------------------------------------------------------------ */ - /** - * 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. - * - * @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); - } - - - /* ------------------------------------------------------------ */ - /** - * A Service that is a ContextHandler is being removed. - * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object) - */ - @Override - public void removedService(ServiceReference reference, Object service) - { - BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext(); - ContextHandler contextHandler = (ContextHandler) context.getService(reference); - removeService (context, contextHandler, reference); - } - - - - /* ------------------------------------------------------------ */ - /** Deploy ContextHandler that is a Service. - * @param context the bundle context - * @param contextHandler the context handler - * @param reference the service reference - * @return the object added - */ - 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 candidates = getDeployers(serverName); - if (candidates != null) - { - boolean added = false; - Iterator> itor = candidates.entrySet().iterator(); - while (!added && itor.hasNext()) - { - Entry e = itor.next(); - try - { - 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); - } - } - } - return context.getService(reference); - } - - - - - /* ------------------------------------------------------------ */ - /** - * Undeploy a ContextHandler that is a Service. - * @param context the bundle context - * @param contextHandler the context handler - * @param reference the service 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 candidates = getDeployers(serverName); - if (candidates != null) - { - boolean removed = false; - Iterator> itor = candidates.entrySet().iterator(); - while (!removed && itor.hasNext()) - { - Entry e = itor.next(); - try - { - removed = e.getValue().serviceRemoved(reference, contextHandler); - } - catch (Exception x) - { - LOG.warn("Error undeploying service representing jetty context ", x); - } - } - } - } -} diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java index 31fe0eaa2c8..6672ff3e1aa 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java @@ -19,13 +19,18 @@ package org.eclipse.jetty.osgi.boot.utils; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Dictionary; import java.util.List; import java.util.StringTokenizer; +import org.eclipse.jetty.osgi.boot.OSGiServerConstants; +import org.eclipse.jetty.util.StringUtil; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Filter; +import org.osgi.framework.InvalidSyntaxException; + /** * Various useful functions utility methods for OSGi wide use. */ @@ -34,6 +39,31 @@ public class Util public static final String DEFAULT_DELIMS = ",;"; + /** + * Create an osgi filter for the given classname and server name. + * + * @param bundleContext + * @param classname the class to match on the filter + * @param managedServerName the name of the jetty server instance + * @return a new filter + * + * @throws InvalidSyntaxException + */ + public static Filter createFilter (BundleContext bundleContext, String classname, String managedServerName) throws InvalidSyntaxException + { + if (StringUtil.isBlank(managedServerName) || managedServerName.equals(OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME)) + { + return bundleContext.createFilter("(&(objectclass=" + classname + + ")(|(managedServerName="+managedServerName + +")(!(managedServerName=*))))"); + } + else + { + return bundleContext.createFilter("(&(objectclass=" + classname+ ")(managedServerName="+managedServerName+"))"); + } + + } + /** * Get the value of a manifest header. * diff --git a/jetty-osgi/pom.xml b/jetty-osgi/pom.xml index 5f7883833f9..50f9c044f85 100644 --- a/jetty-osgi/pom.xml +++ b/jetty-osgi/pom.xml @@ -26,6 +26,7 @@ test-jetty-osgi-webapp test-jetty-osgi-context test-jetty-osgi-fragment + test-jetty-osgi-server jetty-osgi-alpn test-jetty-osgi diff --git a/jetty-osgi/test-jetty-osgi-context/src/main/java/com/acme/osgi/Activator.java b/jetty-osgi/test-jetty-osgi-context/src/main/java/com/acme/osgi/Activator.java index 262cbabe413..3bbb4f45c58 100644 --- a/jetty-osgi/test-jetty-osgi-context/src/main/java/com/acme/osgi/Activator.java +++ b/jetty-osgi/test-jetty-osgi-context/src/main/java/com/acme/osgi/Activator.java @@ -27,6 +27,7 @@ import javax.servlet.ServletContextListener; import org.eclipse.jetty.server.handler.ContextHandler; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; /** * Bootstrap a ContextHandler @@ -36,6 +37,7 @@ import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { + private ServiceRegistration _sr; /** * * @param context @@ -48,20 +50,20 @@ public class Activator implements BundleActivator @Override public void contextInitialized(ServletContextEvent sce) { - // System.err.println("Context is initialized"); + //System.err.println("Context is initialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { - // System.err.println("CONTEXT IS DESTROYED!"); + //System.err.println("CONTEXT IS DESTROYED!"); } }); Dictionary props = new Hashtable(); props.put("contextPath","/acme"); props.put("Jetty-ContextFilePath", "acme.xml"); - context.registerService(ContextHandler.class.getName(),ch,props); + _sr = context.registerService(ContextHandler.class.getName(),ch,props); } /** @@ -72,5 +74,6 @@ public class Activator implements BundleActivator */ public void stop(BundleContext context) throws Exception { + _sr.unregister(); } } diff --git a/jetty-osgi/test-jetty-osgi-server/pom.xml b/jetty-osgi/test-jetty-osgi-server/pom.xml new file mode 100644 index 00000000000..bd609f680f7 --- /dev/null +++ b/jetty-osgi/test-jetty-osgi-server/pom.xml @@ -0,0 +1,84 @@ + + + org.eclipse.jetty.osgi + jetty-osgi-project + 9.4.0-SNAPSHOT + + 4.0.0 + test-jetty-osgi-server + Jetty :: OSGi :: Server + Test Jetty OSGi bundle with a Server + http://www.eclipse.org/jetty + + ${project.groupId}.testserver + + + + org.eclipse.jetty + jetty-server + ${project.version} + + + org.eclipse.osgi + org.eclipse.osgi + provided + + + org.eclipse.osgi + org.eclipse.osgi.services + provided + + + org.eclipse.jetty.toolchain + jetty-schemas + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + + true + + + + org.apache.felix + maven-bundle-plugin + true + + + ${bundle-symbolic-name} + Jetty OSGi Test Server + com.acme.osgi.Activator + J2SE-1.5 + + <_nouses>true + + javax.servlet;version="[3.1,3.2)", + javax.servlet.resources;version="[3.1,3.2)", + org.osgi.framework, + org.osgi.service.cm;version="1.2.0", + org.osgi.service.packageadmin, + org.osgi.service.startlevel;version="1.0.o", + org.osgi.service.url;version="1.0.0", + org.osgi.util.tracker;version="1.3.0", + org.slf4j;resolution:=optional, + org.slf4j.spi;resolution:=optional, + org.slf4j.helpers;resolution:=optional, + org.xml.sax, + org.xml.sax.helpers, + * + + org.eclipse.jetty.*;version="[9.1,10.0)" + + + + + + + diff --git a/jetty-osgi/test-jetty-osgi-server/src/main/java/com/acme/osgi/Activator.java b/jetty-osgi/test-jetty-osgi-server/src/main/java/com/acme/osgi/Activator.java new file mode 100644 index 00000000000..27d1fbe61d2 --- /dev/null +++ b/jetty-osgi/test-jetty-osgi-server/src/main/java/com/acme/osgi/Activator.java @@ -0,0 +1,68 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// 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. +// ======================================================================== +// + +package com.acme.osgi; + +import java.util.Dictionary; +import java.util.Hashtable; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +/** + * Bootstrap a Server + * + * + */ +public class Activator implements BundleActivator +{ + + private ServiceRegistration _sr; + + /** + * + * @param context + */ + public void start(BundleContext context) throws Exception + { + Server server = new Server(9999); + ContextHandlerCollection contexts = new ContextHandlerCollection(); + server.setHandler(contexts); + + Dictionary serverProps = new Hashtable(); + //define the unique name of the server instance + serverProps.put("managedServerName", "fooServer"); + //serverProps.put("jetty.http.port", "9999"); + //register as an OSGi Service for Jetty to find + _sr = context.registerService(Server.class.getName(), server, serverProps); + } + + /** + * Stop the activator. + * + * @see + * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception + { + _sr.unregister(); + } +} diff --git a/jetty-osgi/test-jetty-osgi-server/src/main/resources/index.html b/jetty-osgi/test-jetty-osgi-server/src/main/resources/index.html new file mode 100644 index 00000000000..9e62c04bb91 --- /dev/null +++ b/jetty-osgi/test-jetty-osgi-server/src/main/resources/index.html @@ -0,0 +1,6 @@ + + +

Test OSGi WebApp

+

Webapp registered by bundle as service successfully deployed.

+ + diff --git a/jetty-osgi/test-jetty-osgi-webapp/pom.xml b/jetty-osgi/test-jetty-osgi-webapp/pom.xml index 25565e007a3..534ae36d465 100644 --- a/jetty-osgi/test-jetty-osgi-webapp/pom.xml +++ b/jetty-osgi/test-jetty-osgi-webapp/pom.xml @@ -31,6 +31,12 @@ + + + src/main/resources + + + org.apache.maven.plugins diff --git a/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java b/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java index 5ca58be8c3f..a2d26bd131e 100644 --- a/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java +++ b/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java @@ -25,6 +25,7 @@ import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.webapp.WebAppContext; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; /** * Bootstrap a webapp @@ -34,6 +35,9 @@ import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { + private ServiceRegistration _srA; + private ServiceRegistration _srB; + /** * * @param context @@ -42,29 +46,24 @@ public class Activator implements BundleActivator { String serverName = "defaultJettyServer"; - /* Uncomment to create a different server instance to deploy to. Also change - * TestJettyOSGiBootWebAppAsService to use the port 9999 - - Server server = new Server(); - //do any setup on Server in here - serverName = "fooServer"; - Dictionary serverProps = new Hashtable(); - //define the unique name of the server instance - serverProps.put("managedServerName", serverName); - serverProps.put("jetty.http.port", "9999"); - //let Jetty apply some configuration files to the Server instance - serverProps.put("jetty.etc.config.urls", "file:/opt/jetty/etc/jetty.xml,file:/opt/jetty/etc/jetty-selector.xml,file:/opt/jetty/etc/jetty-deployer.xml"); - //register as an OSGi Service for Jetty to find - context.registerService(Server.class.getName(), server, serverProps); - */ + - //Create a webapp context as a Service and target it at the Server created above + //Create webappA as a Service and target it at the default server WebAppContext webapp = new WebAppContext(); Dictionary props = new Hashtable(); - props.put("war","."); + props.put("war","webappA"); props.put("contextPath","/acme"); - props.put("managedServerName", serverName); - context.registerService(ContextHandler.class.getName(),webapp,props); + props.put("managedServerName", "defaultJettyServer"); + _srA = context.registerService(WebAppContext.class.getName(),webapp,props); + + //Create a second webappB as a Service and target it at a custom Server + //deployed by another bundle + WebAppContext webappB = new WebAppContext(); + Dictionary propsB = new Hashtable(); + propsB.put("war", "webappB"); + propsB.put("contextPath", "/acme"); + propsB.put("managedServerName", "fooServer"); + _srB = context.registerService(WebAppContext.class.getName(), webappB, propsB); } /** @@ -75,5 +74,7 @@ public class Activator implements BundleActivator */ public void stop(BundleContext context) throws Exception { + _srA.unregister(); + _srB.unregister(); } } diff --git a/jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappA/index.html b/jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappA/index.html new file mode 100644 index 00000000000..bc4c64faa54 --- /dev/null +++ b/jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappA/index.html @@ -0,0 +1,6 @@ + + +

Test OSGi WebAppA

+

Webapp registered by bundle as service successfully deployed.

+ + diff --git a/jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappB/index.html b/jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappB/index.html new file mode 100644 index 00000000000..4c6a3ae9b22 --- /dev/null +++ b/jetty-osgi/test-jetty-osgi-webapp/src/main/resources/webappB/index.html @@ -0,0 +1,6 @@ + + +

Test OSGi WebAppB

+

Webapp registered by bundle as service successfully deployed.

+ + diff --git a/jetty-osgi/test-jetty-osgi/pom.xml b/jetty-osgi/test-jetty-osgi/pom.xml index 19abddb01a0..75663acf2d2 100644 --- a/jetty-osgi/test-jetty-osgi/pom.xml +++ b/jetty-osgi/test-jetty-osgi/pom.xml @@ -368,7 +368,12 @@ ${project.version} test - + + org.eclipse.jetty.osgi + test-jetty-osgi-server + ${project.version} + test + org.eclipse.jetty.tests test-mock-resources diff --git a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java index 5777e3bfac7..1e141781fc4 100644 --- a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java +++ b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java @@ -83,7 +83,8 @@ public class TestJettyOSGiBootWebAppAsService options.addAll(Arrays.asList(options(systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value(LOG_LEVEL)))); options.addAll(Arrays.asList(options(systemProperty("org.eclipse.jetty.LEVEL").value(LOG_LEVEL)))); - options.addAll(jspDependencies()); + options.addAll(TestJettyOSGiBootCore.jspDependencies()); + options.addAll(testDependencies()); return options.toArray(new Option[options.size()]); } @@ -107,15 +108,21 @@ public class TestJettyOSGiBootWebAppAsService return options; } - public static List