diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-16 16:00:02 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-16 16:00:02 +0000 |
commit | 8f59985ebd68c003c646dfd605df1f9183e630af (patch) | |
tree | 7a471352a921fe11b4be6d16fbcc5bd287c03d41 /java | |
parent | 9f0d481f0fe19becc950d7f802e6ac90042ca16f (diff) |
Start to move the runtime pieces into a new implementation-osgi-runtime module
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@754921 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
32 files changed, 46 insertions, 2448 deletions
diff --git a/java/sca/modules/implementation-osgi/pom.xml b/java/sca/modules/implementation-osgi/pom.xml index 06a2bef49d..2ec193e443 100644 --- a/java/sca/modules/implementation-osgi/pom.xml +++ b/java/sca/modules/implementation-osgi/pom.xml @@ -124,6 +124,13 @@ </dependency> <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-node-impl-osgi</artifactId> + <version>2.0-SNAPSHOT</version> + <scope>test</scope> + </dependency> + + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.5</version> diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java deleted file mode 100644 index ecf685c679..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.implementation.osgi.runtime; - -import static org.apache.tuscany.sca.implementation.osgi.runtime.OSGiImplementationManager.isSCABundle; - -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleEvent; -import org.osgi.framework.SynchronousBundleListener; - -/** - * Bundle activator to receive the BundleContext - */ -public class OSGiImplementationActivator implements BundleActivator, SynchronousBundleListener { - private static BundleContext bundleContext; - private boolean inited; - - private void init() { - synchronized (this) { - if (inited) { - return; - } - inited = true; - } - } - - public void start(BundleContext context) throws Exception { - bundleContext = context; - boolean found = false; - for (Bundle b : context.getBundles()) { - if (isSCABundle(b)) { - found = true; - break; - } - } - - if (found) { - init(); - } else { - context.addBundleListener(this); - } - } - - public void stop(BundleContext context) throws Exception { - context.removeBundleListener(this); - bundleContext = null; - } - - public static BundleContext getBundleContext() { - return bundleContext; - } - - public void bundleChanged(BundleEvent event) { - if (event.getType() == BundleEvent.STARTING) { - if (isSCABundle(event.getBundle())) { - // The bundle is an SCA implementation, init implementation.osgi - bundleContext.removeBundleListener(this); - init(); - } - } - - } - -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java deleted file mode 100644 index a6dc7c22cc..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.implementation.osgi.runtime; - -import java.util.Dictionary; -import java.util.Enumeration; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleEvent; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; -import org.osgi.framework.SynchronousBundleListener; - -/** - * Managing the mapping between OSGi bundles and SCA implementation.osgi - */ -public class OSGiImplementationManager implements SynchronousBundleListener, ServiceListener { - private BundleContext bundleContext; - private Map<Bundle, OSGiImplementation> implementations = new ConcurrentHashMap<Bundle, OSGiImplementation>(); - - public OSGiImplementationManager(BundleContext bundleContext) { - super(); - this.bundleContext = bundleContext; - } - - public void start() { - for (Bundle b : bundleContext.getBundles()) { - if ((b.getState() & Bundle.ACTIVE) != 0) { - // Process the active bundles - bundleStarted(b); - } - } - } - - public static boolean isSCABundle(Bundle bundle) { - Dictionary<?, ?> headers = bundle.getHeaders(); - // OSGi RFC 119 SCA - if (headers.get("SCA-Composite") != null) { - return true; - } - Enumeration<?> entries = bundle.getEntryPaths("OSGI-INF/sca/"); - if (entries != null && entries.hasMoreElements()) { - return true; - } - - // OSGi Declarative Services - if (headers.get("Service-Component") != null) { - return true; - } - - // OSGI RFC 124: BluePrint Service - if (headers.get("Bundle-Blueprint") != null) { - return true; - } - - entries = bundle.getEntryPaths("OSGI-INF/blueprint/"); - if (entries != null && entries.hasMoreElements()) { - return true; - } - return false; - } - - private void bundleStarted(Bundle bundle) { - if (!isSCABundle(bundle)) { - return; - } - } - - private void bundleStopping(Bundle bundle) { - OSGiImplementation impl = implementations.remove(bundle); - if (impl == null) { - return; - } - } - - public void serviceChanged(ServiceEvent event) { - } - - public void bundleChanged(BundleEvent event) { - int type = event.getType(); - if (type == BundleEvent.STOPPING) { - bundleStopping(event.getBundle()); - } else if (type == BundleEvent.STARTED) { - bundleStarted(event.getBundle()); - } - } - -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java deleted file mode 100644 index c4788a8865..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java +++ /dev/null @@ -1,850 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.sca.implementation.osgi.runtime; - -import java.lang.reflect.Method; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import java.util.ArrayList; -import java.util.Dictionary; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -import org.apache.tuscany.sca.assembly.Component; -import org.apache.tuscany.sca.assembly.ComponentReference; -import org.apache.tuscany.sca.assembly.ComponentService; -import org.apache.tuscany.sca.assembly.Extensible; -import org.apache.tuscany.sca.assembly.Multiplicity; -import org.apache.tuscany.sca.assembly.Reference; -import org.apache.tuscany.sca.assembly.Service; -import org.apache.tuscany.sca.core.factory.ObjectCreationException; -import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; -import org.apache.tuscany.sca.core.scope.Scope; -import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; -import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; -import org.apache.tuscany.sca.interfacedef.Interface; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.interfacedef.java.JavaInterface; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.provider.ImplementationProvider; -import org.apache.tuscany.sca.runtime.EndpointReference; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeComponentService; -import org.apache.tuscany.sca.runtime.RuntimeWire; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleEvent; -import org.osgi.framework.BundleException; -import org.osgi.framework.BundleListener; -import org.osgi.framework.Constants; -import org.osgi.framework.FrameworkEvent; -import org.osgi.framework.FrameworkListener; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; -import org.osgi.framework.ServiceReference; -import org.osgi.service.packageadmin.PackageAdmin; - -/** - * The runtime instantiation of OSGi component implementations - * - * @version $Rev$ $Date$ - */ -public class OSGiImplementationProvider implements ImplementationProvider, FrameworkListener, BundleListener { - - private static final String COMPONENT_SERVICE_NAME = "component.service.name"; - - // Maximum milliseconds to wait for a method to complete - private static final long METHOD_TIMEOUT_MILLIS = 60000; - // Maximum milliseconds to wait for services to be registered into OSGi service registry - private static final long SERVICE_TIMEOUT_MILLIS = 300000; - - private OSGiImplementation implementation; - private Hashtable<RuntimeWire, Reference> referenceWires = new Hashtable<RuntimeWire, Reference>(); - private Hashtable<RuntimeWire, ComponentReference> componentReferenceWires = - new Hashtable<RuntimeWire, ComponentReference>(); - private HashSet<RuntimeWire> resolvedWires = new HashSet<RuntimeWire>(); - private boolean wiresResolved; - - private AtomicInteger startBundleEntryCount = new AtomicInteger(); - - private Hashtable<String, Object> componentProperties = new Hashtable<String, Object>(); - private RuntimeComponent runtimeComponent; - - Bundle osgiBundle; - private ArrayList<Bundle> dependentBundles = new ArrayList<Bundle>(); - private OSGiServiceListener osgiServiceListener; - private PackageAdmin packageAdmin; - - private ProxyFactoryExtensionPoint proxyFactoryExtensionPoint; - - private boolean packagesRefreshed; - - public OSGiImplementationProvider(RuntimeComponent component, - OSGiImplementation impl, - ProxyFactoryExtensionPoint proxyFactoryExtensionPoint) throws BundleException { - - this.implementation = impl; - this.runtimeComponent = component; - this.proxyFactoryExtensionPoint = proxyFactoryExtensionPoint; - - BundleContext bundleContext = OSGiImplementationActivator.getBundleContext(); - osgiBundle = (Bundle)implementation.getBundle(); - bundleContext.addBundleListener(this); - osgiServiceListener = new OSGiServiceListener(osgiBundle); - bundleContext.addServiceListener(osgiServiceListener); - - // PackageAdmin is used to resolve bundles - org.osgi.framework.ServiceReference packageAdminReference = - bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); - if (packageAdminReference != null) { - packageAdmin = (PackageAdmin)bundleContext.getService(packageAdminReference); - bundleContext.addFrameworkListener(this); - } - - } - - protected RuntimeComponent getRuntimeComponent() { - return runtimeComponent; - } - - protected OSGiImplementation getImplementation() { - return implementation; - } - - private String getOSGiFilter(Hashtable<String, Object> props) { - - String filter = ""; - - if (props != null && props.size() > 0) { - int propCount = 0; - for (String propName : props.keySet()) { - if (propName.equals("service.pid")) - continue; - filter = filter + "(" + propName + "=" + props.get(propName) + ")"; - propCount++; - } - - if (propCount > 1) - filter = "(&" + filter + ")"; - } else - filter = null; - return filter; - } - - /* - * Return a matching service registered by the specified bundle. - * If <implementation.osgi /> has the attribute filter defined, return a service - * reference that matches the filter. Otherwise, return a service which has a component - * name equal to this component's name. If not found, return a service which no - * component name set. - * - * Even though services registered by this bundle can be filtered using the - * service listener, we use this method to filter all service references so that - * the service matching functionality of OSGi can be directly used. - */ - private ServiceReference getOSGiServiceReference(String scaServiceName, - String osgiServiceName, - String filter) throws InvalidSyntaxException { - - String compServiceName = runtimeComponent.getName() + "/" + scaServiceName; - if (filter != null && filter.length() > 0) { - org.osgi.framework.ServiceReference[] references = - osgiBundle.getBundleContext().getServiceReferences(osgiServiceName, filter); - - ServiceReference reference = null; - if (references != null) { - for (ServiceReference ref : references) { - if (ref.getBundle() != osgiBundle) - continue; - Object compName = ref.getProperty(COMPONENT_SERVICE_NAME); - if (compName == null && reference == null) - reference = ref; - if (scaServiceName == null || compServiceName.equals(compName)) { - reference = ref; - break; - } - } - } - - return reference; - - } - - filter = scaServiceName == null ? null : "(" + COMPONENT_SERVICE_NAME + "=" + compServiceName + ")"; - - ServiceReference[] references = - osgiBundle.getBundleContext().getServiceReferences(osgiServiceName, filter); - - if (references != null) { - for (ServiceReference ref : references) { - if (ref.getBundle() == osgiBundle) { - return ref; - } - } - } - - references = osgiBundle.getBundleContext().getServiceReferences(osgiServiceName, null); - - ServiceReference reference = null; - - if (references != null) { - for (ServiceReference ref : references) { - - if (ref.getBundle() != osgiBundle) - continue; - Object compName = ref.getProperty(COMPONENT_SERVICE_NAME); - if (compName == null && reference == null) - reference = ref; - if (compServiceName.equals(compName)) { - reference = ref; - break; - } - } - } - - return reference; - } - - /** - * This method is used to avoid full synchronization of methods which should - * be executed only once. - * - * entryCount=0: The count is incremented, and this thread executes the method. Returns true. - * - * entryCount=1: Another thread is already executing this method. - * Wait for the thread to complete if doWait is true. Returns false. - * - * entryCount=2: The method has already been executed. Returns false. - * - * @param doWait If true, and another method is executing this method - * wait for method execution to complete - * @param entryCount Atomic integer used to ensure that the method is - * executed only once - * @return true if this thread has exclusive access to execute this method - */ - private boolean enterMethod(boolean doWait, AtomicInteger entryCount) { - - if (entryCount.compareAndSet(0, 1)) { - return true; - } else { - if (doWait) { - synchronized (entryCount) { - if (entryCount.get() != 2) { - try { - entryCount.wait(METHOD_TIMEOUT_MILLIS); - } catch (InterruptedException e) { - } - } - } - } - return false; - } - } - - /** - * Called on method exit of methods which were entered after - * enterMethod returned true. Increments entryCount, and wakes - * up threads waiting for the method to complete. - * - * @param entryCount Atomic integer used for synchronization - */ - private void exitMethod(AtomicInteger entryCount) { - entryCount.compareAndSet(1, 2); - synchronized (entryCount) { - entryCount.notifyAll(); - } - } - - protected Bundle startBundle(boolean doWait) throws ObjectCreationException { - - try { - - if (enterMethod(doWait, startBundleEntryCount)) { - - configurePropertiesUsingConfigAdmin(); - - resolveBundle(); - - for (Bundle bundle : dependentBundles) { - try { - if (bundle.getState() != Bundle.ACTIVE && bundle.getState() != Bundle.STARTING) { - bundle.start(); - } - } catch (BundleException e) { - if (bundle.getHeaders().get("Fragment-Host") == null) - throw e; - } - } - - } - - if (osgiBundle.getState() != Bundle.ACTIVE && osgiBundle.getState() != Bundle.STARTING) { - - int retry = 0; - - while (retry++ < 10) { - try { - AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { - public Object run() throws BundleException { - osgiBundle.start(); - return null; - } - }); - break; - // } catch ( BundleException e) { - } catch (PrivilegedActionException e) { - // It is possible that the thread "Refresh Packages" is in the process of - // changing the state of this bundle. - Thread.yield(); - - if (retry == 10) - throw e; - } - } - } - - } catch (Exception e) { - throw new ObjectCreationException(e); - } finally { - exitMethod(startBundleEntryCount); - } - return osgiBundle; - } - - // This method is called by OSGiInstanceWrapper.getInstance to obtain the OSGi service reference - // corresponding to the specified service. The properties used to filter the service should - // be chosen based on whether this is a normal service or a callback. - /** - * @param component - * @param service - * @return - */ - protected ServiceReference getOSGiServiceReference(Component component, ComponentService service) - throws ObjectCreationException { - - Hashtable<String, Object> props = getOSGiProperties(service); - - String filter = getOSGiFilter(props); - Interface serviceInterface = service.getInterfaceContract().getInterface(); - String scaServiceName = service.getName(); - - return getOSGiServiceReference(serviceInterface, filter, scaServiceName); - - } - - /** - * Get all the OSGi properties from the extension list - * @param extensible - * @return - */ - private Hashtable<String, Object> getOSGiProperties(Extensible extensible) { - Hashtable<String, Object> props = new Hashtable<String, Object>(); - for (Object ext : extensible.getExtensions()) { - if (ext instanceof OSGiProperty) { - OSGiProperty p = (OSGiProperty)ext; - props.put(p.getName(), p.getValue()); - } - } - return props; - } - - protected ServiceReference getOSGiServiceReference(EndpointReference from, Interface callbackInterface) - throws ObjectCreationException { - - RuntimeWire refWire = null; - String filter = null; - for (RuntimeWire wire : referenceWires.keySet()) { - if (wire.getSource() == from) { - refWire = wire; - break; - } - } - if (refWire != null) { - ComponentReference scaRef = componentReferenceWires.get(refWire); - Hashtable<String, Object> props = getOSGiProperties(scaRef); - filter = getOSGiFilter(props); - } - - return getOSGiServiceReference(callbackInterface, filter, null); - } - - private ServiceReference getOSGiServiceReference(Interface serviceInterface, String filter, String scaServiceName) - throws ObjectCreationException { - - try { - - String serviceInterfaceName = null; - - ServiceReference osgiServiceReference = null; - - if (serviceInterface instanceof JavaInterface) { - serviceInterfaceName = ((JavaInterface)serviceInterface).getJavaClass().getName(); - - if ((osgiServiceReference = getOSGiServiceReference(scaServiceName, serviceInterfaceName, filter)) == null) { - - // The service listener for our bundle will notify us when the service is registered. - synchronized (implementation) { - - // When declarative services are used, the component is started asynchronously - // So this thread has to wait for the service to be registered by the component - // activate method - // For regular bundle activators, bundle.start activates the bundle synchronously - // and hence the service would probably have been started by the bundle activator - long startTime = System.currentTimeMillis(); - while ((osgiServiceReference = - getOSGiServiceReference(scaServiceName, serviceInterfaceName, filter)) == null) { - - // Wait for the bundle to register the service - implementation.wait(100); - if (System.currentTimeMillis() - startTime > SERVICE_TIMEOUT_MILLIS) - break; - } - } - - } - } - - return osgiServiceReference; - - } catch (Exception e) { - throw new ObjectCreationException(e); - } - } - - /** - * For OSGi->Java wires, create a proxy corresponding to the Java interfaces - * and register the proxy with the OSGi registry, so that the source OSGi bundle can - * locate the target Java instance from the registry like a regular OSGi service. - * - * For OSGi->OSGi wires, start the target OSGi bundle, so that references of the - * target are resolved before the source OSGi bundle is started. If the reference - * has properties specified, create a Proxy and register a service with highest - * possible ranking. The Proxy should wire to the correct OSGi instance specified - * in the SCA composite. - * - * The first phase determines whether a proxy should be installed. It also registers - * a dummy bundle if necessary to resolve the bundle. When phase1 is completed on all - * wires of the component, the bundle should be resolved. Phase2 registers the proxy service. - */ - private boolean resolveWireResolveReferences(Bundle bundle, - Class<?> interfaceClass, - RuntimeWire wire, - boolean isOSGiToOSGiWire) throws Exception { - - // FIXME: At the moment injection of values into instances require an instance to be obtained - // through the instance wrapper, and hence requires a proxy. When we do this processing here, - // we don't yet know whether the target requires any property or callback injection. So it is - // safer to create a proxy all the time. - boolean createProxy = true; - - ComponentReference scaRef = componentReferenceWires.get(wire); - Hashtable<String, Object> targetProperties = getOSGiProperties(scaRef); - - if (isOSGiToOSGiWire) { - - OSGiImplementationProvider implProvider = - (OSGiImplementationProvider)wire.getTarget().getComponent().getImplementationProvider(); - - // This is an OSGi->OSGi wire - isOSGiToOSGiWire = true; - - // If the target component is stateless, use a proxy to create a new service each time - if (!implProvider.getScope().equals(Scope.COMPOSITE)) - createProxy = true; - - Interface interfaze = wire.getTarget().getInterfaceContract().getInterface(); - - // If the target interface is remotable, create a proxy to support pass-by-value semantics - // AllowsPassByReference is not detected until the target instance is obtained. - if (interfaze.isRemotable()) - createProxy = true; - - // If any of the operations in the target interface is non-blocking, create a proxy - List<Operation> ops = interfaze.getOperations(); - for (Operation op : ops) { - if (op.isNonBlocking()) - createProxy = true; - } - - // If properties are specified for the reference, create a proxy since rewiring may be required - if (targetProperties.size() > 0) { - createProxy = true; - } - - // If properties are specified for the component, create a proxy for configuring - // the component services. - if (componentProperties.size() > 0) { - createProxy = true; - } - - // Since this is an OSGi->OSGi wire, start the target bundle before starting the - // source bundle if there is no proxy. For direct wiring without a proxy, this ordering - // is irrelevant in terms of class resolution, but the target needs to be started at some - // point. But there is no opportunity later on to start the target OSGi bundle without a proxy. - // When a Proxy is used, the target bundle needs to be resolved for the source bundle - // to be resolved so that the interface is visible to the source. In this case the bundle - // will be started when an instance is needed. - if (!createProxy) { - implProvider.startBundle(false); - } else { - implProvider.resolveBundle(); - } - } else { - createProxy = true; - } - - return createProxy; - } - - // Register proxy service - private void resolveWireRegisterProxyService(final Bundle bundle, final Class interfaceClass, RuntimeWire wire) - throws Exception { - - ComponentReference scaRef = componentReferenceWires.get(wire); - Hashtable<String, Object> targetProperties = getOSGiProperties(scaRef); - targetProperties.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE); - - if (targetProperties.get(COMPONENT_SERVICE_NAME) == null && wire.getTarget().getComponent() != null) { - String compServiceName = - wire.getTarget().getComponent().getName() + "/" + wire.getTarget().getContract().getName(); - targetProperties.put(COMPONENT_SERVICE_NAME, compServiceName); - } - - ProxyFactory proxyService = proxyFactoryExtensionPoint.getInterfaceProxyFactory(); - if (!interfaceClass.isInterface()) { - proxyService = proxyFactoryExtensionPoint.getClassProxyFactory(); - } - - // Allow privileged access to load classes. Requires getClassLoader permission in security - // policy. - final Class<?> proxyInterface = AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() { - public Class<?> run() throws Exception { - return bundle.loadClass(interfaceClass.getName()); - } - }); - - final Object proxy = proxyService.createProxy(proxyInterface, wire); - final Hashtable<String, Object> finalTargetProperties = targetProperties; - AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { - public Object run() throws Exception { - osgiBundle.getBundleContext().registerService(proxyInterface.getName(), proxy, finalTargetProperties); - return null; - } - }); - - } - - private void refreshPackages() { - - if (packageAdmin != null) { - synchronized (this) { - packagesRefreshed = false; - packageAdmin.refreshPackages(null); - - if (!packagesRefreshed) { - try { - this.wait(2000); - } catch (InterruptedException e) { - // ignore - } - } - packagesRefreshed = false; - } - } - } - - private void resolveBundle() throws ObjectCreationException { - - try { - - if (!wiresResolved) { - wiresResolved = true; - - if (!setReferencesAndProperties()) { - wiresResolved = false; - return; - } - - int refPlusServices = referenceWires.size() + runtimeComponent.getServices().size(); - boolean[] createProxyService = new boolean[refPlusServices]; - Class<?>[] interfaceClasses = new Class<?>[refPlusServices]; - boolean[] isOSGiToOSGiWire = new boolean[refPlusServices]; - boolean[] wireResolved = new boolean[refPlusServices]; - int index = 0; - for (RuntimeWire wire : referenceWires.keySet()) { - - Reference reference = referenceWires.get(wire); - - isOSGiToOSGiWire[index] = - wire.getTarget().getComponent() != null && wire.getTarget().getComponent() - .getImplementationProvider() instanceof OSGiImplementationProvider; - - Interface refInterface = reference.getInterfaceContract().getInterface(); - if (refInterface instanceof JavaInterface) { - interfaceClasses[index] = ((JavaInterface)refInterface).getJavaClass(); - - // if (!isOSGiToOSGiWire[index]) - // resolveWireCreateDummyBundles(interfaceClasses[index]); - - } - - if (!resolvedWires.contains(wire)) { - resolvedWires.add(wire); - } else - wireResolved[index] = true; - - index++; - } - for (ComponentService service : runtimeComponent.getServices()) { - Interface callbackInterface = service.getInterfaceContract().getCallbackInterface(); - if (callbackInterface instanceof JavaInterface) { - interfaceClasses[index] = ((JavaInterface)callbackInterface).getJavaClass(); - -// resolveWireCreateDummyBundles(interfaceClasses[index]); - } - - index++; - } - - index = 0; - for (RuntimeWire wire : referenceWires.keySet()) { - - if (!wireResolved[index]) { - createProxyService[index] = - resolveWireResolveReferences(osgiBundle, - interfaceClasses[index], - wire, - isOSGiToOSGiWire[index]); - } - index++; - } - - refreshPackages(); - - index = 0; - for (RuntimeWire wire : referenceWires.keySet()) { - - if (createProxyService[index] && !wireResolved[index]) - resolveWireRegisterProxyService(osgiBundle, interfaceClasses[index], wire); - index++; - } - } else if (osgiBundle.getState() == Bundle.INSTALLED && packageAdmin != null) { - packageAdmin.resolveBundles(new Bundle[] {osgiBundle}); - } - - } catch (Exception e) { - e.printStackTrace(); - throw new ObjectCreationException(e); - } - } - - @SuppressWarnings("unchecked") - private void configurePropertiesUsingConfigAdmin() { - - try { - - if (componentProperties.size() == 0) - return; - - ServiceReference configAdminReference = - osgiBundle.getBundleContext().getServiceReference("org.osgi.service.cm.ConfigurationAdmin"); - if (configAdminReference != null) { - - Object cm = osgiBundle.getBundleContext().getService(configAdminReference); - Class cmClass = cm.getClass().getClassLoader().loadClass("org.osgi.service.cm.ConfigurationAdmin"); - Method getConfigMethod = cmClass.getMethod("getConfiguration", String.class, String.class); - - Class configClass = cm.getClass().getClassLoader().loadClass("org.osgi.service.cm.Configuration"); - - Method getMethod = configClass.getMethod("getProperties"); - Method updateMethod = configClass.getMethod("update", Dictionary.class); - - List<Service> services = implementation.getServices(); - HashSet<String> pidsProcessed = new HashSet<String>(); - - for (Service service : services) { - - Hashtable<String, Object> properties = getOSGiProperties(service); - String pid = null; - - if (properties != null) { - for (Map.Entry<String, Object> prop : properties.entrySet()) { - pid = (String)prop.getValue(); - } - } - if (pid == null || pidsProcessed.contains(pid)) - continue; - - Object config = getConfigMethod.invoke(cm, pid, null); - Dictionary props = (Dictionary)getMethod.invoke(config); - if (props == null) { - props = new Hashtable<String, Object>(); - } - for (String propertyName : componentProperties.keySet()) { - - props.put(propertyName, componentProperties.get(propertyName)); - } - - updateMethod.invoke(config, props); - - } - - } - - } catch (Exception e) { - e.printStackTrace(); - } - - } - - public boolean isOptimizable() { - return false; - } - - public Scope getScope() { - return Scope.COMPOSITE; - } - - public boolean isEagerInit() { - return false; - } - - public long getMaxAge() { - return 0l; - } - - public long getMaxIdleTime() { - return 0l; - } - - public Invoker createTargetInvoker(RuntimeComponentService service, Operation operation) { - Interface serviceInterface = operation.getInterface(); - Invoker invoker = new OSGiTargetInvoker(operation, this, service); - return invoker; - } - - public Invoker createInvoker(RuntimeComponentService service, Operation operation) { - return createTargetInvoker(service, operation); - } - - public boolean supportsOneWayInvocation() { - return false; - } - - private boolean setReferencesAndProperties() { - - for (Reference ref : implementation.getReferences()) { - List<RuntimeWire> wireList = null; - ComponentReference compRef = null; - for (ComponentReference cRef : runtimeComponent.getReferences()) { - if (cRef.getName().equals(ref.getName())) { - - wireList = ((RuntimeComponentReference)cRef).getRuntimeWires(); - - compRef = cRef; - break; - } - } - - if (ref.getMultiplicity() == Multiplicity.ONE_N || ref.getMultiplicity() == Multiplicity.ZERO_N) { - for (RuntimeWire wire : wireList) { - referenceWires.put(wire, ref); - componentReferenceWires.put(wire, compRef); - } - - } else { - if (wireList == null && ref.getMultiplicity() == Multiplicity.ONE_ONE) { - throw new IllegalStateException("Required reference is missing: " + ref.getName()); - } - if (wireList != null && !wireList.isEmpty()) { - RuntimeWire wire = wireList.get(0); - referenceWires.put(wire, ref); - componentReferenceWires.put(wire, compRef); - } - - } - - } - - componentProperties = getOSGiProperties(runtimeComponent); - - return true; - - } - - public void start() { - setReferencesAndProperties(); - } - - public void stop() { - - if (osgiServiceListener != null) { - OSGiImplementationActivator.getBundleContext().removeServiceListener(osgiServiceListener); - } - } - - public void frameworkEvent(FrameworkEvent event) { - if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) { - synchronized (this) { - packagesRefreshed = true; - this.notifyAll(); - } - } - - } - - public void bundleChanged(BundleEvent event) { - if (event.getType() == BundleEvent.RESOLVED && event.getBundle() == osgiBundle) { - // TODO - } - } - - private class OSGiServiceListener implements ServiceListener { - - private Bundle bundle; - - OSGiServiceListener(Bundle bundle) { - this.bundle = bundle; - } - - public void serviceChanged(org.osgi.framework.ServiceEvent event) { - - ServiceReference reference = event.getServiceReference(); - - if (event.getType() == ServiceEvent.REGISTERED && reference.getBundle() == bundle) { - - synchronized (implementation) { - - implementation.notifyAll(); - } - } - - if (event.getType() == ServiceEvent.UNREGISTERING && reference.getBundle() == bundle) { - // TODO: Process deregistering of OSGi services. - } - } - } -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java deleted file mode 100644 index 0e4a3ef995..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.sca.implementation.osgi.runtime; - -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; -import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; -import org.apache.tuscany.sca.provider.ImplementationProvider; -import org.apache.tuscany.sca.provider.ImplementationProviderFactory; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.osgi.framework.BundleException; - -/** - * Builds a OSGi-based implementation provider from a component definition - * - * @version $Rev$ $Date$ - */ -public class OSGiImplementationProviderFactory implements ImplementationProviderFactory<OSGiImplementation> { - - private ProxyFactoryExtensionPoint proxyFactoryExtensionPoint; - - public OSGiImplementationProviderFactory(ExtensionPointRegistry extensionPoints) { - proxyFactoryExtensionPoint = extensionPoints.getExtensionPoint(ProxyFactoryExtensionPoint.class); - } - - public ImplementationProvider createImplementationProvider(RuntimeComponent component, - OSGiImplementation implementation) { - - try { - - return new OSGiImplementationProvider(component, implementation, proxyFactoryExtensionPoint); - - } catch (BundleException e) { - throw new RuntimeException(e); - } - - } - - public Class<OSGiImplementation> getModelType() { - return OSGiImplementation.class; - } - -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTargetInvoker.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTargetInvoker.java deleted file mode 100644 index f6e2eb09ed..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTargetInvoker.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.implementation.osgi.runtime; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import org.apache.tuscany.sca.core.factory.InstanceWrapper; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.interfacedef.java.JavaInterface; -import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentService; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - -/** - * Java->OSGi references use OSGiTargetInvoker to call methods from OSGi bundles - * OSGi->Java references use JDKProxyService and invocation handler and do not use this class - * OSGi->OSGi references go through OSGi reference mechanisms when a proxy is not used - * When a proxy is used, this invoker is used to call methods from OSGi bundles - * A proxy is used for OSGi->OSGi if - * 1) target reference properties are specified OR - * 2) there are one or more non-blocking methods in the target interface OR - * 3) scope is not COMPOSITE - * - * @version $Rev$ $Date$ - */ -public class OSGiTargetInvoker<T> implements Invoker { - - private Operation operation; - protected InstanceWrapper<T> target; - - private final OSGiImplementationProvider provider; - private final RuntimeComponent component; - private final RuntimeComponentService service; - - public OSGiTargetInvoker(Operation operation, OSGiImplementationProvider provider, RuntimeComponentService service) { - - this.operation = operation; - this.service = service; - this.provider = provider; - this.component = provider.getRuntimeComponent(); - - } - - private Object invokeTarget(Message msg) throws InvocationTargetException { - - Operation op = msg.getOperation(); - if (op == null) { - op = this.operation; - } - - try { - BundleContext bundleContext = provider.getImplementation().getBundle().getBundleContext(); - JavaInterface javaInterface = (JavaInterface)op.getInterface(); - // FIXME: What is the filter? - String filter = "(sca.service=" + component.getURI() + "#service-name\\(" + service.getName() + "\\))"; - ServiceReference[] refs = bundleContext.getServiceReferences(javaInterface.getName(), filter); - Object instance = bundleContext.getService(refs[0]); - Method m = JavaInterfaceUtil.findMethod(instance.getClass(), operation); - - Object ret = invokeMethod(instance, m, msg); - - return ret; - } catch (InvocationTargetException e) { - throw e; - } catch (Exception e) { - throw new InvocationTargetException(e); - } - } - - protected Object invokeMethod(Object instance, Method m, Message msg) throws InvocationTargetException { - - try { - - Object payload = msg.getBody(); - - if (payload != null && !payload.getClass().isArray()) { - return m.invoke(instance, payload); - } else { - return m.invoke(instance, (Object[])payload); - } - - } catch (InvocationTargetException e) { - throw e; - } catch (Exception e) { - throw new InvocationTargetException(e); - } - } - - public Message invoke(Message msg) { - try { - Object resp = invokeTarget(msg); - msg.setBody(resp); - } catch (InvocationTargetException e) { - msg.setFaultBody(e.getCause()); - } - return msg; - } - -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiServiceTracker.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationActivator.java index c7fd3058dc..ce7b1dc920 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiServiceTracker.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationActivator.java @@ -17,24 +17,31 @@ * under the License. */ -package org.apache.tuscany.sca.implementation.osgi.runtime; +package org.apache.tuscany.sca.implementation.osgi.xml; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTrackerCustomizer; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; /** - * A ServiceTracker + * Bundle activator to receive the BundleContext */ -public class OSGiServiceTracker implements ServiceTrackerCustomizer { +public class OSGiImplementationActivator implements BundleActivator { + private static BundleContext bundleContext; - public Object addingService(ServiceReference serviceReference) { - return null; + public void start(BundleContext context) throws Exception { + bundleContext = context; } - public void modifiedService(ServiceReference serviceReference, Object service) { + public void stop(BundleContext context) throws Exception { + bundleContext = null; } - public void removedService(ServiceReference serviceReference, Object service) { + static BundleContext getBundleContext() { + return bundleContext; + } + + static void setBundleContext(BundleContext bundleContext) { + OSGiImplementationActivator.bundleContext = bundleContext; } } diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java index cf401ba630..fca5f8fd96 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java @@ -19,13 +19,12 @@ package org.apache.tuscany.sca.implementation.osgi.xml; import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; import static org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.BUNDLE_SYMBOLICNAME; import static org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.BUNDLE_VERSION; import static org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.IMPLEMENTATION_OSGI; -import java.util.ArrayList; import java.util.List; -import java.util.StringTokenizer; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; @@ -46,11 +45,8 @@ import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; -import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; -import org.apache.tuscany.sca.implementation.osgi.runtime.OSGiImplementationActivator; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; -import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.Problem; import org.apache.tuscany.sca.monitor.Problem.Severity; @@ -67,38 +63,16 @@ import org.osgi.framework.Version; * @version $Rev$ $Date$ */ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiImplementation> { - private JavaInterfaceFactory javaInterfaceFactory; private AssemblyFactory assemblyFactory; - private FactoryExtensionPoint modelFactories; + // private FactoryExtensionPoint modelFactories; private OSGiImplementationFactory osgiImplementationFactory; private Monitor monitor; public OSGiImplementationProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) { this.monitor = monitor; - this.modelFactories = modelFactories; + // this.modelFactories = modelFactories; this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); this.osgiImplementationFactory = modelFactories.getFactory(OSGiImplementationFactory.class); - this.javaInterfaceFactory = modelFactories.getFactory(JavaInterfaceFactory.class); - } - - /** - * Report a exception. - * - * @param problems - * @param message - * @param model - */ - private void error(String message, Object model, Exception ex) { - if (monitor != null) { - Problem problem = - monitor.createProblem(this.getClass().getName(), - "impl-osgi-validation-messages", - Severity.ERROR, - model, - message, - ex); - monitor.problem(problem); - } } /** @@ -129,43 +103,32 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm return OSGiImplementation.class; } - private String[] tokenize(String str) { - StringTokenizer tokenizer = new StringTokenizer(str); - String[] tokens = new String[tokenizer.countTokens()]; - for (int i = 0; i < tokens.length; i++) { - tokens[i] = tokenizer.nextToken(); - } - - return tokens; - } - public OSGiImplementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { assert IMPLEMENTATION_OSGI.equals(reader.getName()); String bundleSymbolicName = reader.getAttributeValue(null, BUNDLE_SYMBOLICNAME); String bundleVersion = reader.getAttributeValue(null, BUNDLE_VERSION); - List<OSGiProperty> refProperties = new ArrayList<OSGiProperty>(); - List<OSGiProperty> serviceProperties = new ArrayList<OSGiProperty>(); - List<OSGiProperty> refCallbackProperties = new ArrayList<OSGiProperty>(); - List<OSGiProperty> serviceCallbackProperties = new ArrayList<OSGiProperty>(); - - // Skip to the end of <implementation.osgi> - while (reader.hasNext()) { - int next = reader.next(); - if (next == END_ELEMENT && IMPLEMENTATION_OSGI.equals(reader.getName())) { - break; - } - } - OSGiImplementation implementation = osgiImplementationFactory.createOSGiImplementation(); implementation.setBundleSymbolicName(bundleSymbolicName); implementation.setBundleVersion(bundleVersion); implementation.setUnresolved(true); + // Skip to the end of <implementation.osgi> + while (reader.hasNext()) { + int next = reader.next(); + switch (next) { + case START_ELEMENT: + break; + case END_ELEMENT: + if (IMPLEMENTATION_OSGI.equals(reader.getName())) { + return implementation; + } + break; + } + } return implementation; - } public void resolve(OSGiImplementation impl, ModelResolver resolver) throws ContributionResolveException { @@ -217,14 +180,12 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm if (javaInterface.getJavaClass() == null) { javaInterface.setJavaClass(getJavaClass(resolver, javaInterface.getName())); } - Class<?> callback = null; if (service.getInterfaceContract().getCallbackInterface() instanceof JavaInterface) { JavaInterface callbackInterface = (JavaInterface)service.getInterfaceContract().getCallbackInterface(); if (callbackInterface.getJavaClass() == null) { callbackInterface.setJavaClass(getJavaClass(resolver, callbackInterface.getName())); } - callback = callbackInterface.getJavaClass(); } impl.getServices().add(service); 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 deleted file mode 100644 index 1489ae2b07..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package calculator; - -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.service.packageadmin.PackageAdmin; - -import calculator.operations.AddService; - -/** - * - */ -public class CalculatorActivator implements BundleActivator { - private Logger logger = Logger.getLogger(CalculatorActivator.class.getName()); - - 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 { - logger.info("Starting " + context.getBundle()); - Dictionary<String, Object> props = new Hashtable<String, Object>(); - props.put("sca.service", "CalculatorComponent#service-name(Calculator)"); - props.put("calculator", "Calculator"); - logger.info("Registering " + CalculatorService.class.getName()); - CalculatorService calculator = new CalculatorServiceImpl(context); - context.registerService(CalculatorService.class.getName(), calculator, props); - - getBundle(context, AddService.class); - - } - - public void stop(BundleContext context) throws Exception { - logger.info("Stopping " + context.getBundle()); - // Registered services will be automatically unregistered - } - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorService.java deleted file mode 100644 index 0f7dec116f..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorService.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator; - -import java.rmi.Remote; -import java.rmi.RemoteException; - -import org.oasisopen.sca.annotation.Remotable; - -/** - * The Calculator service interface. - */ -@Remotable -public interface CalculatorService extends Remote { - - double add(double n1, double n2) throws RemoteException; - - double subtract(double n1, double n2) throws RemoteException; - - double multiply(double n1, double n2) throws RemoteException; - - double divide(double n1, double n2) throws RemoteException; -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceDSImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceDSImpl.java deleted file mode 100644 index 7f9fa6803c..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceDSImpl.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator; - -import org.osgi.service.component.ComponentContext; - -import calculator.operations.AddService; -import calculator.operations.DivideService; -import calculator.operations.MultiplyService; -import calculator.operations.SubtractService; - -/** - * An implementation of the Calculator service. - */ -public class CalculatorServiceDSImpl implements CalculatorService { - private AddService addService; - private SubtractService subtractService; - private MultiplyService multiplyService; - private DivideService divideService; - - public CalculatorServiceDSImpl() { - super(); - System.out.println("CalculatorServiceDSImpl()"); - } - - protected void activate(ComponentContext context) { - System.out.println("Activating " + context); - } - - protected void deactivate(ComponentContext context) { - System.out.println("Deactivating " + context); - } - - /* - * The following setters can be used for DS injection - */ - public void setAddService(AddService addService) { - System.out.println("setAddService()"); - this.addService = addService; - } - - public void setSubtractService(SubtractService subtractService) { - this.subtractService = subtractService; - } - - public void setDivideService(DivideService divideService) { - this.divideService = divideService; - } - - public void setMultiplyService(MultiplyService multiplyService) { - this.multiplyService = multiplyService; - } - - /* - * The following setters can be used for DS injection - */ - public void unsetAddService(AddService addService) { - System.out.println("unsetAddService()"); - this.addService = null; - } - - public void unsetSubtractService(SubtractService subtractService) { - this.subtractService = null; - } - - public void unsetDivideService(DivideService divideService) { - this.divideService = null; - } - - public void unsetMultiplyService(MultiplyService multiplyService) { - this.multiplyService = null; - } - private <T> T getService(Class<T> cls) { - for (Object s : new Object[] {addService, subtractService, multiplyService, divideService}) { - if (cls.isInstance(s)) { - return cls.cast(s); - } - } - throw new IllegalStateException(cls.getSimpleName() + " is not available"); - } - - public double add(double n1, double n2) { - return getService(AddService.class).add(n1, n2); - } - - public double subtract(double n1, double n2) { - return getService(SubtractService.class).subtract(n1, n2); - } - - public double multiply(double n1, double n2) { - return getService(MultiplyService.class).multiply(n1, n2); - } - - public double divide(double n1, double n2) { - return getService(DivideService.class).divide(n1, n2); - } -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java deleted file mode 100644 index 2d36ba76ad..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.util.tracker.ServiceTracker; - -import calculator.operations.AddService; -import calculator.operations.DivideService; -import calculator.operations.MultiplyService; -import calculator.operations.SubtractService; - -/** - * An implementation of the Calculator service. - */ -public class CalculatorServiceImpl implements CalculatorService { -// private AddService addService; -// private SubtractService subtractService; -// private MultiplyService multiplyService; -// private DivideService divideService; - - private ServiceTracker tracker; - - public CalculatorServiceImpl() { - super(); - } - - public CalculatorServiceImpl(BundleContext context) { - super(); - Filter filter = null; - try { - filter = context.createFilter("(" + Constants.OBJECTCLASS + "=calculator.operations.*)"); - } catch (InvalidSyntaxException e) { - e.printStackTrace(); - } - this.tracker = new ServiceTracker(context, filter, null); - tracker.open(); - } - - /* - * The following setters can be used for DS injection - */ - /* - public void setAddService(AddService addService) { - this.addService = addService; - } - - public void setSubtractService(SubtractService subtractService) { - this.subtractService = subtractService; - } - - public void setDivideService(DivideService divideService) { - this.divideService = divideService; - } - - public void setMultiplyService(MultiplyService multiplyService) { - this.multiplyService = multiplyService; - } - */ - - private <T> T getService(Class<T> cls) { - for (Object s : tracker.getServices()) { - if (cls.isInstance(s)) { - return cls.cast(s); - } - } - throw new IllegalStateException(cls.getSimpleName() + " is not available"); - } - - public double add(double n1, double n2) { - return getService(AddService.class).add(n1, n2); - } - - public double subtract(double n1, double n2) { - return getService(SubtractService.class).subtract(n1, n2); - } - - public double multiply(double n1, double n2) { - return getService(MultiplyService.class).multiply(n1, n2); - } - - public double divide(double n1, double n2) { - return getService(DivideService.class).divide(n1, n2); - } -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddService.java deleted file mode 100644 index 2fe405a322..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddService.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import org.oasisopen.sca.annotation.Remotable; - -/** - * The interface for the add service - */ -@Remotable -public interface AddService { - - double add(double n1, double n2); - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddServiceImpl.java deleted file mode 100644 index 685d641b1c..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddServiceImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * An implementation of the Add service - */ -public class AddServiceImpl implements AddService { - - public double add(double n1, double n2) { - Logger logger = Logger.getLogger("calculator"); - logger.log(Level.INFO, "Adding " + n1 + " and " + n2); - return n1 + n2; - } - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideService.java deleted file mode 100644 index dfc5df6784..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideService.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import org.oasisopen.sca.annotation.Remotable; - -/** - * The interface for the divide service - */ -@Remotable -public interface DivideService { - - double divide(double n1, double n2); - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideServiceImpl.java deleted file mode 100644 index 945b0315b0..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideServiceImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * An implementation of the Divide service. - */ -public class DivideServiceImpl implements DivideService { - - public double divide(double n1, double n2) { - Logger logger = Logger.getLogger("calculator"); - logger.log(Level.INFO, "Dividing " + n1 + " with " + n2); - return n1 / n2; - } - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyService.java deleted file mode 100644 index b70c233f1a..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyService.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import org.oasisopen.sca.annotation.Remotable; - -/** - * The interface for the multiply service - */ -@Remotable -public interface MultiplyService { - - double multiply(double n1, double n2); - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyServiceImpl.java deleted file mode 100644 index 175322c14d..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyServiceImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * An implementation of the Multiply service. - */ -public class MultiplyServiceImpl implements MultiplyService { - - public double multiply(double n1, double n2) { - Logger logger = Logger.getLogger("calculator"); - logger.log(Level.INFO, "Multiplying " + n1 + " with " + n2); - return n1 * n2; - } - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java deleted file mode 100644 index 5da79ee803..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package calculator.operations; - -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.logging.Logger; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; - -/** - * - */ -public class OperationsActivator implements BundleActivator { - private Logger logger = Logger.getLogger(OperationsActivator.class.getName()); - - public void start(BundleContext context) throws Exception { - logger.info("Starting " + context.getBundle()); - - Dictionary<String, Object> props = new Hashtable<String, Object>(); - - logger.info("Registering " + AddService.class.getName()); - props.put("sca.service", "AddComponent#service-name(Add)"); - context.registerService(AddService.class.getName(), new AddServiceImpl(), props); - - logger.info("Registering " + SubtractService.class.getName()); - props.put("sca.service", "SubtractComponent#service-name(Subtract)"); - context.registerService(SubtractService.class.getName(), new SubtractServiceImpl(), props); - - logger.info("Registering " + MultiplyService.class.getName()); - props.put("sca.service", "MultiplyComponent#service-name(Multiply)"); - context.registerService(MultiplyService.class.getName(), new MultiplyServiceImpl(), props); - - logger.info("Registering " + DivideService.class.getName()); - props.put("sca.service", "DivideComponent#service-name(Divide)"); - context.registerService(DivideService.class.getName(), new DivideServiceImpl(), props); - - } - - public void stop(BundleContext context) throws Exception { - logger.info("Stopping " + context.getBundle()); - // Registered services will be automatically unregistered - } - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractService.java deleted file mode 100644 index 1d83a23229..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractService.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import org.oasisopen.sca.annotation.Remotable; - -/** - * The interface for the subtract service - */ -@Remotable -public interface SubtractService { - - double subtract(double n1, double n2); - -} diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractServiceImpl.java deleted file mode 100644 index cf1744bc61..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractServiceImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package calculator.operations; - -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * An implementation of the subtract service. - */ -public class SubtractServiceImpl implements SubtractService { - - public double subtract(double n1, double n2) { - Logger logger = Logger.getLogger("calculator"); - logger.log(Level.INFO, "Subtracting " + n1 + " from " + n2); - return n1 - n2; - } - -} 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 deleted file mode 100644 index 3477732c5f..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package calculator.test; - -import java.io.File; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import java.net.URL; -import java.util.HashSet; -import java.util.Set; - -import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestBundles; -import org.apache.tuscany.sca.node.equinox.launcher.EquinoxHost; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - -import calculator.CalculatorActivator; -import calculator.CalculatorService; -import calculator.CalculatorServiceDSImpl; -import calculator.CalculatorServiceImpl; -import calculator.operations.AddService; -import calculator.operations.AddServiceImpl; -import calculator.operations.DivideService; -import calculator.operations.DivideServiceImpl; -import calculator.operations.MultiplyService; -import calculator.operations.MultiplyServiceImpl; -import calculator.operations.OperationsActivator; -import calculator.operations.SubtractService; -import calculator.operations.SubtractServiceImpl; - -/** - * - */ -public class CalculatorOSGiTestCase { - private static EquinoxHost host; - - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - Set<URL> bundles = new HashSet<URL>(); - - File plugins = new File("target/test-classes/plugins"); - for (File f : plugins.listFiles()) { - if (f.isFile()) { - bundles.add(f.toURI().toURL()); - } - } - - bundles.add(OSGiTestBundles.createBundle("target/test-classes/calculator-bundle.jar", - "calculator/META-INF/MANIFEST.MF", - new String[] {"OSGI-INF/calculator-component.xml"}, - CalculatorService.class, - CalculatorServiceImpl.class, - CalculatorServiceDSImpl.class, - CalculatorActivator.class)); - - bundles.add(OSGiTestBundles.createBundle("target/test-classes/operations-bundle.jar", - "calculator/operations/META-INF/MANIFEST.MF", - new String[] {"OSGI-INF/add-component.xml", - "OSGI-INF/subtract-component.xml", - "OSGI-INF/multiply-component.xml", - "OSGI-INF/divide-component.xml"}, - OperationsActivator.class, - AddService.class, - AddServiceImpl.class, - SubtractService.class, - SubtractServiceImpl.class, - MultiplyService.class, - MultiplyServiceImpl.class, - DivideService.class, - DivideServiceImpl.class)); - try { - host = new EquinoxHost(bundles); - BundleContext context = host.start(); - for (Bundle b : context.getBundles()) { - if (b.getSymbolicName().equals("org.eclipse.equinox.ds")) { - b.start(); - System.out.println(string(b, false)); - } - } - for (Bundle b : context.getBundles()) { - if (b.getSymbolicName().startsWith("calculator")) { - b.start(); - System.out.println(string(b, false)); - } - } - ServiceReference ref = context.getServiceReference(CalculatorService.class.getName()); - CalculatorService calculator = cast(context.getService(ref), CalculatorService.class); - System.out.println("2.0 + 1.0 = " + calculator.add(2.0, 1.0)); - System.out.println("2.0 - 1.0 = " + calculator.subtract(2.0, 1.0)); - System.out.println("2.0 * 1.0 = " + calculator.multiply(2.0, 1.0)); - System.out.println("2.0 / 1.0 = " + calculator.divide(2.0, 1.0)); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - private static <T> T cast(Object obj, Class<T> cls) { - if (cls.isInstance(obj)) { - return cls.cast(obj); - } else { - return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(), - new Class<?>[] {cls}, - new InvocationHandlerImpl(obj))); - } - } - - private static class InvocationHandlerImpl implements InvocationHandler { - private Object instance; - - public InvocationHandlerImpl(Object instance) { - super(); - this.instance = instance; - } - - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - Method m = instance.getClass().getMethod(method.getName(), method.getParameterTypes()); - return m.invoke(instance, args); - } - - } - - @Test - public void testOSGi() { - - } - - /** - * Returns a string representation of the given bundle. - * - * @param b - * @param verbose - * @return - */ - static String string(Bundle bundle, boolean verbose) { - StringBuffer sb = new StringBuffer(); - sb.append(bundle.getBundleId()).append(" ").append(bundle.getSymbolicName()); - int s = bundle.getState(); - if ((s & Bundle.UNINSTALLED) != 0) { - sb.append(" UNINSTALLED"); - } - if ((s & Bundle.INSTALLED) != 0) { - sb.append(" INSTALLED"); - } - if ((s & Bundle.RESOLVED) != 0) { - sb.append(" RESOLVED"); - } - if ((s & Bundle.STARTING) != 0) { - sb.append(" STARTING"); - } - if ((s & Bundle.STOPPING) != 0) { - sb.append(" STOPPING"); - } - if ((s & Bundle.ACTIVE) != 0) { - sb.append(" ACTIVE"); - } - - if (verbose) { - sb.append(" ").append(bundle.getLocation()); - sb.append(" ").append(bundle.getHeaders()); - } - return sb.toString(); - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - if (host != null) { - host.stop(); - } - } - -} 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 deleted file mode 100644 index 6cdc2db347..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.implementation.osgi.runtime; - -import java.io.File; -import java.lang.reflect.Proxy; - -import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestBundles; -import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestImpl; -import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestInterface; -import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.node.equinox.launcher.Contribution; -import org.apache.tuscany.sca.node.equinox.launcher.NodeLauncher; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * - * Test the execution of an OSGi implementation type - * - * @version $Rev$ $Date$ - */ -public class OSGiTestCase { - private static NodeLauncher host; - private static Node node; - protected static String className; - protected static String compositeName; - - @BeforeClass - public static void setUp() throws Exception { - host = NodeLauncher.newInstance(); - className = OSGiTestImpl.class.getName(); - compositeName = "osgitest.composite"; - OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar", - OSGiTestInterface.class.getName(), - null, - null, - (String[]) null, - OSGiTestImpl.class, OSGiTestInterface.class); - - node = - host.createNode("osgitest.composite", new Contribution("c1", new File("target/test-classes").toURI() - .toString())); - node.start(); - } - - @AfterClass - public static void tearDown() throws Exception { - if (host != null) { - node.stop(); - host.destroy(); - } - } - - @Test - public void testOSGiComponent() throws Exception { - - OSGiTestInterface testService = node.getService(OSGiTestInterface.class, "OSGiTestServiceComponent"); - assert (testService != null); - - assert (testService instanceof Proxy); - - String str = testService.testService(); - - Assert.assertEquals(className, str); - - } - -} 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 716d89c8cd..6d55f6b7ec 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 @@ -154,15 +154,18 @@ public class OSGiTestBundles { private static void addClass(JarOutputStream jarOut, Class<?> javaClass) throws IOException, FileNotFoundException { String classFile = javaClass.getName().replace('.', '/') + ".class"; - - ClassLoader cl = javaClass.getClassLoader(); - - addResource(jarOut, cl, classFile); + URL url = javaClass.getResource(javaClass.getSimpleName() + ".class"); + addEntry(jarOut, url, classFile); } private static void addResource(JarOutputStream jarOut, ClassLoader cl, String resourceName) throws IOException, FileNotFoundException { URL url = cl.getResource(resourceName); + addEntry(jarOut, url, resourceName); + } + + private static void addEntry(JarOutputStream jarOut, URL url, String resourceName) throws IOException, + FileNotFoundException { String path = url.getPath(); ZipEntry ze = new ZipEntry(resourceName); diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/add-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/add-component.xml deleted file mode 100644 index f1ed05111a..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/add-component.xml +++ /dev/null @@ -1,25 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. ---> -<scr:component name="AddComponent" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"> - <implementation class="calculator.operations.AddServiceImpl" /> - <service> - <provide interface="calculator.operations.AddService" /> - </service> -</scr:component> diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/divide-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/divide-component.xml deleted file mode 100644 index 60a6cef4e1..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/divide-component.xml +++ /dev/null @@ -1,25 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. ---> -<scr:component name="DivideComponent" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"> - <implementation class="calculator.operations.DivideServiceImpl" /> - <service> - <provide interface="calculator.operations.DivideService" /> - </service> -</scr:component> diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/multiply-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/multiply-component.xml deleted file mode 100644 index 2fd843b272..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/multiply-component.xml +++ /dev/null @@ -1,25 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. ---> -<scr:component name="MultiplyComponent" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"> - <implementation class="calculator.operations.MultiplyServiceImpl" /> - <service> - <provide interface="calculator.operations.MultiplyService" /> - </service> -</scr:component> diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/subtract-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/subtract-component.xml deleted file mode 100644 index ceb0a5d821..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/subtract-component.xml +++ /dev/null @@ -1,25 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. ---> -<scr:component name="SubtractComponent" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"> - <implementation class="calculator.operations.SubtractServiceImpl" /> - <service> - <provide interface="calculator.operations.SubtractService" /> - </service> -</scr:component> diff --git a/java/sca/modules/implementation-osgi/src/test/resources/calculator/META-INF/MANIFEST.MF b/java/sca/modules/implementation-osgi/src/test/resources/calculator/META-INF/MANIFEST.MF deleted file mode 100644 index 808cc60c79..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/calculator/META-INF/MANIFEST.MF +++ /dev/null @@ -1,18 +0,0 @@ -Manifest-Version: 1.0
-Export-Package: calculator;version="1.0.0"
-Bundle-Version: 1.0.0
-Bundle-Name: calculator
-Bundle-Activator: calculator.CalculatorActivator
-Bundle-ManifestVersion: 2
-Import-Package: calculator.operations;version="1.0.0",
- org.osgi.framework,
- org.osgi.service.packageadmin,
- org.osgi.util.tracker,
- org.osgi.service.component;resolution:=optional
-Bundle-SymbolicName: calculator
-Bundle-Vendor: The Apache Software Foundation
-Bundle-ActivationPolicy: lazy
-Eclipse-LazyStart: true
-Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
-Bundle-DocURL: http://www.apache.org/
-Service-Component: OSGI-INF/calculator-component.xml
diff --git a/java/sca/modules/implementation-osgi/src/test/resources/calculator/calculator.composite b/java/sca/modules/implementation-osgi/src/test/resources/calculator/calculator.composite deleted file mode 100644 index 9674e6d68f..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/calculator/calculator.composite +++ /dev/null @@ -1,38 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?>
-<!--
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
--->
-<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"
- xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- targetNamespace="http://calculator"
- name="CalculatorComposite">
-
- <component name="CalculatorComponent">
- <tuscany:implementation.osgi bundleSymbolicName="calculator" bundleVersion="1.0.0" />
- <reference name="addService" target="OperationsComponent/AddService">
- </reference>
- <reference name="subtractService" target="OperationsComponent/SubtractService">
- </reference>
- <reference name="multiplyService" target="OperationsComponent/MultiplyService">
- </reference>
- <reference name="divideService" target="OperationsComponent/DivideService">
- </reference>
- </component>
-
-</composite>
diff --git a/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/META-INF/MANIFEST.MF b/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/META-INF/MANIFEST.MF deleted file mode 100644 index 1414d227b0..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/META-INF/MANIFEST.MF +++ /dev/null @@ -1,19 +0,0 @@ -Manifest-Version: 1.0
-Export-Package: calculator.operations;version="1.0.0"
-Bundle-Version: 1.0.0
-Bundle-Name: calculator.operations
-Bundle-Activator: calculator.operations.OperationsActivator
-Bundle-ManifestVersion: 2
-Import-Package: calculator.operations;version="1.0.0",
- org.osgi.framework,
- org.osgi.service.component;resolution:=optional
-Bundle-SymbolicName: calculator.operations
-Bundle-Vendor: The Apache Software Foundation
-Bundle-ActivationPolicy: lazy
-Eclipse-LazyStart: true
-Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
-Bundle-DocURL: http://www.apache.org/
-Service-Component: OSGI-INF/add-component.xml,
- OSGI-INF/subtract-component.xml,
- OSGI-INF/multiply-component.xml,
- OSGI-INF/divide-component.xml
diff --git a/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/operations.composite b/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/operations.composite deleted file mode 100644 index 8afb7dc8a2..0000000000 --- a/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/operations.composite +++ /dev/null @@ -1,38 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?>
-<!--
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
--->
-<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"
- xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- targetNamespace="http://calculator"
- name="OperationsComposite">
-
- <component name="OperationsComponent">
- <tuscany:implementation.osgi bundleSymbolicName="calculator.operations" bundleVersion="1.0.0" />
- <service name="AddService">
- </service>
- <service name="SubtractService">
- </service>
- <service name="MultiplyService">
- </service>
- <service name="DivideService">
- </service>
- </component>
-
-</composite>
|