2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.osgi
.boot
;
18 import java
.io
.BufferedReader
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStreamReader
;
23 import java
.util
.ArrayList
;
24 import java
.util
.HashMap
;
25 import java
.util
.Iterator
;
26 import java
.util
.List
;
28 import java
.util
.Properties
;
30 import java
.util
.SortedMap
;
31 import java
.util
.StringTokenizer
;
32 import java
.util
.TreeMap
;
33 import java
.util
.TreeSet
;
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
;
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.
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";
56 public final static String PROP_OSGI_STARTLEVEL
= "osgi.startLevel";
57 public final static String PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL
= "osgi.bundles.defaultStartLevel";
59 public final static String PROP_ARGEO_OSGI_DATA_DIR
= "argeo.osgi.data.dir";
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";
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";
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";
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";
80 public final static String DEFAULT_BASE_URL
= "reference:file:";
81 public final static String EXCLUDES_SVN_PATTERN
= "**/.svn/**";
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";
87 private boolean debug
= Boolean
.valueOf(
88 System
.getProperty(PROP_ARGEO_OSGI_BOOT_DEBUG
, "false"))
90 /** Exclude svn metadata implicitely(a bit costly) */
91 private boolean excludeSvn
= Boolean
.valueOf(
92 System
.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN
, "false"))
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.
101 private boolean installInLexicographicOrder
= Boolean
104 PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER
,
105 "true")).booleanValue();;
107 /** Default is 10s (set in constructor) */
108 private long defaultTimeout
;
110 /** Default is ',' (set in constructor) */
111 private String modulesUrlSeparator
= ",";
113 private final BundleContext bundleContext
;
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();
129 * Set additional system properties, especially ${argeo.osgi.data.dir} as an
130 * OS file path (and not a file:// URL)
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");
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");
150 System
.setProperty(PROP_ARGEO_OSGI_DATA_DIR
, dataDir
.getAbsolutePath());
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());
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");
175 // display packages exported twice
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());
192 System
.out
.println();
198 /** Install a single url. Convenience method. */
199 public Bundle
installUrl(String url
) {
200 List urls
= new ArrayList();
203 return (Bundle
) getBundlesByLocation().get(url
);
206 /** Install the bundles at this URL list. */
207 public void installUrls(List urls
) {
208 Map installedBundles
= getBundlesByLocation();
210 if (installInLexicographicOrder
) {
211 SortedMap map
= new TreeMap();
213 for (int i
= 0; i
< urls
.size(); i
++) {
214 String url
= (String
) urls
.get(i
);
215 int index
= url
.lastIndexOf('/');
218 fileName
= url
.substring(index
+ 1);
221 map
.put(fileName
, url
);
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
);
232 for (int i
= 0; i
< urls
.size(); i
++) {
233 String url
= (String
) urls
.get(i
);
234 installUrl(url
, installedBundles
);
240 /** Actually install the provided URL */
241 protected void installUrl(String url
, Map installedBundles
) {
243 if (installedBundles
.containsKey(url
)) {
244 Bundle bundle
= (Bundle
) installedBundles
.get(url
);
247 debug("Bundle " + bundle
.getSymbolicName()
248 + " already installed from " + url
);
250 Bundle bundle
= bundleContext
.installBundle(url
);
252 debug("Installed bundle " + bundle
.getSymbolicName()
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...
263 OsgiBootUtils
.warn("Could not install bundle from " + url
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"));
281 SortedMap
/* <Integer, List<String>> */startLevels
= new TreeMap();
282 computeStartLevels(startLevels
, System
.getProperties(),
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
));
291 .warn("Not all bundles started for level " + level
);
292 if (level
.equals(activeStartLevel
))
293 break;// active start level reached
298 public static void computeStartLevels(
299 SortedMap
/* <Integer, List<String>> */startLevels
,
300 Properties properties
, Integer defaultStartLevel
) {
302 // default (and previously, only behaviour)
303 appendToStartLevels(startLevels
, defaultStartLevel
,
304 properties
.getProperty(PROP_ARGEO_OSGI_START
, ""));
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
)) {
313 String suffix
= key
.substring(prefix
.length());
314 String
[] tokens
= suffix
.split("\\.");
315 if (tokens
.length
> 0 && !tokens
[0].trim().equals(""))
317 // first token is start level
318 startLevel
= new Integer(tokens
[0]);
319 } catch (NumberFormatException e
) {
320 startLevel
= defaultStartLevel
;
323 startLevel
= defaultStartLevel
;
325 // append bundle names
326 String bundleNames
= properties
.getProperty(key
);
327 appendToStartLevels(startLevels
, startLevel
, bundleNames
);
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(""))
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
]);
349 * Convenience method accepting a comma-separated list of bundle to start
353 public void startBundles(String bundlesToStartStr
) {
354 if (bundlesToStartStr
== null)
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
);
363 startBundles(bundlesToStart
);
367 * Start the provided list of bundles
369 * @return whether all bundlesa are now in active state
371 public boolean startBundles(List bundlesToStart
) {
372 if (bundlesToStart
.size() == 0)
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
);
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
))
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
);
398 startedBundles
.add(bundle
);
400 notFoundBundles
.remove(symbolicName
);
401 } catch (Exception e
) {
402 OsgiBootUtils
.warn("Bundle " + symbolicName
403 + " cannot be started: " + e
.getMessage());
406 // was found even if start failed
407 notFoundBundles
.remove(symbolicName
);
411 for (int i
= 0; i
< notFoundBundles
.size(); i
++)
412 OsgiBootUtils
.warn("Bundle '" + notFoundBundles
.get(i
)
413 + "' not started because it was not found.");
415 // monitors that all bundles are started
416 long beginMonitor
= System
.currentTimeMillis();
417 boolean allStarted
= !(startedBundles
.size() > 0);
418 List
/* <String> */notStarted
= new ArrayList();
420 && (System
.currentTimeMillis() - beginMonitor
) < defaultTimeout
) {
421 notStarted
= new ArrayList();
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
) {
428 notStarted
.add(bundle
.getSymbolicName());
433 } catch (InterruptedException e
) {
437 long duration
= System
.currentTimeMillis() - beginMonitor
;
440 for (int i
= 0; i
< notStarted
.size(); i
++)
441 OsgiBootUtils
.warn("Bundle '" + notStarted
.get(i
)
442 + "' not ACTIVE after " + (duration
/ 1000) + "s");
450 /** Check unresolved bundles */
451 protected void checkUnresolved() {
453 ServiceReference packageAdminRef
= bundleContext
454 .getServiceReference(PackageAdmin
.class.getName());
455 PackageAdmin packageAdmin
= (PackageAdmin
) bundleContext
456 .getService(packageAdminRef
);
457 packageAdmin
.resolveBundles(null);
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
]);
468 if (unresolvedBundles
.size() != 0) {
469 OsgiBootUtils
.warn("Unresolved bundles " + unresolvedBundles
);
473 /** List packages exported twice. */
474 public Map
findPackagesExportedTwice() {
475 ServiceReference paSr
= bundleContext
476 .getServiceReference(PackageAdmin
.class.getName());
477 PackageAdmin packageAdmin
= (PackageAdmin
) bundleContext
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
);
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());
492 ((Set
) exportedPackages
.get(pkgName
)).add(bundle
493 .getSymbolicName() + "_" + bundle
.getVersion());
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
);
504 return duplicatePackages
;
507 /** Waits for a bundle to become active or resolved */
508 protected void waitForBundleResolvedOrActive(long startBegin
, Bundle bundle
)
510 int originalState
= bundle
.getState();
511 if ((originalState
== Bundle
.RESOLVED
)
512 || (originalState
== Bundle
.ACTIVE
))
515 String originalStateStr
= OsgiBootUtils
.stateAsString(originalState
);
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
) + ")");
529 } catch (InterruptedException e
) {
532 currentState
= bundle
.getState();
537 * EXPLICIT LOCATIONS INSTALLATION
539 /** Gets the list of resolved explicit URL locations. */
540 public List
getLocationsUrls() {
541 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
,
543 String bundleLocations
= OsgiBootUtils
544 .getProperty(PROP_ARGEO_OSGI_LOCATIONS
);
545 return getLocationsUrls(baseUrl
, bundleLocations
);
549 * Gets a list of URLs based on explicit locations, resolving placeholder
550 * ${...} containing system properties, e.g. ${user.home}.
552 public List
getLocationsUrls(String baseUrl
, String bundleLocations
) {
553 List urls
= new ArrayList();
555 if (bundleLocations
== null)
557 bundleLocations
= SystemPropertyUtils
558 .resolvePlaceholders(bundleLocations
);
560 debug(PROP_ARGEO_OSGI_LOCATIONS
+ "=" + bundleLocations
);
562 StringTokenizer st
= new StringTokenizer(bundleLocations
,
564 while (st
.hasMoreTokens()) {
565 urls
.add(locationToUrl(baseUrl
, st
.nextToken().trim()));
571 * BUNDLE PATTERNS INSTALLATION
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
581 public List
getBundlesUrls() {
582 String bundlePatterns
= OsgiBootUtils
583 .getProperty(PROP_ARGEO_OSGI_BUNDLES
);
584 return getBundlesUrls(bundlePatterns
);
588 * Compute alist of URLs to install based on the provided patterns, with
591 public List
getBundlesUrls(String bundlePatterns
) {
592 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
,
594 return getBundlesUrls(baseUrl
, bundlePatterns
);
597 /** Implements the path matching logic */
598 List
getBundlesUrls(String baseUrl
, String bundlePatterns
) {
599 List urls
= new ArrayList();
600 if (bundlePatterns
== null)
603 bundlePatterns
= SystemPropertyUtils
604 .resolvePlaceholders(bundlePatterns
);
606 debug(PROP_ARGEO_OSGI_BUNDLES
+ "=" + bundlePatterns
607 + " (excludeSvn=" + excludeSvn
+ ")");
609 StringTokenizer st
= new StringTokenizer(bundlePatterns
, ",");
610 List bundlesSets
= new ArrayList();
611 while (st
.hasMoreTokens()) {
612 bundlesSets
.add(new BundlesSet(st
.nextToken()));
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
);
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
);
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
));
647 * DISTRIBUTION JAR INSTALLATION
649 public List
getDistributionUrls() {
650 List urls
= new ArrayList();
651 String distributionUrl
= OsgiBootUtils
652 .getProperty(PROP_ARGEO_OSGI_DISTRIBUTION_URL
);
653 if (distributionUrl
== null)
655 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
);
657 DistributionBundle distributionBundle
;
659 && !(distributionUrl
.startsWith("http") || distributionUrl
660 .startsWith("file"))) {
662 distributionBundle
= new DistributionBundle(baseUrl
,
665 distributionBundle
= new DistributionBundle(distributionUrl
);
667 distributionBundle
.setBaseUrl(baseUrl
);
670 distributionBundle
.processUrl();
671 return distributionBundle
.listUrls();
675 * MODULES LIST INSTALLATION (${argeo.osgi.modulesUrl})
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
686 public List
getModulesUrls() {
687 List urls
= new ArrayList();
688 String modulesUrlStr
= OsgiBootUtils
689 .getProperty(PROP_ARGEO_OSGI_MODULES_URL
);
690 if (modulesUrlStr
== null)
693 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
);
695 Map installedBundles
= getBundlesBySymbolicName();
697 BufferedReader reader
= null;
699 URL modulesUrl
= new URL(modulesUrlStr
);
700 reader
= new BufferedReader(new InputStreamReader(
701 modulesUrl
.openStream()));
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();
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
,
719 OsgiBootUtils
.warn("Installed version " + bundleVersion
720 + " of bundle " + moduleName
721 + " is newer than provided version "
723 } else if (comp
< 0) {
725 OsgiBootUtils
.info("Updated bundle " + moduleName
726 + " with version " + moduleVersion
727 + " (old version was " + bundleVersion
+ ")");
735 } catch (Exception e1
) {
736 throw new RuntimeException("Cannot read url " + modulesUrlStr
, e1
);
741 } catch (IOException e
) {
749 * HIGH LEVEL UTILITIES
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) {
756 File baseDir
= new File(base
.replace('/', File
.separatorChar
));
757 File
[] files
= baseDir
.listFiles();
761 OsgiBootUtils
.warn("Base dir " + baseDir
762 + " has no children, exists=" + baseDir
.exists()
763 + ", isDirectory=" + baseDir
.isDirectory());
767 for (int i
= 0; i
< files
.length
; i
++)
768 match(matcher
, matched
, base
, files
[i
].getName(), pattern
);
770 String fullPath
= base
+ '/' + currentPath
;
771 if (matched
.contains(fullPath
))
772 return;// don't try deeper if already matched
774 boolean ok
= matcher
.match(pattern
, currentPath
);
776 // debug(currentPath + " " + (ok ? "" : " not ")
777 // + " matched with " + pattern);
779 matched
.add(fullPath
);
782 String newFullPath
= relativeToFullPath(base
, currentPath
);
783 File newFile
= new File(newFullPath
);
784 File
[] files
= newFile
.listFiles();
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
,
797 + " does not start match with "
802 boolean nonDirectoryOk
= matcher
.match(pattern
,
805 debug(currentPath
+ " " + (ok ?
"" : " not ")
806 + " matched with " + pattern
);
808 matched
.add(relativeToFullPath(base
,
817 protected void matchFile() {
822 * LOW LEVEL UTILITIES
825 * The bundles already installed. Key is location (String) , value is a
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
]);
834 return installedBundles
;
838 * The bundles already installed. Key is symbolic name (String) , value is a
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
]);
850 /** Creates an URL from a location */
851 protected String
locationToUrl(String baseUrl
, String location
) {
852 // int extInd = location.lastIndexOf('.');
853 // String ext = null;
855 // ext = location.substring(extInd);
857 // if (baseUrl.startsWith("reference:") && ".jar".equals(ext))
858 // return "file:" + location;
860 return baseUrl
+ location
;
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
);
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());
878 * Convenience method to avoid cluttering the code with
879 * OsgiBootUtils.debug()
881 protected void debug(Object obj
) {
882 OsgiBootUtils
.debug(obj
);
889 public boolean getDebug() {
893 public void setDebug(boolean debug
) {
897 public BundleContext
getBundleContext() {
898 return bundleContext
;
901 public void setInstallInLexicographicOrder(
902 boolean installInAlphabeticalOrder
) {
903 this.installInLexicographicOrder
= installInAlphabeticalOrder
;
906 public boolean isInstallInLexicographicOrder() {
907 return installInLexicographicOrder
;
910 public void setDefaultTimeout(long defaultTimeout
) {
911 this.defaultTimeout
= defaultTimeout
;
914 public void setModulesUrlSeparator(String modulesUrlSeparator
) {
915 this.modulesUrlSeparator
= modulesUrlSeparator
;
918 public boolean isExcludeSvn() {
922 public void setExcludeSvn(boolean excludeSvn
) {
923 this.excludeSvn
= excludeSvn
;
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();
937 public BundlesSet(String def
) {
938 StringTokenizer st
= new StringTokenizer(def
, ";");
940 if (!st
.hasMoreTokens())
941 throw new RuntimeException("Base dir not defined.");
943 String dirPath
= st
.nextToken();
945 if (dirPath
.startsWith("file:"))
946 dirPath
= dirPath
.substring("file:".length());
948 dir
= new File(dirPath
.replace('/', File
.separatorChar
))
951 debug("Base dir: " + dir
);
952 } catch (IOException e
) {
953 throw new RuntimeException("Cannot convert to absolute path", e
);
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
)) {
968 System
.err
.println("Unkown bundles pattern type " + type
);
972 if (excludeSvn
&& !excludes
.contains(EXCLUDES_SVN_PATTERN
)) {
973 excludes
.add(EXCLUDES_SVN_PATTERN
);
977 public String
getDir() {
981 public List
getIncludes() {
985 public List
getExcludes() {
989 public String
getBaseUrl() {
995 /* @deprecated Doesn't seem to be used anymore. */
996 // public void installOrUpdateUrls(Map urls) {
997 // Map installedBundles = getBundles();
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);
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);
1018 // } catch (IOException e) {
1019 // e.printStackTrace();
1023 // Bundle bundle = bundleContext.installBundle(urlStr);
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());