2 * Copyright (C) 2007-2012 Mathieu Baudier
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
;
29 import java
.util
.SortedMap
;
30 import java
.util
.StringTokenizer
;
31 import java
.util
.TreeMap
;
32 import java
.util
.TreeSet
;
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
;
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.
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";
55 public final static String PROP_ARGEO_OSGI_DATA_DIR
= "argeo.osgi.data.dir";
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";
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";
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";
75 public final static String DEFAULT_BASE_URL
= "reference:file:";
76 public final static String EXCLUDES_SVN_PATTERN
= "**/.svn/**";
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";
82 private boolean debug
= Boolean
.valueOf(
83 System
.getProperty(PROP_ARGEO_OSGI_BOOT_DEBUG
, "false"))
85 /** Exclude svn metadata implicitely(a bit costly) */
86 private boolean excludeSvn
= Boolean
.valueOf(
87 System
.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN
, "false"))
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.
96 private boolean installInLexicographicOrder
= Boolean
99 PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER
,
100 "true")).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 a single url. Convenience method. */
193 public Bundle
installUrl(String url
) {
194 List urls
= new ArrayList();
197 return (Bundle
) getBundlesByLocation().get(url
);
200 /** Install the bundles at this URL list. */
201 public void installUrls(List urls
) {
202 Map installedBundles
= getBundlesByLocation();
204 if (installInLexicographicOrder
) {
205 SortedMap map
= new TreeMap();
207 for (int i
= 0; i
< urls
.size(); i
++) {
208 String url
= (String
) urls
.get(i
);
209 int index
= url
.lastIndexOf('/');
212 fileName
= url
.substring(index
+ 1);
215 map
.put(fileName
, url
);
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
);
226 for (int i
= 0; i
< urls
.size(); i
++) {
227 String url
= (String
) urls
.get(i
);
228 installUrl(url
, installedBundles
);
234 /** Actually install the provided URL */
235 protected void installUrl(String url
, Map installedBundles
) {
237 if (installedBundles
.containsKey(url
)) {
238 Bundle bundle
= (Bundle
) installedBundles
.get(url
);
241 debug("Bundle " + bundle
.getSymbolicName()
242 + " already installed from " + url
);
244 Bundle bundle
= bundleContext
.installBundle(url
);
246 debug("Installed bundle " + bundle
.getSymbolicName()
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...
257 OsgiBootUtils
.warn("Could not install bundle from " + url
265 /* @deprecated Doesn't seem to be used anymore. */
266 // public void installOrUpdateUrls(Map urls) {
267 // Map installedBundles = getBundles();
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);
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);
288 // } catch (IOException e) {
289 // e.printStackTrace();
293 // Bundle bundle = bundleContext.installBundle(urlStr);
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());
309 public void startBundles() {
310 String bundlesToStart
= OsgiBootUtils
311 .getProperty(PROP_ARGEO_OSGI_START
);
312 startBundles(bundlesToStart
);
315 /** Convenience method accepting a comma-separated list of bundle to start */
316 public void startBundles(String bundlesToStartStr
) {
317 if (bundlesToStartStr
== null)
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
);
326 startBundles(bundlesToStart
);
329 /** Start the provided list of bundles */
330 public void startBundles(List bundlesToStart
) {
331 if (bundlesToStart
.size() == 0)
334 // used to log the bundles not found
335 List notFoundBundles
= new ArrayList(bundlesToStart
);
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
))
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
);
356 notFoundBundles
.remove(symbolicName
);
357 } catch (Exception e
) {
358 OsgiBootUtils
.warn("Bundle " + symbolicName
359 + " cannot be started: " + e
.getMessage());
362 // was found even if start failed
363 notFoundBundles
.remove(symbolicName
);
367 for (int i
= 0; i
< notFoundBundles
.size(); i
++)
368 OsgiBootUtils
.warn("Bundle " + notFoundBundles
.get(i
)
369 + " not started because it was not found.");
375 /** Check unresolved bundles */
376 protected void checkUnresolved() {
378 ServiceReference packageAdminRef
= bundleContext
379 .getServiceReference(PackageAdmin
.class.getName());
380 PackageAdmin packageAdmin
= (PackageAdmin
) bundleContext
381 .getService(packageAdminRef
);
382 packageAdmin
.resolveBundles(null);
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
]);
393 if (unresolvedBundles
.size() != 0) {
394 OsgiBootUtils
.warn("Unresolved bundles " + unresolvedBundles
);
398 /** List packages exported twice. */
399 public Map
findPackagesExportedTwice() {
400 ServiceReference paSr
= bundleContext
401 .getServiceReference(PackageAdmin
.class.getName());
402 PackageAdmin packageAdmin
= (PackageAdmin
) bundleContext
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
);
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());
417 ((Set
) exportedPackages
.get(pkgName
)).add(bundle
418 .getSymbolicName() + "_" + bundle
.getVersion());
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
);
429 return duplicatePackages
;
432 /** Waits for a bundle to become active or resolved */
433 protected void waitForBundleResolvedOrActive(long startBegin
, Bundle bundle
)
435 int originalState
= bundle
.getState();
436 if ((originalState
== Bundle
.RESOLVED
)
437 || (originalState
== Bundle
.ACTIVE
))
440 String originalStateStr
= OsgiBootUtils
.stateAsString(originalState
);
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
) + ")");
454 } catch (InterruptedException e
) {
457 currentState
= bundle
.getState();
462 * EXPLICIT LOCATIONS INSTALLATION
464 /** Gets the list of resolved explicit URL locations. */
465 public List
getLocationsUrls() {
466 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
,
468 String bundleLocations
= OsgiBootUtils
469 .getProperty(PROP_ARGEO_OSGI_LOCATIONS
);
470 return getLocationsUrls(baseUrl
, bundleLocations
);
474 * Gets a list of URLs based on explicit locations, resolving placeholder
475 * ${...} containing system properties, e.g. ${user.home}.
477 public List
getLocationsUrls(String baseUrl
, String bundleLocations
) {
478 List urls
= new ArrayList();
480 if (bundleLocations
== null)
482 bundleLocations
= SystemPropertyUtils
483 .resolvePlaceholders(bundleLocations
);
485 debug(PROP_ARGEO_OSGI_LOCATIONS
+ "=" + bundleLocations
);
487 StringTokenizer st
= new StringTokenizer(bundleLocations
,
489 while (st
.hasMoreTokens()) {
490 urls
.add(locationToUrl(baseUrl
, st
.nextToken().trim()));
496 * BUNDLE PATTERNS INSTALLATION
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
506 public List
getBundlesUrls() {
507 String bundlePatterns
= OsgiBootUtils
508 .getProperty(PROP_ARGEO_OSGI_BUNDLES
);
509 return getBundlesUrls(bundlePatterns
);
513 * Compute alist of URLs to install based on the provided patterns, with
516 public List
getBundlesUrls(String bundlePatterns
) {
517 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
,
519 return getBundlesUrls(baseUrl
, bundlePatterns
);
522 /** Implements the path matching logic */
523 List
getBundlesUrls(String baseUrl
, String bundlePatterns
) {
524 List urls
= new ArrayList();
525 if (bundlePatterns
== null)
528 bundlePatterns
= SystemPropertyUtils
529 .resolvePlaceholders(bundlePatterns
);
531 debug(PROP_ARGEO_OSGI_BUNDLES
+ "=" + bundlePatterns
532 + " (excludeSvn=" + excludeSvn
+ ")");
534 StringTokenizer st
= new StringTokenizer(bundlePatterns
, ",");
535 List bundlesSets
= new ArrayList();
536 while (st
.hasMoreTokens()) {
537 bundlesSets
.add(new BundlesSet(st
.nextToken()));
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
);
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
);
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
));
572 * MODULES LIST INSTALLATION (${argeo.osgi.modulesUrl})
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
581 public List
getModulesUrls() {
582 List urls
= new ArrayList();
583 String modulesUrlStr
= OsgiBootUtils
584 .getProperty(PROP_ARGEO_OSGI_MODULES_URL
);
585 if (modulesUrlStr
== null)
588 String baseUrl
= OsgiBootUtils
.getProperty(PROP_ARGEO_OSGI_BASE_URL
);
590 Map installedBundles
= getBundlesBySymbolicName();
592 BufferedReader reader
= null;
594 URL modulesUrl
= new URL(modulesUrlStr
);
595 reader
= new BufferedReader(new InputStreamReader(
596 modulesUrl
.openStream()));
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();
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
,
614 OsgiBootUtils
.warn("Installed version " + bundleVersion
615 + " of bundle " + moduleName
616 + " is newer than provided version "
618 } else if (comp
< 0) {
620 OsgiBootUtils
.info("Updated bundle " + moduleName
621 + " with version " + moduleVersion
622 + " (old version was " + bundleVersion
+ ")");
630 } catch (Exception e1
) {
631 throw new RuntimeException("Cannot read url " + modulesUrlStr
, e1
);
636 } catch (IOException e
) {
644 * HIGH LEVEL UTILITIES
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) {
651 File baseDir
= new File(base
.replace('/', File
.separatorChar
));
652 File
[] files
= baseDir
.listFiles();
656 OsgiBootUtils
.warn("Base dir " + baseDir
657 + " has no children, exists=" + baseDir
.exists()
658 + ", isDirectory=" + baseDir
.isDirectory());
662 for (int i
= 0; i
< files
.length
; i
++)
663 match(matcher
, matched
, base
, files
[i
].getName(), pattern
);
665 String fullPath
= base
+ '/' + currentPath
;
666 if (matched
.contains(fullPath
))
667 return;// don't try deeper if already matched
669 boolean ok
= matcher
.match(pattern
, currentPath
);
671 // debug(currentPath + " " + (ok ? "" : " not ")
672 // + " matched with " + pattern);
674 matched
.add(fullPath
);
677 String newFullPath
= relativeToFullPath(base
, currentPath
);
678 File newFile
= new File(newFullPath
);
679 File
[] files
= newFile
.listFiles();
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
,
692 + " does not start match with "
697 boolean nonDirectoryOk
= matcher
.match(pattern
,
700 debug(currentPath
+ " " + (ok ?
"" : " not ")
701 + " matched with " + pattern
);
703 matched
.add(relativeToFullPath(base
,
712 protected void matchFile() {
717 * LOW LEVEL UTILITIES
720 * The bundles already installed. Key is location (String) , value is a
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
]);
729 return installedBundles
;
733 * The bundles already installed. Key is symbolic name (String) , value is a
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
]);
745 /** Creates an URL from a location */
746 protected String
locationToUrl(String baseUrl
, String location
) {
747 int extInd
= location
.lastIndexOf('.');
750 ext
= location
.substring(extInd
);
752 if (baseUrl
.startsWith("reference:") && ".jar".equals(ext
))
753 return "file:" + location
;
755 return baseUrl
+ location
;
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
);
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());
773 * Convenience method to avoid cluttering the code with
774 * OsgiBootUtils.debug()
776 protected void debug(Object obj
) {
777 OsgiBootUtils
.debug(obj
);
784 public boolean getDebug() {
788 public void setDebug(boolean debug
) {
792 public BundleContext
getBundleContext() {
793 return bundleContext
;
796 public void setInstallInLexicographicOrder(
797 boolean installInAlphabeticalOrder
) {
798 this.installInLexicographicOrder
= installInAlphabeticalOrder
;
801 public boolean isInstallInLexicographicOrder() {
802 return installInLexicographicOrder
;
805 public void setDefaultTimeout(long defaultTimeout
) {
806 this.defaultTimeout
= defaultTimeout
;
809 public void setModulesUrlSeparator(String modulesUrlSeparator
) {
810 this.modulesUrlSeparator
= modulesUrlSeparator
;
813 public boolean isExcludeSvn() {
817 public void setExcludeSvn(boolean excludeSvn
) {
818 this.excludeSvn
= excludeSvn
;
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();
832 public BundlesSet(String def
) {
833 StringTokenizer st
= new StringTokenizer(def
, ";");
835 if (!st
.hasMoreTokens())
836 throw new RuntimeException("Base dir not defined.");
838 String dirPath
= st
.nextToken();
840 if (dirPath
.startsWith("file:"))
841 dirPath
= dirPath
.substring("file:".length());
843 dir
= new File(dirPath
.replace('/', File
.separatorChar
))
846 debug("Base dir: " + dir
);
847 } catch (IOException e
) {
848 throw new RuntimeException("Cannot convert to absolute path", e
);
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
)) {
863 System
.err
.println("Unkown bundles pattern type " + type
);
867 if (excludeSvn
&& !excludes
.contains(EXCLUDES_SVN_PATTERN
)) {
868 excludes
.add(EXCLUDES_SVN_PATTERN
);
872 public String
getDir() {
876 public List
getIncludes() {
880 public List
getExcludes() {
884 public String
getBaseUrl() {