View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.osgi.boot;
20  
21  import org.eclipse.jetty.osgi.boot.internal.serverfactory.DefaultJettyAtJettyHomeHelper;
22  import org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker;
23  import org.eclipse.jetty.osgi.boot.internal.webapp.BundleWatcher;
24  import org.eclipse.jetty.osgi.boot.internal.webapp.ServiceWatcher;
25  import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.server.handler.ContextHandler;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  import org.osgi.framework.Bundle;
31  import org.osgi.framework.BundleActivator;
32  import org.osgi.framework.BundleContext;
33  import org.osgi.framework.ServiceRegistration;
34  import org.osgi.util.tracker.BundleTracker;
35  import org.osgi.util.tracker.ServiceTracker;
36  
37  /**
38   * JettyBootstrapActivator
39   * <p>
40   * Bootstrap jetty and publish a default Server instance as an OSGi service.
41   * <p>
42   * Listen for other Server instances to be published as services and support them as deployment targets.
43   * <p>
44   * Listen for Bundles to be activated, and deploy those that represent webapps/ContextHandlers to one of the known Server instances.
45   */
46  public class JettyBootstrapActivator implements BundleActivator
47  {
48      private static final Logger LOG = Log.getLogger(JettyBootstrapActivator.class);
49      
50      private static JettyBootstrapActivator INSTANCE = null;
51  
52      public static JettyBootstrapActivator getInstance()
53      {
54          return INSTANCE;
55      }
56  
57      private ServiceRegistration _registeredServer;
58  
59      private ServiceTracker _contextHandlerTracker;
60      
61      private PackageAdminServiceTracker _packageAdminServiceTracker;
62  
63      private BundleTracker _webBundleTracker;
64  
65      private JettyServerServiceTracker _jettyServerServiceTracker;
66      
67      
68      
69      /* ------------------------------------------------------------ */
70      /**
71       * Setup a new jetty Server, registers it as a service. Setup the Service
72       * tracker for the jetty ContextHandlers that are in charge of deploying the
73       * webapps. Setup the BundleListener that supports the extender pattern for
74       * the jetty ContextHandler.
75       * 
76       * @param context the bundle context
77       */
78      public void start(final BundleContext context) throws Exception
79      {
80          try {
81          INSTANCE = this;
82  
83          // track other bundles and fragments attached to this bundle that we
84          // should activate.
85          _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
86  
87          // track jetty Server instances that we should support as deployment targets
88          _jettyServerServiceTracker = new JettyServerServiceTracker();
89          context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")");
90  
91          // Create a default jetty instance right now.
92          Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
93          
94          // track ContextHandler class instances and deploy them to one of the known Servers
95          _contextHandlerTracker = new ServiceTracker(context, context.createFilter("(objectclass=" + ContextHandler.class.getName() + ")"), new ServiceWatcher());
96          _contextHandlerTracker.open();
97  
98          //Create a bundle tracker to help deploy webapps and ContextHandlers
99          BundleWatcher bundleTrackerCustomizer = new BundleWatcher();
100         bundleTrackerCustomizer.setWaitForDefaultServer(defaultServer != null);
101         _webBundleTracker =  new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, bundleTrackerCustomizer);
102         bundleTrackerCustomizer.setBundleTracker(_webBundleTracker);
103         bundleTrackerCustomizer.open();
104         } catch (Exception e) { e.printStackTrace();}
105     }
106     
107     
108     
109     /* ------------------------------------------------------------ */
110     /**
111      * Stop the activator.
112      * 
113      * @see
114      * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
115      */
116     public void stop(BundleContext context) throws Exception
117     {
118         try
119         {
120             if (_webBundleTracker != null)
121             {
122                 _webBundleTracker.close();
123                 _webBundleTracker = null;
124             }
125             if (_contextHandlerTracker != null)
126             {
127                 _contextHandlerTracker.close();
128                 _contextHandlerTracker = null;
129             }
130             if (_jettyServerServiceTracker != null)
131             {
132                 _jettyServerServiceTracker.stop();
133                 context.removeServiceListener(_jettyServerServiceTracker);
134                 _jettyServerServiceTracker = null;
135             }
136             if (_packageAdminServiceTracker != null)
137             {
138                 _packageAdminServiceTracker.stop();
139                 context.removeServiceListener(_packageAdminServiceTracker);
140                 _packageAdminServiceTracker = null;
141             }
142             if (_registeredServer != null)
143             {
144                 try
145                 {
146                     _registeredServer.unregister();
147                 }
148                 catch (IllegalArgumentException ill)
149                 {
150                     // already unregistered.
151                 }
152                 finally
153                 {
154                     _registeredServer = null;
155                 }
156             }
157         }
158         finally
159         {
160             INSTANCE = null;
161         }
162     }
163 }