1 package org
.argeo
.slc
.osgiboot
;
3 import java
.io
.BufferedReader
;
5 import java
.io
.FileReader
;
6 import java
.io
.IOException
;
7 import java
.util
.ArrayList
;
8 import java
.util
.HashMap
;
11 import java
.util
.StringTokenizer
;
13 import org
.argeo
.slc
.osgiboot
.internal
.springutil
.AntPathMatcher
;
14 import org
.argeo
.slc
.osgiboot
.internal
.springutil
.PathMatcher
;
15 import org
.argeo
.slc
.osgiboot
.internal
.springutil
.SystemPropertyUtils
;
16 import org
.osgi
.framework
.Bundle
;
17 import org
.osgi
.framework
.BundleActivator
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.BundleException
;
21 public class Activator
implements BundleActivator
{
22 public final static String PROP_SLC_OSGI_START
= "slc.osgi.start";
23 public final static String PROP_SLC_OSGI_BUNDLES
= "slc.osgi.bundles";
24 public final static String PROP_SLC_OSGI_DEV_BASES
= "slc.osgi.devBases";
25 public final static String PROP_SLC_OSGI_DEV_PATTERNS
= "slc.osgi.devPatterns";
26 public final static String PROP_SLC_OSGI_LOCATIONS
= "slc.osgi.locations";
27 public final static String PROP_SLC_MAVEN_DEPENDENCY_FILE
= "slc.maven.dependencyFile";
29 private static Boolean debug
= false;
31 public void start(BundleContext bundleContext
) throws Exception
{
33 info("SLC OSGi bootstrap starting...");
34 installUrls(bundleContext
, getDevLocationsUrls());
36 installUrls(bundleContext
, getLocationsUrls());
37 installUrls(bundleContext
, getBundlesUrls());
39 List
<String
> urls
= getMavenUrls();
40 installUrls(bundleContext
, urls
);
42 startBundles(bundleContext
);
44 info("SLC OSGi bootstrap completed");
45 } catch (Exception e
) {
51 public void stop(BundleContext context
) throws Exception
{
54 protected static void installUrls(BundleContext bundleContext
,
56 Map
<String
, Bundle
> installedBundles
= getInstalledBundles(bundleContext
);
57 for (String url
: urls
) {
60 if (installedBundles
.containsKey(url
)) {
61 Bundle bundle
= installedBundles
.get(url
);
64 debug("Bundle " + bundle
.getSymbolicName()
65 + " already installed from " + url
);
67 Bundle bundle
= bundleContext
.installBundle(url
);
69 debug("Installed bundle " + bundle
.getSymbolicName()
72 } catch (BundleException e
) {
73 warn("Could not install bundle from " + url
+ ": "
80 protected List
<String
> getLocationsUrls() {
81 List
<String
> urlsProvided
= new ArrayList
<String
>();
83 String bundlesList
= getProperty(PROP_SLC_OSGI_LOCATIONS
);
84 if (bundlesList
== null)
86 bundlesList
= SystemPropertyUtils
.resolvePlaceholders(bundlesList
);
88 StringTokenizer st
= new StringTokenizer(bundlesList
,
90 while (st
.hasMoreTokens()) {
91 urlsProvided
.add("reference:file:" + st
.nextToken().trim());
96 protected List
<String
> getDevLocationsUrls() {
97 List
<String
> urls
= new ArrayList
<String
>();
99 String devBase
= getProperty(PROP_SLC_OSGI_DEV_BASES
);
100 String devPatterns
= getProperty(PROP_SLC_OSGI_DEV_PATTERNS
);
103 devBase
= SystemPropertyUtils
.resolvePlaceholders(devBase
);
104 devBase
= devBase
.replace(File
.separatorChar
, '/');
105 devPatterns
= SystemPropertyUtils
.resolvePlaceholders(devPatterns
);
107 List
<String
> bases
= new ArrayList
<String
>();
108 StringTokenizer st
= new StringTokenizer(devBase
, ",");
109 while (st
.hasMoreTokens()) {
110 String token
= st
.nextToken().trim();
111 char lastChar
= token
.charAt(token
.length() - 1);
117 List
<String
> patterns
= new ArrayList
<String
>();
118 st
= new StringTokenizer(devPatterns
, ";");
119 while (st
.hasMoreTokens()) {
120 patterns
.add(st
.nextToken().trim());
123 List
<String
> matched
= new ArrayList
<String
>();
124 PathMatcher matcher
= new AntPathMatcher();
125 for (String base
: bases
)
126 for (String pattern
: patterns
)
127 match(matcher
, matched
, base
, null, pattern
);
129 for (String fullPath
: matched
)
130 urls
.add("reference:file:" + fullPath
);
135 protected List
<String
> getBundlesUrls() {
136 List
<String
> urls
= new ArrayList
<String
>();
138 List
<BundlesSet
> bundlesSets
= new ArrayList
<BundlesSet
>();
139 String bundles
= getProperty(PROP_SLC_OSGI_BUNDLES
);
142 info(PROP_SLC_OSGI_BUNDLES
+ "=" + bundles
);
144 StringTokenizer st
= new StringTokenizer(bundles
, ",");
145 while (st
.hasMoreTokens()) {
146 bundlesSets
.add(new BundlesSet(st
.nextToken()));
149 List
<String
> included
= new ArrayList
<String
>();
150 PathMatcher matcher
= new AntPathMatcher();
151 for (BundlesSet bundlesSet
: bundlesSets
)
152 for (String pattern
: bundlesSet
.getIncludes())
153 match(matcher
, included
, bundlesSet
.getDir(), null, pattern
);
155 List
<String
> excluded
= new ArrayList
<String
>();
156 for (BundlesSet bundlesSet
: bundlesSets
)
157 for (String pattern
: bundlesSet
.getExcludes())
158 match(matcher
, excluded
, bundlesSet
.getDir(), null, pattern
);
160 for (String fullPath
: included
) {
161 if (!excluded
.contains(fullPath
))
162 urls
.add("reference:file:" + fullPath
);
168 private class BundlesSet
{
169 private String baseUrl
= "reference:file";
170 private final String dir
;
171 private List
<String
> includes
= new ArrayList
<String
>();
172 private List
<String
> excludes
= new ArrayList
<String
>();
174 public BundlesSet(String def
) {
175 StringTokenizer st
= new StringTokenizer(def
, ";");
177 if (!st
.hasMoreTokens())
178 throw new RuntimeException("Base dir not defined.");
180 String dirPath
= st
.nextToken();
181 dir
= new File(dirPath
.replace('/', File
.separatorChar
))
184 debug("Base dir: " + dir
);
185 } catch (IOException e
) {
186 throw new RuntimeException("Cannot convert to absolute path", e
);
189 while (st
.hasMoreTokens()) {
190 String tk
= st
.nextToken();
191 StringTokenizer stEq
= new StringTokenizer(tk
, "=");
192 String type
= stEq
.nextToken();
193 String pattern
= stEq
.nextToken();
194 if ("in".equals(type
) || "include".equals(type
)) {
195 includes
.add(pattern
);
196 } else if ("ex".equals(type
) || "exclude".equals(type
)) {
197 excludes
.add(pattern
);
198 } else if ("baseUrl".equals(type
)) {
201 System
.err
.println("Unkown bundles pattern type " + type
);
206 public String
getDir() {
210 public List
<String
> getIncludes() {
214 public List
<String
> getExcludes() {
218 public String
getBaseUrl() {
224 protected void match(PathMatcher matcher
, List
<String
> matched
,
225 String base
, String currentPath
, String pattern
) {
226 if (currentPath
== null) {
228 File baseDir
= new File(base
.replace('/', File
.separatorChar
));
229 File
[] files
= baseDir
.listFiles();
232 warn("Base dir " + baseDir
+ " has no children, exists="
233 + baseDir
.exists() + ", isDirectory="
234 + baseDir
.isDirectory());
238 for (File file
: files
)
239 match(matcher
, matched
, base
, file
.getName(), pattern
);
241 String fullPath
= base
+ '/' + currentPath
;
242 if (matched
.contains(fullPath
))
243 return;// don't try deeper if already matched
245 boolean ok
= matcher
.match(pattern
, currentPath
);
247 debug(currentPath
+ " " + (ok ?
"" : " not ")
248 + " matched with " + pattern
);
250 matched
.add(fullPath
);
253 String newFullPath
= (base
+ '/' + currentPath
).replace('/',
255 File
[] files
= new File(newFullPath
).listFiles();
257 for (File file
: files
)
258 if (file
.isDirectory()) {
259 String newCurrentPath
= currentPath
+ '/'
261 if (matcher
.matchStart(pattern
, newCurrentPath
)) {
262 // recurse only if start matches
263 match(matcher
, matched
, base
, newCurrentPath
,
268 + " does not start match with "
274 warn("Not a directory: " + newFullPath
);
280 protected List
<String
> getMavenUrls() throws Exception
{
281 String baseUrl
= "reference:file:" + System
.getProperty("user.home")
282 + "/.m2/repository/";
283 String config
= getProperty(PROP_SLC_MAVEN_DEPENDENCY_FILE
);
285 return new ArrayList
<String
>();
287 List
<MavenFile
> mavenFiles
= new ArrayList
<MavenFile
>();
288 BufferedReader in
= new BufferedReader(new FileReader(config
));
290 while ((line
= in
.readLine()) != null) {
295 .startsWith("The following files have been resolved:"))
298 mavenFiles
.add(convert(line
));
299 } catch (Exception e
) {
300 warn("Could not load line " + line
);
304 return asUrls(baseUrl
, mavenFiles
);
307 protected void startBundles(BundleContext bundleContext
) throws Exception
{
308 String bundlesToStart
= getProperty(PROP_SLC_OSGI_START
);
309 if (bundlesToStart
== null)
312 StringTokenizer st
= new StringTokenizer(bundlesToStart
, ",");
313 Map
<String
, Bundle
> bundles
= getBundles(bundleContext
);
314 while (st
.hasMoreTokens()) {
315 String name
= st
.nextToken().trim();
316 Bundle bundle
= bundles
.get(name
);
320 } catch (Exception e
) {
321 warn("Bundle " + name
+ " cannot be started: "
325 warn("Bundle " + name
+ " not installed.");
330 protected static Map
<String
, Bundle
> getInstalledBundles(
331 BundleContext bundleContext
) {
332 Map
<String
, Bundle
> installedBundles
= new HashMap
<String
, Bundle
>();
333 for (Bundle bundle
: bundleContext
.getBundles())
334 installedBundles
.put(bundle
.getLocation(), bundle
);
335 return installedBundles
;
338 protected static Map
<String
, Bundle
> getBundles(BundleContext bundleContext
) {
339 Map
<String
, Bundle
> installedBundles
= new HashMap
<String
, Bundle
>();
340 for (Bundle bundle
: bundleContext
.getBundles())
341 installedBundles
.put(bundle
.getSymbolicName(), bundle
);
342 return installedBundles
;
345 protected static List
<String
> asUrls(String baseUrl
,
346 List
<MavenFile
> mavenFiles
) {
347 List
<String
> urls
= new ArrayList
<String
>();
348 for (MavenFile mf
: mavenFiles
)
349 urls
.add(convertToUrl(baseUrl
, mf
));
353 protected static String
convertToUrl(String baseUrl
, MavenFile mf
) {
354 return baseUrl
+ mf
.getGroupId().replace('.', '/') + '/'
355 + mf
.getArtifactId() + '/' + mf
.getVersion() + '/'
356 + mf
.getArtifactId() + '-' + mf
.getVersion() + '.'
360 protected static MavenFile
convert(String str
) {
361 StringTokenizer st
= new StringTokenizer(str
, ":");
362 MavenFile component
= new MavenFile();
363 component
.setGroupId(st
.nextToken());
364 component
.setArtifactId(st
.nextToken());
365 component
.setType(st
.nextToken());
366 component
.setVersion(st
.nextToken());
367 component
.setScope(st
.nextToken());
371 protected String
getProperty(String name
) {
372 String value
= System
.getProperty(name
);
373 if (value
== null || value
.equals(""))
379 private static void info(Object obj
) {
380 System
.out
.println("# INFO " + obj
);
383 private static void debug(Object obj
) {
385 System
.out
.println("# DBUG " + obj
);
388 private static void warn(Object obj
) {
389 if (System
.getProperty("os.name").contains("Windows"))
390 System
.out
.println("# WARN " + obj
);
392 System
.err
.println("# WARN " + obj
);
395 static class MavenFile
{
396 private String groupId
;
397 private String artifactId
;
398 private String version
;
400 private String classifier
;
401 private String scope
;
403 public String
getScope() {
407 public void setScope(String scope
) {
411 private String distributionId
;
413 public String
getDistributionId() {
414 return distributionId
;
417 public void setDistributionId(String distributionId
) {
418 this.distributionId
= distributionId
;
421 public String
getGroupId() {
425 public void setGroupId(String groupId
) {
426 this.groupId
= groupId
;
429 public String
getArtifactId() {
433 public void setArtifactId(String artifactId
) {
434 this.artifactId
= artifactId
;
437 public String
getVersion() {
441 public void setVersion(String version
) {
442 this.version
= version
;
445 public String
getType() {
449 public void setType(String type
) {
453 public String
getClassifier() {
457 public void setClassifier(String classifier
) {
458 this.classifier
= classifier
;