1 package org
.argeo
.slc
.osgiboot
;
3 import java
.io
.BufferedReader
;
5 import java
.io
.FileReader
;
6 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
10 import java
.util
.StringTokenizer
;
12 import org
.argeo
.slc
.osgiboot
.internal
.springutil
.AntPathMatcher
;
13 import org
.argeo
.slc
.osgiboot
.internal
.springutil
.PathMatcher
;
14 import org
.argeo
.slc
.osgiboot
.internal
.springutil
.SystemPropertyUtils
;
15 import org
.osgi
.framework
.Bundle
;
16 import org
.osgi
.framework
.BundleActivator
;
17 import org
.osgi
.framework
.BundleContext
;
18 import org
.osgi
.framework
.BundleException
;
20 public class Activator
implements BundleActivator
{
21 public final static String PROP_SLC_OSGI_START
= "slc.osgi.start";
22 public final static String PROP_SLC_OSGI_DEV_BASES
= "slc.osgi.devBases";
23 public final static String PROP_SLC_OSGI_DEV_PATTERNS
= "slc.osgi.devPatterns";
24 public final static String PROP_SLC_OSGI_LOCATIONS
= "slc.osgi.locations";
25 public final static String PROP_SLC_MAVEN_DEPENDENCY_FILE
= "slc.maven.dependencyFile";
27 private static Boolean debug
= false;
29 public void start(BundleContext bundleContext
) throws Exception
{
31 info("SLC OSGi bootstrap starting...");
32 installUrls(bundleContext
, getDevLocationsUrls());
34 installUrls(bundleContext
, getLocationsUrls());
36 List
<String
> urls
= getMavenUrls();
37 installUrls(bundleContext
, urls
);
39 startBundles(bundleContext
);
41 info("SLC OSGi bootstrap completed");
42 } catch (Exception e
) {
48 public void stop(BundleContext context
) throws Exception
{
51 protected static void installUrls(BundleContext bundleContext
,
53 Map
<String
, Bundle
> installedBundles
= getInstalledBundles(bundleContext
);
54 for (String url
: urls
) {
57 if (installedBundles
.containsKey(url
)) {
58 Bundle bundle
= installedBundles
.get(url
);
61 debug("Bundle " + bundle
.getSymbolicName()
62 + " already installed from " + url
);
64 Bundle bundle
= bundleContext
.installBundle(url
);
66 debug("Installed bundle " + bundle
.getSymbolicName()
69 } catch (BundleException e
) {
70 warn("Could not install bundle from " + url
+ ": "
77 protected List
<String
> getLocationsUrls() {
78 List
<String
> urlsProvided
= new ArrayList
<String
>();
80 String bundlesList
= getProperty(PROP_SLC_OSGI_LOCATIONS
);
81 if (bundlesList
== null)
83 bundlesList
= SystemPropertyUtils
.resolvePlaceholders(bundlesList
);
85 StringTokenizer st
= new StringTokenizer(bundlesList
,
87 while (st
.hasMoreTokens()) {
88 urlsProvided
.add("reference:file:" + st
.nextToken().trim());
93 protected List
<String
> getDevLocationsUrls() {
94 List
<String
> urls
= new ArrayList
<String
>();
96 String devBase
= getProperty(PROP_SLC_OSGI_DEV_BASES
);
97 String devPatterns
= getProperty(PROP_SLC_OSGI_DEV_PATTERNS
);
100 devBase
= SystemPropertyUtils
.resolvePlaceholders(devBase
);
101 devBase
= devBase
.replace(File
.separatorChar
, '/');
102 devPatterns
= SystemPropertyUtils
.resolvePlaceholders(devPatterns
);
104 List
<String
> bases
= new ArrayList
<String
>();
105 StringTokenizer st
= new StringTokenizer(devBase
, ",");
106 while (st
.hasMoreTokens()) {
107 String token
= st
.nextToken().trim();
108 char lastChar
= token
.charAt(token
.length() - 1);
114 List
<String
> patterns
= new ArrayList
<String
>();
115 st
= new StringTokenizer(devPatterns
, ";");
116 while (st
.hasMoreTokens()) {
117 patterns
.add(st
.nextToken().trim());
120 List
<String
> matched
= new ArrayList
<String
>();
121 PathMatcher matcher
= new AntPathMatcher();
122 for (String base
: bases
)
123 for (String pattern
: patterns
)
124 match(matcher
, matched
, base
, null, pattern
);
126 for (String fullPath
: matched
)
127 urls
.add("reference:file:" + fullPath
);
132 protected void match(PathMatcher matcher
, List
<String
> matched
,
133 String base
, String currentPath
, String pattern
) {
134 if (currentPath
== null) {
136 File baseDir
= new File(base
.replace('/', File
.separatorChar
));
137 File
[] files
= baseDir
.listFiles();
140 warn("Base dir " + baseDir
+ " has no children, exists="
141 + baseDir
.exists() + ", isDirectory="
142 + baseDir
.isDirectory());
146 for (File file
: files
)
147 if (file
.isDirectory())
148 match(matcher
, matched
, base
, file
.getName(), pattern
);
150 String fullPath
= base
+ currentPath
;
151 if (matched
.contains(fullPath
))
152 return;// don't try deeper if already matched
154 boolean ok
= matcher
.match(pattern
, currentPath
);
156 debug(currentPath
+ " " + (ok ?
"" : " not ")
157 + " matched with " + pattern
);
159 matched
.add(fullPath
);
162 File
[] files
= new File((base
+ currentPath
).replace('/',
163 File
.separatorChar
)).listFiles();
164 for (File file
: files
)
165 if (file
.isDirectory()) {
166 String newCurrentPath
= currentPath
+ '/'
168 if (matcher
.matchStart(pattern
, newCurrentPath
)) {
169 // recurse only if start matches
170 match(matcher
, matched
, base
, newCurrentPath
,
175 + " does not start match with "
184 protected List
<String
> getMavenUrls() throws Exception
{
185 String baseUrl
= "reference:file:" + System
.getProperty("user.home")
186 + "/.m2/repository/";
187 String config
= getProperty(PROP_SLC_MAVEN_DEPENDENCY_FILE
);
189 return new ArrayList
<String
>();
191 List
<MavenFile
> mavenFiles
= new ArrayList
<MavenFile
>();
192 BufferedReader in
= new BufferedReader(new FileReader(config
));
194 while ((line
= in
.readLine()) != null) {
199 .startsWith("The following files have been resolved:"))
202 mavenFiles
.add(convert(line
));
203 } catch (Exception e
) {
204 warn("Could not load line " + line
);
208 return asUrls(baseUrl
, mavenFiles
);
211 protected void startBundles(BundleContext bundleContext
) throws Exception
{
212 String bundlesToStart
= getProperty(PROP_SLC_OSGI_START
);
213 if (bundlesToStart
== null)
216 StringTokenizer st
= new StringTokenizer(bundlesToStart
, ",");
217 Map
<String
, Bundle
> bundles
= getBundles(bundleContext
);
218 while (st
.hasMoreTokens()) {
219 String name
= st
.nextToken().trim();
220 Bundle bundle
= bundles
.get(name
);
224 } catch (Exception e
) {
225 warn("Bundle " + name
+ " cannot be started: " + e
.getMessage());
228 warn("Bundle " + name
+ " not installed.");
233 protected static Map
<String
, Bundle
> getInstalledBundles(
234 BundleContext bundleContext
) {
235 Map
<String
, Bundle
> installedBundles
= new HashMap
<String
, Bundle
>();
236 for (Bundle bundle
: bundleContext
.getBundles())
237 installedBundles
.put(bundle
.getLocation(), bundle
);
238 return installedBundles
;
241 protected static Map
<String
, Bundle
> getBundles(BundleContext bundleContext
) {
242 Map
<String
, Bundle
> installedBundles
= new HashMap
<String
, Bundle
>();
243 for (Bundle bundle
: bundleContext
.getBundles())
244 installedBundles
.put(bundle
.getSymbolicName(), bundle
);
245 return installedBundles
;
248 protected static List
<String
> asUrls(String baseUrl
,
249 List
<MavenFile
> mavenFiles
) {
250 List
<String
> urls
= new ArrayList
<String
>();
251 for (MavenFile mf
: mavenFiles
)
252 urls
.add(convertToUrl(baseUrl
, mf
));
256 protected static String
convertToUrl(String baseUrl
, MavenFile mf
) {
257 return baseUrl
+ mf
.getGroupId().replace('.', '/') + '/'
258 + mf
.getArtifactId() + '/' + mf
.getVersion() + '/'
259 + mf
.getArtifactId() + '-' + mf
.getVersion() + '.'
263 protected static MavenFile
convert(String str
) {
264 StringTokenizer st
= new StringTokenizer(str
, ":");
265 MavenFile component
= new MavenFile();
266 component
.setGroupId(st
.nextToken());
267 component
.setArtifactId(st
.nextToken());
268 component
.setType(st
.nextToken());
269 component
.setVersion(st
.nextToken());
270 component
.setScope(st
.nextToken());
274 protected String
getProperty(String name
) {
275 String value
= System
.getProperty(name
);
276 if (value
== null || value
.equals(""))
282 private static void info(Object obj
) {
283 System
.out
.println("# INFO " + obj
);
286 private static void debug(Object obj
) {
288 System
.out
.println("# DBUG " + obj
);
291 private static void warn(Object obj
) {
292 System
.err
.println("# WARN " + obj
);
295 static class MavenFile
{
296 private String groupId
;
297 private String artifactId
;
298 private String version
;
300 private String classifier
;
301 private String scope
;
303 public String
getScope() {
307 public void setScope(String scope
) {
311 private String distributionId
;
313 public String
getDistributionId() {
314 return distributionId
;
317 public void setDistributionId(String distributionId
) {
318 this.distributionId
= distributionId
;
321 public String
getGroupId() {
325 public void setGroupId(String groupId
) {
326 this.groupId
= groupId
;
329 public String
getArtifactId() {
333 public void setArtifactId(String artifactId
) {
334 this.artifactId
= artifactId
;
337 public String
getVersion() {
341 public void setVersion(String version
) {
342 this.version
= version
;
345 public String
getType() {
349 public void setType(String type
) {
353 public String
getClassifier() {
357 public void setClassifier(String classifier
) {
358 this.classifier
= classifier
;