aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1688204 - in /aries/trunk/subsystem/subsystem-core/src: main/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepository.java test/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepositoryTest.java
Date Mon, 29 Jun 2015 13:14:11 GMT
Author: davidb
Date: Mon Jun 29 13:14:11 2015
New Revision: 1688204

URL: http://svn.apache.org/r1688204
Log:
ARIES-1339 IllegalAccessException when installing a new subsystem

This commit fixes the issue. I also included a unit test.

Added:
    aries/trunk/subsystem/subsystem-core/src/test/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepositoryTest.java
Modified:
    aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepository.java

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepository.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepository.java?rev=1688204&r1=1688203&r2=1688204&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepository.java
(original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepository.java
Mon Jun 29 13:14:11 2015
@@ -26,15 +26,20 @@ import org.osgi.framework.InvalidSyntaxE
 import org.osgi.framework.ServiceReference;
 import org.osgi.resource.Capability;
 import org.osgi.resource.Requirement;
+import org.osgi.service.repository.Repository;
 import org.osgi.service.subsystem.SubsystemException;
 
 public class RepositoryServiceRepository implements org.apache.aries.subsystem.core.repository.Repository
{
-	private BundleContext context;
-	
+    final BundleContext context;
+
 	public RepositoryServiceRepository() {
-		context = Activator.getInstance().getBundleContext();
+		this(Activator.getInstance().getBundleContext());
+	}
+
+	RepositoryServiceRepository(BundleContext ctx) {
+	    context = ctx;
 	}
-	
+
 	@SuppressWarnings("unchecked")
 	public Collection<Capability> findProviders(Requirement requirement) {
 		Set<Capability> result = new HashSet<Capability>();
@@ -52,10 +57,31 @@ public class RepositoryServiceRepository
 			if (repository == null)
 				continue;
 			try {
+			    // Reflection is used here to allow the service to work with a mixture of
+			    // Repository services implementing different versions of the API.
+
 				Class<?> clazz = repository.getClass();
+				Class<?> repoInterface = null;
+
+				while (clazz != null && repoInterface == null) {
+				    for (Class<?> intf : clazz.getInterfaces()) {
+				        if (Repository.class.getName().equals(intf.getName())) {
+				            // Compare interfaces by name so that we can work with different versions
of the
+				            // interface.
+				            repoInterface = intf;
+				            break;
+				        }
+				    }
+                    clazz = clazz.getSuperclass();
+				}
+
+				if (repoInterface == null) {
+				    continue;
+				}
+
 				Map<Requirement, Collection<Capability>> map;
 				try {
-					Method method = clazz.getMethod("findProviders", Collection.class);
+					Method method = repoInterface.getMethod("findProviders", Collection.class);
 					map = (Map<Requirement, Collection<Capability>>)method.invoke(repository,
Collections.singleton(requirement));
 				}
 				catch (Exception e) {
@@ -72,7 +98,7 @@ public class RepositoryServiceRepository
 		}
 		return result;
 	}
-	
+
 	@Override
 	public Map<Requirement, Collection<Capability>> findProviders(Collection<?
extends Requirement> requirements) {
 		Map<Requirement, Collection<Capability>> result = new HashMap<Requirement,
Collection<Capability>>();

Added: aries/trunk/subsystem/subsystem-core/src/test/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepositoryTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/test/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepositoryTest.java?rev=1688204&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/test/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepositoryTest.java
(added)
+++ aries/trunk/subsystem/subsystem-core/src/test/java/org/apache/aries/subsystem/core/internal/RepositoryServiceRepositoryTest.java
Mon Jun 29 13:14:11 2015
@@ -0,0 +1,166 @@
+/*
+ * Licensed 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.aries.subsystem.core.internal;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.easymock.EasyMock;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Requirement;
+import org.osgi.resource.Resource;
+import org.osgi.service.repository.Repository;
+
+public class RepositoryServiceRepositoryTest {
+    @Test
+    public void testFindProviders() throws Exception {
+        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+        RepositoryServiceRepository rsr = new RepositoryServiceRepository(bc);
+
+        @SuppressWarnings("unchecked")
+        ServiceReference<Object> sr = EasyMock.createMock(ServiceReference.class);
+        @SuppressWarnings("unchecked")
+        ServiceReference<Object> sr2 = EasyMock.createMock(ServiceReference.class);
+        EasyMock.expect(bc.getAllServiceReferences("org.osgi.service.repository.Repository",
null)).
+            andReturn(new ServiceReference[] {sr, sr2}).anyTimes();
+
+        TestRepository tr = new TestRepository();
+        EasyMock.expect(bc.getService(sr)).andReturn(tr).anyTimes();
+
+        ToastRepository tr2 = new ToastRepository();
+        EasyMock.expect(bc.getService(sr2)).andReturn(tr2).anyTimes();
+        EasyMock.replay(bc);
+
+
+        Map<String, String> dirs = Collections.singletonMap("filter", "(org.foo=bar)");
+        Requirement req = new TestRequirement("org.foo", dirs);
+        Collection<Capability> res = rsr.findProviders(req);
+        assertEquals(1, res.size());
+        Capability cap = res.iterator().next();
+        assertEquals("org.foo", cap.getNamespace());
+        assertEquals(1, cap.getAttributes().size());
+        assertEquals("bar", cap.getAttributes().get("org.foo"));
+
+        Map<String, String> dirs2 = Collections.singletonMap("filter", "(org.foo=b)");
+        Requirement req2 = new TestRequirement("poing", dirs2);
+        Collection<Capability> res2 = rsr.findProviders(req2);
+        assertEquals(1, res2.size());
+        Capability cap2 = res2.iterator().next();
+        assertEquals("poing", cap2.getNamespace());
+        assertEquals(1, cap2.getAttributes().size());
+        assertEquals("b", cap2.getAttributes().get("org.foo"));
+    }
+
+    private static class TestRequirement implements Requirement {
+        private final String namespace;
+        private final Map<String, String> directives;
+
+        private TestRequirement(String ns, Map<String, String> dirs) {
+            namespace = ns;
+            directives = dirs;
+        }
+
+        @Override
+        public String getNamespace() {
+            return namespace;
+        }
+
+        @Override
+        public Map<String, Object> getAttributes() {
+            return Collections.emptyMap();
+        }
+
+        @Override
+        public Map<String, String> getDirectives() {
+            return directives;
+        }
+
+        @Override
+        public Resource getResource() {
+            return null;
+        }
+    }
+
+    private static class TestCapability implements Capability {
+        private final String namespace;
+        private final Map<String, Object> attributes;
+
+        private TestCapability(String ns, Map<String, Object> attrs) {
+            namespace = ns;
+            attributes = attrs;
+        }
+
+        @Override
+        public String getNamespace() {
+            return namespace;
+        }
+
+        @Override
+        public Map<String, String> getDirectives() {
+            return Collections.emptyMap();
+        }
+
+        @Override
+        public Map<String, Object> getAttributes() {
+            return attributes;
+        }
+
+        @Override
+        public Resource getResource() {
+            return null;
+        }
+    }
+
+    private static class TestRepository implements Repository {
+        @Override
+        public Map<Requirement, Collection<Capability>> findProviders(Collection<?
extends Requirement> requirements) {
+            Map<Requirement, Collection<Capability>> res = new HashMap<Requirement,
Collection<Capability>>();
+
+            for (Requirement req : requirements) {
+                if (req.getNamespace().equals("org.foo") &&
+                        req.getDirectives().equals(Collections.singletonMap("filter", "(org.foo=bar)")))
{
+                    TestCapability cap = new TestCapability("org.foo",
+                            Collections.<String, Object>singletonMap("org.foo", "bar"));
+                    Collection<Capability> caps = Collections.<Capability>singleton(cap);
+                    res.put(req, caps);
+                }
+            }
+
+            return res;
+        }
+    }
+
+    private static class ToastRepository extends TestRepository {
+        @Override
+        public Map<Requirement, Collection<Capability>> findProviders(Collection<?
extends Requirement> requirements) {
+            for (Requirement req : requirements) {
+                if (req.getNamespace().equals("poing") &&
+                        req.getDirectives().equals(Collections.singletonMap("filter", "(org.foo=b)")))
{
+                    TestCapability cap = new TestCapability("poing",
+                            Collections.<String, Object>singletonMap("org.foo", "b"));
+                    Collection<Capability> caps = Collections.<Capability>singleton(cap);
+                    return Collections.singletonMap(req, caps);
+                }
+            }
+            return Collections.emptyMap();
+        }
+    }
+}



Mime
View raw message