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