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.URI;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Dictionary;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29 import java.util.List;
30 import java.util.StringTokenizer;
31
32 import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator;
33 import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
34 import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelperFactory;
35 import org.eclipse.jetty.osgi.boot.utils.OSGiClassLoader;
36 import org.eclipse.jetty.osgi.boot.utils.Util;
37 import org.eclipse.jetty.server.Server;
38 import org.eclipse.jetty.util.log.Log;
39 import org.eclipse.jetty.util.log.Logger;
40 import org.eclipse.jetty.util.resource.JarResource;
41 import org.eclipse.jetty.util.resource.Resource;
42 import org.osgi.framework.Bundle;
43 import org.osgi.framework.BundleContext;
44
45
46
47
48
49
50
51
52
53
54
55
56 public class DefaultJettyAtJettyHomeHelper
57 {
58 private static final Logger LOG = Log.getLogger(DefaultJettyAtJettyHomeHelper.class);
59
60
61
62
63 public static final String JETTY_ETC_FILES = OSGiServerConstants.MANAGED_JETTY_XML_CONFIG_URLS;
64
65
66
67
68 public static final String DEFAULT_JETTY_ETC_FILES = "etc/jetty.xml,etc/jetty-selector.xml,etc/jetty-deployer.xml";
69
70
71
72
73 public static final String DEFAULT_JETTYHOME = "/jettyhome";
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 LOG.info("JETTY.HOME="+properties.get(OSGiServerConstants.JETTY_HOME));
161 ClassLoader contextCl = Thread.currentThread().getContextClassLoader();
162 try
163 {
164 Thread.currentThread().setContextClassLoader(JettyBootstrapActivator.class.getClassLoader());
165
166
167
168 properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME);
169 Util.setProperty(properties, OSGiServerConstants.JETTY_HOST, System.getProperty(OSGiServerConstants.JETTY_HOST));
170 Util.setProperty(properties, OSGiServerConstants.JETTY_PORT, System.getProperty(OSGiServerConstants.JETTY_PORT));
171 Util.setProperty(properties, OSGiServerConstants.JETTY_PORT_SSL, System.getProperty(OSGiServerConstants.JETTY_PORT_SSL));
172
173 Server server = ServerInstanceWrapper.configure(null, configURLs, properties);
174
175 server.setAttribute(OSGiServerConstants.JETTY_HOME, properties.get(OSGiServerConstants.JETTY_HOME));
176
177
178
179 bundleContext.registerService(Server.class.getName(), server, properties);
180 LOG.info("Default jetty server configured");
181 return server;
182 }
183 finally
184 {
185 Thread.currentThread().setContextClassLoader(contextCl);
186 }
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 private static List<URL> getJettyConfigurationURLs(File jettyhome)
204 throws MalformedURLException
205 {
206 List<URL> configURLs = new ArrayList<URL>();
207 String jettyetc = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);
208 StringTokenizer tokenizer = new StringTokenizer(jettyetc, ";,", false);
209 while (tokenizer.hasMoreTokens())
210 {
211 String next = tokenizer.nextToken().trim();
212
213 if (!next.startsWith("/") && (next.indexOf(':') == -1))
214 configURLs.add(new File(jettyhome, next).toURI().toURL());
215 else
216 configURLs.add(new URL(next));
217 }
218 return configURLs;
219 }
220
221
222
223
224
225
226
227
228
229
230
231 private static List<URL> getJettyConfigurationURLs(Bundle configurationBundle, Dictionary properties)
232 throws Exception
233 {
234 List<URL> configURLs = new ArrayList<URL>();
235 String files = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);
236 StringTokenizer tokenizer = new StringTokenizer(files, ";,", false);
237
238 while (tokenizer.hasMoreTokens())
239 {
240 String etcFile = tokenizer.nextToken().trim();
241
242
243 if (etcFile.startsWith("/") || etcFile.indexOf(":") != -1)
244 configURLs.add(new URL(etcFile));
245 else
246 {
247 Enumeration<URL> enUrls = BundleFileLocatorHelperFactory.getFactory().getHelper().findEntries(configurationBundle, etcFile);
248
249 String home = null;
250
251
252 if ((enUrls == null || !enUrls.hasMoreElements()))
253 {
254 home = DEFAULT_JETTYHOME;
255 String tmp = DEFAULT_JETTYHOME+(DEFAULT_JETTYHOME.endsWith("/")?"":"/")+etcFile;
256 enUrls = BundleFileLocatorHelperFactory.getFactory().getHelper().findEntries(configurationBundle, tmp);
257 LOG.info("Configuring jetty from bundle: {} with {}", configurationBundle.getSymbolicName(),tmp);
258 }
259
260
261 if (properties.get(OSGiServerConstants.JETTY_HOME) == null)
262 {
263 Resource res = findDir(configurationBundle, home);
264 if (res != null)
265 properties.put(OSGiServerConstants.JETTY_HOME, res.toString());
266 }
267
268 if (enUrls == null || !enUrls.hasMoreElements())
269 throw new IllegalStateException ("Unable to locate a jetty configuration file for " + etcFile);
270
271 URL url = BundleFileLocatorHelperFactory.getFactory().getHelper().getFileURL(enUrls.nextElement());
272 configURLs.add(url);
273
274 }
275 }
276 return configURLs;
277 }
278
279
280
281
282
283
284
285
286
287 public static Resource findDir (Bundle bundle, String dir)
288 {
289 if (bundle == null)
290 return null;
291
292 try
293 {
294 File f = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(bundle);
295 URL u = f.toURI().toURL();
296 u = BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(u);
297 Resource res = Resource.newResource(u);
298 String s = res.toString();
299
300
301 if (s.endsWith(".jar") && s.startsWith("file:"))
302 res = JarResource.newJarResource(res);
303
304
305 if (dir != null)
306 res = res.addPath(dir);
307
308 return res;
309
310 }
311 catch (Exception e)
312 {
313 LOG.warn("Bad bundle location" , e);
314 return null;
315 }
316 }
317 }