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.internal.serverfactory;
20
21 import java.io.File;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Dictionary;
26 import java.util.Enumeration;
27 import java.util.Hashtable;
28 import java.util.List;
29 import java.util.StringTokenizer;
30
31 import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator;
32 import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
33 import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelperFactory;
34 import org.eclipse.jetty.osgi.boot.utils.Util;
35 import org.eclipse.jetty.server.Server;
36 import org.eclipse.jetty.util.log.Log;
37 import org.eclipse.jetty.util.log.Logger;
38 import org.eclipse.jetty.util.resource.JarResource;
39 import org.eclipse.jetty.util.resource.Resource;
40 import org.osgi.framework.Bundle;
41 import org.osgi.framework.BundleContext;
42
43
44
45
46
47
48
49
50
51
52
53 public class DefaultJettyAtJettyHomeHelper
54 {
55 private static final Logger LOG = Log.getLogger(DefaultJettyAtJettyHomeHelper.class);
56
57
58
59
60 public static final String JETTY_ETC_FILES = OSGiServerConstants.MANAGED_JETTY_XML_CONFIG_URLS;
61
62
63
64
65 public static final String DEFAULT_JETTY_ETC_FILES = "etc/jetty.xml,etc/jetty-http.xml,etc/jetty-deployer.xml";
66
67
68
69
70 public static final String DEFAULT_JETTYHOME = "/jettyhome";
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public static Server startJettyAtJettyHome(BundleContext bundleContext) throws Exception
99 {
100 String jettyHomeSysProp = System.getProperty(OSGiServerConstants.JETTY_HOME);
101 String jettyHomeBundleSysProp = System.getProperty(OSGiServerConstants.JETTY_HOME_BUNDLE);
102 File jettyHomeDir = null;
103 Bundle jettyHomeBundle = null;
104
105 Dictionary<String,String> properties = new Hashtable<String,String>();
106 if (jettyHomeSysProp != null)
107 {
108 jettyHomeSysProp = Util.resolvePropertyValue(jettyHomeSysProp);
109
110 if (jettyHomeSysProp.startsWith("\"") && jettyHomeSysProp.endsWith("\"") || (jettyHomeSysProp.startsWith("'") && jettyHomeSysProp.endsWith("'")))
111 jettyHomeSysProp = jettyHomeSysProp.substring(1, jettyHomeSysProp.length() - 1);
112
113 if (jettyHomeBundleSysProp != null)
114 LOG.warn("Both jetty.home and jetty.home.bundle property defined: jetty.home.bundle ignored.");
115
116 jettyHomeDir = new File(jettyHomeSysProp);
117 if (!jettyHomeDir.exists() || !jettyHomeDir.isDirectory())
118 {
119 LOG.warn("Unable to locate the jetty.home folder " + jettyHomeSysProp);
120 return null;
121 }
122
123
124 Util.setProperty(properties, OSGiServerConstants.JETTY_HOME, jettyHomeDir.getAbsolutePath());
125 }
126 else if (jettyHomeBundleSysProp != null)
127 {
128 jettyHomeBundleSysProp = Util.resolvePropertyValue(jettyHomeBundleSysProp);
129 for (Bundle b : bundleContext.getBundles())
130 {
131 if (b.getState() == Bundle.UNINSTALLED)
132 continue;
133
134 if (b.getSymbolicName().equals(jettyHomeBundleSysProp))
135 {
136 jettyHomeBundle = b;
137 break;
138 }
139 }
140 if (jettyHomeBundle == null)
141 {
142 LOG.warn("Unable to find the jetty.home.bundle named " + jettyHomeSysProp);
143 return null;
144 }
145 }
146
147 if (jettyHomeDir == null && jettyHomeBundle == null)
148 {
149 LOG.warn("No default jetty created.");
150 return null;
151 }
152
153
154
155
156
157 List<URL> configURLs = jettyHomeDir != null ? getJettyConfigurationURLs(jettyHomeDir) : getJettyConfigurationURLs(jettyHomeBundle, properties);
158
159 LOG.info("Configuring the default jetty server with {}",configURLs);
160 String home=properties.get(OSGiServerConstants.JETTY_HOME);
161 String base=properties.get(OSGiServerConstants.JETTY_BASE);
162 if (base==null)
163 base=home;
164 LOG.info("JETTY.HOME="+home);
165 LOG.info("JETTY.BASE="+base);
166 ClassLoader contextCl = Thread.currentThread().getContextClassLoader();
167 try
168 {
169 Thread.currentThread().setContextClassLoader(JettyBootstrapActivator.class.getClassLoader());
170
171
172
173 properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME);
174 Util.setProperty(properties, OSGiServerConstants.JETTY_HOST, System.getProperty(OSGiServerConstants.JETTY_HOST, System.getProperty("jetty.host")));
175 Util.setProperty(properties, OSGiServerConstants.JETTY_PORT, System.getProperty(OSGiServerConstants.JETTY_PORT, System.getProperty("jetty.port")));
176 Util.setProperty(properties, OSGiServerConstants.JETTY_PORT_SSL, System.getProperty(OSGiServerConstants.JETTY_PORT_SSL, System.getProperty("ssl.port")));
177 Util.setProperty(properties, OSGiServerConstants.JETTY_HOME, home);
178 Util.setProperty(properties, OSGiServerConstants.JETTY_BASE, base);
179 Server server = ServerInstanceWrapper.configure(null, configURLs, properties);
180
181
182
183
184 bundleContext.registerService(Server.class.getName(), server, properties);
185 LOG.info("Default jetty server configured");
186 return server;
187 }
188 catch (Exception e)
189 {
190 LOG.warn(e);
191 throw e;
192 }
193 finally
194 {
195 Thread.currentThread().setContextClassLoader(contextCl);
196 }
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 private static List<URL> getJettyConfigurationURLs(File jettyhome)
214 throws MalformedURLException
215 {
216 List<URL> configURLs = new ArrayList<URL>();
217 String jettyetc = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);
218 StringTokenizer tokenizer = new StringTokenizer(jettyetc, ";,", false);
219 while (tokenizer.hasMoreTokens())
220 {
221 String next = tokenizer.nextToken().trim();
222
223 if (!next.startsWith("/") && (next.indexOf(':') == -1))
224 configURLs.add(new File(jettyhome, next).toURI().toURL());
225 else
226 configURLs.add(new URL(next));
227 }
228 return configURLs;
229 }
230
231
232
233
234
235
236
237
238
239
240
241 private static List<URL> getJettyConfigurationURLs(Bundle configurationBundle, Dictionary properties)
242 throws Exception
243 {
244 List<URL> configURLs = new ArrayList<URL>();
245 String files = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);
246 StringTokenizer tokenizer = new StringTokenizer(files, ";,", false);
247
248 while (tokenizer.hasMoreTokens())
249 {
250 String etcFile = tokenizer.nextToken().trim();
251
252
253 if (etcFile.startsWith("/") || etcFile.indexOf(":") != -1)
254 configURLs.add(new URL(etcFile));
255 else
256 {
257 Enumeration<URL> enUrls = BundleFileLocatorHelperFactory.getFactory().getHelper().findEntries(configurationBundle, etcFile);
258
259 String home = null;
260
261
262 if ((enUrls == null || !enUrls.hasMoreElements()))
263 {
264 home = DEFAULT_JETTYHOME;
265 String tmp = DEFAULT_JETTYHOME+(DEFAULT_JETTYHOME.endsWith("/")?"":"/")+etcFile;
266 enUrls = BundleFileLocatorHelperFactory.getFactory().getHelper().findEntries(configurationBundle, tmp);
267 LOG.info("Configuring jetty from bundle: {} with {}", configurationBundle.getSymbolicName(),tmp);
268 }
269
270
271 if (properties.get(OSGiServerConstants.JETTY_HOME) == null)
272 {
273 Resource res = findDir(configurationBundle, home);
274 if (res != null)
275 properties.put(OSGiServerConstants.JETTY_HOME, res.toString());
276 }
277
278 if (enUrls == null || !enUrls.hasMoreElements())
279 throw new IllegalStateException ("Unable to locate a jetty configuration file for " + etcFile);
280
281 URL url = BundleFileLocatorHelperFactory.getFactory().getHelper().getFileURL(enUrls.nextElement());
282 configURLs.add(url);
283
284 }
285 }
286 return configURLs;
287 }
288
289
290
291
292
293
294
295
296
297 public static Resource findDir (Bundle bundle, String dir)
298 {
299 if (bundle == null)
300 return null;
301
302 try
303 {
304 File f = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(bundle);
305 URL u = f.toURI().toURL();
306 u = BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(u);
307 Resource res = Resource.newResource(u);
308 String s = res.toString();
309
310
311 if (s.endsWith(".jar") && s.startsWith("file:"))
312 res = JarResource.newJarResource(res);
313
314
315 if (dir != null)
316 res = res.addPath(dir);
317
318 return res;
319
320 }
321 catch (Exception e)
322 {
323 LOG.warn("Bad bundle location" , e);
324 return null;
325 }
326 }
327 }