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.io.File;
22 import java.net.URL;
23 import java.util.Dictionary;
24 import java.util.HashMap;
25
26 import org.eclipse.jetty.deploy.App;
27 import org.eclipse.jetty.deploy.AppProvider;
28 import org.eclipse.jetty.deploy.DeploymentManager;
29 import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
30 import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelperFactory;
31 import org.eclipse.jetty.osgi.boot.utils.OSGiClassLoader;
32 import org.eclipse.jetty.server.handler.ContextHandler;
33 import org.eclipse.jetty.util.component.AbstractLifeCycle;
34 import org.eclipse.jetty.util.log.Log;
35 import org.eclipse.jetty.util.log.Logger;
36 import org.eclipse.jetty.util.resource.JarResource;
37 import org.eclipse.jetty.util.resource.Resource;
38 import org.eclipse.jetty.xml.XmlConfiguration;
39 import org.osgi.framework.Bundle;
40
41
42
43
44
45
46
47
48
49
50
51 public abstract class AbstractContextProvider extends AbstractLifeCycle implements AppProvider
52 {
53 private static final Logger LOG = Log.getLogger(AbstractContextProvider.class);
54
55 private DeploymentManager _deploymentManager;
56
57 private ServerInstanceWrapper _serverWrapper;
58
59
60
61
62
63
64
65
66
67
68 public class OSGiApp extends AbstractOSGiApp
69 {
70 private String _contextFile;
71 private ContextHandler _contextHandler;
72 private boolean _configured = false;
73
74 public OSGiApp(DeploymentManager manager, AppProvider provider, String originId, Bundle bundle, String contextFile)
75 {
76 super(manager, provider, bundle, originId);
77 _contextFile = contextFile;
78 }
79
80 public OSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary properties, String contextFile, String originId)
81 {
82 super(manager, provider, bundle, properties, originId);
83 _contextFile = contextFile;
84 }
85
86 public String getContextFile ()
87 {
88 return _contextFile;
89 }
90
91 public void setHandler(ContextHandler h)
92 {
93 _contextHandler = h;
94 }
95
96 public ContextHandler createContextHandler()
97 throws Exception
98 {
99 configureContextHandler();
100 return _contextHandler;
101 }
102
103 public void configureContextHandler()
104 throws Exception
105 {
106 if (_configured)
107 return;
108
109 _configured = true;
110
111
112 String bundleOverrideLocation = (String)_properties.get(OSGiWebappConstants.JETTY_BUNDLE_INSTALL_LOCATION_OVERRIDE);
113 if (bundleOverrideLocation == null)
114 bundleOverrideLocation = (String)_properties.get(OSGiWebappConstants.SERVICE_PROP_BUNDLE_INSTALL_LOCATION_OVERRIDE);
115
116
117 File bundleLocation = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(_bundle);
118 File root = (bundleOverrideLocation==null?bundleLocation:new File(bundleOverrideLocation));
119 Resource rootResource = Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(root.toURI().toURL()));
120
121
122 if (rootResource.exists()&& !rootResource.isDirectory() && !rootResource.toString().startsWith("jar:"))
123 {
124 Resource jarResource = JarResource.newJarResource(rootResource);
125 if (jarResource.exists() && jarResource.isDirectory())
126 rootResource = jarResource;
127 }
128
129
130 if (_contextHandler != null && _contextHandler.getBaseResource() == null)
131 {
132 _contextHandler.setBaseResource(rootResource);
133 }
134
135
136 OSGiClassLoader classLoader = new OSGiClassLoader(getServerInstanceWrapper().getParentClassLoaderForWebapps(), _bundle);
137
138
139 if (_contextFile == null && _contextHandler == null)
140 throw new IllegalStateException("No context file or ContextHandler");
141
142 if (_contextFile != null)
143 {
144
145 Resource res = null;
146
147
148 if (_contextFile.startsWith("/"))
149 res = getFileAsResource(_contextFile);
150
151
152 if (res == null)
153 {
154
155 String jettyHome = (String)getServerInstanceWrapper().getServer().getAttribute(OSGiServerConstants.JETTY_HOME);
156 if (jettyHome != null)
157 res = getFileAsResource(jettyHome, _contextFile);
158
159
160 if (res == null)
161 {
162 jettyHome = System.getProperty(OSGiServerConstants.JETTY_HOME);
163
164 if (jettyHome != null)
165 {
166 if (jettyHome.startsWith("\"") || jettyHome.startsWith("'"))
167 jettyHome = jettyHome.substring(1);
168 if (jettyHome.endsWith("\"") || (jettyHome.endsWith("'")))
169 jettyHome = jettyHome.substring(0,jettyHome.length()-1);
170
171 res = getFileAsResource(jettyHome, _contextFile);
172 LOG.debug("jetty home context file: {}",res);
173 }
174 }
175 }
176
177
178 if (res == null)
179 {
180 if (bundleOverrideLocation != null)
181 {
182 try(Resource location=Resource.newResource(bundleOverrideLocation))
183 {
184 res=location.addPath(_contextFile);
185 }
186 LOG.debug("Bundle override location context file: {}",res);
187 }
188 }
189
190
191 if (res == null)
192 {
193 if (_contextFile.startsWith("./"))
194 _contextFile = _contextFile.substring(1);
195
196 if (!_contextFile.startsWith("/"))
197 _contextFile = "/" + _contextFile;
198
199 URL contextURL = _bundle.getEntry(_contextFile);
200 if (contextURL != null)
201 res = Resource.newResource(contextURL);
202 }
203
204
205
206 if (res != null)
207 {
208 ClassLoader cl = Thread.currentThread().getContextClassLoader();
209
210 LOG.debug("Context classloader = " + cl);
211 try
212 {
213 Thread.currentThread().setContextClassLoader(classLoader);
214
215 XmlConfiguration xmlConfiguration = new XmlConfiguration(res.getInputStream());
216 HashMap properties = new HashMap();
217
218 properties.put("Server", getServerInstanceWrapper().getServer());
219
220 properties.put(OSGiWebappConstants.JETTY_BUNDLE_ROOT, rootResource.toString());
221
222
223 xmlConfiguration.getProperties().putAll(properties);
224
225 if (_contextHandler == null)
226 _contextHandler = (ContextHandler) xmlConfiguration.configure();
227 else
228 xmlConfiguration.configure(_contextHandler);
229 }
230 finally
231 {
232 Thread.currentThread().setContextClassLoader(cl);
233 }
234 }
235 }
236
237
238 _contextHandler.setClassLoader(classLoader);
239
240
241
242 String contextPath = (String)_properties.get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
243 if (contextPath == null)
244 contextPath = (String)_properties.get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH);
245 if (contextPath != null)
246 _contextHandler.setContextPath(contextPath);
247
248
249 _contextHandler.setAttribute(OSGiWebappConstants.OSGI_BUNDLECONTEXT, _bundle.getBundleContext());
250
251
252 String[] targets = _contextHandler.getProtectedTargets();
253 int length = (targets==null?0:targets.length);
254
255 String[] updatedTargets = null;
256 if (targets != null)
257 {
258 updatedTargets = new String[length+OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length];
259 System.arraycopy(targets, 0, updatedTargets, 0, length);
260
261 }
262 else
263 updatedTargets = new String[OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length];
264 System.arraycopy(OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS, 0, updatedTargets, length, OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length);
265 _contextHandler.setProtectedTargets(updatedTargets);
266
267 }
268
269
270 private Resource getFileAsResource (String dir, String file)
271 {
272 Resource r = null;
273 try
274 {
275 File asFile = new File (dir, file);
276 if (asFile.exists())
277 r = Resource.newResource(asFile);
278 }
279 catch (Exception e)
280 {
281 r = null;
282 }
283 return r;
284 }
285
286 private Resource getFileAsResource (String file)
287 {
288 Resource r = null;
289 try
290 {
291 File asFile = new File (file);
292 if (asFile.exists())
293 r = Resource.newResource(asFile);
294 }
295 catch (Exception e)
296 {
297 r = null;
298 }
299 return r;
300 }
301 }
302
303
304 public AbstractContextProvider(ServerInstanceWrapper wrapper)
305 {
306 _serverWrapper = wrapper;
307 }
308
309
310
311 public ServerInstanceWrapper getServerInstanceWrapper()
312 {
313 return _serverWrapper;
314 }
315
316
317
318
319
320 public ContextHandler createContextHandler(App app) throws Exception
321 {
322 if (app == null)
323 return null;
324 if (!(app instanceof OSGiApp))
325 throw new IllegalStateException(app+" is not a BundleApp");
326
327
328 ContextHandler h = ((OSGiApp)app).createContextHandler();
329 return h;
330 }
331
332
333 public void setDeploymentManager(DeploymentManager deploymentManager)
334 {
335 _deploymentManager = deploymentManager;
336 }
337
338
339 public DeploymentManager getDeploymentManager()
340 {
341 return _deploymentManager;
342 }
343 }