]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Activator.java
0051c1b91264494ce15d5aee52700fb0a12b8c50
[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 = false;
28
29 public void start(BundleContext bundleContext) throws Exception {
30 try {
31 info("SLC OSGi bootstrap starting...");
32 installUrls(bundleContext, getDevLocationsUrls());
33
34 installUrls(bundleContext, getLocationsUrls());
35
36 List<String> urls = getMavenUrls();
37 installUrls(bundleContext, urls);
38
39 startBundles(bundleContext);
40
41 info("SLC OSGi bootstrap completed");
42 } catch (Exception e) {
43 e.printStackTrace();
44 throw e;
45 }
46 }
47
48 public void stop(BundleContext context) throws Exception {
49 }
50
51 protected static void installUrls(BundleContext bundleContext,
52 List<String> urls) {
53 Map<String, Bundle> installedBundles = getInstalledBundles(bundleContext);
54 for (String url : urls) {
55 try {
56
57 if (installedBundles.containsKey(url)) {
58 Bundle bundle = installedBundles.get(url);
59 // bundle.update();
60 if (debug)
61 debug("Bundle " + bundle.getSymbolicName()
62 + " already installed from " + url);
63 } else {
64 Bundle bundle = bundleContext.installBundle(url);
65 if (debug)
66 debug("Installed bundle " + bundle.getSymbolicName()
67 + " from " + url);
68 }
69 } catch (BundleException e) {
70 warn("Could not install bundle from " + url + ": "
71 + e.getMessage());
72 }
73 }
74
75 }
76
77 protected List<String> getLocationsUrls() {
78 List<String> urlsProvided = new ArrayList<String>();
79
80 String bundlesList = getProperty(PROP_SLC_OSGI_LOCATIONS);
81 if (bundlesList == null)
82 return urlsProvided;
83 bundlesList = SystemPropertyUtils.resolvePlaceholders(bundlesList);
84
85 StringTokenizer st = new StringTokenizer(bundlesList,
86 File.pathSeparator);
87 while (st.hasMoreTokens()) {
88 urlsProvided.add("reference:file:" + st.nextToken().trim());
89 }
90 return urlsProvided;
91 }
92
93 protected List<String> getDevLocationsUrls() {
94 List<String> urls = new ArrayList<String>();
95
96 String devBase = getProperty(PROP_SLC_OSGI_DEV_BASES);
97 String devPatterns = getProperty(PROP_SLC_OSGI_DEV_PATTERNS);
98 if (devBase == null)
99 return urls;
100 devBase = SystemPropertyUtils.resolvePlaceholders(devBase);
101 devBase = devBase.replace(File.separatorChar, '/');
102 devPatterns = SystemPropertyUtils.resolvePlaceholders(devPatterns);
103
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);
109 if (lastChar != '/')
110 token = token + '/';
111 bases.add(token);
112 }
113
114 List<String> patterns = new ArrayList<String>();
115 st = new StringTokenizer(devPatterns, ";");
116 while (st.hasMoreTokens()) {
117 patterns.add(st.nextToken().trim());
118 }
119
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);
125
126 for (String fullPath : matched)
127 urls.add("reference:file:" + fullPath);
128
129 return urls;
130 }
131
132 protected void match(PathMatcher matcher, List<String> matched,
133 String base, String currentPath, String pattern) {
134 if (currentPath == null) {
135 // Init
136 File baseDir = new File(base.replace('/', File.separatorChar));
137 File[] files = baseDir.listFiles();
138
139 if (files == null) {
140 warn("Base dir " + baseDir + " has no children, exists="
141 + baseDir.exists() + ", isDirectory="
142 + baseDir.isDirectory());
143 return;
144 }
145
146 for (File file : files)
147 if (file.isDirectory())
148 match(matcher, matched, base, file.getName(), pattern);
149 } else {
150 String fullPath = base + currentPath;
151 if (matched.contains(fullPath))
152 return;// don't try deeper if already matched
153
154 boolean ok = matcher.match(pattern, currentPath);
155 if (debug)
156 debug(currentPath + " " + (ok ? "" : " not ")
157 + " matched with " + pattern);
158 if (ok) {
159 matched.add(fullPath);
160 return;
161 } else {
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 + '/'
167 + file.getName();
168 if (matcher.matchStart(pattern, newCurrentPath)) {
169 // recurse only if start matches
170 match(matcher, matched, base, newCurrentPath,
171 pattern);
172 } else {
173 if (debug)
174 debug(newCurrentPath
175 + " does not start match with "
176 + pattern);
177
178 }
179 }
180 }
181 }
182 }
183
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);
188 if (config == null)
189 return new ArrayList<String>();
190
191 List<MavenFile> mavenFiles = new ArrayList<MavenFile>();
192 BufferedReader in = new BufferedReader(new FileReader(config));
193 String line = null;
194 while ((line = in.readLine()) != null) {
195 try {
196 line = line.trim();
197 if (line.equals("")
198 || line
199 .startsWith("The following files have been resolved:"))
200 continue;// skip
201
202 mavenFiles.add(convert(line));
203 } catch (Exception e) {
204 warn("Could not load line " + line);
205 }
206 }
207
208 return asUrls(baseUrl, mavenFiles);
209 }
210
211 protected void startBundles(BundleContext bundleContext) throws Exception {
212 String bundlesToStart = getProperty(PROP_SLC_OSGI_START);
213 if (bundlesToStart == null)
214 return;
215
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);
221 if (bundle != null)
222 try {
223 bundle.start();
224 } catch (Exception e) {
225 warn("Bundle " + name + " cannot be started: " + e.getMessage());
226 }
227 else
228 warn("Bundle " + name + " not installed.");
229
230 }
231 }
232
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;
239 }
240
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;
246 }
247
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));
253 return urls;
254 }
255
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() + '.'
260 + mf.getType();
261 }
262
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());
271 return component;
272 }
273
274 protected String getProperty(String name) {
275 String value = System.getProperty(name);
276 if (value == null || value.equals(""))
277 return null;
278 else
279 return value;
280 }
281
282 private static void info(Object obj) {
283 System.out.println("# INFO " + obj);
284 }
285
286 private static void debug(Object obj) {
287 if (debug)
288 System.out.println("# DBUG " + obj);
289 }
290
291 private static void warn(Object obj) {
292 System.err.println("# WARN " + obj);
293 }
294
295 static class MavenFile {
296 private String groupId;
297 private String artifactId;
298 private String version;
299 private String type;
300 private String classifier;
301 private String scope;
302
303 public String getScope() {
304 return scope;
305 }
306
307 public void setScope(String scope) {
308 this.scope = scope;
309 }
310
311 private String distributionId;
312
313 public String getDistributionId() {
314 return distributionId;
315 }
316
317 public void setDistributionId(String distributionId) {
318 this.distributionId = distributionId;
319 }
320
321 public String getGroupId() {
322 return groupId;
323 }
324
325 public void setGroupId(String groupId) {
326 this.groupId = groupId;
327 }
328
329 public String getArtifactId() {
330 return artifactId;
331 }
332
333 public void setArtifactId(String artifactId) {
334 this.artifactId = artifactId;
335 }
336
337 public String getVersion() {
338 return version;
339 }
340
341 public void setVersion(String version) {
342 this.version = version;
343 }
344
345 public String getType() {
346 return type;
347 }
348
349 public void setType(String type) {
350 this.type = type;
351 }
352
353 public String getClassifier() {
354 return classifier;
355 }
356
357 public void setClassifier(String classifier) {
358 this.classifier = classifier;
359 }
360
361 }
362
363 }