X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.osgiboot%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fosgiboot%2FActivator.java;h=78d6024f93798f26309b740d8f2cbf81c6a5f293;hb=e1384618cb6d1c06ec570d7798b783fa04a3d807;hp=0051c1b91264494ce15d5aee52700fb0a12b8c50;hpb=7beaa7b00689e51eabe523402c7571c4f2507f77;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Activator.java b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Activator.java index 0051c1b91..78d6024f9 100644 --- a/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Activator.java +++ b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Activator.java @@ -3,6 +3,7 @@ package org.argeo.slc.osgiboot; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -19,22 +20,27 @@ import org.osgi.framework.BundleException; public class Activator implements BundleActivator { public final static String PROP_SLC_OSGI_START = "slc.osgi.start"; + public final static String PROP_SLC_OSGI_BUNDLES = "slc.osgi.bundles"; public final static String PROP_SLC_OSGI_DEV_BASES = "slc.osgi.devBases"; public final static String PROP_SLC_OSGI_DEV_PATTERNS = "slc.osgi.devPatterns"; public final static String PROP_SLC_OSGI_LOCATIONS = "slc.osgi.locations"; + public final static String PROP_SLC_OSGI_BASE_URL = "slc.osgi.baseUrl"; public final static String PROP_SLC_MAVEN_DEPENDENCY_FILE = "slc.maven.dependencyFile"; + public final static String PROP_SLC_OSGIBOOT_DEBUG = "slc.osgiboot.debug"; - private static Boolean debug = false; + private static Boolean debug = Boolean.parseBoolean(System.getProperty( + PROP_SLC_OSGIBOOT_DEBUG, "false")); public void start(BundleContext bundleContext) throws Exception { try { info("SLC OSGi bootstrap starting..."); - installUrls(bundleContext, getDevLocationsUrls()); +// installUrls(bundleContext, getDevLocationsUrls()); installUrls(bundleContext, getLocationsUrls()); - List urls = getMavenUrls(); - installUrls(bundleContext, urls); + installUrls(bundleContext, getBundlesUrls()); + + installUrls(bundleContext, getMavenUrls()); startBundles(bundleContext); @@ -53,13 +59,11 @@ public class Activator implements BundleActivator { Map installedBundles = getInstalledBundles(bundleContext); for (String url : urls) { try { - if (installedBundles.containsKey(url)) { Bundle bundle = installedBundles.get(url); // bundle.update(); - if (debug) - debug("Bundle " + bundle.getSymbolicName() - + " already installed from " + url); + info("Bundle " + bundle.getSymbolicName() + + " already installed from " + url); } else { Bundle bundle = bundleContext.installBundle(url); if (debug) @@ -77,6 +81,7 @@ public class Activator implements BundleActivator { protected List getLocationsUrls() { List urlsProvided = new ArrayList(); + String baseUrl = getProperty(PROP_SLC_OSGI_BASE_URL, "reference:file:"); String bundlesList = getProperty(PROP_SLC_OSGI_LOCATIONS); if (bundlesList == null) return urlsProvided; @@ -85,7 +90,7 @@ public class Activator implements BundleActivator { StringTokenizer st = new StringTokenizer(bundlesList, File.pathSeparator); while (st.hasMoreTokens()) { - urlsProvided.add("reference:file:" + st.nextToken().trim()); + urlsProvided.add(baseUrl + st.nextToken().trim()); } return urlsProvided; } @@ -129,6 +134,95 @@ public class Activator implements BundleActivator { return urls; } + protected List getBundlesUrls() { + List urls = new ArrayList(); + + List bundlesSets = new ArrayList(); + String bundles = getProperty(PROP_SLC_OSGI_BUNDLES); + if (bundles == null) + return urls; + info(PROP_SLC_OSGI_BUNDLES + "=" + bundles); + + StringTokenizer st = new StringTokenizer(bundles, ","); + while (st.hasMoreTokens()) { + bundlesSets.add(new BundlesSet(st.nextToken())); + } + + List included = new ArrayList(); + PathMatcher matcher = new AntPathMatcher(); + for (BundlesSet bundlesSet : bundlesSets) + for (String pattern : bundlesSet.getIncludes()) + match(matcher, included, bundlesSet.getDir(), null, pattern); + + List excluded = new ArrayList(); + for (BundlesSet bundlesSet : bundlesSets) + for (String pattern : bundlesSet.getExcludes()) + match(matcher, excluded, bundlesSet.getDir(), null, pattern); + + for (String fullPath : included) { + if (!excluded.contains(fullPath)) + urls.add("reference:file:" + fullPath); + } + + return urls; + } + + private class BundlesSet { + private String baseUrl = "reference:file"; + private final String dir; + private List includes = new ArrayList(); + private List excludes = new ArrayList(); + + public BundlesSet(String def) { + StringTokenizer st = new StringTokenizer(def, ";"); + + if (!st.hasMoreTokens()) + throw new RuntimeException("Base dir not defined."); + try { + String dirPath = st.nextToken(); + dir = new File(dirPath.replace('/', File.separatorChar)) + .getCanonicalPath(); + if (debug) + debug("Base dir: " + dir); + } catch (IOException e) { + throw new RuntimeException("Cannot convert to absolute path", e); + } + + while (st.hasMoreTokens()) { + String tk = st.nextToken(); + StringTokenizer stEq = new StringTokenizer(tk, "="); + String type = stEq.nextToken(); + String pattern = stEq.nextToken(); + if ("in".equals(type) || "include".equals(type)) { + includes.add(pattern); + } else if ("ex".equals(type) || "exclude".equals(type)) { + excludes.add(pattern); + } else if ("baseUrl".equals(type)) { + baseUrl = pattern; + } else { + System.err.println("Unkown bundles pattern type " + type); + } + } + } + + public String getDir() { + return dir; + } + + public List getIncludes() { + return includes; + } + + public List getExcludes() { + return excludes; + } + + public String getBaseUrl() { + return baseUrl; + } + + } + protected void match(PathMatcher matcher, List matched, String base, String currentPath, String pattern) { if (currentPath == null) { @@ -144,10 +238,9 @@ public class Activator implements BundleActivator { } for (File file : files) - if (file.isDirectory()) - match(matcher, matched, base, file.getName(), pattern); + match(matcher, matched, base, file.getName(), pattern); } else { - String fullPath = base + currentPath; + String fullPath = base + '/' + currentPath; if (matched.contains(fullPath)) return;// don't try deeper if already matched @@ -159,24 +252,29 @@ public class Activator implements BundleActivator { matched.add(fullPath); return; } else { - File[] files = new File((base + currentPath).replace('/', - File.separatorChar)).listFiles(); - for (File file : files) - if (file.isDirectory()) { - String newCurrentPath = currentPath + '/' - + file.getName(); - if (matcher.matchStart(pattern, newCurrentPath)) { - // recurse only if start matches - match(matcher, matched, base, newCurrentPath, - pattern); - } else { - if (debug) - debug(newCurrentPath - + " does not start match with " - + pattern); - + String newFullPath = (base + '/' + currentPath).replace('/', + File.separatorChar); + File[] files = new File(newFullPath).listFiles(); + if (files != null) { + for (File file : files) + if (file.isDirectory()) { + String newCurrentPath = currentPath + '/' + + file.getName(); + if (matcher.matchStart(pattern, newCurrentPath)) { + // recurse only if start matches + match(matcher, matched, base, newCurrentPath, + pattern); + } else { + if (debug) + debug(newCurrentPath + + " does not start match with " + + pattern); + + } } - } + } else { + warn("Not a directory: " + newFullPath); + } } } } @@ -222,7 +320,8 @@ public class Activator implements BundleActivator { try { bundle.start(); } catch (Exception e) { - warn("Bundle " + name + " cannot be started: " + e.getMessage()); + warn("Bundle " + name + " cannot be started: " + + e.getMessage()); } else warn("Bundle " + name + " not installed."); @@ -233,8 +332,14 @@ public class Activator implements BundleActivator { protected static Map getInstalledBundles( BundleContext bundleContext) { Map installedBundles = new HashMap(); - for (Bundle bundle : bundleContext.getBundles()) - installedBundles.put(bundle.getLocation(), bundle); + + for (Bundle bundle : bundleContext.getBundles()) { + String key = bundle.getSymbolicName(); + if (key == null) { + key = bundle.getLocation(); + } + installedBundles.put(key, bundle); + } return installedBundles; } @@ -271,14 +376,23 @@ public class Activator implements BundleActivator { return component; } - protected String getProperty(String name) { - String value = System.getProperty(name); + protected static String getProperty(String name, String defaultValue) { + final String value; + if (defaultValue != null) + value = System.getProperty(name, defaultValue); + else + value = System.getProperty(name); + if (value == null || value.equals("")) return null; else return value; } + protected static String getProperty(String name) { + return getProperty(name, null); + } + private static void info(Object obj) { System.out.println("# INFO " + obj); } @@ -289,7 +403,11 @@ public class Activator implements BundleActivator { } private static void warn(Object obj) { - System.err.println("# WARN " + obj); + System.out.println("# WARN " + obj); + // if (System.getProperty("os.name").contains("Windows")) + // System.out.println("# WARN " + obj); + // else + // System.err.println("# WARN " + obj); } static class MavenFile {