diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-20 16:46:55 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-20 16:46:55 +0000 |
commit | b099739d69690535623b126b37f47bfdfecf040f (patch) | |
tree | 252ab5e26d6e6f2c804c43744ff22d68ce3b353f /java/sca/modules/extensibility-equinox | |
parent | f1863df3c7da7a4632ef3ca70d430d62ed387bad (diff) |
Refactoring a few methods on the ServiceDiscover
Adding an OSGi service registry based ExtensionPointRegistry implementation
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@776759 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/extensibility-equinox')
4 files changed, 102 insertions, 9 deletions
diff --git a/java/sca/modules/extensibility-equinox/META-INF/MANIFEST.MF b/java/sca/modules/extensibility-equinox/META-INF/MANIFEST.MF index 15d36e331c..06764168e6 100644 --- a/java/sca/modules/extensibility-equinox/META-INF/MANIFEST.MF +++ b/java/sca/modules/extensibility-equinox/META-INF/MANIFEST.MF @@ -16,9 +16,11 @@ Bundle-ManifestVersion: 2 Bundle-Description: Apache Tuscany SCA Extensibility for Eclipse Equin
ox
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
-Import-Package: org.apache.tuscany.sca.extensibility;version="2.0.0",
+Import-Package: org.apache.tuscany.sca.core;version="2.0.0",
+ org.apache.tuscany.sca.extensibility;version="2.0.0",
org.eclipse.core.runtime.adaptor;resolution:=optional,
org.osgi.framework;version="1.4"
Bundle-SymbolicName: org.apache.tuscany.sca.extensibility.equinox
Bundle-DocURL: http://www.apache.org/
Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 +Export-Package: org.apache.tuscany.sca.extensibility.equinox;version="2.0.0"
diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java index 7e2d39f182..930b308dfc 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java @@ -28,6 +28,7 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; @@ -117,6 +118,10 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { return sb.toString(); } + public Bundle getBundle() { + return bundle; + } + } /** @@ -186,8 +191,8 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { return attributes; } - public ServiceDeclaration getFirstServiceDeclaration(String name) throws IOException { - Set<ServiceDeclaration> declarations = getServiceDeclarations(name); + public ServiceDeclaration getServiceDeclaration(String name) throws IOException { + Collection<ServiceDeclaration> declarations = getServiceDeclarations(name); if (declarations.isEmpty()) { return null; } else { @@ -195,9 +200,9 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } } - public Set<ServiceDeclaration> getServiceDeclarations(String serviceName) throws IOException { + public Collection<ServiceDeclaration> getServiceDeclarations(String serviceName) throws IOException { boolean debug = logger.isLoggable(Level.FINE); - Set<ServiceDeclaration> descriptors = new HashSet<ServiceDeclaration>(); + Collection<ServiceDeclaration> descriptors = new HashSet<ServiceDeclaration>(); // http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPathFactory.html boolean isPropertyFile = "javax.xml.xpath.XPathFactory".equals(serviceName); diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java new file mode 100644 index 0000000000..07bf4dc18e --- /dev/null +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java @@ -0,0 +1,86 @@ +/*
+ * 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.extensibility.equinox;
+
+import java.util.Dictionary;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.sca.extensibility.ServiceDeclaration;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ *
+ */
+public class OSGiExtensionPointRegistry extends DefaultExtensionPointRegistry {
+ private Map<Class<?>, ServiceRegistration> services = new ConcurrentHashMap<Class<?>, ServiceRegistration>();
+ private BundleContext bundleContext;
+
+ public OSGiExtensionPointRegistry(BundleContext bundleContext) {
+ super();
+ this.bundleContext = bundleContext;
+ }
+
+ @Override
+ protected <T> Object findExtensionPoint(Class<T> extensionPointType) {
+ ServiceRegistration registration = services.get(extensionPointType);
+ if (registration != null) {
+ ServiceReference ref = registration.getReference();
+ if (ref != null) {
+ return ref.getBundle().getBundleContext().getService(ref);
+ }
+ }
+ /*
+ else {
+ ServiceReference ref = bundleContext.getServiceReference(extensionPointType.getName());
+ if (ref != null) {
+ return bundleContext.getService(ref);
+ }
+ }
+ */
+ return null;
+ }
+
+ @Override
+ protected void registerExtensionPoint(Class<?> i, Object extensionPoint, ServiceDeclaration declaration) {
+ BundleContext context = bundleContext;
+ if (declaration instanceof EquinoxServiceDiscoverer.ServiceDeclarationImpl) {
+ EquinoxServiceDiscoverer.ServiceDeclarationImpl declarationImpl =
+ (EquinoxServiceDiscoverer.ServiceDeclarationImpl)declaration;
+ context = declarationImpl.getBundle().getBundleContext();
+ }
+ Dictionary<Object, Object> props = new java.util.Hashtable<Object, Object>();
+ ServiceRegistration registration = context.registerService(i.getName(), extensionPoint, props);
+ services.put(i, registration);
+ }
+
+ @Override
+ protected void unregisterExtensionPoint(Class<?> i) {
+ ServiceRegistration registration = services.get(i);
+ if (registration != null) {
+ registration.unregister();
+ }
+ services.remove(i);
+ }
+
+}
diff --git a/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java index 642bad0b2b..8010ead253 100644 --- a/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java +++ b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java @@ -26,8 +26,8 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collection; import java.util.List; -import java.util.Set; import java.util.jar.JarInputStream; import java.util.jar.Manifest; @@ -134,7 +134,7 @@ public class EquinoxServiceDiscovererTestCase { @Test public void testDiscovery() throws IOException { - Set<ServiceDeclaration> descriptors = discoverer.getServiceDeclarations("test.TestService"); + Collection<ServiceDeclaration> descriptors = discoverer.getServiceDeclarations("test.TestService"); Assert.assertEquals(1, descriptors.size()); descriptors = discoverer.getServiceDeclarations("notthere"); Assert.assertEquals(0, descriptors.size()); @@ -142,9 +142,9 @@ public class EquinoxServiceDiscovererTestCase { @Test public void testDiscoveryFirst() throws IOException { - ServiceDeclaration descriptor = discoverer.getFirstServiceDeclaration("test.TestService"); + ServiceDeclaration descriptor = discoverer.getServiceDeclaration("test.TestService"); Assert.assertNotNull(descriptor); - descriptor = discoverer.getFirstServiceDeclaration("notthere"); + descriptor = discoverer.getServiceDeclaration("notthere"); Assert.assertNull(descriptor); } |