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