]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Activator.java
7b02c785ab3db7f719be98039e63869411c5dc0f
[gpl/argeo-slc.git] / runtime / org.argeo.slc.osgiboot / src / main / java / org / argeo / slc / osgiboot / Activator.java
1 package org.argeo.slc.osgiboot;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.StringTokenizer;
11
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;
19
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";
26
27 private static Boolean debug = true;
28
29 public void start(BundleContext bundleContext) throws Exception {
30 installUrls(bundleContext, getDevLocationsUrls());
31
32 installUrls(bundleContext, getLocationsUrls());
33
34 List<String> urls = getMavenUrls();
35 installUrls(bundleContext, urls);
36
37 startBundles(bundleContext);
38 }
39
40 public void stop(BundleContext context) throws Exception {
41 }
42
43 protected static void installUrls(BundleContext bundleContext,
44 List<String> urls) {
45 Map<String, Bundle> installedBundles = getInstalledBundles(bundleContext);
46 for (String url : urls) {
47 try {
48
49 if (installedBundles.containsKey(url)) {
50 Bundle bundle = installedBundles.get(url);
51 // bundle.update();
52 info("Bundle " + bundle.getSymbolicName()
53 + " already installed from " + url);
54 } else {
55 Bundle bundle = bundleContext.installBundle(url);
56 info("Installed bundle " + bundle.getSymbolicName()
57 + " from " + url);
58 }
59 } catch (BundleException e) {
60 warn("Could not install bundle from " + url + ": "
61 + e.getMessage());
62 }
63 }
64
65 }
66
67 protected List<String> getLocationsUrls() {
68 List<String> urlsProvided = new ArrayList<String>();
69
70 String bundlesList = getProperty(PROP_SLC_OSGI_LOCATIONS);
71 if (bundlesList == null)
72 return urlsProvided;
73 bundlesList = SystemPropertyUtils.resolvePlaceholders(bundlesList);
74
75 StringTokenizer st = new StringTokenizer(bundlesList,
76 File.pathSeparator);
77 while (st.hasMoreTokens()) {
78 urlsProvided.add("reference:file:" + st.nextToken().trim());
79 }
80 return urlsProvided;
81 }
82
83 protected List<String> getDevLocationsUrls() {
84 List<String> urls = new ArrayList<String>();
85
86 String devBase = getProperty(PROP_SLC_OSGI_DEV_BASES);
87 String devPatterns = getProperty(PROP_SLC_OSGI_DEV_PATTERNS);
88 if (devBase == null)
89 return urls;
90 devBase = SystemPropertyUtils.resolvePlaceholders(devBase);
91 devBase = devBase.replace(File.separatorChar, '/');
92 devPatterns = SystemPropertyUtils.resolvePlaceholders(devPatterns);
93
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);
99 if (lastChar != '/')
100 token = token + '/';
101 bases.add(token);
102 }
103
104 List<String> patterns = new ArrayList<String>();
105 st = new StringTokenizer(devPatterns, ";");
106 while (st.hasMoreTokens()) {
107 patterns.add(st.nextToken().trim());
108 }
109
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);
115
116 for (String fullPath : matched)
117 urls.add("reference:file:" + fullPath);
118
119 return urls;
120 }
121
122 protected void match(PathMatcher matcher, List<String> matched,
123 String base, String currentPath, String pattern) {
124 if (currentPath == null) {
125 // Init
126 File[] files = new File(base.replace('/', File.separatorChar))
127 .listFiles();
128 for (File file : files)
129 if (file.isDirectory())
130 match(matcher, matched, base, file.getName(), pattern);
131 } else {
132 String fullPath = base + currentPath;
133 if (matched.contains(fullPath))
134 return;// don't try deeper if already matched
135
136 boolean ok = matcher.match(pattern, currentPath);
137 if (debug)
138 debug(currentPath + " " + (ok ? "" : " not ")
139 + " matched with " + pattern);
140 if (ok) {
141 matched.add(fullPath);
142 return;
143 } else {
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 + '/'
149 + file.getName();
150 if (matcher.matchStart(pattern, newCurrentPath)) {
151 // recurse only if start matches
152 match(matcher, matched, base, newCurrentPath,
153 pattern);
154 } else {
155 debug(newCurrentPath
156 + " does not start match with " + pattern);
157
158 }
159 }
160 }
161 }
162 }
163
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);
168 if (config == null)
169 return new ArrayList<String>();
170
171 List<MavenFile> mavenFiles = new ArrayList<MavenFile>();
172 BufferedReader in = new BufferedReader(new FileReader(config));
173 String line = null;
174 while ((line = in.readLine()) != null) {
175 try {
176 line = line.trim();
177 if (line.equals("")
178 || line
179 .startsWith("The following files have been resolved:"))
180 continue;// skip
181
182 mavenFiles.add(convert(line));
183 } catch (Exception e) {
184 System.err.println("Could not load line " + line);
185 }
186 }
187
188 return asUrls(baseUrl, mavenFiles);
189 }
190
191 protected void startBundles(BundleContext bundleContext) throws Exception {
192 String bundlesToStart = getProperty(PROP_SLC_OSGI_START);
193 if (bundlesToStart == null)
194 return;
195
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);
201 if (bundle != null)
202 try {
203 bundle.start();
204 } catch (Exception e) {
205 warn("Bundle name cannot be started: " + e.getMessage());
206 }
207 else
208 warn("Bundle " + name + " not installed.");
209
210 }
211 }
212
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;
219 }
220
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;
226 }
227
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));
233 return urls;
234 }
235
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() + '.'
240 + mf.getType();
241 }
242
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());
251 return component;
252 }
253
254 protected String getProperty(String name) {
255 String value = System.getProperty(name);
256 if (value == null || value.equals(""))
257 return null;
258 else
259 return value;
260 }
261
262 private static void info(Object obj) {
263 System.out.println("#INFO " + obj);
264 }
265
266 private static void debug(Object obj) {
267 System.out.println("#DBUG " + obj);
268 }
269
270 private static void warn(Object obj) {
271 System.err.println("#WARN " + obj);
272 }
273
274 static class MavenFile {
275 private String groupId;
276 private String artifactId;
277 private String version;
278 private String type;
279 private String classifier;
280 private String scope;
281
282 public String getScope() {
283 return scope;
284 }
285
286 public void setScope(String scope) {
287 this.scope = scope;
288 }
289
290 private String distributionId;
291
292 public String getDistributionId() {
293 return distributionId;
294 }
295
296 public void setDistributionId(String distributionId) {
297 this.distributionId = distributionId;
298 }
299
300 public String getGroupId() {
301 return groupId;
302 }
303
304 public void setGroupId(String groupId) {
305 this.groupId = groupId;
306 }
307
308 public String getArtifactId() {
309 return artifactId;
310 }
311
312 public void setArtifactId(String artifactId) {
313 this.artifactId = artifactId;
314 }
315
316 public String getVersion() {
317 return version;
318 }
319
320 public void setVersion(String version) {
321 this.version = version;
322 }
323
324 public String getType() {
325 return type;
326 }
327
328 public void setType(String type) {
329 this.type = type;
330 }
331
332 public String getClassifier() {
333 return classifier;
334 }
335
336 public void setClassifier(String classifier) {
337 this.classifier = classifier;
338 }
339
340 }
341
342 }