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