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  import org.osgi.util.tracker.ServiceTracker;
36  
37  /**
38   * JettyBootstrapActivator
39   * 
40   * Bootstrap jetty and publish a default Server instance as an OSGi service.
41   * 
42   * Listen for other Server instances to be published as services and support them as deployment targets.
43   * 
44   * Listen for Bundles to be activated, and deploy those that represent webapps/ContextHandlers to one of the known Server instances.
45   * 
46   */
47  public class JettyBootstrapActivator implements BundleActivator
48  {
49      private static final Logger LOG = Log.getLogger(JettyBootstrapActivator.class);
50      
51      private static JettyBootstrapActivator INSTANCE = null;
52  
53      public static JettyBootstrapActivator getInstance()
54      {
55          return INSTANCE;
56      }
57  
58      private ServiceRegistration _registeredServer;
59  
60      private ServiceTracker _contextHandlerTracker;
61      
62      private PackageAdminServiceTracker _packageAdminServiceTracker;
63  
64      private BundleTracker _webBundleTracker;
65  
66      private JettyServerServiceTracker _jettyServerServiceTracker;
67      
68      
69      
70      /* ------------------------------------------------------------ */
71      /**
72       * Setup a new jetty Server, registers it as a service. Setup the Service
73       * tracker for the jetty ContextHandlers that are in charge of deploying the
74       * webapps. Setup the BundleListener that supports the extender pattern for
75       * the jetty ContextHandler.
76       * 
77       * @param context
78       */
79      public void start(final BundleContext context) throws Exception
80      {
81          try {
82          INSTANCE = this;
83  
84          // track other bundles and fragments attached to this bundle that we
85          // should activate.
86          _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
87  
88          // track jetty Server instances that we should support as deployment targets
89          _jettyServerServiceTracker = new JettyServerServiceTracker();
90          context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")");
91  
92          // Create a default jetty instance right now.
93          Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
94          
95          // track ContextHandler class instances and deploy them to one of the known Servers
96          _contextHandlerTracker = new ServiceTracker(context, context.createFilter("(objectclass=" + ContextHandler.class.getName() + ")"), new ServiceWatcher());
97          _contextHandlerTracker.open();
98  
99          //Create a bundle tracker to help deploy webapps and ContextHandlers
100         BundleWatcher bundleTrackerCustomizer = new BundleWatcher();
101         bundleTrackerCustomizer.setWaitForDefaultServer(defaultServer != null);
102         _webBundleTracker =  new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, bundleTrackerCustomizer);
103         bundleTrackerCustomizer.setBundleTracker(_webBundleTracker);
104         bundleTrackerCustomizer.open();
105         } catch (Exception e) { e.printStackTrace();}
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 (_contextHandlerTracker != null)
127             {
128                 _contextHandlerTracker.close();
129                 _contextHandlerTracker = 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 }