View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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  
36  /**
37   * JettyBootstrapActivator
38   * 
39   * Bootstrap jetty and publish a default Server instance as an OSGi service.
40   * 
41   * Listen for other Server instances to be published as services and support them as deployment targets.
42   * 
43   * Listen for Bundles to be activated, and deploy those that represent webapps/ContextHandlers to one of the known Server instances.
44   * 
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 ServiceWatcher _jettyContextHandlerTracker;
60  
61      private PackageAdminServiceTracker _packageAdminServiceTracker;
62  
63      private BundleTracker _webBundleTracker;
64  
65      private BundleContext _bundleContext;
66  
67      private JettyServerServiceTracker _jettyServerServiceTracker;
68      
69      
70      
71      /* ------------------------------------------------------------ */
72      /**
73       * Setup a new jetty Server, registers it as a service. Setup the Service
74       * tracker for the jetty ContextHandlers that are in charge of deploying the
75       * webapps. Setup the BundleListener that supports the extender pattern for
76       * the jetty ContextHandler.
77       * 
78       * @param context
79       */
80      public void start(final BundleContext context) throws Exception
81      {
82          INSTANCE = this;
83          _bundleContext = context;
84  
85          // track other bundles and fragments attached to this bundle that we
86          // should activate.
87          _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
88  
89          // track jetty Server instances that we should support as deployment targets
90          _jettyServerServiceTracker = new JettyServerServiceTracker();
91          context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")");
92  
93          // track ContextHandler class instances and deploy them to one of the known Servers
94          _jettyContextHandlerTracker = new ServiceWatcher();
95          context.addServiceListener(_jettyContextHandlerTracker, "(objectclass=" + ContextHandler.class.getName() + ")");
96  
97          // Create a default jetty instance right now.
98          Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
99  
100         //Create a bundle tracker to help deploy webapps and ContextHandlers
101         BundleWatcher bundleTrackerCustomizer = new BundleWatcher();
102         bundleTrackerCustomizer.setWaitForDefaultServer(defaultServer != null);
103         _webBundleTracker =  new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, bundleTrackerCustomizer);
104         bundleTrackerCustomizer.setBundleTracker(_webBundleTracker);
105         bundleTrackerCustomizer.open();
106     }
107     
108     
109     
110     /* ------------------------------------------------------------ */
111     /**
112      * Stop the activator.
113      * 
114      * @see
115      * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
116      */
117     public void stop(BundleContext context) throws Exception
118     {
119         try
120         {
121             if (_webBundleTracker != null)
122             {
123                 _webBundleTracker.close();
124                 _webBundleTracker = null;
125             }
126             if (_jettyContextHandlerTracker != null)
127             {
128                 context.removeServiceListener(_jettyContextHandlerTracker);
129                 _jettyContextHandlerTracker = null;
130             }
131             if (_jettyServerServiceTracker != null)
132             {
133                 _jettyServerServiceTracker.stop();
134                 context.removeServiceListener(_jettyServerServiceTracker);
135                 _jettyServerServiceTracker = null;
136             }
137             if (_packageAdminServiceTracker != null)
138             {
139                 _packageAdminServiceTracker.stop();
140                 context.removeServiceListener(_packageAdminServiceTracker);
141                 _packageAdminServiceTracker = null;
142             }
143             if (_registeredServer != null)
144             {
145                 try
146                 {
147                     _registeredServer.unregister();
148                 }
149                 catch (IllegalArgumentException ill)
150                 {
151                     // already unregistered.
152                 }
153                 finally
154                 {
155                     _registeredServer = null;
156                 }
157             }
158         }
159         finally
160         {
161             INSTANCE = null;
162         }
163     }
164 }