1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.eclipse.jetty.osgi.boot;
16
17 import java.util.Dictionary;
18 import java.util.Hashtable;
19
20 import org.eclipse.jetty.osgi.boot.internal.serverfactory.DefaultJettyAtJettyHomeHelper;
21 import org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker;
22 import org.eclipse.jetty.osgi.boot.internal.webapp.IWebBundleDeployerHelper;
23 import org.eclipse.jetty.osgi.boot.internal.webapp.JettyContextHandlerServiceTracker;
24 import org.eclipse.jetty.osgi.boot.internal.webapp.WebBundleTrackerCustomizer;
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.webapp.WebAppContext;
29 import org.osgi.framework.Bundle;
30 import org.osgi.framework.BundleActivator;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.BundleException;
33 import org.osgi.framework.FrameworkUtil;
34 import org.osgi.framework.ServiceRegistration;
35 import org.osgi.util.tracker.BundleTracker;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class JettyBootstrapActivator implements BundleActivator
59 {
60
61 private static JettyBootstrapActivator INSTANCE = null;
62
63 public static JettyBootstrapActivator getInstance()
64 {
65 return INSTANCE;
66 }
67
68 private ServiceRegistration _registeredServer;
69 private Server _server;
70 private JettyContextHandlerServiceTracker _jettyContextHandlerTracker;
71 private PackageAdminServiceTracker _packageAdminServiceTracker;
72 private BundleTracker _webBundleTracker;
73 private BundleContext _bundleContext;
74
75
76 private JettyServerServiceTracker _jettyServerServiceTracker;
77
78
79
80
81
82
83
84
85
86
87 public void start(BundleContext context) throws Exception
88 {
89 INSTANCE = this;
90 _bundleContext = context;
91
92
93
94 _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
95
96 _jettyServerServiceTracker = new JettyServerServiceTracker();
97 context.addServiceListener(_jettyServerServiceTracker,"(objectclass=" + Server.class.getName() + ")");
98
99
100
101
102
103
104
105
106 _jettyContextHandlerTracker = new JettyContextHandlerServiceTracker(_jettyServerServiceTracker);
107
108
109
110 context.addServiceListener(_jettyContextHandlerTracker,"(objectclass=" + ContextHandler.class.getName() + ")");
111
112
113 DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
114
115
116 _webBundleTracker = new BundleTracker(context,
117 Bundle.ACTIVE | Bundle.STOPPING, new WebBundleTrackerCustomizer());
118 _webBundleTracker.open();
119
120 }
121
122
123
124
125
126
127
128 public void stop(BundleContext context) throws Exception
129 {
130 try
131 {
132
133 if (_webBundleTracker != null)
134 {
135 _webBundleTracker.close();
136 _webBundleTracker = null;
137 }
138 if (_jettyContextHandlerTracker != null)
139 {
140 _jettyContextHandlerTracker.stop();
141 context.removeServiceListener(_jettyContextHandlerTracker);
142 _jettyContextHandlerTracker = null;
143 }
144 if (_jettyServerServiceTracker != null)
145 {
146 _jettyServerServiceTracker.stop();
147 context.removeServiceListener(_jettyServerServiceTracker);
148 _jettyServerServiceTracker = null;
149 }
150 if (_packageAdminServiceTracker != null)
151 {
152 _packageAdminServiceTracker.stop();
153 context.removeServiceListener(_packageAdminServiceTracker);
154 _packageAdminServiceTracker = null;
155 }
156 if (_registeredServer != null)
157 {
158 try
159 {
160 _registeredServer.unregister();
161 }
162 catch (IllegalArgumentException ill)
163 {
164
165 }
166 finally
167 {
168 _registeredServer = null;
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 }
188 finally
189 {
190 if (_server != null)
191 {
192 _server.stop();
193 }
194 INSTANCE = null;
195 }
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public static void registerWebapplication(Bundle contributor, String webappFolderPath, String contextPath) throws Exception
213 {
214 checkBundleActivated();
215 WebAppContext contextHandler = new WebAppContext();
216 Dictionary dic = new Hashtable();
217 dic.put(OSGiWebappConstants.SERVICE_PROP_WAR,webappFolderPath);
218 dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH,contextPath);
219 String requireTldBundle = (String)contributor.getHeaders().get(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
220 if (requireTldBundle != null) {
221 dic.put(OSGiWebappConstants.SERVICE_PROP_REQUIRE_TLD_BUNDLE, requireTldBundle);
222 }
223 contributor.getBundleContext().registerService(ContextHandler.class.getName(),contextHandler,dic);
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 public static void registerWebapplication(Bundle contributor, String webappFolderPath, String contextPath, Dictionary<String, String> dic) throws Exception
243 {
244 checkBundleActivated();
245 WebAppContext contextHandler = new WebAppContext();
246 dic.put(OSGiWebappConstants.SERVICE_PROP_WAR,webappFolderPath);
247 dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH,contextPath);
248 contributor.getBundleContext().registerService(ContextHandler.class.getName(),contextHandler,dic);
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263 public static void registerContext(Bundle contributor, String contextFilePath) throws Exception
264 {
265 registerContext(contributor,contextFilePath,new Hashtable<String, String>());
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 public static void registerContext(Bundle contributor, String contextFilePath, Dictionary<String, String> dic) throws Exception
283 {
284 checkBundleActivated();
285 ContextHandler contextHandler = new ContextHandler();
286 dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH,contextFilePath);
287 dic.put(IWebBundleDeployerHelper.INTERNAL_SERVICE_PROP_UNKNOWN_CONTEXT_HANDLER_TYPE,Boolean.TRUE.toString());
288 contributor.getBundleContext().registerService(ContextHandler.class.getName(),contextHandler,dic);
289 }
290
291 public static void unregister(String contextPath)
292 {
293
294 }
295
296
297
298
299
300
301 private static void checkBundleActivated()
302 {
303 if (INSTANCE == null)
304 {
305 Bundle thisBundle = FrameworkUtil.getBundle(JettyBootstrapActivator.class);
306 try
307 {
308 thisBundle.start();
309 }
310 catch (BundleException e)
311 {
312
313 }
314 }
315 }
316
317
318
319
320 public static BundleContext getBundleContext()
321 {
322 checkBundleActivated();
323 return INSTANCE._bundleContext;
324 }
325
326
327 }