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 _webBundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, new WebBundleTrackerCustomizer());
109 _webBundleTracker.open();
110 }
111
112
113
114
115
116
117
118 public void stop(BundleContext context) throws Exception
119 {
120 try
121 {
122
123 if (_webBundleTracker != null)
124 {
125 _webBundleTracker.close();
126 _webBundleTracker = null;
127 }
128 if (_jettyContextHandlerTracker != null)
129 {
130 context.removeServiceListener(_jettyContextHandlerTracker);
131 _jettyContextHandlerTracker = null;
132 }
133 if (_jettyServerServiceTracker != null)
134 {
135 _jettyServerServiceTracker.stop();
136 context.removeServiceListener(_jettyServerServiceTracker);
137 _jettyServerServiceTracker = null;
138 }
139 if (_packageAdminServiceTracker != null)
140 {
141 _packageAdminServiceTracker.stop();
142 context.removeServiceListener(_packageAdminServiceTracker);
143 _packageAdminServiceTracker = null;
144 }
145 if (_registeredServer != null)
146 {
147 try
148 {
149 _registeredServer.unregister();
150 }
151 catch (IllegalArgumentException ill)
152 {
153
154 }
155 finally
156 {
157 _registeredServer = null;
158 }
159 }
160 }
161 finally
162 {
163 INSTANCE = null;
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178 public static void registerWebapplication(Bundle contributor, String webappFolderPath, String contextPath) throws Exception
179 {
180 checkBundleActivated();
181 WebAppContext contextHandler = new WebAppContext();
182 Dictionary<String,String> dic = new Hashtable<String,String>();
183 dic.put(OSGiWebappConstants.SERVICE_PROP_WAR, webappFolderPath);
184 dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH, contextPath);
185 String requireTldBundle = (String) contributor.getHeaders().get(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
186 if (requireTldBundle != null)
187 {
188 dic.put(OSGiWebappConstants.SERVICE_PROP_REQUIRE_TLD_BUNDLE, requireTldBundle);
189 }
190 contributor.getBundleContext().registerService(ContextHandler.class.getName(), contextHandler, dic);
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205 public static void registerWebapplication(Bundle contributor, String webappFolderPath, String contextPath, Dictionary<String, String> dic) throws Exception
206 {
207 checkBundleActivated();
208 WebAppContext contextHandler = new WebAppContext();
209 dic.put(OSGiWebappConstants.SERVICE_PROP_WAR, webappFolderPath);
210 dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH, contextPath);
211 contributor.getBundleContext().registerService(ContextHandler.class.getName(), contextHandler, dic);
212 }
213
214
215
216
217
218
219
220
221
222
223
224 public static void registerContext(Bundle contributor, String contextFilePath) throws Exception
225 {
226 registerContext(contributor, contextFilePath, new Hashtable<String, String>());
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240 public static void registerContext(Bundle contributor, String contextFilePath, Dictionary<String, String> dic) throws Exception
241 {
242 checkBundleActivated();
243 ContextHandler contextHandler = new ContextHandler();
244 dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH, contextFilePath);
245 dic.put(IWebBundleDeployerHelper.INTERNAL_SERVICE_PROP_UNKNOWN_CONTEXT_HANDLER_TYPE, Boolean.TRUE.toString());
246 contributor.getBundleContext().registerService(ContextHandler.class.getName(), contextHandler, dic);
247 }
248
249 public static void unregister(String contextPath)
250 {
251
252 }
253
254
255
256
257
258
259 private static void checkBundleActivated()
260 {
261 if (INSTANCE == null)
262 {
263 Bundle thisBundle = FrameworkUtil.getBundle(JettyBootstrapActivator.class);
264 try
265 {
266 thisBundle.start();
267 }
268 catch (BundleException e)
269 {
270
271 }
272 }
273 }
274
275
276
277
278 public static BundleContext getBundleContext()
279 {
280 checkBundleActivated();
281 return INSTANCE._bundleContext;
282 }
283
284 }