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 static org
.argeo
.osgi
.boot
.OsgiBootUtils
.debug
;
19 import static org
.argeo
.osgi
.boot
.OsgiBootUtils
.warn
;
22 import java
.nio
.file
.Paths
;
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
;
34 import org
.argeo
.osgi
.boot
.a2
.ProvisioningManager
;
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
.FrameworkEvent
;
42 import org
.osgi
.framework
.Version
;
43 import org
.osgi
.framework
.startlevel
.BundleStartLevel
;
44 import org
.osgi
.framework
.startlevel
.FrameworkStartLevel
;
45 import org
.osgi
.framework
.wiring
.FrameworkWiring
;
48 * Basic provisioning of an OSGi runtime via file path patterns and system
49 * properties. The approach is to generate list of URLs based on various
50 * methods, configured via properties.
52 public class OsgiBoot
implements OsgiBootConstants
{
53 public final static String PROP_ARGEO_OSGI_START
= "argeo.osgi.start";
54 public final static String PROP_ARGEO_OSGI_SOURCES
= "argeo.osgi.sources";
56 public final static String PROP_ARGEO_OSGI_BUNDLES
= "argeo.osgi.bundles";
57 public final static String PROP_ARGEO_OSGI_BASE_URL
= "argeo.osgi.baseUrl";
58 public final static String PROP_ARGEO_OSGI_LOCAL_CACHE
= "argeo.osgi.localCache";
59 public final static String PROP_ARGEO_OSGI_DISTRIBUTION_URL
= "argeo.osgi.distributionUrl";
62 public final static String PROP_ARGEO_OSGI_BOOT_DEBUG
= "argeo.osgi.boot.debug";
63 // public final static String PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN =
64 // "argeo.osgi.boot.excludeSvn";
66 public final static String PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE
= "argeo.osgi.boot.systemPropertiesFile";
67 public final static String PROP_ARGEO_OSGI_BOOT_APPCLASS
= "argeo.osgi.boot.appclass";
68 public final static String PROP_ARGEO_OSGI_BOOT_APPARGS
= "argeo.osgi.boot.appargs";
70 public final static String DEFAULT_BASE_URL
= "reference:file:";
71 // public final static String EXCLUDES_SVN_PATTERN = "**/.svn/**";
73 // OSGi system properties
74 final static String PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL
= "osgi.bundles.defaultStartLevel";
75 final static String PROP_OSGI_STARTLEVEL
= "osgi.startLevel";
76 final static String INSTANCE_AREA_PROP
= "osgi.instance.area";
77 final static String CONFIGURATION_AREA_PROP
= "osgi.configuration.area";
80 public final static String SYMBOLIC_NAME_OSGI_BOOT
= "org.argeo.osgi.boot";
81 public final static String SYMBOLIC_NAME_EQUINOX
= "org.eclipse.osgi";
83 /** Exclude svn metadata implicitely(a bit costly) */
84 // private boolean excludeSvn =
85 // Boolean.valueOf(System.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN,
91 private long defaultTimeout
= 10000l;
93 private final BundleContext bundleContext
;
94 private final String localCache
;
96 private final ProvisioningManager provisioningManager
;
102 public OsgiBoot(BundleContext bundleContext
) {
103 this.bundleContext
= bundleContext
;
104 String homeUri
= Paths
.get(System
.getProperty("user.home")).toUri().toString();
105 localCache
= getProperty(PROP_ARGEO_OSGI_LOCAL_CACHE
, homeUri
+ ".m2/repository/");
107 provisioningManager
= new ProvisioningManager(bundleContext
);
108 String sources
= getProperty(PROP_ARGEO_OSGI_SOURCES
);
109 if (sources
== null) {
110 provisioningManager
.registerDefaultSource();
112 for (String source
: sources
.split(",")) {
113 provisioningManager
.registerSource(source
);
118 ProvisioningManager
getProvisioningManager() {
119 return provisioningManager
;
125 /** Bootstraps the OSGi runtime */
126 public void bootstrap() {
128 long begin
= System
.currentTimeMillis();
129 System
.out
.println();
130 String osgiInstancePath
= bundleContext
.getProperty(INSTANCE_AREA_PROP
);
132 .info("OSGi bootstrap starting" + (osgiInstancePath
!= null ?
" (" + osgiInstancePath
+ ")" : ""));
133 installUrls(getBundlesUrls());
134 installUrls(getDistributionUrls());
135 provisioningManager
.install(null);
137 long duration
= System
.currentTimeMillis() - begin
;
138 OsgiBootUtils
.info("OSGi bootstrap completed in " + Math
.round(((double) duration
) / 1000) + "s ("
139 + duration
+ "ms), " + bundleContext
.getBundles().length
+ " bundles");
140 } catch (RuntimeException e
) {
141 OsgiBootUtils
.error("OSGi bootstrap FAILED", e
);
146 if (OsgiBootUtils
.debug
) {
147 OsgiBootDiagnostics diagnostics
= new OsgiBootDiagnostics(bundleContext
);
148 diagnostics
.checkUnresolved();
149 Map
<String
, Set
<String
>> duplicatePackages
= diagnostics
.findPackagesExportedTwice();
150 if (duplicatePackages
.size() > 0) {
151 OsgiBootUtils
.info("Packages exported twice:");
152 Iterator
<String
> it
= duplicatePackages
.keySet().iterator();
153 while (it
.hasNext()) {
154 String pkgName
= it
.next();
155 OsgiBootUtils
.info(pkgName
);
156 Set
<String
> bdles
= duplicatePackages
.get(pkgName
);
157 Iterator
<String
> bdlesIt
= bdles
.iterator();
158 while (bdlesIt
.hasNext())
159 OsgiBootUtils
.info(" " + bdlesIt
.next());
163 System
.out
.println();
166 public void update() {
167 provisioningManager
.update();
173 /** Install a single url. Convenience method. */
174 public Bundle
installUrl(String url
) {
175 List
<String
> urls
= new ArrayList
<String
>();
178 return (Bundle
) getBundlesByLocation().get(url
);
181 /** Install the bundles at this URL list. */
182 public void installUrls(List
<String
> urls
) {
183 Map
<String
, Bundle
> installedBundles
= getBundlesByLocation();
184 for (int i
= 0; i
< urls
.size(); i
++) {
185 String url
= (String
) urls
.get(i
);
186 installUrl(url
, installedBundles
);
191 /** Actually install the provided URL */
192 protected void installUrl(String url
, Map
<String
, Bundle
> installedBundles
) {
194 if (installedBundles
.containsKey(url
)) {
195 Bundle bundle
= (Bundle
) installedBundles
.get(url
);
196 if (OsgiBootUtils
.debug
)
197 debug("Bundle " + bundle
.getSymbolicName() + " already installed from " + url
);
198 } else if (url
.contains("/" + SYMBOLIC_NAME_EQUINOX
+ "/")
199 || url
.contains("/" + SYMBOLIC_NAME_OSGI_BOOT
+ "/")) {
200 if (OsgiBootUtils
.debug
)
204 Bundle bundle
= bundleContext
.installBundle(url
);
205 if (url
.startsWith("http"))
207 .info("Installed " + bundle
.getSymbolicName() + "-" + bundle
.getVersion() + " from " + url
);
208 else if (OsgiBootUtils
.debug
)
210 "Installed " + bundle
.getSymbolicName() + "-" + bundle
.getVersion() + " from " + url
);
211 assert bundle
.getSymbolicName() != null;
212 // uninstall previous versions
213 bundles
: for (Bundle b
: bundleContext
.getBundles()) {
214 if (b
.getSymbolicName() == null)
216 if (bundle
.getSymbolicName().equals(b
.getSymbolicName())) {
217 Version bundleV
= bundle
.getVersion();
218 Version bV
= b
.getVersion();
221 if (bundleV
.getMajor() == bV
.getMajor() && bundleV
.getMinor() == bV
.getMinor()) {
222 if (bundleV
.getMicro() > bV
.getMicro()) {
223 // uninstall older bundles
225 OsgiBootUtils
.debug("Uninstalled " + b
);
226 } else if (bundleV
.getMicro() < bV
.getMicro()) {
227 // uninstall just installed bundle if newer
229 OsgiBootUtils
.debug("Uninstalled " + bundle
);
232 // uninstall any other with same major/minor
233 if (!bundleV
.getQualifier().equals(bV
.getQualifier())) {
235 OsgiBootUtils
.debug("Uninstalled " + b
);
242 } catch (BundleException e
) {
243 final String ALREADY_INSTALLED
= "is already installed";
244 String message
= e
.getMessage();
245 if ((message
.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT
+ "\"")
246 || message
.contains("Bundle \"" + SYMBOLIC_NAME_EQUINOX
+ "\""))
247 && message
.contains(ALREADY_INSTALLED
)) {
248 // silent, in order to avoid warnings: we know that both
249 // have already been installed...
251 if (message
.contains(ALREADY_INSTALLED
)) {
252 if (OsgiBootUtils
.isDebug())
253 OsgiBootUtils
.warn("Duplicate install from " + url
+ ": " + message
);
255 OsgiBootUtils
.warn("Could not install bundle from " + url
+ ": " + message
);
257 if (OsgiBootUtils
.debug
&& !message
.contains(ALREADY_INSTALLED
))
265 public void startBundles() {
266 startBundles(System
.getProperties());
269 public void startBundles(Properties properties
) {
270 FrameworkStartLevel frameworkStartLevel
= bundleContext
.getBundle(0).adapt(FrameworkStartLevel
.class);
272 // default and active start levels from System properties
273 Integer defaultStartLevel
= new Integer(
274 Integer
.parseInt(getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL
, "4")));
275 Integer activeStartLevel
= new Integer(getProperty(PROP_OSGI_STARTLEVEL
, "6"));
277 SortedMap
<Integer
, List
<String
>> startLevels
= new TreeMap
<Integer
, List
<String
>>();
278 computeStartLevels(startLevels
, properties
, defaultStartLevel
);
279 // inverts the map for the time being, TODO optimise
280 Map
<String
, Integer
> bundleStartLevels
= new HashMap
<>();
281 for (Integer level
: startLevels
.keySet()) {
282 for (String bsn
: startLevels
.get(level
))
283 bundleStartLevels
.put(bsn
, level
);
285 for (Bundle bundle
: bundleContext
.getBundles()) {
286 String bsn
= bundle
.getSymbolicName();
287 if (bundleStartLevels
.containsKey(bsn
)) {
288 BundleStartLevel bundleStartLevel
= bundle
.adapt(BundleStartLevel
.class);
289 Integer level
= bundleStartLevels
.get(bsn
);
290 if (bundleStartLevel
.getStartLevel() != level
|| !bundleStartLevel
.isPersistentlyStarted()) {
291 bundleStartLevel
.setStartLevel(level
);
294 } catch (BundleException e
) {
295 OsgiBootUtils
.error("Cannot mark " + bsn
+ " as started", e
);
298 OsgiBootUtils
.debug(bsn
+ " starts at level " + level
);
302 frameworkStartLevel
.setStartLevel(activeStartLevel
, (FrameworkEvent event
) -> {
304 OsgiBootUtils
.debug("Framework event: " + event
);
305 int initialStartLevel
= frameworkStartLevel
.getInitialBundleStartLevel();
306 int startLevel
= frameworkStartLevel
.getStartLevel();
307 OsgiBootUtils
.debug("Framework start level: " + startLevel
+ " (initial: " + initialStartLevel
+ ")");
311 private static void computeStartLevels(SortedMap
<Integer
, List
<String
>> startLevels
, Properties properties
,
312 Integer defaultStartLevel
) {
314 // default (and previously, only behaviour)
315 appendToStartLevels(startLevels
, defaultStartLevel
, properties
.getProperty(PROP_ARGEO_OSGI_START
, ""));
317 // list argeo.osgi.start.* system properties
318 Iterator
<Object
> keys
= properties
.keySet().iterator();
319 final String prefix
= PROP_ARGEO_OSGI_START
+ ".";
320 while (keys
.hasNext()) {
321 String key
= keys
.next().toString();
322 if (key
.startsWith(prefix
)) {
324 String suffix
= key
.substring(prefix
.length());
325 String
[] tokens
= suffix
.split("\\.");
326 if (tokens
.length
> 0 && !tokens
[0].trim().equals(""))
328 // first token is start level
329 startLevel
= new Integer(tokens
[0]);
330 } catch (NumberFormatException e
) {
331 startLevel
= defaultStartLevel
;
334 startLevel
= defaultStartLevel
;
336 // append bundle names
337 String bundleNames
= properties
.getProperty(key
);
338 appendToStartLevels(startLevels
, startLevel
, bundleNames
);
343 /** Append a comma-separated list of bundles to the start levels. */
344 private static void appendToStartLevels(SortedMap
<Integer
, List
<String
>> startLevels
, Integer startLevel
,
346 if (str
== null || str
.trim().equals(""))
349 if (!startLevels
.containsKey(startLevel
))
350 startLevels
.put(startLevel
, new ArrayList
<String
>());
351 String
[] bundleNames
= str
.split(",");
352 for (int i
= 0; i
< bundleNames
.length
; i
++) {
353 if (bundleNames
[i
] != null && !bundleNames
[i
].trim().equals(""))
354 (startLevels
.get(startLevel
)).add(bundleNames
[i
]);
359 * Start the provided list of bundles
361 * @return whether all bundles are now in active state
365 public boolean startBundles(List
<String
> bundlesToStart
) {
366 if (bundlesToStart
.size() == 0)
369 // used to monitor ACTIVE states
370 List
<Bundle
> startedBundles
= new ArrayList
<Bundle
>();
371 // used to log the bundles not found
372 List
<String
> notFoundBundles
= new ArrayList
<String
>(bundlesToStart
);
374 Bundle
[] bundles
= bundleContext
.getBundles();
375 long startBegin
= System
.currentTimeMillis();
376 for (int i
= 0; i
< bundles
.length
; i
++) {
377 Bundle bundle
= bundles
[i
];
378 String symbolicName
= bundle
.getSymbolicName();
379 if (bundlesToStart
.contains(symbolicName
))
383 if (OsgiBootUtils
.debug
)
384 debug("Bundle " + symbolicName
+ " started");
385 } catch (Exception e
) {
386 OsgiBootUtils
.warn("Start of bundle " + symbolicName
+ " failed because of " + e
387 + ", maybe bundle is not yet resolved," + " waiting and trying again.");
388 waitForBundleResolvedOrActive(startBegin
, bundle
);
390 startedBundles
.add(bundle
);
392 notFoundBundles
.remove(symbolicName
);
393 } catch (Exception e
) {
394 OsgiBootUtils
.warn("Bundle " + symbolicName
+ " cannot be started: " + e
.getMessage());
395 if (OsgiBootUtils
.debug
)
397 // was found even if start failed
398 notFoundBundles
.remove(symbolicName
);
402 for (int i
= 0; i
< notFoundBundles
.size(); i
++)
403 OsgiBootUtils
.warn("Bundle '" + notFoundBundles
.get(i
) + "' not started because it was not found.");
405 // monitors that all bundles are started
406 long beginMonitor
= System
.currentTimeMillis();
407 boolean allStarted
= !(startedBundles
.size() > 0);
408 List
<String
> notStarted
= new ArrayList
<String
>();
409 while (!allStarted
&& (System
.currentTimeMillis() - beginMonitor
) < defaultTimeout
) {
410 notStarted
= new ArrayList
<String
>();
412 for (int i
= 0; i
< startedBundles
.size(); i
++) {
413 Bundle bundle
= (Bundle
) startedBundles
.get(i
);
414 // TODO check behaviour of lazs bundles
415 if (bundle
.getState() != Bundle
.ACTIVE
) {
417 notStarted
.add(bundle
.getSymbolicName());
422 } catch (InterruptedException e
) {
426 long duration
= System
.currentTimeMillis() - beginMonitor
;
429 for (int i
= 0; i
< notStarted
.size(); i
++)
430 OsgiBootUtils
.warn("Bundle '" + notStarted
.get(i
) + "' not ACTIVE after " + (duration
/ 1000) + "s");
435 /** Waits for a bundle to become active or resolved */
437 private void waitForBundleResolvedOrActive(long startBegin
, Bundle bundle
) throws Exception
{
438 int originalState
= bundle
.getState();
439 if ((originalState
== Bundle
.RESOLVED
) || (originalState
== Bundle
.ACTIVE
))
442 String originalStateStr
= OsgiBootUtils
.stateAsString(originalState
);
444 int currentState
= bundle
.getState();
445 while (!(currentState
== Bundle
.RESOLVED
|| currentState
== Bundle
.ACTIVE
)) {
446 long now
= System
.currentTimeMillis();
447 if ((now
- startBegin
) > defaultTimeout
* 10)
448 throw new Exception("Bundle " + bundle
.getSymbolicName() + " was not RESOLVED or ACTIVE after "
449 + (now
- startBegin
) + "ms (originalState=" + originalStateStr
+ ", currentState="
450 + OsgiBootUtils
.stateAsString(currentState
) + ")");
454 } catch (InterruptedException e
) {
457 currentState
= bundle
.getState();
462 * BUNDLE PATTERNS INSTALLATION
465 * Computes a list of URLs based on Ant-like include/exclude patterns defined by
466 * ${argeo.osgi.bundles} with the following format:<br>
467 * <code>/base/directory;in=*.jar;in=**;ex=org.eclipse.osgi_*;jar</code><br>
468 * WARNING: <code>/base/directory;in=*.jar,\</code> at the end of a file,
469 * without a new line causes a '.' to be appended with unexpected side effects.
471 public List
<String
> getBundlesUrls() {
472 String bundlePatterns
= getProperty(PROP_ARGEO_OSGI_BUNDLES
);
473 return getBundlesUrls(bundlePatterns
);
477 * Compute a list of URLs to install based on the provided patterns, with
480 public List
<String
> getBundlesUrls(String bundlePatterns
) {
481 String baseUrl
= getProperty(PROP_ARGEO_OSGI_BASE_URL
, DEFAULT_BASE_URL
);
482 return getBundlesUrls(baseUrl
, bundlePatterns
);
485 /** Implements the path matching logic */
486 public List
<String
> getBundlesUrls(String baseUrl
, String bundlePatterns
) {
487 List
<String
> urls
= new ArrayList
<String
>();
488 if (bundlePatterns
== null)
491 bundlePatterns
= SystemPropertyUtils
.resolvePlaceholders(bundlePatterns
);
492 if (OsgiBootUtils
.debug
)
493 debug(PROP_ARGEO_OSGI_BUNDLES
+ "=" + bundlePatterns
);
495 StringTokenizer st
= new StringTokenizer(bundlePatterns
, ",");
496 List
<BundlesSet
> bundlesSets
= new ArrayList
<BundlesSet
>();
497 while (st
.hasMoreTokens()) {
498 String token
= st
.nextToken();
499 if (new File(token
).exists()) {
500 String url
= locationToUrl(baseUrl
, token
);
503 bundlesSets
.add(new BundlesSet(token
));
507 List
<String
> included
= new ArrayList
<String
>();
508 PathMatcher matcher
= new AntPathMatcher();
509 for (int i
= 0; i
< bundlesSets
.size(); i
++) {
510 BundlesSet bundlesSet
= (BundlesSet
) bundlesSets
.get(i
);
511 for (int j
= 0; j
< bundlesSet
.getIncludes().size(); j
++) {
512 String pattern
= (String
) bundlesSet
.getIncludes().get(j
);
513 match(matcher
, included
, bundlesSet
.getDir(), null, pattern
);
518 List
<String
> excluded
= new ArrayList
<String
>();
519 for (int i
= 0; i
< bundlesSets
.size(); i
++) {
520 BundlesSet bundlesSet
= (BundlesSet
) bundlesSets
.get(i
);
521 for (int j
= 0; j
< bundlesSet
.getExcludes().size(); j
++) {
522 String pattern
= (String
) bundlesSet
.getExcludes().get(j
);
523 match(matcher
, excluded
, bundlesSet
.getDir(), null, pattern
);
528 for (int i
= 0; i
< included
.size(); i
++) {
529 String fullPath
= (String
) included
.get(i
);
530 if (!excluded
.contains(fullPath
))
531 urls
.add(locationToUrl(baseUrl
, fullPath
));
538 * DISTRIBUTION JAR INSTALLATION
540 public List
<String
> getDistributionUrls() {
541 String distributionUrl
= getProperty(PROP_ARGEO_OSGI_DISTRIBUTION_URL
);
542 String baseUrl
= getProperty(PROP_ARGEO_OSGI_BASE_URL
);
543 return getDistributionUrls(distributionUrl
, baseUrl
);
546 public List
<String
> getDistributionUrls(String distributionUrl
, String baseUrl
) {
547 List
<String
> urls
= new ArrayList
<String
>();
548 if (distributionUrl
== null)
551 DistributionBundle distributionBundle
;
552 if (distributionUrl
.startsWith("http") || distributionUrl
.startsWith("file")) {
553 distributionBundle
= new DistributionBundle(distributionUrl
);
555 distributionBundle
.setBaseUrl(baseUrl
);
558 if (baseUrl
== null) {
559 baseUrl
= localCache
;
562 if (distributionUrl
.contains(":")) {
563 // TODO make it safer
564 String
[] parts
= distributionUrl
.trim().split(":");
565 String
[] categoryParts
= parts
[0].split("\\.");
566 String artifactId
= parts
[1];
567 String version
= parts
[2];
568 StringBuilder sb
= new StringBuilder();
569 for (String categoryPart
: categoryParts
) {
570 sb
.append(categoryPart
).append('/');
572 sb
.append(artifactId
).append('/');
573 sb
.append(version
).append('/');
574 sb
.append(artifactId
).append('-').append(version
).append(".jar");
575 distributionUrl
= sb
.toString();
578 distributionBundle
= new DistributionBundle(baseUrl
, distributionUrl
, localCache
);
580 // if (baseUrl != null && !(distributionUrl.startsWith("http") ||
581 // distributionUrl.startsWith("file"))) {
583 // distributionBundle = new DistributionBundle(baseUrl, distributionUrl,
586 // distributionBundle = new DistributionBundle(distributionUrl);
587 // if (baseUrl != null)
588 // distributionBundle.setBaseUrl(baseUrl);
590 distributionBundle
.processUrl();
591 return distributionBundle
.listUrls();
595 * HIGH LEVEL UTILITIES
597 /** Actually performs the matching logic. */
598 protected void match(PathMatcher matcher
, List
<String
> matched
, String base
, String currentPath
, String pattern
) {
599 if (currentPath
== null) {
601 File baseDir
= new File(base
.replace('/', File
.separatorChar
));
602 File
[] files
= baseDir
.listFiles();
605 if (OsgiBootUtils
.debug
)
606 OsgiBootUtils
.warn("Base dir " + baseDir
+ " has no children, exists=" + baseDir
.exists()
607 + ", isDirectory=" + baseDir
.isDirectory());
611 for (int i
= 0; i
< files
.length
; i
++)
612 match(matcher
, matched
, base
, files
[i
].getName(), pattern
);
614 String fullPath
= base
+ '/' + currentPath
;
615 if (matched
.contains(fullPath
))
616 return;// don't try deeper if already matched
618 boolean ok
= matcher
.match(pattern
, currentPath
);
620 // debug(currentPath + " " + (ok ? "" : " not ")
621 // + " matched with " + pattern);
623 matched
.add(fullPath
);
626 String newFullPath
= relativeToFullPath(base
, currentPath
);
627 File newFile
= new File(newFullPath
);
628 File
[] files
= newFile
.listFiles();
630 for (int i
= 0; i
< files
.length
; i
++) {
631 String newCurrentPath
= currentPath
+ '/' + files
[i
].getName();
632 if (files
[i
].isDirectory()) {
633 if (matcher
.matchStart(pattern
, newCurrentPath
)) {
634 // recurse only if start matches
635 match(matcher
, matched
, base
, newCurrentPath
, pattern
);
637 if (OsgiBootUtils
.debug
)
638 debug(newCurrentPath
+ " does not start match with " + pattern
);
642 boolean nonDirectoryOk
= matcher
.match(pattern
, newCurrentPath
);
643 if (OsgiBootUtils
.debug
)
644 debug(currentPath
+ " " + (ok ?
"" : " not ") + " matched with " + pattern
);
646 matched
.add(relativeToFullPath(base
, newCurrentPath
));
654 protected void matchFile() {
659 * LOW LEVEL UTILITIES
662 * The bundles already installed. Key is location (String) , value is a
665 public Map
<String
, Bundle
> getBundlesByLocation() {
666 Map
<String
, Bundle
> installedBundles
= new HashMap
<String
, Bundle
>();
667 Bundle
[] bundles
= bundleContext
.getBundles();
668 for (int i
= 0; i
< bundles
.length
; i
++) {
669 installedBundles
.put(bundles
[i
].getLocation(), bundles
[i
]);
671 return installedBundles
;
675 * The bundles already installed. Key is symbolic name (String) , value is a
678 public Map
<String
, Bundle
> getBundlesBySymbolicName() {
679 Map
<String
, Bundle
> namedBundles
= new HashMap
<String
, Bundle
>();
680 Bundle
[] bundles
= bundleContext
.getBundles();
681 for (int i
= 0; i
< bundles
.length
; i
++) {
682 namedBundles
.put(bundles
[i
].getSymbolicName(), bundles
[i
]);
687 /** Creates an URL from a location */
688 protected String
locationToUrl(String baseUrl
, String location
) {
689 return baseUrl
+ location
;
692 /** Transforms a relative path in a full system path. */
693 protected String
relativeToFullPath(String basePath
, String relativePath
) {
694 return (basePath
+ '/' + relativePath
).replace('/', File
.separatorChar
);
697 private void refreshFramework() {
698 Bundle systemBundle
= bundleContext
.getBundle(0);
699 FrameworkWiring frameworkWiring
= systemBundle
.adapt(FrameworkWiring
.class);
700 frameworkWiring
.refreshBundles(null);
704 * Gets a property value
706 * @return null when defaultValue is ""
708 public String
getProperty(String name
, String defaultValue
) {
709 String value
= bundleContext
.getProperty(name
);
711 return defaultValue
; // may be null
716 public String
getProperty(String name
) {
717 return getProperty(name
, null);
724 public boolean getDebug() {
725 return OsgiBootUtils
.debug
;
728 // public void setDebug(boolean debug) {
729 // this.debug = debug;
732 public BundleContext
getBundleContext() {
733 return bundleContext
;
736 public String
getLocalCache() {
740 // public void setDefaultTimeout(long defaultTimeout) {
741 // this.defaultTimeout = defaultTimeout;
744 // public boolean isExcludeSvn() {
745 // return excludeSvn;
748 // public void setExcludeSvn(boolean excludeSvn) {
749 // this.excludeSvn = excludeSvn;