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
= true;
29 public void start(BundleContext bundleContext
) throws Exception
{
30 installUrls(bundleContext
, getDevLocationsUrls());
32 installUrls(bundleContext
, getLocationsUrls());
34 List
<String
> urls
= getMavenUrls();
35 installUrls(bundleContext
, urls
);
37 startBundles(bundleContext
);
40 public void stop(BundleContext context
) throws Exception
{
43 protected static void installUrls(BundleContext bundleContext
,
45 Map
<String
, Bundle
> installedBundles
= getInstalledBundles(bundleContext
);
46 for (String url
: urls
) {
49 if (installedBundles
.containsKey(url
)) {
50 Bundle bundle
= installedBundles
.get(url
);
52 info("Bundle " + bundle
.getSymbolicName()
53 + " already installed from " + url
);
55 Bundle bundle
= bundleContext
.installBundle(url
);
56 info("Installed bundle " + bundle
.getSymbolicName()
59 } catch (BundleException e
) {
60 warn("Could not install bundle from " + url
+ ": "
67 protected List
<String
> getLocationsUrls() {
68 List
<String
> urlsProvided
= new ArrayList
<String
>();
70 String bundlesList
= getProperty(PROP_SLC_OSGI_LOCATIONS
);
71 if (bundlesList
== null)
73 bundlesList
= SystemPropertyUtils
.resolvePlaceholders(bundlesList
);
75 StringTokenizer st
= new StringTokenizer(bundlesList
,
77 while (st
.hasMoreTokens()) {
78 urlsProvided
.add("reference:file:" + st
.nextToken().trim());
83 protected List
<String
> getDevLocationsUrls() {
84 List
<String
> urls
= new ArrayList
<String
>();
86 String devBase
= getProperty(PROP_SLC_OSGI_DEV_BASES
);
87 String devPatterns
= getProperty(PROP_SLC_OSGI_DEV_PATTERNS
);
90 devBase
= SystemPropertyUtils
.resolvePlaceholders(devBase
);
91 devBase
= devBase
.replace(File
.separatorChar
, '/');
92 devPatterns
= SystemPropertyUtils
.resolvePlaceholders(devPatterns
);
94 List
<String
> bases
= new ArrayList
<String
>();
95 StringTokenizer st
= new StringTokenizer(devBase
, ",");
96 while (st
.hasMoreTokens()) {
97 String token
= st
.nextToken().trim();
98 char lastChar
= token
.charAt(token
.length() - 1);
104 List
<String
> patterns
= new ArrayList
<String
>();
105 st
= new StringTokenizer(devPatterns
, ";");
106 while (st
.hasMoreTokens()) {
107 patterns
.add(st
.nextToken().trim());
110 List
<String
> matched
= new ArrayList
<String
>();
111 PathMatcher matcher
= new AntPathMatcher();
112 for (String base
: bases
)
113 for (String pattern
: patterns
)
114 match(matcher
, matched
, base
, null, pattern
);
116 for (String fullPath
: matched
)
117 urls
.add("reference:file:" + fullPath
);
122 protected void match(PathMatcher matcher
, List
<String
> matched
,
123 String base
, String currentPath
, String pattern
) {
124 if (currentPath
== null) {
126 File
[] files
= new File(base
.replace('/', File
.separatorChar
))
128 for (File file
: files
)
129 if (file
.isDirectory())
130 match(matcher
, matched
, base
, file
.getName(), pattern
);
132 String fullPath
= base
+ currentPath
;
133 if (matched
.contains(fullPath
))
134 return;// don't try deeper if already matched
136 boolean ok
= matcher
.match(pattern
, currentPath
);
138 debug(currentPath
+ " " + (ok ?
"" : " not ")
139 + " matched with " + pattern
);
141 matched
.add(fullPath
);
144 File
[] files
= new File((base
+ currentPath
).replace('/',
145 File
.separatorChar
)).listFiles();
146 for (File file
: files
)
147 if (file
.isDirectory()) {
148 String newCurrentPath
= currentPath
+ '/'
150 if (matcher
.matchStart(pattern
, newCurrentPath
)) {
151 // recurse only if start matches
152 match(matcher
, matched
, base
, newCurrentPath
,
156 + " does not start match with " + pattern
);
164 protected List
<String
> getMavenUrls() throws Exception
{
165 String baseUrl
= "reference:file:" + System
.getProperty("user.home")
166 + "/.m2/repository/";
167 String config
= getProperty(PROP_SLC_MAVEN_DEPENDENCY_FILE
);
169 return new ArrayList
<String
>();
171 List
<MavenFile
> mavenFiles
= new ArrayList
<MavenFile
>();
172 BufferedReader in
= new BufferedReader(new FileReader(config
));
174 while ((line
= in
.readLine()) != null) {
179 .startsWith("The following files have been resolved:"))
182 mavenFiles
.add(convert(line
));
183 } catch (Exception e
) {
184 System
.err
.println("Could not load line " + line
);
188 return asUrls(baseUrl
, mavenFiles
);
191 protected void startBundles(BundleContext bundleContext
) throws Exception
{
192 String bundlesToStart
= getProperty(PROP_SLC_OSGI_START
);
193 if (bundlesToStart
== null)
196 StringTokenizer st
= new StringTokenizer(bundlesToStart
, ",");
197 Map
<String
, Bundle
> bundles
= getBundles(bundleContext
);
198 while (st
.hasMoreTokens()) {
199 String name
= st
.nextToken().trim();
200 Bundle bundle
= bundles
.get(name
);
204 } catch (Exception e
) {
205 warn("Bundle name cannot be started: " + e
.getMessage());
208 warn("Bundle " + name
+ " not installed.");
213 protected static Map
<String
, Bundle
> getInstalledBundles(
214 BundleContext bundleContext
) {
215 Map
<String
, Bundle
> installedBundles
= new HashMap
<String
, Bundle
>();
216 for (Bundle bundle
: bundleContext
.getBundles())
217 installedBundles
.put(bundle
.getLocation(), bundle
);
218 return installedBundles
;
221 protected static Map
<String
, Bundle
> getBundles(BundleContext bundleContext
) {
222 Map
<String
, Bundle
> installedBundles
= new HashMap
<String
, Bundle
>();
223 for (Bundle bundle
: bundleContext
.getBundles())
224 installedBundles
.put(bundle
.getSymbolicName(), bundle
);
225 return installedBundles
;
228 protected static List
<String
> asUrls(String baseUrl
,
229 List
<MavenFile
> mavenFiles
) {
230 List
<String
> urls
= new ArrayList
<String
>();
231 for (MavenFile mf
: mavenFiles
)
232 urls
.add(convertToUrl(baseUrl
, mf
));
236 protected static String
convertToUrl(String baseUrl
, MavenFile mf
) {
237 return baseUrl
+ mf
.getGroupId().replace('.', '/') + '/'
238 + mf
.getArtifactId() + '/' + mf
.getVersion() + '/'
239 + mf
.getArtifactId() + '-' + mf
.getVersion() + '.'
243 protected static MavenFile
convert(String str
) {
244 StringTokenizer st
= new StringTokenizer(str
, ":");
245 MavenFile component
= new MavenFile();
246 component
.setGroupId(st
.nextToken());
247 component
.setArtifactId(st
.nextToken());
248 component
.setType(st
.nextToken());
249 component
.setVersion(st
.nextToken());
250 component
.setScope(st
.nextToken());
254 protected String
getProperty(String name
) {
255 String value
= System
.getProperty(name
);
256 if (value
== null || value
.equals(""))
262 private static void info(Object obj
) {
263 System
.out
.println("#INFO " + obj
);
266 private static void debug(Object obj
) {
267 System
.out
.println("#DBUG " + obj
);
270 private static void warn(Object obj
) {
271 System
.err
.println("#WARN " + obj
);
274 static class MavenFile
{
275 private String groupId
;
276 private String artifactId
;
277 private String version
;
279 private String classifier
;
280 private String scope
;
282 public String
getScope() {
286 public void setScope(String scope
) {
290 private String distributionId
;
292 public String
getDistributionId() {
293 return distributionId
;
296 public void setDistributionId(String distributionId
) {
297 this.distributionId
= distributionId
;
300 public String
getGroupId() {
304 public void setGroupId(String groupId
) {
305 this.groupId
= groupId
;
308 public String
getArtifactId() {
312 public void setArtifactId(String artifactId
) {
313 this.artifactId
= artifactId
;
316 public String
getVersion() {
320 public void setVersion(String version
) {
321 this.version
= version
;
324 public String
getType() {
328 public void setType(String type
) {
332 public String
getClassifier() {
336 public void setClassifier(String classifier
) {
337 this.classifier
= classifier
;