1 package org.springframework.roo.bootstrap;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.StringTokenizer;
11
12 import org.osgi.framework.Bundle;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.BundleException;
15 import org.osgi.framework.Constants;
16 import org.osgi.service.startlevel.StartLevel;
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class AutoProcessor
31 {
32
33
34
35 public static final String AUTO_DEPLOY_DIR_PROPERY = "felix.auto.deploy.dir";
36
37
38
39 public static final String AUTO_DEPLOY_DIR_VALUE = "bundle";
40
41
42
43 public static final String AUTO_DEPLOY_ACTION_PROPERY = "felix.auto.deploy.action";
44
45
46
47 public static final String AUTO_DEPLOY_INSTALL_VALUE = "install";
48
49
50
51 public static final String AUTO_DEPLOY_START_VALUE = "start";
52
53
54
55 public static final String AUTO_DEPLOY_UPDATE_VALUE = "update";
56
57
58
59 public static final String AUTO_DEPLOY_UNINSTALL_VALUE = "uninstall";
60
61
62
63 public static final String AUTO_INSTALL_PROP = "felix.auto.install";
64
65
66
67 public static final String AUTO_START_PROP = "felix.auto.start";
68
69
70
71
72
73
74
75 public static void process(Map configMap, BundleContext context)
76 {
77 configMap = (configMap == null) ? new HashMap() : configMap;
78 processAutoDeploy(configMap, context);
79 processAutoProperties(configMap, context);
80 }
81
82
83
84
85
86
87
88 private static void processAutoDeploy(Map configMap, BundleContext context)
89 {
90
91 String action = (String) configMap.get(AUTO_DEPLOY_ACTION_PROPERY);
92 action = (action == null) ? "" : action;
93 List actionList = new ArrayList();
94 StringTokenizer st = new StringTokenizer(action, ",");
95 while (st.hasMoreTokens())
96 {
97 String s = st.nextToken().trim().toLowerCase();
98 if (s.equals(AUTO_DEPLOY_INSTALL_VALUE)
99 || s.equals(AUTO_DEPLOY_START_VALUE)
100 || s.equals(AUTO_DEPLOY_UPDATE_VALUE)
101 || s.equals(AUTO_DEPLOY_UNINSTALL_VALUE))
102 {
103 actionList.add(s);
104 }
105 }
106
107
108 if (actionList.size() > 0)
109 {
110
111 Map installedBundleMap = new HashMap();
112 Bundle[] bundles = context.getBundles();
113 for (int i = 0; i < bundles.length; i++)
114 {
115 installedBundleMap.put(bundles[i].getLocation(), bundles[i]);
116 }
117
118
119 String autoDir = (String) configMap.get(AUTO_DEPLOY_DIR_PROPERY);
120 autoDir = (autoDir == null) ? AUTO_DEPLOY_DIR_VALUE : autoDir;
121
122
123 File[] files = new File(autoDir).listFiles();
124 List jarList = new ArrayList();
125 if (files != null)
126 {
127 Arrays.sort(files);
128 for (int i = 0; i < files.length; i++)
129 {
130 if (files[i].getName().endsWith(".jar"))
131 {
132 jarList.add(files[i]);
133 }
134 }
135 }
136
137
138 final List startBundleList = new ArrayList();
139 for (int i = 0; i < jarList.size(); i++)
140 {
141
142
143
144 Bundle b = (Bundle) installedBundleMap.remove(
145 ((File) jarList.get(i)).toURI().toString());
146 try
147 {
148
149
150 if ((b == null) && actionList.contains(AUTO_DEPLOY_INSTALL_VALUE))
151 {
152 b = context.installBundle(
153 ((File) jarList.get(i)).toURI().toString());
154 }
155
156
157 else if (actionList.contains(AUTO_DEPLOY_UPDATE_VALUE))
158 {
159 b.update();
160 }
161
162
163
164 if (b != null)
165 {
166 startBundleList.add(b);
167 }
168 }
169 catch (BundleException ex)
170 {
171 System.err.println("Auto-deploy install: "
172 + ex + ((ex.getCause() != null) ? " - " + ex.getCause() : ""));
173 }
174 }
175
176
177
178 if (actionList.contains(AUTO_DEPLOY_UNINSTALL_VALUE))
179 {
180 for (Iterator it = installedBundleMap.entrySet().iterator(); it.hasNext(); )
181 {
182 Map.Entry entry = (Map.Entry) it.next();
183 Bundle b = (Bundle) entry.getValue();
184 if (b.getBundleId() != 0)
185 {
186 try
187 {
188 b.uninstall();
189 }
190 catch (BundleException ex)
191 {
192 System.err.println("Auto-deploy uninstall: "
193 + ex + ((ex.getCause() != null) ? " - " + ex.getCause() : ""));
194 }
195 }
196 }
197 }
198
199
200
201 if (actionList.contains(AUTO_DEPLOY_START_VALUE))
202 {
203 for (int i = 0; i < startBundleList.size(); i++)
204 {
205 try
206 {
207 if (!isFragment((Bundle) startBundleList.get(i)))
208 {
209 ((Bundle) startBundleList.get(i)).start();
210 }
211 }
212 catch (BundleException ex)
213 {
214 System.err.println("Auto-deploy start: "
215 + ex + ((ex.getCause() != null) ? " - " + ex.getCause() : ""));
216 }
217 }
218 }
219 }
220 }
221
222
223
224
225
226
227
228 private static void processAutoProperties(Map configMap, BundleContext context)
229 {
230
231
232 StartLevel sl = (StartLevel) context.getService(
233 context.getServiceReference(org.osgi.service.startlevel.StartLevel.class.getName()));
234
235
236
237
238
239
240
241
242
243
244 for (Iterator i = configMap.keySet().iterator(); i.hasNext(); )
245 {
246 String key = ((String) i.next()).toLowerCase();
247
248
249 if (!key.startsWith(AUTO_INSTALL_PROP) && !key.startsWith(AUTO_START_PROP))
250 {
251 continue;
252 }
253
254
255
256
257 int startLevel = sl.getInitialBundleStartLevel();
258 if (!key.equals(AUTO_INSTALL_PROP) && !key.equals(AUTO_START_PROP))
259 {
260 try
261 {
262 startLevel = Integer.parseInt(key.substring(key.lastIndexOf('.') + 1));
263 }
264 catch (NumberFormatException ex)
265 {
266 System.err.println("Invalid property: " + key);
267 }
268 }
269
270
271 StringTokenizer st = new StringTokenizer((String) configMap.get(key), "\" ", true);
272 for (String location = nextLocation(st); location != null; location = nextLocation(st))
273 {
274 try
275 {
276 Bundle b = context.installBundle(location, null);
277 sl.setBundleStartLevel(b, startLevel);
278 }
279 catch (Exception ex)
280 {
281 System.err.println("Auto-properties install: " + location + " ("
282 + ex + ((ex.getCause() != null) ? " - " + ex.getCause() : "") + ")");
283 if (ex.getCause() != null)
284 ex.printStackTrace();
285 }
286 }
287 }
288
289
290 for (Iterator i = configMap.keySet().iterator(); i.hasNext(); )
291 {
292 String key = ((String) i.next()).toLowerCase();
293 if (key.startsWith(AUTO_START_PROP))
294 {
295 StringTokenizer st = new StringTokenizer((String) configMap.get(key), "\" ", true);
296 for (String location = nextLocation(st); location != null; location = nextLocation(st))
297 {
298
299 try
300 {
301 Bundle b = context.installBundle(location, null);
302 if (b != null)
303 {
304 b.start();
305 }
306 }
307 catch (Exception ex)
308 {
309 System.err.println("Auto-properties start: " + location + " ("
310 + ex + ((ex.getCause() != null) ? " - " + ex.getCause() : "") + ")");
311 }
312 }
313 }
314 }
315 }
316
317 private static String nextLocation(StringTokenizer st)
318 {
319 String retVal = null;
320
321 if (st.countTokens() > 0)
322 {
323 String tokenList = "\" ";
324 StringBuffer tokBuf = new StringBuffer(10);
325 String tok = null;
326 boolean inQuote = false;
327 boolean tokStarted = false;
328 boolean exit = false;
329 while ((st.hasMoreTokens()) && (!exit))
330 {
331 tok = st.nextToken(tokenList);
332 if (tok.equals("\""))
333 {
334 inQuote = ! inQuote;
335 if (inQuote)
336 {
337 tokenList = "\"";
338 }
339 else
340 {
341 tokenList = "\" ";
342 }
343
344 }
345 else if (tok.equals(" "))
346 {
347 if (tokStarted)
348 {
349 retVal = tokBuf.toString();
350 tokStarted=false;
351 tokBuf = new StringBuffer(10);
352 exit = true;
353 }
354 }
355 else
356 {
357 tokStarted = true;
358 tokBuf.append(tok.trim());
359 }
360 }
361
362
363
364 if ((!exit) && (tokStarted))
365 {
366 retVal = tokBuf.toString();
367 }
368 }
369
370 return retVal;
371 }
372
373 private static boolean isFragment(Bundle bundle)
374 {
375 return bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null;
376 }
377 }