2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.osgi
.boot
;
19 import java
.io
.BufferedReader
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStreamReader
;
24 import java
.util
.ArrayList
;
25 import java
.util
.HashMap
;
26 import java
.util
.Iterator
;
27 import java
.util
.List
;
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_ARGEO_OSGI_DATA_DIR
= "argeo.osgi.data.dir";
58 public final static String PROP_ARGEO_OSGI_START
= "argeo.osgi.start";
59 public final static String PROP_ARGEO_OSGI_BUNDLES
= "argeo.osgi.bundles";
60 public final static String PROP_ARGEO_OSGI_LOCATIONS
= "argeo.osgi.locations";
61 public final static String PROP_ARGEO_OSGI_BASE_URL
= "argeo.osgi.baseUrl";
62 /** Use org.argeo.osgi */
63 public final static String PROP_ARGEO_OSGI_MODULES_URL
= "argeo.osgi.modulesUrl";
66 public final static String PROP_ARGEO_OSGI_BOOT_DEBUG
= "argeo.osgi.boot.debug";
67 public final static String PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN
= "argeo.osgi.boot.excludeSvn";
68 public final static String PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER
= "argeo.osgi.boot.installInLexicographicOrder";
70 public final static String PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT
= "argeo.osgi.boot.defaultTimeout";
71 public final static String PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR
= "argeo.osgi.boot.modulesUrlSeparator";
72 public final static String PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE
= "argeo.osgi.boot.systemPropertiesFile";
73 public final static String PROP_ARGEO_OSGI_BOOT_APPCLASS
= "argeo.osgi.boot.appclass";
74 public final static String PROP_ARGEO_OSGI_BOOT_APPARGS
= "argeo.osgi.boot.appargs";
76 public final static String DEFAULT_BASE_URL
= "reference:file:";
77 public final static String EXCLUDES_SVN_PATTERN
= "**/.svn/**";
79 // OSGi system properties
80 public final static String INSTANCE_AREA_PROP
= "osgi.instance.area";
81 public final static String INSTANCE_AREA_DEFAULT_PROP
= "osgi.instance.area.default";
83 private boolean debug
= Boolean
.valueOf(
84 System
.getProperty(PROP_ARGEO_OSGI_BOOT_DEBUG
, "false"))
86 /** Exclude svn metadata implicitely(a bit costly) */
87 private boolean excludeSvn
= Boolean
.valueOf(
88 System
.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN
, "false"))
92 * The {@link #installUrls(List)} methods won't follow the list order but
93 * order the urls according to the alphabetical order of the file names
94 * (last part of the URL). The goal is to stay closer from Eclipse PDE way
95 * of installing target platform bundles.
97 private boolean installInLexicographicOrder
= Boolean
.valueOf(
99 PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER
,
100 "false")).booleanValue();;
102 /** Default is 10s (set in constructor) */
103 private long defaultTimeout
;
105 /** Default is ',' (set in constructor) */
106 private String modulesUrlSeparator
= ",";
108 private final BundleContext bundleContext
;
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();
124 * Set additional system properties, especially ${argeo.osgi.data.dir} as an
125 * OS file path (and not a file:// URL)
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");
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");
145 System
.setProperty(PROP_ARGEO_OSGI_DATA_DIR
, dataDir
.getAbsolutePath());
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());
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");
169 // display packages exported twice
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());
186 System
.out
.println();
192 /** Install the bundles at this URL list. */
193 public void installUrls(List urls
) {
194 Map installedBundles
= getBundlesByLocation();
196 if (installInLexicographicOrder
) {
197 SortedMap map
= new TreeMap();
199 for (int i
= 0; i
< urls
.size(); i
++) {
200 String url
= (String
) urls
.get(i
);
201 int index
= url
.lastIndexOf('/');
204 fileName
= url
.substring(index
+ 1);
207 map
.put(fileName
, url
);
211 Iterator keys
= map
.keySet().iterator();
212 while (keys
.hasNext()) {
213 Object key
= keys
.next();
214 String url
= map
.get(key
).toString();
215 installUrl(url
, installedBundles
);
218 for (int i
= 0; i
< urls
.size(); i
++) {
219 String url
= (String
) urls
.get(i
);
220 installUrl(url
, installedBundles
);
226 /** Actually install the provided URL */
227 protected void installUrl(String url
, Map installedBundles
) {
229 if (installedBundles
.containsKey(url
)) {
230 Bundle bundle
= (Bundle
) installedBundles
.get(url
);
233 debug("Bundle " + bundle
.getSymbolicName()
234 + " already installed from " + url
);
236 Bundle bundle
= bundleContext
.installBundle(url
);
238 debug("Installed bundle " + bundle
.getSymbolicName()
241 } catch (BundleException e
) {
242 String message
= e
.getMessage();
243 if ((message
.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT
+ "\"") || message
244 .contains("Bundle \"" + SYMBOLIC_NAME_EQUINOX
+ "\""))
245 && message
.contains("has already been installed")) {
246 // silent, in order to avoid warnings: we know that both
247 // have already been installed...
249 OsgiBootUtils
.warn("Could not install bundle from " + url
257 /* @deprecated Doesn't seem to be used anymore. */
258 // public void installOrUpdateUrls(Map urls) {
259 // Map installedBundles = getBundles();
261 // for (Iterator modules = urls.keySet().iterator(); modules.hasNext();) {
262 // String moduleName = (String) modules.next();
263 // String urlStr = (String) urls.get(moduleName);
264 // if (installedBundles.containsKey(moduleName)) {
265 // Bundle bundle = (Bundle) installedBundles.get(moduleName);
268 // URL url = new URL(urlStr);
269 // in = url.openStream();
270 // bundle.update(in);
271 // OsgiBootUtils.info("Updated bundle " + moduleName
272 // + " from " + urlStr);
273 // } catch (Exception e) {
274 // throw new RuntimeException("Cannot update " + moduleName
275 // + " from " + urlStr);
280 // } catch (IOException e) {
281 // e.printStackTrace();
285 // Bundle bundle = bundleContext.installBundle(urlStr);
287 // debug("Installed bundle " + bundle.getSymbolicName()
288 // + " from " + urlStr);
289 // } catch (BundleException e) {
290 // OsgiBootUtils.warn("Could not install bundle from "
291 // + urlStr + ": " + e.getMessage());
301 public void startBundles() {
302 String bundlesToStart
= OsgiBootUtils
303 .getProperty(PROP_ARGEO_OSGI_START
);
304 startBundles(bundlesToStart
);
307 /** Convenience method accepting a comma-separated list of bundle to start */
308 public void startBundles(String bundlesToStartStr
) {
309 if (bundlesToStartStr
== null)
312 StringTokenizer st
= new StringTokenizer(bundlesToStartStr
, ",");
313 List bundlesToStart
= new ArrayList();
314 while (st
.hasMoreTokens()) {
315 String name
= st
.nextToken().trim();
316 bundlesToStart
.add(name
);
318 startBundles(bundlesToStart
);
321 /** Start the provided list of bundles */
322 public void startBundles(List bundlesToStart
) {
323 if (bundlesToStart
.size() == 0)
326 // used to log the bundles not found
327 List notFoundBundles
= new ArrayList(bundlesToStart
);
329 Bundle
[] bundles
= bundleContext
.getBundles();
330 long startBegin
= System
.currentTimeMillis();
331 for (int i
= 0; i
< bundles
.length
; i
++) {
332 Bundle bundle
= bundles
[i
];
333 String symbolicName
= bundle
.getSymbolicName();
334 if (bundlesToStart
.contains(symbolicName
))
339 debug("Bundle " + symbolicName
+ " started");
340 } catch (Exception e
) {
341 OsgiBootUtils
.warn("Start of bundle " + symbolicName
342 + " failed because of " + e
343 + ", maybe bundle is not yet resolved,"
344 + " waiting and trying again.");
345 waitForBundleResolvedOrActive(startBegin
, bundle
);
348 notFoundBundles
.remove(symbolicName
);
349 } catch (Exception e
) {
350 OsgiBootUtils
.warn("Bundle " + symbolicName
351 + " cannot be started: " + e
.getMessage());
354 // was found even if start failed
355 notFoundBundles
.remove(symbolicName
);
359 for (int i
= 0; i
< notFoundBundles
.size(); i
++)
360 OsgiBootUtils
.warn("Bundle " + notFoundBundles
.get(i
)
361 + " not started because it was not found.");
367 /** Check unresolved bundles */
368 protected void checkUnresolved() {
370 ServiceReference packageAdminRef
= bundleContext
371 .getServiceReference(PackageAdmin
.class.getName());
372 PackageAdmin packageAdmin
= (PackageAdmin
) bundleContext
373 .getService(packageAdminRef
);
374 packageAdmin
.resolveBundles(null);
376 Bundle
[] bundles
= bundleContext
.getBundles();
377 List
/* Bundle */unresolvedBundles
= new ArrayList();
378 for (int i
= 0; i
< bundles
.length
; i
++) {
379 int bundleState
= bundles
[i
].getState();
380 if (!(bundleState
== Bundle
.ACTIVE
381 || bundleState
== Bundle
.RESOLVED
|| bundleState
== Bundle
.STARTING
))
382 unresolvedBundles
.add(bundles
[i
]);
385 if (unresolvedBundles
.size() != 0) {
386 OsgiBootUtils
.warn("Unresolved bundles " + unresolvedBundles
);
390 /** List packages exported twice. */
391 public Map
findPackagesExportedTwice() {
392 ServiceReference paSr
= bundleContext
393 .getServiceReference(PackageAdmin
.class.getName());
394 PackageAdmin packageAdmin
= (PackageAdmin
) bundleContext
397 // find packages exported twice
398 Bundle
[] bundles
= bundleContext
.getBundles();
399 Map
/* <String,Set<String>> */exportedPackages
= new TreeMap();
400 for (int i
= 0; i
< bundles
.length
; i
++) {
401 Bundle bundle
= bundles
[i
];
402 ExportedPackage
[] pkgs
= packageAdmin
.getExportedPackages(bundle
);
404 for (int j
= 0; j
< pkgs
.length
; j
++) {
405 String pkgName
= pkgs
[j
].getName();
406 if (!exportedPackages
.containsKey(pkgName
)) {
407 exportedPackages
.put(pkgName
, new TreeSet());
409 ((Set
) exportedPackages
.get(pkgName
)).add(bundle
410 .getSymbolicName() + "_" + bundle
.getVersion());
413 Map
/* <String,Set<String>> */duplicatePackages
= new TreeMap();
414 Iterator it
= exportedPackages
.keySet().iterator();
415 while (it
.hasNext()) {
416 String pkgName
= it
.next().toString();
417 Set bdles
= (Set
) exportedPackages
.get(pkgName
);
418 if (bdles
.size() > 1)
419 duplicatePackages
.put(pkgName
, bdles
);
421 return duplicatePackages
;
424 /** Waits for a bundle to become active or resolved */
425 protected void waitForBundleResolvedOrActive(long startBegin
, Bundle bundle
)
427 int originalState
= bundle
.getState();
428 if ((originalState
== Bundle
.RESOLVED
)
429 || (originalState
== Bundle
.ACTIVE
))
432 String originalStateStr
= OsgiBootUtils
.stateAsString(originalState
);
434 int currentState
= bundle
.getState();
435 while (!(currentState
== Bundle
.RESOLVED
|| currentState
== Bundle
.ACTIVE
)) {
436 long now
= System
.currentTimeMillis();
437 if ((now
- startBegin
) > defaultTimeout
)
438 throw new Exception("Bundle " + bundle
.getSymbolicName()
439 + " was not RESOLVED or ACTIVE after "
440 + (now
- startBegin
) + "ms (originalState="
441 + originalStateStr
+ ", currentState="
442 + OsgiBootUtils
.stateAsString(currentState
) + ")");
446 } catch (InterruptedException e
) {
449 currentState
= bundle
.getState();
454 * EXPLICIT LOCATIONS INSTALLATION
456 /** Gets the list of resolved explicit URL locations. */
457 public List
getLocationsUrls() {
458 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
,
460 String bundleLocations
= OsgiBootUtils
461 .getProperty(PROP_ARGEO_OSGI_LOCATIONS
);
462 return getLocationsUrls(baseUrl
, bundleLocations
);
466 * Gets a list of URLs based on explicit locations, resolving placeholder
467 * ${...} containing system properties, e.g. ${user.home}.
469 public List
getLocationsUrls(String baseUrl
, String bundleLocations
) {
470 List urls
= new ArrayList();
472 if (bundleLocations
== null)
474 bundleLocations
= SystemPropertyUtils
475 .resolvePlaceholders(bundleLocations
);
477 debug(PROP_ARGEO_OSGI_LOCATIONS
+ "=" + bundleLocations
);
479 StringTokenizer st
= new StringTokenizer(bundleLocations
,
481 while (st
.hasMoreTokens()) {
482 urls
.add(locationToUrl(baseUrl
, st
.nextToken().trim()));
488 * BUNDLE PATTERNS INSTALLATION
491 * Computes a list of URLs based on Ant-like incluide/exclude patterns
492 * defined by ${argeo.osgi.bundles} with the following format:<br>
493 * <code>/base/directory;in=*.jar;in=**;ex=org.eclipse.osgi_*;jar</code><br>
494 * WARNING: <code>/base/directory;in=*.jar,\</code> at the end of a file,
495 * without a new line causes a '.' to be appended with unexpected side
498 public List
getBundlesUrls() {
499 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
,
501 String bundlePatterns
= OsgiBootUtils
502 .getProperty(PROP_ARGEO_OSGI_BUNDLES
);
503 return getBundlesUrls(baseUrl
, bundlePatterns
);
506 /** Implements the path matching logic */
507 public List
getBundlesUrls(String baseUrl
, String bundlePatterns
) {
508 List urls
= new ArrayList();
509 if (bundlePatterns
== null)
512 bundlePatterns
= SystemPropertyUtils
513 .resolvePlaceholders(bundlePatterns
);
515 debug(PROP_ARGEO_OSGI_BUNDLES
+ "=" + bundlePatterns
516 + " (excludeSvn=" + excludeSvn
+ ")");
518 StringTokenizer st
= new StringTokenizer(bundlePatterns
, ",");
519 List bundlesSets
= new ArrayList();
520 while (st
.hasMoreTokens()) {
521 bundlesSets
.add(new BundlesSet(st
.nextToken()));
525 List included
= new ArrayList();
526 PathMatcher matcher
= new AntPathMatcher();
527 for (int i
= 0; i
< bundlesSets
.size(); i
++) {
528 BundlesSet bundlesSet
= (BundlesSet
) bundlesSets
.get(i
);
529 for (int j
= 0; j
< bundlesSet
.getIncludes().size(); j
++) {
530 String pattern
= (String
) bundlesSet
.getIncludes().get(j
);
531 match(matcher
, included
, bundlesSet
.getDir(), null, pattern
);
536 List excluded
= new ArrayList();
537 for (int i
= 0; i
< bundlesSets
.size(); i
++) {
538 BundlesSet bundlesSet
= (BundlesSet
) bundlesSets
.get(i
);
539 for (int j
= 0; j
< bundlesSet
.getExcludes().size(); j
++) {
540 String pattern
= (String
) bundlesSet
.getExcludes().get(j
);
541 match(matcher
, excluded
, bundlesSet
.getDir(), null, pattern
);
546 for (int i
= 0; i
< included
.size(); i
++) {
547 String fullPath
= (String
) included
.get(i
);
548 if (!excluded
.contains(fullPath
))
549 urls
.add(locationToUrl(baseUrl
, fullPath
));
556 * MODULES LIST INSTALLATION (${argeo.osgi.modulesUrl})
559 * Downloads a list of URLs in CSV format from ${argeo.osgi.modulesUrl}:<br>
560 * <code>Bundle-SymbolicName,Bundle-Version,url</code>)<br>
561 * If ${argeo.osgi.baseUrl} is set, URLs will be considered relative paths
562 * and be concatenated with the base URL, typically the root of a Maven
565 public List
getModulesUrls() {
566 List urls
= new ArrayList();
567 String modulesUrlStr
= OsgiBootUtils
568 .getProperty(PROP_ARGEO_OSGI_MODULES_URL
);
569 if (modulesUrlStr
== null)
572 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
);
574 Map installedBundles
= getBundlesBySymbolicName();
576 BufferedReader reader
= null;
578 URL modulesUrl
= new URL(modulesUrlStr
);
579 reader
= new BufferedReader(new InputStreamReader(
580 modulesUrl
.openStream()));
582 while ((line
= reader
.readLine()) != null) {
583 StringTokenizer st
= new StringTokenizer(line
,
584 modulesUrlSeparator
);
585 String moduleName
= st
.nextToken();
586 String moduleVersion
= st
.nextToken();
587 String url
= st
.nextToken();
591 if (installedBundles
.containsKey(moduleName
)) {
592 Bundle bundle
= (Bundle
) installedBundles
.get(moduleName
);
593 String bundleVersion
= bundle
.getHeaders()
594 .get(Constants
.BUNDLE_VERSION
).toString();
595 int comp
= OsgiBootUtils
.compareVersions(bundleVersion
,
598 OsgiBootUtils
.warn("Installed version " + bundleVersion
599 + " of bundle " + moduleName
600 + " is newer than provided version "
602 } else if (comp
< 0) {
604 OsgiBootUtils
.info("Updated bundle " + moduleName
605 + " with version " + moduleVersion
606 + " (old version was " + bundleVersion
+ ")");
614 } catch (Exception e1
) {
615 throw new RuntimeException("Cannot read url " + modulesUrlStr
, e1
);
620 } catch (IOException e
) {
628 * HIGH LEVEL UTILITIES
630 /** Actually performs the matching logic. */
631 protected void match(PathMatcher matcher
, List matched
, String base
,
632 String currentPath
, String pattern
) {
633 if (currentPath
== null) {
635 File baseDir
= new File(base
.replace('/', File
.separatorChar
));
636 File
[] files
= baseDir
.listFiles();
638 if (files
== null && debug
) {
639 OsgiBootUtils
.warn("Base dir " + baseDir
640 + " has no children, exists=" + baseDir
.exists()
641 + ", isDirectory=" + baseDir
.isDirectory());
645 for (int i
= 0; i
< files
.length
; i
++)
646 match(matcher
, matched
, base
, files
[i
].getName(), pattern
);
648 String fullPath
= base
+ '/' + currentPath
;
649 if (matched
.contains(fullPath
))
650 return;// don't try deeper if already matched
652 boolean ok
= matcher
.match(pattern
, currentPath
);
654 // debug(currentPath + " " + (ok ? "" : " not ")
655 // + " matched with " + pattern);
657 matched
.add(fullPath
);
660 String newFullPath
= relativeToFullPath(base
, currentPath
);
661 File newFile
= new File(newFullPath
);
662 File
[] files
= newFile
.listFiles();
664 for (int i
= 0; i
< files
.length
; i
++) {
665 String newCurrentPath
= currentPath
+ '/'
666 + files
[i
].getName();
667 if (files
[i
].isDirectory()) {
668 if (matcher
.matchStart(pattern
, newCurrentPath
)) {
669 // recurse only if start matches
670 match(matcher
, matched
, base
, newCurrentPath
,
675 + " does not start match with "
680 boolean nonDirectoryOk
= matcher
.match(pattern
,
683 debug(currentPath
+ " " + (ok ?
"" : " not ")
684 + " matched with " + pattern
);
686 matched
.add(relativeToFullPath(base
,
695 protected void matchFile() {
700 * LOW LEVEL UTILITIES
703 * The bundles already installed. Key is location (String) , value is a
706 public Map
getBundlesByLocation() {
707 Map installedBundles
= new HashMap();
708 Bundle
[] bundles
= bundleContext
.getBundles();
709 for (int i
= 0; i
< bundles
.length
; i
++) {
710 installedBundles
.put(bundles
[i
].getLocation(), bundles
[i
]);
712 return installedBundles
;
716 * The bundles already installed. Key is symbolic name (String) , value is a
719 public Map
getBundlesBySymbolicName() {
720 Map namedBundles
= new HashMap();
721 Bundle
[] bundles
= bundleContext
.getBundles();
722 for (int i
= 0; i
< bundles
.length
; i
++) {
723 namedBundles
.put(bundles
[i
].getSymbolicName(), bundles
[i
]);
728 /** Creates an URL from a location */
729 protected String
locationToUrl(String baseUrl
, String location
) {
730 int extInd
= location
.lastIndexOf('.');
733 ext
= location
.substring(extInd
);
735 if (baseUrl
.startsWith("reference:") && ".jar".equals(ext
))
736 return "file:" + location
;
738 return baseUrl
+ location
;
741 /** Transforms a relative path in a full system path. */
742 protected String
relativeToFullPath(String basePath
, String relativePath
) {
743 return (basePath
+ '/' + relativePath
).replace('/', File
.separatorChar
);
746 private String
removeFilePrefix(String url
) {
747 if (url
.startsWith("file:"))
748 return url
.substring("file:".length());
749 else if (url
.startsWith("reference:file:"))
750 return url
.substring("reference:file:".length());
756 * Convenience method to avoid cluttering the code with
757 * OsgiBootUtils.debug()
759 protected void debug(Object obj
) {
760 OsgiBootUtils
.debug(obj
);
767 public boolean getDebug() {
771 public void setDebug(boolean debug
) {
775 public BundleContext
getBundleContext() {
776 return bundleContext
;
779 public void setInstallInLexicographicOrder(
780 boolean installInAlphabeticalOrder
) {
781 this.installInLexicographicOrder
= installInAlphabeticalOrder
;
784 public boolean isInstallInLexicographicOrder() {
785 return installInLexicographicOrder
;
788 public void setDefaultTimeout(long defaultTimeout
) {
789 this.defaultTimeout
= defaultTimeout
;
792 public void setModulesUrlSeparator(String modulesUrlSeparator
) {
793 this.modulesUrlSeparator
= modulesUrlSeparator
;
800 /** Intermediary structure used by path matching */
801 protected class BundlesSet
{
802 private String baseUrl
= "reference:file";// not used yet
803 private final String dir
;
804 private List includes
= new ArrayList();
805 private List excludes
= new ArrayList();
807 public BundlesSet(String def
) {
808 StringTokenizer st
= new StringTokenizer(def
, ";");
810 if (!st
.hasMoreTokens())
811 throw new RuntimeException("Base dir not defined.");
813 String dirPath
= st
.nextToken();
815 if (dirPath
.startsWith("file:"))
816 dirPath
= dirPath
.substring("file:".length());
818 dir
= new File(dirPath
.replace('/', File
.separatorChar
))
821 debug("Base dir: " + dir
);
822 } catch (IOException e
) {
823 throw new RuntimeException("Cannot convert to absolute path", e
);
826 while (st
.hasMoreTokens()) {
827 String tk
= st
.nextToken();
828 StringTokenizer stEq
= new StringTokenizer(tk
, "=");
829 String type
= stEq
.nextToken();
830 String pattern
= stEq
.nextToken();
831 if ("in".equals(type
) || "include".equals(type
)) {
832 includes
.add(pattern
);
833 } else if ("ex".equals(type
) || "exclude".equals(type
)) {
834 excludes
.add(pattern
);
835 } else if ("baseUrl".equals(type
)) {
838 System
.err
.println("Unkown bundles pattern type " + type
);
842 if (excludeSvn
&& !excludes
.contains(EXCLUDES_SVN_PATTERN
)) {
843 excludes
.add(EXCLUDES_SVN_PATTERN
);
847 public String
getDir() {
851 public List
getIncludes() {
855 public List
getExcludes() {
859 public String
getBaseUrl() {