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