From 4490c4b376d6f334faadf3932d6517fea2b57db1 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 11 Mar 2009 04:27:50 +0000 Subject: Add import/export for the test bundles git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@752364 13f79535-47bb-0310-9956-ffa450edef68 --- .../test/java/calculator/CalculatorActivator.java | 24 +++++++++++++++++ .../calculator/test/CalculatorOSGiTestCase.java | 14 ++++++---- .../implementation/osgi/runtime/OSGiTestCase.java | 1 + .../implementation/osgi/test/OSGiTestBundles.java | 31 +++++++++++++--------- .../osgi/xml/OSGiReadImplTestCase.java | 1 + 5 files changed, 54 insertions(+), 17 deletions(-) (limited to 'java/sca/modules/implementation-osgi/src') diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java index b06474cb46..b31a960d14 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java @@ -23,9 +23,14 @@ import java.util.Dictionary; import java.util.Hashtable; import java.util.logging.Logger; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.packageadmin.PackageAdmin; + +import calculator.operations.AddService; /** * @@ -35,11 +40,30 @@ public class CalculatorActivator implements BundleActivator { private ServiceRegistration registration; + private Bundle getBundle(BundleContext bundleContext, Class cls) { + PackageAdmin packageAdmin = null; + // PackageAdmin is used to resolve bundles + ServiceReference ref = bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); + if (ref != null) { + packageAdmin = (PackageAdmin)bundleContext.getService(ref); + Bundle bundle = packageAdmin.getBundle(cls); + if (bundle != null) { + logger.info(cls.getName() + " is loaded by bundle: " + bundle.getSymbolicName()); + } + bundleContext.ungetService(ref); + return bundle; + } + return null; + } + public void start(BundleContext context) throws Exception { Dictionary props = new Hashtable(); props.put("sca.service", "CalculatorComponent#service-name(Calculator)"); logger.info("Registering " + CalculatorService.class.getName()); registration = context.registerService(CalculatorService.class.getName(), new CalculatorServiceImpl(), props); + + getBundle(context, AddService.class); + } public void stop(BundleContext context) throws Exception { diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java index 1d37e69a03..1acd581d22 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java @@ -58,13 +58,16 @@ public class CalculatorOSGiTestCase { Set bundles = new HashSet(); bundles.add(OSGiTestBundles.createBundle("target/test-classes/calculator-bundle.jar", "calculator", - "calculator.operations", + "calculator", + "calculator.operations,org.osgi.service.packageadmin", CalculatorService.class, - CalculatorServiceImpl.class, CalculatorActivator.class)); + CalculatorServiceImpl.class, + CalculatorActivator.class)); bundles.add(OSGiTestBundles.createBundle("target/test-classes/operations-bundle.jar", - "operations", - null, + "calculator.operations", + "calculator.operations", + "calculator.operations", OperationsActivator.class, AddService.class, AddServiceImpl.class, @@ -72,7 +75,8 @@ public class CalculatorOSGiTestCase { SubtractServiceImpl.class, MultiplyService.class, MultiplyServiceImpl.class, - DivideService.class, DivideServiceImpl.class)); + DivideService.class, + DivideServiceImpl.class)); try { host = new EquinoxHost(bundles); BundleContext context = host.start(); diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java index 92d99a04ba..26bf78b395 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java +++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java @@ -53,6 +53,7 @@ public class OSGiTestCase { OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar", OSGiTestInterface.class.getName(), null, + null, OSGiTestImpl.class, OSGiTestInterface.class); node = diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java index 15cbd77c4e..c062aba40b 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java +++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java @@ -48,25 +48,32 @@ public class OSGiTestBundles { return index == -1 ? "" : name.substring(0, index); } - public static URL createBundle(String jarName, String bundleName, String imports, Class... classes) - throws Exception { + public static URL createBundle(String jarName, + String bundleName, + String exports, + String imports, + Class... classes) throws Exception { Class activator = null; Set packages = new HashSet(); - StringBuffer exports = new StringBuffer(); + StringBuffer exportPackages = new StringBuffer(); + if (exports != null) { + exportPackages.append(exports); + } for (Class cls : classes) { if (BundleActivator.class.isAssignableFrom(cls)) { activator = cls; } - if (cls.isInterface()) { + if (exports == null && cls.isInterface()) { String pkg = getPackageName(cls); if (packages.add(pkg)) { - exports.append(pkg).append(","); + exportPackages.append(pkg).append(","); } } } - if (exports.length() > 0) { - exports.deleteCharAt(exports.length() - 1); + int len = exportPackages.length(); + if (len > 0 && exportPackages.charAt(len - 1) == ',') { + exportPackages.deleteCharAt(len - 1); } Manifest manifest = new Manifest(); @@ -76,14 +83,14 @@ public class OSGiTestBundles { manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, bundleName); manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0.0"); manifest.getMainAttributes().putValue(Constants.BUNDLE_NAME, bundleName); - manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, exports.toString()); - String importPackages = null; + manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, exportPackages.toString()); + StringBuffer importPackages = new StringBuffer(); if (imports != null) { - importPackages = "org.osgi.framework," + imports; + importPackages.append(imports).append(",org.osgi.framework"); } else { - importPackages = "org.osgi.framework"; + importPackages.append("org.osgi.framework"); } - manifest.getMainAttributes().putValue(Constants.IMPORT_PACKAGE, importPackages); + manifest.getMainAttributes().putValue(Constants.IMPORT_PACKAGE, importPackages.toString()); if (activator != null) { manifest.getMainAttributes().putValue(Constants.BUNDLE_ACTIVATOR, activator.getName()); diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java index ca458a2d4e..7be96b32c5 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java +++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java @@ -73,6 +73,7 @@ public class OSGiReadImplTestCase { OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar", OSGiTestInterface.class.getName(), null, + null, OSGiTestImpl.class, OSGiTestInterface.class); } -- cgit v1.2.3