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.HashMap;
23 import java.util.Hashtable;
24 import java.util.Map;
25
26 import org.eclipse.jetty.deploy.App;
27 import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.FrameworkUtil;
32 import org.osgi.framework.ServiceRegistration;
33
34
35
36
37
38
39
40
41
42 public class BundleWebAppProvider extends AbstractWebAppProvider implements BundleProvider
43 {
44 private static final Logger LOG = Log.getLogger(AbstractWebAppProvider.class);
45
46
47
48
49 private Map<Bundle, App> _bundleMap = new HashMap<Bundle, App>();
50
51 private ServiceRegistration _serviceRegForBundles;
52
53
54
55
56
57
58 public BundleWebAppProvider (ServerInstanceWrapper wrapper)
59 {
60 super(wrapper);
61 }
62
63
64
65
66
67
68
69
70 protected void doStart() throws Exception
71 {
72
73 Dictionary<String,String> properties = new Hashtable<String,String>();
74 properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName());
75 _serviceRegForBundles = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(BundleProvider.class.getName(), this, properties);
76 super.doStart();
77 }
78
79
80
81
82
83 @Override
84 protected void doStop() throws Exception
85 {
86
87 if (_serviceRegForBundles != null)
88 {
89 try
90 {
91 _serviceRegForBundles.unregister();
92 }
93 catch (Exception e)
94 {
95 LOG.warn(e);
96 }
97 }
98
99 super.doStop();
100 }
101
102
103
104
105
106
107
108
109
110
111
112 public boolean bundleAdded (Bundle bundle) throws Exception
113 {
114 if (bundle == null)
115 return false;
116
117 ClassLoader cl = Thread.currentThread().getContextClassLoader();
118 Thread.currentThread().setContextClassLoader(getServerInstanceWrapper().getParentClassLoaderForWebapps());
119 String contextPath = null;
120 try
121 {
122 Dictionary headers = bundle.getHeaders();
123
124
125 if (headers.get(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH) != null)
126 {
127 String base = (String)headers.get(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH);
128 contextPath = getContextPath(bundle);
129 String originId = getOriginId(bundle, base);
130
131
132
133
134 OSGiApp app = new OSGiApp(getDeploymentManager(), this, bundle, originId);
135 app.setWebAppPath(base);
136 app.setContextPath(contextPath);
137 _bundleMap.put(bundle, app);
138 getDeploymentManager().addApp(app);
139 return true;
140 }
141
142
143
144 if (bundle.getEntry("/WEB-INF/web.xml") != null)
145 {
146 String base = ".";
147 contextPath = getContextPath(bundle);
148 String originId = getOriginId(bundle, base);
149
150 OSGiApp app = new OSGiApp(getDeploymentManager(), this, bundle, originId);
151 app.setContextPath(contextPath);
152 app.setWebAppPath(base);
153 _bundleMap.put(bundle, app);
154 getDeploymentManager().addApp(app);
155 return true;
156 }
157
158
159 if (headers.get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH) != null)
160 {
161
162 String base = ".";
163 contextPath = (String)headers.get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
164 String originId = getOriginId(bundle,base);
165
166 OSGiApp app = new OSGiApp(getDeploymentManager(), this, bundle, originId);
167 app.setContextPath(contextPath);
168 app.setWebAppPath(base);
169 _bundleMap.put(bundle, app);
170 getDeploymentManager().addApp(app);
171 return true;
172 }
173
174 return false;
175 }
176 catch (Exception e)
177 {
178
179 throw e;
180 }
181 finally
182 {
183 Thread.currentThread().setContextClassLoader(cl);
184 }
185 }
186
187
188
189
190
191
192
193
194
195 public boolean bundleRemoved (Bundle bundle) throws Exception
196 {
197 App app = _bundleMap.remove(bundle);
198 if (app != null)
199 {
200 getDeploymentManager().removeApp(app);
201 return true;
202 }
203 return false;
204 }
205
206
207
208
209
210
211 private static String getContextPath(Bundle bundle)
212 {
213 Dictionary<?, ?> headers = bundle.getHeaders();
214 String contextPath = (String) headers.get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
215 if (contextPath == null)
216 {
217
218
219
220
221 String location = bundle.getLocation();
222 String toks[] = location.replace('\\', '/').split("/");
223 contextPath = toks[toks.length - 1];
224
225 int lastDot = contextPath.lastIndexOf('.');
226 if (lastDot != -1)
227 contextPath = contextPath.substring(0, lastDot);
228 }
229 if (!contextPath.startsWith("/"))
230 contextPath = "/" + contextPath;
231
232 return contextPath;
233 }
234
235
236
237 }