]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/OsgiBoot.java
2015f7504fbbc39df99be7fce1a64e03c05be9c0
[lgpl/argeo-commons.git] / base / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / osgi / boot / OsgiBoot.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.osgi.boot;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.net.URL;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.Set;
30 import java.util.SortedMap;
31 import java.util.StringTokenizer;
32 import java.util.TreeMap;
33 import java.util.TreeSet;
34
35 import org.argeo.osgi.boot.internal.springutil.AntPathMatcher;
36 import org.argeo.osgi.boot.internal.springutil.PathMatcher;
37 import org.argeo.osgi.boot.internal.springutil.SystemPropertyUtils;
38 import org.osgi.framework.Bundle;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.BundleException;
41 import org.osgi.framework.Constants;
42 import org.osgi.framework.ServiceReference;
43 import org.osgi.service.packageadmin.ExportedPackage;
44 import org.osgi.service.packageadmin.PackageAdmin;
45
46 /**
47 * Basic provisioning of an OSGi runtime via file path patterns and system
48 * properties. Java 1.4 compatible.<br>
49 * The approach is to generate list of URLs based on various methods, configured
50 * via system properties.
51 */
52 public class OsgiBoot {
53 public final static String SYMBOLIC_NAME_OSGI_BOOT = "org.argeo.osgi.boot";
54 public final static String SYMBOLIC_NAME_EQUINOX = "org.eclipse.osgi";
55
56 public final static String PROP_OSGI_STARTLEVEL = "osgi.startLevel";
57 public final static String PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL = "osgi.bundles.defaultStartLevel";
58
59 public final static String PROP_ARGEO_OSGI_DATA_DIR = "argeo.osgi.data.dir";
60
61 public final static String PROP_ARGEO_OSGI_START = "argeo.osgi.start";
62 public final static String PROP_ARGEO_OSGI_BUNDLES = "argeo.osgi.bundles";
63 public final static String PROP_ARGEO_OSGI_LOCATIONS = "argeo.osgi.locations";
64 public final static String PROP_ARGEO_OSGI_BASE_URL = "argeo.osgi.baseUrl";
65 /** @deprecated */
66 public final static String PROP_ARGEO_OSGI_MODULES_URL = "argeo.osgi.modulesUrl";
67 public final static String PROP_ARGEO_OSGI_DISTRIBUTION_URL = "argeo.osgi.distributionUrl";
68
69 // booleans
70 public final static String PROP_ARGEO_OSGI_BOOT_DEBUG = "argeo.osgi.boot.debug";
71 public final static String PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN = "argeo.osgi.boot.excludeSvn";
72 public final static String PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER = "argeo.osgi.boot.installInLexicographicOrder";
73
74 public final static String PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT = "argeo.osgi.boot.defaultTimeout";
75 public final static String PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR = "argeo.osgi.boot.modulesUrlSeparator";
76 public final static String PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE = "argeo.osgi.boot.systemPropertiesFile";
77 public final static String PROP_ARGEO_OSGI_BOOT_APPCLASS = "argeo.osgi.boot.appclass";
78 public final static String PROP_ARGEO_OSGI_BOOT_APPARGS = "argeo.osgi.boot.appargs";
79
80 public final static String DEFAULT_BASE_URL = "reference:file:";
81 public final static String EXCLUDES_SVN_PATTERN = "**/.svn/**";
82
83 // OSGi system properties
84 public final static String INSTANCE_AREA_PROP = "osgi.instance.area";
85 public final static String INSTANCE_AREA_DEFAULT_PROP = "osgi.instance.area.default";
86
87 private boolean debug = Boolean.valueOf(
88 System.getProperty(PROP_ARGEO_OSGI_BOOT_DEBUG, "false"))
89 .booleanValue();
90 /** Exclude svn metadata implicitely(a bit costly) */
91 private boolean excludeSvn = Boolean.valueOf(
92 System.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN, "false"))
93 .booleanValue();
94
95 /**
96 * The {@link #installUrls(List)} methods won't follow the list order but
97 * order the urls according to the alphabetical order of the file names
98 * (last part of the URL). The goal is to stay closer from Eclipse PDE way
99 * of installing target platform bundles.
100 */
101 private boolean installInLexicographicOrder = Boolean
102 .valueOf(
103 System.getProperty(
104 PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER,
105 "true")).booleanValue();;
106
107 /** Default is 10s (set in constructor) */
108 private long defaultTimeout;
109
110 /** Default is ',' (set in constructor) */
111 private String modulesUrlSeparator = ",";
112
113 private final BundleContext bundleContext;
114
115 /*
116 * INITIALIZATION
117 */
118 /** Constructor */
119 public OsgiBoot(BundleContext bundleContext) {
120 this.bundleContext = bundleContext;
121 defaultTimeout = Long.parseLong(OsgiBootUtils.getProperty(
122 PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT, "10000"));
123 modulesUrlSeparator = OsgiBootUtils.getProperty(
124 PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR, ",");
125 initSystemProperties();
126 }
127
128 /**
129 * Set additional system properties, especially ${argeo.osgi.data.dir} as an
130 * OS file path (and not a file:// URL)
131 */
132 protected void initSystemProperties() {
133 String osgiInstanceArea = System.getProperty(INSTANCE_AREA_PROP);
134 String osgiInstanceAreaDefault = System
135 .getProperty(INSTANCE_AREA_DEFAULT_PROP);
136 String tempDir = System.getProperty("java.io.tmpdir");
137
138 File dataDir = null;
139 if (osgiInstanceArea != null) {
140 // within OSGi with -data specified
141 osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
142 dataDir = new File(osgiInstanceArea);
143 } else if (osgiInstanceAreaDefault != null) {
144 // within OSGi without -data specified
145 osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
146 dataDir = new File(osgiInstanceAreaDefault);
147 } else {// outside OSGi
148 dataDir = new File(tempDir + File.separator + "argeoOsgiData");
149 }
150 System.setProperty(PROP_ARGEO_OSGI_DATA_DIR, dataDir.getAbsolutePath());
151 }
152
153 /*
154 * HIGH-LEVEL METHODS
155 */
156 /** Bootstraps the OSGi runtime */
157 public void bootstrap() {
158 long begin = System.currentTimeMillis();
159 System.out.println();
160 OsgiBootUtils.info("OSGi bootstrap starting...");
161 OsgiBootUtils.info("Writable data directory : "
162 + System.getProperty(PROP_ARGEO_OSGI_DATA_DIR)
163 + " (set as system property " + PROP_ARGEO_OSGI_DATA_DIR + ")");
164 installUrls(getBundlesUrls());
165 installUrls(getLocationsUrls());
166 installUrls(getModulesUrls());
167 installUrls(getDistributionUrls());
168 checkUnresolved();
169 startBundles();
170 long duration = System.currentTimeMillis() - begin;
171 OsgiBootUtils.info("OSGi bootstrap completed in "
172 + Math.round(((double) duration) / 1000) + "s (" + duration
173 + "ms), " + bundleContext.getBundles().length + " bundles");
174
175 // display packages exported twice
176 if (debug) {
177 Map /* <String,Set<String>> */duplicatePackages = findPackagesExportedTwice();
178 if (duplicatePackages.size() > 0) {
179 OsgiBootUtils.info("Packages exported twice:");
180 Iterator it = duplicatePackages.keySet().iterator();
181 while (it.hasNext()) {
182 String pkgName = it.next().toString();
183 OsgiBootUtils.info(pkgName);
184 Set bdles = (Set) duplicatePackages.get(pkgName);
185 Iterator bdlesIt = bdles.iterator();
186 while (bdlesIt.hasNext())
187 OsgiBootUtils.info(" " + bdlesIt.next());
188 }
189 }
190 }
191
192 System.out.println();
193 }
194
195 /*
196 * INSTALLATION
197 */
198 /** Install a single url. Convenience method. */
199 public Bundle installUrl(String url) {
200 List urls = new ArrayList();
201 urls.add(url);
202 installUrls(urls);
203 return (Bundle) getBundlesByLocation().get(url);
204 }
205
206 /** Install the bundles at this URL list. */
207 public void installUrls(List urls) {
208 Map installedBundles = getBundlesByLocation();
209
210 if (installInLexicographicOrder) {
211 SortedMap map = new TreeMap();
212 // reorder
213 for (int i = 0; i < urls.size(); i++) {
214 String url = (String) urls.get(i);
215 int index = url.lastIndexOf('/');
216 String fileName;
217 if (index >= 0)
218 fileName = url.substring(index + 1);
219 else
220 fileName = url;
221 map.put(fileName, url);
222 }
223
224 // install
225 Iterator keys = map.keySet().iterator();
226 while (keys.hasNext()) {
227 Object key = keys.next();
228 String url = map.get(key).toString();
229 installUrl(url, installedBundles);
230 }
231 } else {
232 for (int i = 0; i < urls.size(); i++) {
233 String url = (String) urls.get(i);
234 installUrl(url, installedBundles);
235 }
236 }
237
238 }
239
240 /** Actually install the provided URL */
241 protected void installUrl(String url, Map installedBundles) {
242 try {
243 if (installedBundles.containsKey(url)) {
244 Bundle bundle = (Bundle) installedBundles.get(url);
245 // bundle.update();
246 if (debug)
247 debug("Bundle " + bundle.getSymbolicName()
248 + " already installed from " + url);
249 } else {
250 Bundle bundle = bundleContext.installBundle(url);
251 if (debug)
252 debug("Installed bundle " + bundle.getSymbolicName()
253 + " from " + url);
254 }
255 } catch (BundleException e) {
256 String message = e.getMessage();
257 if ((message.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT + "\"") || message
258 .contains("Bundle \"" + SYMBOLIC_NAME_EQUINOX + "\""))
259 && message.contains("has already been installed")) {
260 // silent, in order to avoid warnings: we know that both
261 // have already been installed...
262 } else {
263 OsgiBootUtils.warn("Could not install bundle from " + url
264 + ": " + message);
265 }
266 if (debug)
267 e.printStackTrace();
268 }
269 }
270
271 /*
272 * START
273 */
274 public void startBundles() {
275 // default and active start levels from System properties
276 Integer defaultStartLevel = new Integer(Integer.parseInt(OsgiBootUtils
277 .getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4")));
278 Integer activeStartLevel = new Integer(OsgiBootUtils.getProperty(
279 PROP_OSGI_STARTLEVEL, "6"));
280
281 SortedMap/* <Integer, List<String>> */startLevels = new TreeMap();
282 computeStartLevels(startLevels, System.getProperties(),
283 defaultStartLevel);
284
285 Iterator/* <Integer> */levels = startLevels.keySet().iterator();
286 while (levels.hasNext()) {
287 Integer level = (Integer) levels.next();
288 boolean allStarted = startBundles((List) startLevels.get(level));
289 if (!allStarted)
290 OsgiBootUtils
291 .warn("Not all bundles started for level " + level);
292 if (level.equals(activeStartLevel))
293 break;// active start level reached
294 }
295
296 }
297
298 public static void computeStartLevels(
299 SortedMap/* <Integer, List<String>> */startLevels,
300 Properties properties, Integer defaultStartLevel) {
301
302 // default (and previously, only behaviour)
303 appendToStartLevels(startLevels, defaultStartLevel,
304 properties.getProperty(PROP_ARGEO_OSGI_START, ""));
305
306 // list argeo.osgi.start.* system properties
307 Iterator/* <String> */keys = properties.keySet().iterator();
308 final String prefix = PROP_ARGEO_OSGI_START + ".";
309 while (keys.hasNext()) {
310 String key = (String) keys.next();
311 if (key.startsWith(prefix)) {
312 Integer startLevel;
313 String suffix = key.substring(prefix.length());
314 String[] tokens = suffix.split("\\.");
315 if (tokens.length > 0 && !tokens[0].trim().equals(""))
316 try {
317 // first token is start level
318 startLevel = new Integer(tokens[0]);
319 } catch (NumberFormatException e) {
320 startLevel = defaultStartLevel;
321 }
322 else
323 startLevel = defaultStartLevel;
324
325 // append bundle names
326 String bundleNames = properties.getProperty(key);
327 appendToStartLevels(startLevels, startLevel, bundleNames);
328 }
329 }
330 }
331
332 /** Append a comma-separated list of bundles to the start levels. */
333 private static void appendToStartLevels(
334 SortedMap/* <Integer, List<String>> */startLevels,
335 Integer startLevel, String str) {
336 if (str == null || str.trim().equals(""))
337 return;
338
339 if (!startLevels.containsKey(startLevel))
340 startLevels.put(startLevel, new ArrayList());
341 String[] bundleNames = str.split(",");
342 for (int i = 0; i < bundleNames.length; i++) {
343 if (bundleNames[i] != null && !bundleNames[i].trim().equals(""))
344 ((List) startLevels.get(startLevel)).add(bundleNames[i]);
345 }
346 }
347
348 /**
349 * Convenience method accepting a comma-separated list of bundle to start
350 *
351 * @deprecated
352 */
353 public void startBundles(String bundlesToStartStr) {
354 if (bundlesToStartStr == null)
355 return;
356
357 StringTokenizer st = new StringTokenizer(bundlesToStartStr, ",");
358 List bundlesToStart = new ArrayList();
359 while (st.hasMoreTokens()) {
360 String name = st.nextToken().trim();
361 bundlesToStart.add(name);
362 }
363 startBundles(bundlesToStart);
364 }
365
366 /**
367 * Start the provided list of bundles
368 *
369 * @return whether all bundlesa are now in active state
370 */
371 public boolean startBundles(List bundlesToStart) {
372 if (bundlesToStart.size() == 0)
373 return true;
374
375 // used to monitor ACTIVE states
376 List/* <Bundle> */startedBundles = new ArrayList();
377 // used to log the bundles not found
378 List/* <String> */notFoundBundles = new ArrayList(bundlesToStart);
379
380 Bundle[] bundles = bundleContext.getBundles();
381 long startBegin = System.currentTimeMillis();
382 for (int i = 0; i < bundles.length; i++) {
383 Bundle bundle = bundles[i];
384 String symbolicName = bundle.getSymbolicName();
385 if (bundlesToStart.contains(symbolicName))
386 try {
387 try {
388 bundle.start();
389 if (debug)
390 debug("Bundle " + symbolicName + " started");
391 } catch (Exception e) {
392 OsgiBootUtils.warn("Start of bundle " + symbolicName
393 + " failed because of " + e
394 + ", maybe bundle is not yet resolved,"
395 + " waiting and trying again.");
396 waitForBundleResolvedOrActive(startBegin, bundle);
397 bundle.start();
398 startedBundles.add(bundle);
399 }
400 notFoundBundles.remove(symbolicName);
401 } catch (Exception e) {
402 OsgiBootUtils.warn("Bundle " + symbolicName
403 + " cannot be started: " + e.getMessage());
404 if (debug)
405 e.printStackTrace();
406 // was found even if start failed
407 notFoundBundles.remove(symbolicName);
408 }
409 }
410
411 for (int i = 0; i < notFoundBundles.size(); i++)
412 OsgiBootUtils.warn("Bundle '" + notFoundBundles.get(i)
413 + "' not started because it was not found.");
414
415 // monitors that all bundles are started
416 long beginMonitor = System.currentTimeMillis();
417 boolean allStarted = !(startedBundles.size() > 0);
418 List/* <String> */notStarted = new ArrayList();
419 while (!allStarted
420 && (System.currentTimeMillis() - beginMonitor) < defaultTimeout) {
421 notStarted = new ArrayList();
422 allStarted = true;
423 for (int i = 0; i < startedBundles.size(); i++) {
424 Bundle bundle = (Bundle) startedBundles.get(i);
425 // TODO check behaviour of lazs bundles
426 if (bundle.getState() != Bundle.ACTIVE) {
427 allStarted = false;
428 notStarted.add(bundle.getSymbolicName());
429 }
430 }
431 try {
432 Thread.sleep(100);
433 } catch (InterruptedException e) {
434 // silent
435 }
436 }
437 long duration = System.currentTimeMillis() - beginMonitor;
438
439 if (!allStarted)
440 for (int i = 0; i < notStarted.size(); i++)
441 OsgiBootUtils.warn("Bundle '" + notStarted.get(i)
442 + "' not ACTIVE after " + (duration / 1000) + "s");
443
444 return allStarted;
445 }
446
447 /*
448 * DIAGNOSTICS
449 */
450 /** Check unresolved bundles */
451 protected void checkUnresolved() {
452 // Refresh
453 ServiceReference packageAdminRef = bundleContext
454 .getServiceReference(PackageAdmin.class.getName());
455 PackageAdmin packageAdmin = (PackageAdmin) bundleContext
456 .getService(packageAdminRef);
457 packageAdmin.resolveBundles(null);
458
459 Bundle[] bundles = bundleContext.getBundles();
460 List /* Bundle */unresolvedBundles = new ArrayList();
461 for (int i = 0; i < bundles.length; i++) {
462 int bundleState = bundles[i].getState();
463 if (!(bundleState == Bundle.ACTIVE
464 || bundleState == Bundle.RESOLVED || bundleState == Bundle.STARTING))
465 unresolvedBundles.add(bundles[i]);
466 }
467
468 if (unresolvedBundles.size() != 0) {
469 OsgiBootUtils.warn("Unresolved bundles " + unresolvedBundles);
470 }
471 }
472
473 /** List packages exported twice. */
474 public Map findPackagesExportedTwice() {
475 ServiceReference paSr = bundleContext
476 .getServiceReference(PackageAdmin.class.getName());
477 PackageAdmin packageAdmin = (PackageAdmin) bundleContext
478 .getService(paSr);
479
480 // find packages exported twice
481 Bundle[] bundles = bundleContext.getBundles();
482 Map /* <String,Set<String>> */exportedPackages = new TreeMap();
483 for (int i = 0; i < bundles.length; i++) {
484 Bundle bundle = bundles[i];
485 ExportedPackage[] pkgs = packageAdmin.getExportedPackages(bundle);
486 if (pkgs != null)
487 for (int j = 0; j < pkgs.length; j++) {
488 String pkgName = pkgs[j].getName();
489 if (!exportedPackages.containsKey(pkgName)) {
490 exportedPackages.put(pkgName, new TreeSet());
491 }
492 ((Set) exportedPackages.get(pkgName)).add(bundle
493 .getSymbolicName() + "_" + bundle.getVersion());
494 }
495 }
496 Map /* <String,Set<String>> */duplicatePackages = new TreeMap();
497 Iterator it = exportedPackages.keySet().iterator();
498 while (it.hasNext()) {
499 String pkgName = it.next().toString();
500 Set bdles = (Set) exportedPackages.get(pkgName);
501 if (bdles.size() > 1)
502 duplicatePackages.put(pkgName, bdles);
503 }
504 return duplicatePackages;
505 }
506
507 /** Waits for a bundle to become active or resolved */
508 protected void waitForBundleResolvedOrActive(long startBegin, Bundle bundle)
509 throws Exception {
510 int originalState = bundle.getState();
511 if ((originalState == Bundle.RESOLVED)
512 || (originalState == Bundle.ACTIVE))
513 return;
514
515 String originalStateStr = OsgiBootUtils.stateAsString(originalState);
516
517 int currentState = bundle.getState();
518 while (!(currentState == Bundle.RESOLVED || currentState == Bundle.ACTIVE)) {
519 long now = System.currentTimeMillis();
520 if ((now - startBegin) > defaultTimeout * 10)
521 throw new Exception("Bundle " + bundle.getSymbolicName()
522 + " was not RESOLVED or ACTIVE after "
523 + (now - startBegin) + "ms (originalState="
524 + originalStateStr + ", currentState="
525 + OsgiBootUtils.stateAsString(currentState) + ")");
526
527 try {
528 Thread.sleep(100l);
529 } catch (InterruptedException e) {
530 // silent
531 }
532 currentState = bundle.getState();
533 }
534 }
535
536 /*
537 * EXPLICIT LOCATIONS INSTALLATION
538 */
539 /** Gets the list of resolved explicit URL locations. */
540 public List getLocationsUrls() {
541 String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL,
542 DEFAULT_BASE_URL);
543 String bundleLocations = OsgiBootUtils
544 .getProperty(PROP_ARGEO_OSGI_LOCATIONS);
545 return getLocationsUrls(baseUrl, bundleLocations);
546 }
547
548 /**
549 * Gets a list of URLs based on explicit locations, resolving placeholder
550 * ${...} containing system properties, e.g. ${user.home}.
551 */
552 public List getLocationsUrls(String baseUrl, String bundleLocations) {
553 List urls = new ArrayList();
554
555 if (bundleLocations == null)
556 return urls;
557 bundleLocations = SystemPropertyUtils
558 .resolvePlaceholders(bundleLocations);
559 if (debug)
560 debug(PROP_ARGEO_OSGI_LOCATIONS + "=" + bundleLocations);
561
562 StringTokenizer st = new StringTokenizer(bundleLocations,
563 File.pathSeparator);
564 while (st.hasMoreTokens()) {
565 urls.add(locationToUrl(baseUrl, st.nextToken().trim()));
566 }
567 return urls;
568 }
569
570 /*
571 * BUNDLE PATTERNS INSTALLATION
572 */
573 /**
574 * Computes a list of URLs based on Ant-like incluide/exclude patterns
575 * defined by ${argeo.osgi.bundles} with the following format:<br>
576 * <code>/base/directory;in=*.jar;in=**;ex=org.eclipse.osgi_*;jar</code><br>
577 * WARNING: <code>/base/directory;in=*.jar,\</code> at the end of a file,
578 * without a new line causes a '.' to be appended with unexpected side
579 * effects.
580 */
581 public List getBundlesUrls() {
582 String bundlePatterns = OsgiBootUtils
583 .getProperty(PROP_ARGEO_OSGI_BUNDLES);
584 return getBundlesUrls(bundlePatterns);
585 }
586
587 /**
588 * Compute alist of URLs to install based on the provided patterns, with
589 * default base url
590 */
591 public List getBundlesUrls(String bundlePatterns) {
592 String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL,
593 DEFAULT_BASE_URL);
594 return getBundlesUrls(baseUrl, bundlePatterns);
595 }
596
597 /** Implements the path matching logic */
598 List getBundlesUrls(String baseUrl, String bundlePatterns) {
599 List urls = new ArrayList();
600 if (bundlePatterns == null)
601 return urls;
602
603 bundlePatterns = SystemPropertyUtils
604 .resolvePlaceholders(bundlePatterns);
605 if (debug)
606 debug(PROP_ARGEO_OSGI_BUNDLES + "=" + bundlePatterns
607 + " (excludeSvn=" + excludeSvn + ")");
608
609 StringTokenizer st = new StringTokenizer(bundlePatterns, ",");
610 List bundlesSets = new ArrayList();
611 while (st.hasMoreTokens()) {
612 bundlesSets.add(new BundlesSet(st.nextToken()));
613 }
614
615 // find included
616 List included = new ArrayList();
617 PathMatcher matcher = new AntPathMatcher();
618 for (int i = 0; i < bundlesSets.size(); i++) {
619 BundlesSet bundlesSet = (BundlesSet) bundlesSets.get(i);
620 for (int j = 0; j < bundlesSet.getIncludes().size(); j++) {
621 String pattern = (String) bundlesSet.getIncludes().get(j);
622 match(matcher, included, bundlesSet.getDir(), null, pattern);
623 }
624 }
625
626 // find excluded
627 List excluded = new ArrayList();
628 for (int i = 0; i < bundlesSets.size(); i++) {
629 BundlesSet bundlesSet = (BundlesSet) bundlesSets.get(i);
630 for (int j = 0; j < bundlesSet.getExcludes().size(); j++) {
631 String pattern = (String) bundlesSet.getExcludes().get(j);
632 match(matcher, excluded, bundlesSet.getDir(), null, pattern);
633 }
634 }
635
636 // construct list
637 for (int i = 0; i < included.size(); i++) {
638 String fullPath = (String) included.get(i);
639 if (!excluded.contains(fullPath))
640 urls.add(locationToUrl(baseUrl, fullPath));
641 }
642
643 return urls;
644 }
645
646 /*
647 * DISTRIBUTION JAR INSTALLATION
648 */
649 public List getDistributionUrls() {
650 List urls = new ArrayList();
651 String distributionUrl = OsgiBootUtils
652 .getProperty(PROP_ARGEO_OSGI_DISTRIBUTION_URL);
653 if (distributionUrl == null)
654 return urls;
655 String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL);
656
657 DistributionBundle distributionBundle;
658 if (baseUrl != null
659 && !(distributionUrl.startsWith("http") || distributionUrl
660 .startsWith("file"))) {
661 // relative url
662 distributionBundle = new DistributionBundle(baseUrl,
663 distributionUrl);
664 } else {
665 distributionBundle = new DistributionBundle(distributionUrl);
666 if (baseUrl != null)
667 distributionBundle.setBaseUrl(baseUrl);
668
669 }
670 distributionBundle.processUrl();
671 return distributionBundle.listUrls();
672 }
673
674 /*
675 * MODULES LIST INSTALLATION (${argeo.osgi.modulesUrl})
676 */
677 /**
678 * Downloads a list of URLs in CSV format from ${argeo.osgi.modulesUrl}:<br>
679 * <code>Bundle-SymbolicName,Bundle-Version,url</code>)<br>
680 * If ${argeo.osgi.baseUrl} is set, URLs will be considered relative paths
681 * and be concatenated with the base URL, typically the root of a Maven
682 * repository.
683 *
684 * @deprecated
685 */
686 public List getModulesUrls() {
687 List urls = new ArrayList();
688 String modulesUrlStr = OsgiBootUtils
689 .getProperty(PROP_ARGEO_OSGI_MODULES_URL);
690 if (modulesUrlStr == null)
691 return urls;
692
693 String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL);
694
695 Map installedBundles = getBundlesBySymbolicName();
696
697 BufferedReader reader = null;
698 try {
699 URL modulesUrl = new URL(modulesUrlStr);
700 reader = new BufferedReader(new InputStreamReader(
701 modulesUrl.openStream()));
702 String line = null;
703 while ((line = reader.readLine()) != null) {
704 StringTokenizer st = new StringTokenizer(line,
705 modulesUrlSeparator);
706 String moduleName = st.nextToken();
707 String moduleVersion = st.nextToken();
708 String url = st.nextToken();
709 if (baseUrl != null)
710 url = baseUrl + url;
711
712 if (installedBundles.containsKey(moduleName)) {
713 Bundle bundle = (Bundle) installedBundles.get(moduleName);
714 String bundleVersion = bundle.getHeaders()
715 .get(Constants.BUNDLE_VERSION).toString();
716 int comp = OsgiBootUtils.compareVersions(bundleVersion,
717 moduleVersion);
718 if (comp > 0) {
719 OsgiBootUtils.warn("Installed version " + bundleVersion
720 + " of bundle " + moduleName
721 + " is newer than provided version "
722 + moduleVersion);
723 } else if (comp < 0) {
724 urls.add(url);
725 OsgiBootUtils.info("Updated bundle " + moduleName
726 + " with version " + moduleVersion
727 + " (old version was " + bundleVersion + ")");
728 } else {
729 // do nothing
730 }
731 } else {
732 urls.add(url);
733 }
734 }
735 } catch (Exception e1) {
736 throw new RuntimeException("Cannot read url " + modulesUrlStr, e1);
737 } finally {
738 if (reader != null)
739 try {
740 reader.close();
741 } catch (IOException e) {
742 e.printStackTrace();
743 }
744 }
745 return urls;
746 }
747
748 /*
749 * HIGH LEVEL UTILITIES
750 */
751 /** Actually performs the matching logic. */
752 protected void match(PathMatcher matcher, List matched, String base,
753 String currentPath, String pattern) {
754 if (currentPath == null) {
755 // Init
756 File baseDir = new File(base.replace('/', File.separatorChar));
757 File[] files = baseDir.listFiles();
758
759 if (files == null) {
760 if (debug)
761 OsgiBootUtils.warn("Base dir " + baseDir
762 + " has no children, exists=" + baseDir.exists()
763 + ", isDirectory=" + baseDir.isDirectory());
764 return;
765 }
766
767 for (int i = 0; i < files.length; i++)
768 match(matcher, matched, base, files[i].getName(), pattern);
769 } else {
770 String fullPath = base + '/' + currentPath;
771 if (matched.contains(fullPath))
772 return;// don't try deeper if already matched
773
774 boolean ok = matcher.match(pattern, currentPath);
775 // if (debug)
776 // debug(currentPath + " " + (ok ? "" : " not ")
777 // + " matched with " + pattern);
778 if (ok) {
779 matched.add(fullPath);
780 return;
781 } else {
782 String newFullPath = relativeToFullPath(base, currentPath);
783 File newFile = new File(newFullPath);
784 File[] files = newFile.listFiles();
785 if (files != null) {
786 for (int i = 0; i < files.length; i++) {
787 String newCurrentPath = currentPath + '/'
788 + files[i].getName();
789 if (files[i].isDirectory()) {
790 if (matcher.matchStart(pattern, newCurrentPath)) {
791 // recurse only if start matches
792 match(matcher, matched, base, newCurrentPath,
793 pattern);
794 } else {
795 if (debug)
796 debug(newCurrentPath
797 + " does not start match with "
798 + pattern);
799
800 }
801 } else {
802 boolean nonDirectoryOk = matcher.match(pattern,
803 newCurrentPath);
804 if (debug)
805 debug(currentPath + " " + (ok ? "" : " not ")
806 + " matched with " + pattern);
807 if (nonDirectoryOk)
808 matched.add(relativeToFullPath(base,
809 newCurrentPath));
810 }
811 }
812 }
813 }
814 }
815 }
816
817 protected void matchFile() {
818
819 }
820
821 /*
822 * LOW LEVEL UTILITIES
823 */
824 /**
825 * The bundles already installed. Key is location (String) , value is a
826 * {@link Bundle}
827 */
828 public Map getBundlesByLocation() {
829 Map installedBundles = new HashMap();
830 Bundle[] bundles = bundleContext.getBundles();
831 for (int i = 0; i < bundles.length; i++) {
832 installedBundles.put(bundles[i].getLocation(), bundles[i]);
833 }
834 return installedBundles;
835 }
836
837 /**
838 * The bundles already installed. Key is symbolic name (String) , value is a
839 * {@link Bundle}
840 */
841 public Map getBundlesBySymbolicName() {
842 Map namedBundles = new HashMap();
843 Bundle[] bundles = bundleContext.getBundles();
844 for (int i = 0; i < bundles.length; i++) {
845 namedBundles.put(bundles[i].getSymbolicName(), bundles[i]);
846 }
847 return namedBundles;
848 }
849
850 /** Creates an URL from a location */
851 protected String locationToUrl(String baseUrl, String location) {
852 int extInd = location.lastIndexOf('.');
853 String ext = null;
854 if (extInd > 0)
855 ext = location.substring(extInd);
856
857 if (baseUrl.startsWith("reference:") && ".jar".equals(ext))
858 return "file:" + location;
859 else
860 return baseUrl + location;
861 }
862
863 /** Transforms a relative path in a full system path. */
864 protected String relativeToFullPath(String basePath, String relativePath) {
865 return (basePath + '/' + relativePath).replace('/', File.separatorChar);
866 }
867
868 private String removeFilePrefix(String url) {
869 if (url.startsWith("file:"))
870 return url.substring("file:".length());
871 else if (url.startsWith("reference:file:"))
872 return url.substring("reference:file:".length());
873 else
874 return url;
875 }
876
877 /**
878 * Convenience method to avoid cluttering the code with
879 * OsgiBootUtils.debug()
880 */
881 protected void debug(Object obj) {
882 OsgiBootUtils.debug(obj);
883 }
884
885 /*
886 * BEAN METHODS
887 */
888
889 public boolean getDebug() {
890 return debug;
891 }
892
893 public void setDebug(boolean debug) {
894 this.debug = debug;
895 }
896
897 public BundleContext getBundleContext() {
898 return bundleContext;
899 }
900
901 public void setInstallInLexicographicOrder(
902 boolean installInAlphabeticalOrder) {
903 this.installInLexicographicOrder = installInAlphabeticalOrder;
904 }
905
906 public boolean isInstallInLexicographicOrder() {
907 return installInLexicographicOrder;
908 }
909
910 public void setDefaultTimeout(long defaultTimeout) {
911 this.defaultTimeout = defaultTimeout;
912 }
913
914 public void setModulesUrlSeparator(String modulesUrlSeparator) {
915 this.modulesUrlSeparator = modulesUrlSeparator;
916 }
917
918 public boolean isExcludeSvn() {
919 return excludeSvn;
920 }
921
922 public void setExcludeSvn(boolean excludeSvn) {
923 this.excludeSvn = excludeSvn;
924 }
925
926 /*
927 * INTERNAL CLASSES
928 */
929
930 /** Intermediary structure used by path matching */
931 protected class BundlesSet {
932 private String baseUrl = "reference:file";// not used yet
933 private final String dir;
934 private List includes = new ArrayList();
935 private List excludes = new ArrayList();
936
937 public BundlesSet(String def) {
938 StringTokenizer st = new StringTokenizer(def, ";");
939
940 if (!st.hasMoreTokens())
941 throw new RuntimeException("Base dir not defined.");
942 try {
943 String dirPath = st.nextToken();
944
945 if (dirPath.startsWith("file:"))
946 dirPath = dirPath.substring("file:".length());
947
948 dir = new File(dirPath.replace('/', File.separatorChar))
949 .getCanonicalPath();
950 if (debug)
951 debug("Base dir: " + dir);
952 } catch (IOException e) {
953 throw new RuntimeException("Cannot convert to absolute path", e);
954 }
955
956 while (st.hasMoreTokens()) {
957 String tk = st.nextToken();
958 StringTokenizer stEq = new StringTokenizer(tk, "=");
959 String type = stEq.nextToken();
960 String pattern = stEq.nextToken();
961 if ("in".equals(type) || "include".equals(type)) {
962 includes.add(pattern);
963 } else if ("ex".equals(type) || "exclude".equals(type)) {
964 excludes.add(pattern);
965 } else if ("baseUrl".equals(type)) {
966 baseUrl = pattern;
967 } else {
968 System.err.println("Unkown bundles pattern type " + type);
969 }
970 }
971
972 if (excludeSvn && !excludes.contains(EXCLUDES_SVN_PATTERN)) {
973 excludes.add(EXCLUDES_SVN_PATTERN);
974 }
975 }
976
977 public String getDir() {
978 return dir;
979 }
980
981 public List getIncludes() {
982 return includes;
983 }
984
985 public List getExcludes() {
986 return excludes;
987 }
988
989 public String getBaseUrl() {
990 return baseUrl;
991 }
992
993 }
994
995 /* @deprecated Doesn't seem to be used anymore. */
996 // public void installOrUpdateUrls(Map urls) {
997 // Map installedBundles = getBundles();
998 //
999 // for (Iterator modules = urls.keySet().iterator(); modules.hasNext();) {
1000 // String moduleName = (String) modules.next();
1001 // String urlStr = (String) urls.get(moduleName);
1002 // if (installedBundles.containsKey(moduleName)) {
1003 // Bundle bundle = (Bundle) installedBundles.get(moduleName);
1004 // InputStream in;
1005 // try {
1006 // URL url = new URL(urlStr);
1007 // in = url.openStream();
1008 // bundle.update(in);
1009 // OsgiBootUtils.info("Updated bundle " + moduleName
1010 // + " from " + urlStr);
1011 // } catch (Exception e) {
1012 // throw new RuntimeException("Cannot update " + moduleName
1013 // + " from " + urlStr);
1014 // }
1015 // if (in != null)
1016 // try {
1017 // in.close();
1018 // } catch (IOException e) {
1019 // e.printStackTrace();
1020 // }
1021 // } else {
1022 // try {
1023 // Bundle bundle = bundleContext.installBundle(urlStr);
1024 // if (debug)
1025 // debug("Installed bundle " + bundle.getSymbolicName()
1026 // + " from " + urlStr);
1027 // } catch (BundleException e) {
1028 // OsgiBootUtils.warn("Could not install bundle from "
1029 // + urlStr + ": " + e.getMessage());
1030 // }
1031 // }
1032 // }
1033 //
1034 // }
1035
1036 }