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