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