From: Mathieu Baudier Date: Tue, 2 Jun 2009 18:11:27 +0000 (+0000) Subject: Start implementing OSGi integration tests X-Git-Tag: argeo-slc-2.1.7~1881 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=7274972217cf5bb6772e8833c5723a5f9544ddb5;p=gpl%2Fargeo-slc.git Start implementing OSGi integration tests git-svn-id: https://svn.argeo.org/slc/trunk@2444 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/integration-tests/org.argeo.slc.it.webapp/.settings/org.eclipse.jdt.core.prefs b/integration-tests/org.argeo.slc.it.webapp/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..a1fe8157e --- /dev/null +++ b/integration-tests/org.argeo.slc.it.webapp/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Tue Jun 02 18:41:08 CEST 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/integration-tests/org.argeo.slc.it.webapp/pom.xml b/integration-tests/org.argeo.slc.it.webapp/pom.xml index 6b15ce4a7..c1ba1a27a 100644 --- a/integration-tests/org.argeo.slc.it.webapp/pom.xml +++ b/integration-tests/org.argeo.slc.it.webapp/pom.xml @@ -2,16 +2,33 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - org.argeo.slc - argeo-slc + org.argeo.slc.it + argeo-slc-it 0.11.3-SNAPSHOT - ../../org.argeo.slc + .. org.argeo.slc.it.webapp jar Argeo SLC Web Application Integration Tests + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + pre-integration-test + + copy-dependencies + + + true + true + + + + org.apache.maven.plugins maven-surefire-plugin @@ -34,5 +51,31 @@ + + + org.argeo.slc.server + bundles + ${project.version} + pom + + + + + org.argeo.slc.runtime + org.argeo.slc.support.equinox + tests + test + + + org.springframework.osgi + org.springframework.osgi.test + test + + + org.argeo.slc.runtime + org.argeo.slc.osgiboot + test + + \ No newline at end of file diff --git a/integration-tests/org.argeo.slc.it.webapp/src/test/java/org/argeo/slc/it/webapp/StartStopServerTest.java b/integration-tests/org.argeo.slc.it.webapp/src/test/java/org/argeo/slc/it/webapp/StartStopServerTest.java new file mode 100644 index 000000000..84ac24828 --- /dev/null +++ b/integration-tests/org.argeo.slc.it.webapp/src/test/java/org/argeo/slc/it/webapp/StartStopServerTest.java @@ -0,0 +1,55 @@ +package org.argeo.slc.it.webapp; + +import java.util.Properties; + +import org.argeo.slc.osgi.test.AbstractOsgiRuntimeTest; +import org.argeo.slc.osgiboot.OsgiBoot; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.springframework.osgi.test.platform.EquinoxPlatform; +import org.springframework.osgi.test.platform.OsgiPlatform; +import org.springframework.osgi.util.OsgiStringUtils; + +public class StartStopServerTest extends AbstractOsgiRuntimeTest { + public void testStartStop() throws Exception { + BundleContext bundleContext = osgiPlatform.getBundleContext(); + Bundle[] bundles = bundleContext.getBundles(); + for (int i = 0; i < bundles.length; i++) { + System.out + .println(OsgiStringUtils.nullSafeSymbolicName(bundles[i])); + } + + Thread.sleep(5 * 1000); + + } + + protected void postStart() throws Exception { + String classpath = System.getProperty("java.class.path"); + System.out.println("Classpath=" + classpath); + osgiBoot.installUrls(osgiBoot.getLocationsUrls( + OsgiBoot.DEFAULT_BASE_URL, classpath)); + osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL, + "../../demo/site;in=*;ex=target")); + osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL, + "target/dependency;in=*.jar")); + + String bundlesToStart = "org.argeo.dep.osgi.catalina.start,org.springframework.osgi.extender,org.springframework.osgi.web.extender,org.springframework.osgi.samples.simplewebapp,org.argeo.slc.server.activemq,org.argeo.slc.server.hsqldb,org.argeo.slc.server.hibernate,org.argeo.slc.server.services,org.argeo.slc.server.jms,org.argeo.slc.webapp,org.argeo.slc.ria"; + osgiBoot.startBundles(bundlesToStart); + } + + @Override + protected OsgiPlatform createOsgiPlatform() { + + return new EquinoxPlatform() { + + @Override + public Properties getConfigurationProperties() { + Properties props = super.getConfigurationProperties(); + props.put("osgi.console", ""); + return props; + } + + }; + } + +} diff --git a/org.argeo.slc/pom.xml b/org.argeo.slc/pom.xml index 12eb55d7b..20c25782a 100644 --- a/org.argeo.slc/pom.xml +++ b/org.argeo.slc/pom.xml @@ -263,6 +263,13 @@ limitations under the License. org.argeo.slc.support.equinox ${project.version} + + org.argeo.slc.runtime + org.argeo.slc.support.equinox + tests + test + ${project.version} + org.argeo.slc.runtime org.argeo.slc.support.ws.client @@ -441,13 +448,11 @@ limitations under the License. org.springframework.jms ${version.spring} - + ${version.spring-ws} + --> org.springframework.ws org.springframework.oxm @@ -474,6 +479,11 @@ limitations under the License. org.springframework.osgi.web ${version.spring-osgi} + + org.springframework.osgi + org.springframework.osgi.test + ${version.spring-osgi} + org.argeo.slc.dep.osgi org.argeo.dep.osgi.catalina.start diff --git a/runtime/org.argeo.slc.osgiboot/.classpath b/runtime/org.argeo.slc.osgiboot/.classpath index 16f01e2ee..bab5b6519 100644 --- a/runtime/org.argeo.slc.osgiboot/.classpath +++ b/runtime/org.argeo.slc.osgiboot/.classpath @@ -1,6 +1,7 @@ + diff --git a/runtime/org.argeo.slc.osgiboot/pom.xml b/runtime/org.argeo.slc.osgiboot/pom.xml index df47dc71a..d7c2afbbd 100644 --- a/runtime/org.argeo.slc.osgiboot/pom.xml +++ b/runtime/org.argeo.slc.osgiboot/pom.xml @@ -89,5 +89,13 @@ org.eclipse.osgi + + + org.junit + com.springsource.junit + test + + + 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 4457a4c51..5d70b769a 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 @@ -1,48 +1,29 @@ 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; import java.util.Map; import java.util.StringTokenizer; -import org.argeo.slc.osgiboot.internal.springutil.AntPathMatcher; -import org.argeo.slc.osgiboot.internal.springutil.PathMatcher; -import org.argeo.slc.osgiboot.internal.springutil.SystemPropertyUtils; import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; 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 = 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, getBundlesUrls()); + OsgiBoot osgiBoot = new OsgiBoot(bundleContext); + + osgiBoot.installUrls( osgiBoot.getBundlesUrls()); - installUrls(bundleContext, getLocationsUrls()); + osgiBoot.installUrls( osgiBoot.getLocationsUrls()); - installUrls(bundleContext, getMavenUrls()); +// installUrls(bundleContext, getMavenUrls()); - startBundles(bundleContext); + osgiBoot.startBundles(); info("SLC OSGi bootstrap completed"); } catch (Exception e) { @@ -54,232 +35,7 @@ public class Activator implements BundleActivator { public void stop(BundleContext context) throws Exception { } - protected static void installUrls(BundleContext bundleContext, - List urls) { - 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); - } else { - Bundle bundle = bundleContext.installBundle(url); - if (debug) - debug("Installed bundle " + bundle.getSymbolicName() - + " from " + url); - } - } catch (BundleException e) { - warn("Could not install bundle from " + url + ": " - + e.getMessage()); - } - } - - } - - 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; - bundlesList = SystemPropertyUtils.resolvePlaceholders(bundlesList); - - StringTokenizer st = new StringTokenizer(bundlesList, - File.pathSeparator); - while (st.hasMoreTokens()) { - urlsProvided.add(baseUrl + st.nextToken().trim()); - } - return urlsProvided; - } - - protected List getDevLocationsUrls() { - List urls = new ArrayList(); - - String devBase = getProperty(PROP_SLC_OSGI_DEV_BASES); - String devPatterns = getProperty(PROP_SLC_OSGI_DEV_PATTERNS); - if (devBase == null) - return urls; - devBase = SystemPropertyUtils.resolvePlaceholders(devBase); - devBase = devBase.replace(File.separatorChar, '/'); - devPatterns = SystemPropertyUtils.resolvePlaceholders(devPatterns); - - List bases = new ArrayList(); - StringTokenizer st = new StringTokenizer(devBase, ","); - while (st.hasMoreTokens()) { - String token = st.nextToken().trim(); - char lastChar = token.charAt(token.length() - 1); - if (lastChar != '/') - token = token + '/'; - bases.add(token); - } - - List patterns = new ArrayList(); - st = new StringTokenizer(devPatterns, ";"); - while (st.hasMoreTokens()) { - patterns.add(st.nextToken().trim()); - } - - List matched = new ArrayList(); - PathMatcher matcher = new AntPathMatcher(); - for (String base : bases) - for (String pattern : patterns) - match(matcher, matched, base, null, pattern); - - for (String fullPath : matched) - urls.add("reference:file:" + fullPath); - - 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) { - // Init - File baseDir = new File(base.replace('/', File.separatorChar)); - File[] files = baseDir.listFiles(); - - if (files == null) { - warn("Base dir " + baseDir + " has no children, exists=" - + baseDir.exists() + ", isDirectory=" - + baseDir.isDirectory()); - return; - } - - for (File file : files) - match(matcher, matched, base, file.getName(), pattern); - } else { - String fullPath = base + '/' + currentPath; - if (matched.contains(fullPath)) - return;// don't try deeper if already matched - - boolean ok = matcher.match(pattern, currentPath); - if (debug) - debug(currentPath + " " + (ok ? "" : " not ") - + " matched with " + pattern); - if (ok) { - matched.add(fullPath); - return; - } else { - 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); - } - } - } - } - +/* protected List getMavenUrls() throws Exception { String baseUrl = "reference:file:" + System.getProperty("user.home") + "/.m2/repository/"; @@ -306,49 +62,8 @@ public class Activator implements BundleActivator { return asUrls(baseUrl, mavenFiles); } - - protected void startBundles(BundleContext bundleContext) throws Exception { - String bundlesToStart = getProperty(PROP_SLC_OSGI_START); - if (bundlesToStart == null) - return; - - StringTokenizer st = new StringTokenizer(bundlesToStart, ","); - Map bundles = getBundles(bundleContext); - while (st.hasMoreTokens()) { - String name = st.nextToken().trim(); - Bundle bundle = bundles.get(name); - if (bundle != null) - try { - bundle.start(); - } catch (Exception e) { - warn("Bundle " + name + " cannot be started: " - + e.getMessage()); - } - else - warn("Bundle " + name + " not installed."); - - } - } - - /** Key is location */ - protected static Map getInstalledBundles( - BundleContext bundleContext) { - Map installedBundles = new HashMap(); - - for (Bundle bundle : bundleContext.getBundles()) { - installedBundles.put(bundle.getLocation(), bundle); - } - return installedBundles; - } - - /** Key is symbolic name */ - protected static Map getBundles(BundleContext bundleContext) { - Map installedBundles = new HashMap(); - for (Bundle bundle : bundleContext.getBundles()) - installedBundles.put(bundle.getSymbolicName(), bundle); - return installedBundles; - } - +*/ +/* protected static List asUrls(String baseUrl, List mavenFiles) { List urls = new ArrayList(); @@ -374,7 +89,7 @@ public class Activator implements BundleActivator { component.setScope(st.nextToken()); return component; } - +*//* protected static String getProperty(String name, String defaultValue) { final String value; if (defaultValue != null) @@ -391,11 +106,11 @@ public class Activator implements BundleActivator { protected static String getProperty(String name) { return getProperty(name, null); } - +*/ private static void info(Object obj) { System.out.println("# INFO " + obj); } - +/* private static void debug(Object obj) { if (debug) System.out.println("# DBUG " + obj); @@ -408,7 +123,7 @@ public class Activator implements BundleActivator { // else // System.err.println("# WARN " + obj); } - +*//* static class MavenFile { private String groupId; private String artifactId; @@ -476,5 +191,5 @@ public class Activator implements BundleActivator { } } - +*/ } diff --git a/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java new file mode 100644 index 000000000..fdcb77df0 --- /dev/null +++ b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java @@ -0,0 +1,325 @@ +package org.argeo.slc.osgiboot; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; + +import org.argeo.slc.osgiboot.internal.springutil.AntPathMatcher; +import org.argeo.slc.osgiboot.internal.springutil.PathMatcher; +import org.argeo.slc.osgiboot.internal.springutil.SystemPropertyUtils; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; + +public class OsgiBoot { + 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_LOCATIONS = "slc.osgi.locations"; + public final static String PROP_SLC_OSGI_BASE_URL = "slc.osgi.baseUrl"; + public final static String PROP_SLC_OSGIBOOT_DEBUG = "slc.osgiboot.debug"; + + public final static String DEFAULT_BASE_URL = "reference:file:"; + + private Boolean debug = Boolean.parseBoolean(System.getProperty( + PROP_SLC_OSGIBOOT_DEBUG, "false")); + + private final BundleContext bundleContext; + + public OsgiBoot(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + + public void installUrls(List urls) { + Map installedBundles = getInstalledBundles(); + 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); + } else { + Bundle bundle = bundleContext.installBundle(url); + if (debug) + debug("Installed bundle " + bundle.getSymbolicName() + + " from " + url); + } + } catch (BundleException e) { + warn("Could not install bundle from " + url + ": " + + e.getMessage()); + } + } + + } + + public void startBundles() throws Exception { + String bundlesToStart = getProperty(PROP_SLC_OSGI_START); + startBundles(bundlesToStart); + } + + public void startBundles(String bundlesToStart) throws Exception { + if (bundlesToStart == null) + return; + + StringTokenizer st = new StringTokenizer(bundlesToStart, ","); + Map bundles = getBundles(); + while (st.hasMoreTokens()) { + String name = st.nextToken().trim(); + Bundle bundle = bundles.get(name); + if (bundle != null) + try { + bundle.start(); + } catch (Exception e) { + warn("Bundle " + name + " cannot be started: " + + e.getMessage()); + } + else + warn("Bundle " + name + " not installed."); + + } + } + + /** Key is location */ + public Map getInstalledBundles() { + Map installedBundles = new HashMap(); + + for (Bundle bundle : bundleContext.getBundles()) { + installedBundles.put(bundle.getLocation(), bundle); + } + return installedBundles; + } + + /** Key is symbolic name */ + public Map getBundles() { + Map installedBundles = new HashMap(); + for (Bundle bundle : bundleContext.getBundles()) + installedBundles.put(bundle.getSymbolicName(), bundle); + return installedBundles; + } + + public List getLocationsUrls() { + String baseUrl = getProperty(PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL); + String bundleLocations = getProperty(PROP_SLC_OSGI_LOCATIONS); + return getLocationsUrls(baseUrl, bundleLocations); + } + + public List getLocationsUrls(String baseUrl, String bundleLocations) { + List urls = new ArrayList(); + + if (bundleLocations == null) + return urls; + bundleLocations = SystemPropertyUtils + .resolvePlaceholders(bundleLocations); + + StringTokenizer st = new StringTokenizer(bundleLocations, + File.pathSeparator); + while (st.hasMoreTokens()) { + urls.add(baseUrl + st.nextToken().trim()); + } + return urls; + } + + public List getBundlesUrls() { + String baseUrl = getProperty(PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL); + String bundlePatterns = getProperty(PROP_SLC_OSGI_BUNDLES); + return getBundlesUrls(baseUrl, bundlePatterns); + } + + public List getBundlesUrls(String baseUrl, String bundlePatterns) { + List urls = new ArrayList(); + + List bundlesSets = new ArrayList(); + if (bundlePatterns == null) + return urls; + // TODO: resolve placeholders + info(PROP_SLC_OSGI_BUNDLES + "=" + bundlePatterns); + + StringTokenizer st = new StringTokenizer(bundlePatterns, ","); + 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(baseUrl + fullPath); + } + + return urls; + } + + protected void match(PathMatcher matcher, List matched, + String base, String currentPath, String pattern) { + if (currentPath == null) { + // Init + File baseDir = new File(base.replace('/', File.separatorChar)); + File[] files = baseDir.listFiles(); + + if (files == null) { + warn("Base dir " + baseDir + " has no children, exists=" + + baseDir.exists() + ", isDirectory=" + + baseDir.isDirectory()); + return; + } + + for (File file : files) + match(matcher, matched, base, file.getName(), pattern); + } else { + String fullPath = base + '/' + currentPath; + if (matched.contains(fullPath)) + return;// don't try deeper if already matched + + boolean ok = matcher.match(pattern, currentPath); + if (debug) + debug(currentPath + " " + (ok ? "" : " not ") + + " matched with " + pattern); + if (ok) { + matched.add(fullPath); + return; + } else { + 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); + } + } + } + } + + protected void info(Object obj) { + System.out.println("# INFO " + obj); + } + + protected void debug(Object obj) { + if (debug) + System.out.println("# DBUG " + obj); + } + + protected void warn(Object obj) { + System.out.println("# WARN " + obj); + // Because of a weird bug under Windows when starting it in a forked VM + // if (System.getProperty("os.name").contains("Windows")) + // System.out.println("# WARN " + obj); + // else + // System.err.println("# WARN " + obj); + } + + protected 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 String getProperty(String name) { + return getProperty(name, null); + } + + public Boolean getDebug() { + return debug; + } + + public void setDebug(Boolean debug) { + this.debug = debug; + } + + public BundleContext getBundleContext() { + return bundleContext; + } + + private class BundlesSet { + private String baseUrl = "reference:file";// not used yet + 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; + } + + } + +} diff --git a/runtime/org.argeo.slc.osgiboot/src/test/bundles/others/subdir/org.argeo.slc.osgiboot.test.bundle3/META-INF/MANIFEST.MF b/runtime/org.argeo.slc.osgiboot/src/test/bundles/others/subdir/org.argeo.slc.osgiboot.test.bundle3/META-INF/MANIFEST.MF new file mode 100644 index 000000000..3019742fc --- /dev/null +++ b/runtime/org.argeo.slc.osgiboot/src/test/bundles/others/subdir/org.argeo.slc.osgiboot.test.bundle3/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Bundle-SymbolicName: org.argeo.slc.osgiboot.test.bundle3 +Bundle-Version: 0.1.0 diff --git a/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/excluded/org.argeo.slc.osgiboot.test.bundle0/META-INF/MANIFEST.MF b/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/excluded/org.argeo.slc.osgiboot.test.bundle0/META-INF/MANIFEST.MF new file mode 100644 index 000000000..5216c2788 --- /dev/null +++ b/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/excluded/org.argeo.slc.osgiboot.test.bundle0/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Bundle-SymbolicName: org.argeo.slc.osgiboot.test.bundle0 +Bundle-Version: 0.1.0 diff --git a/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/org.argeo.slc.osgiboot.test.bundle1/META-INF/MANIFEST.MF b/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/org.argeo.slc.osgiboot.test.bundle1/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2c6c008eb --- /dev/null +++ b/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/org.argeo.slc.osgiboot.test.bundle1/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Bundle-SymbolicName: org.argeo.slc.osgiboot.test.bundle1 +Bundle-Version: 0.1.0 diff --git a/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/org.argeo.slc.osgiboot.test.bundle2/META-INF/MANIFEST.MF b/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/org.argeo.slc.osgiboot.test.bundle2/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2ab63282f --- /dev/null +++ b/runtime/org.argeo.slc.osgiboot/src/test/bundles/some/org.argeo.slc.osgiboot.test.bundle2/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Bundle-SymbolicName: org.argeo.slc.osgiboot.test.bundle2 +Bundle-Version: 0.1.0 diff --git a/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java new file mode 100644 index 000000000..aab5799bd --- /dev/null +++ b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java @@ -0,0 +1,35 @@ +package org.argeo.slc.osgiboot; + +import java.io.File; +import java.util.List; + +import junit.framework.TestCase; + +import org.argeo.slc.osgiboot.OsgiBoot; + +public class OsgiBootNoRuntimeTest extends TestCase { + public final static String BUNDLES = "src/test/bundles/some;in=*;ex=excluded," + + "src/test/bundles/others;in=**/org.argeo.*"; + + public void testLocations() { + String baseUrl = "file:"; + String locations = "/mydir/myfile" + File.pathSeparator + + "/myotherdir/myotherfile"; + + OsgiBoot osgiBoot = new OsgiBoot(null); + List urls = osgiBoot.getLocationsUrls(baseUrl, locations); + assertEquals(2, urls.size()); + assertEquals("file:/mydir/myfile", urls.get(0)); + assertEquals("file:/myotherdir/myotherfile", urls.get(1)); + } + + public void testBundles() { + String baseUrl = "file:"; + String bundles = BUNDLES; + OsgiBoot osgiBoot = new OsgiBoot(null); + List urls = osgiBoot.getBundlesUrls(baseUrl, bundles); + for (int i = 0; i < urls.size(); i++) + System.out.println(urls.get(i)); + assertEquals(3, urls.size()); + } +} diff --git a/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java new file mode 100644 index 000000000..401441c42 --- /dev/null +++ b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java @@ -0,0 +1,66 @@ +package org.argeo.slc.osgiboot; + +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; + +import junit.framework.TestCase; + +import org.eclipse.core.runtime.adaptor.EclipseStarter; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +public class OsgiBootRuntimeTest extends TestCase { + protected OsgiBoot osgiBoot = null; + + public void testInstallAndStart() throws Exception { + osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL, + OsgiBootNoRuntimeTest.BUNDLES)); + Map map = new TreeMap(osgiBoot.getBundles()); + for (Iterator keys = map.keySet().iterator(); keys.hasNext();) { + Object key = keys.next(); + Bundle bundle = (Bundle) map.get(key); + System.out.println(key + " : " + bundle.getLocation()); + } + assertEquals(4, map.size()); + Iterator keys = map.keySet().iterator(); + assertEquals("org.argeo.slc.osgiboot.test.bundle1", keys.next()); + assertEquals("org.argeo.slc.osgiboot.test.bundle2", keys.next()); + assertEquals("org.argeo.slc.osgiboot.test.bundle3", keys.next()); + assertEquals("org.eclipse.osgi", keys.next()); + + osgiBoot.startBundles("org.argeo.slc.osgiboot.test.bundle2"); + long begin = System.currentTimeMillis(); + while (System.currentTimeMillis() - begin < 10000) { + Map mapBundles = osgiBoot.getBundles(); + Bundle bundle = (Bundle) mapBundles + .get("org.argeo.slc.osgiboot.test.bundle2"); + if (bundle.getState() == Bundle.ACTIVE) { + System.out.println("Bundle " + bundle + " started."); + return; + } + } + fail("Bundle not started after timeout limit."); + } + + protected BundleContext startRuntime() throws Exception { + String[] args = { "-console" }; + BundleContext bundleContext = EclipseStarter.startup(args, null); + return bundleContext; + } + + protected void stopRuntime() throws Exception { + EclipseStarter.shutdown(); + } + + public void setUp() throws Exception{ + BundleContext bundleContext = startRuntime(); + osgiBoot = new OsgiBoot(bundleContext); + } + + public void tearDown() throws Exception{ + osgiBoot = null; + stopRuntime(); + } + +} diff --git a/runtime/org.argeo.slc.support.equinox/.classpath b/runtime/org.argeo.slc.support.equinox/.classpath index 30baf1326..6e4caca31 100644 --- a/runtime/org.argeo.slc.support.equinox/.classpath +++ b/runtime/org.argeo.slc.support.equinox/.classpath @@ -2,6 +2,7 @@ + diff --git a/runtime/org.argeo.slc.support.equinox/pom.xml b/runtime/org.argeo.slc.support.equinox/pom.xml index 3677085d6..bc01f0705 100644 --- a/runtime/org.argeo.slc.support.equinox/pom.xml +++ b/runtime/org.argeo.slc.support.equinox/pom.xml @@ -14,7 +14,15 @@ + org.apache.maven.plugins maven-jar-plugin + + + + test-jar + + + org.apache.felix @@ -46,7 +54,7 @@ org.springframework.osgi org.springframework.osgi.core - + org.eclipse.osgi org.eclipse.osgi @@ -65,5 +73,18 @@ org.objectweb.asm com.springsource.org.objectweb.asm.attrs + + + + org.springframework.osgi + org.springframework.osgi.test + test + + + org.argeo.slc.runtime + org.argeo.slc.osgiboot + test + + diff --git a/runtime/org.argeo.slc.support.equinox/src/test/bundles/org.argeo.slc.support.osgi.test.hello/META-INF/MANIFEST.MF b/runtime/org.argeo.slc.support.equinox/src/test/bundles/org.argeo.slc.support.osgi.test.hello/META-INF/MANIFEST.MF new file mode 100644 index 000000000..59453934b --- /dev/null +++ b/runtime/org.argeo.slc.support.equinox/src/test/bundles/org.argeo.slc.support.osgi.test.hello/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Bundle-SymbolicName: org.argeo.slc.support.osgi.test.hello +Bundle-Version: 0.1.0 diff --git a/runtime/org.argeo.slc.support.equinox/src/test/bundles/org.argeo.slc.support.osgi.test.hello/META-INF/spring/hello.xml b/runtime/org.argeo.slc.support.equinox/src/test/bundles/org.argeo.slc.support.osgi.test.hello/META-INF/spring/hello.xml new file mode 100644 index 000000000..1f0f8928f --- /dev/null +++ b/runtime/org.argeo.slc.support.equinox/src/test/bundles/org.argeo.slc.support.osgi.test.hello/META-INF/spring/hello.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTest.java b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTest.java new file mode 100644 index 000000000..5dc773aee --- /dev/null +++ b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTest.java @@ -0,0 +1,60 @@ +package org.argeo.slc.osgi.test; + +import junit.framework.TestCase; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.osgiboot.OsgiBoot; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.springframework.osgi.test.platform.EquinoxPlatform; +import org.springframework.osgi.test.platform.OsgiPlatform; +import org.springframework.osgi.util.OsgiStringUtils; + +public abstract class AbstractOsgiRuntimeTest extends TestCase { + private final static Log log = LogFactory + .getLog(AbstractOsgiRuntimeTest.class); + + protected OsgiBoot osgiBoot = null; + protected OsgiPlatform osgiPlatform = null; + + protected OsgiPlatform createOsgiPlatform() { + return new EquinoxPlatform(); + } + + protected void postStart() throws Exception { + + } + + public void setUp() throws Exception { + // To avoid xerces from the classpath being detected as the provider + System + .setProperty("javax.xml.parsers.DocumentBuilderFactory", + "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); + System.setProperty("javax.xml.parsers.SAXParserFactory", + "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"); + + osgiPlatform = createOsgiPlatform(); + osgiPlatform.start(); + osgiBoot = new OsgiBoot(osgiPlatform.getBundleContext()); + log.info("OSGi platform " + osgiPlatform + " started."); + postStart(); + } + + public void tearDown() throws Exception { + osgiBoot = null; + osgiPlatform.stop(); + osgiPlatform = null; + log.info("OSGi platform " + osgiPlatform + " stopped."); + } + + protected void listInstalledBundles() { + BundleContext bundleContext = osgiPlatform.getBundleContext(); + Bundle[] bundles = bundleContext.getBundles(); + for (int i = 0; i < bundles.length; i++) { + System.out + .println(OsgiStringUtils.nullSafeSymbolicName(bundles[i])); + } + + } +} diff --git a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java new file mode 100644 index 000000000..668cc1d6c --- /dev/null +++ b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java @@ -0,0 +1,23 @@ +package org.argeo.slc.osgi.test; + +import org.argeo.slc.osgiboot.OsgiBoot; + +public class HelloTest extends AbstractOsgiRuntimeTest { + public void testHello() throws Exception { + Thread.sleep(5000); + } + + protected void postStart() throws Exception { + osgiBoot.installUrls(osgiBoot.getLocationsUrls( + OsgiBoot.DEFAULT_BASE_URL, System + .getProperty("java.class.path"))); + osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL, + "src/test/bundles;in=*")); + + listInstalledBundles(); + String bundlesToStart = "org.springframework.osgi.extender,org.argeo.slc.support.osgi.test.hello"; + osgiBoot.startBundles(bundlesToStart); + + } + +} diff --git a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java new file mode 100644 index 000000000..2748e8449 --- /dev/null +++ b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java @@ -0,0 +1,18 @@ +package org.argeo.slc.osgi.test; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; + +public class PlatformStartStopTest extends AbstractOsgiRuntimeTest { + + public void testStartStop() { + BundleContext bundleContext = osgiPlatform.getBundleContext(); + System.out.println(bundleContext + .getProperty(Constants.FRAMEWORK_VENDOR)); + System.out.println(bundleContext + .getProperty(Constants.FRAMEWORK_VERSION)); + System.out.println(bundleContext + .getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT)); + + } +}