1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
38
39
40
41
42
43
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
74
75
76
77
78
79
80 public void start(final BundleContext context) throws Exception
81 {
82 INSTANCE = this;
83 _bundleContext = context;
84
85
86
87 _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
88
89
90 _jettyServerServiceTracker = new JettyServerServiceTracker();
91 context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")");
92
93
94 _jettyContextHandlerTracker = new ServiceWatcher();
95 context.addServiceListener(_jettyContextHandlerTracker, "(objectclass=" + ContextHandler.class.getName() + ")");
96
97
98 Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
99
100
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
113
114
115
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
152 }
153 finally
154 {
155 _registeredServer = null;
156 }
157 }
158 }
159 finally
160 {
161 INSTANCE = null;
162 }
163 }
164 }