incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1052204 - in /incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook: SpiFly/src/org/apache/aries/spifly/ SpiFlyTests/src/org/apache/aries/spifly/
Date Thu, 23 Dec 2010 09:21:23 GMT
Author: davidb
Date: Thu Dec 23 09:21:17 2010
New Revision: 1052204

URL: http://svn.apache.org/viewvc?rev=1052204&view=rev
Log:
A few tests are passing again - Work in Progress

Modified:
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ArgRestrictions.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ClientWeavingHook.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFlyTests/src/org/apache/aries/spifly/ClientWeavingHookTest.java

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java?rev=1052204&r1=1052203&r2=1052204&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
Thu Dec 23 09:21:17 2010
@@ -206,13 +206,21 @@ public class Activator implements Bundle
     }
 
     private Collection<Bundle> getBundles(List<BundleDescriptor> descriptors)
{
+        if (descriptors == null) {
+            return null;
+        }
+        
         List<Bundle> bundles = new ArrayList<Bundle>();
         for (Bundle b : bundleContext.getBundles()) {
             for (BundleDescriptor desc : descriptors) {
-                
+                if (b.getSymbolicName().equals(desc.getSymbolicName())) {
+                    if (desc.getVersion() == null || b.getVersion().equals(desc.getVersion()))
{
+                        bundles.add(b);
+                    }
+                }
             }
         }
-        return null;
+        return bundles;
     }
 
     /*

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ArgRestrictions.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ArgRestrictions.java?rev=1052204&r1=1052203&r2=1052204&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ArgRestrictions.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ArgRestrictions.java
Thu Dec 23 09:21:17 2010
@@ -19,27 +19,29 @@
 package org.apache.aries.spifly;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
 
 public class ArgRestrictions {
-    private final SortedMap<Pair<Integer, String>, List<String>> restrictions
= 
-            new TreeMap<Pair<Integer, String>, List<String>>();
+    private final Map<Pair<Integer, String>, List<String>> restrictions
= 
+            new HashMap<Pair<Integer, String>, List<String>>();
     
     public void addRestriction(int argNumber, String className) {
         addRestriction(argNumber, className, null);
     }
 
-    public void addRestriction(int argNumber, String className, String value) {
+    public void addRestriction(int argNumber, String className, String allowedArgValue) {
         Pair<Integer, String> key = new Pair<Integer, String>(argNumber, className);
-        List<String> values = restrictions.get(key);
-        if (values == null && value != null) {
-            values = new ArrayList<String>();
-            restrictions.put(key, values);
+        List<String> allowedValues = restrictions.get(key);
+        if (allowedArgValue != null) {
+            if (allowedValues == null) {
+                allowedValues = new ArrayList<String>();
+                restrictions.put(key, allowedValues);
+            }            
+            allowedValues.add(allowedArgValue);            
         }
-        values.add(value);
+        restrictions.put(key, allowedValues);
     }
 
     public String[] getArgClasses() {

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ClientWeavingHook.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ClientWeavingHook.java?rev=1052204&r1=1052203&r2=1052204&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ClientWeavingHook.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/ClientWeavingHook.java
Thu Dec 23 09:21:17 2010
@@ -19,10 +19,8 @@
 package org.apache.aries.spifly;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.ServiceLoader;
 import java.util.Set;
 
@@ -205,7 +203,7 @@ public class ClientWeavingHook implement
             // Activator.activator.log(LogService.LOG_INFO, "Weaving " + className + "#"
+ methodName + " from bundle " + 
             //    consumerBundle.getSymbolicName() + " to " + (allowedBundles.size() == 0
? " any provider" : allowedBundles));
                         
-            Activator.activator.registerConsumerBundle(consumerBundle, restrictions, allowedBundles);
+            Activator.activator.registerConsumerBundle(consumerBundle, restrictions, allowedBundles.size()
== 0 ? null : allowedBundles);
            
             String[] argClasses = restriction.getMethodRestriction(methodName).getArgClasses();
             // TODO support more than one definition            

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java?rev=1052204&r1=1052203&r2=1052204&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java
Thu Dec 23 09:21:17 2010
@@ -20,8 +20,11 @@ package org.apache.aries.spifly;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleReference;
@@ -53,7 +56,7 @@ public class Util {
         BundleReference br = ((BundleReference) bundleLoader);
         System.out.println("~~~ cls: " + cls + " method: " + method + " clarg:" + clsArg
+ " cl:" + bundleLoader + " clientBundle: " + br.getBundle().getSymbolicName());        
         
-        ClassLoader cl = findContextClassloader(clsArg, br.getBundle());
+        ClassLoader cl = findContextClassloader(br.getBundle(), cls, method, clsArg);
         if (cl != null) {
             Activator.activator.log(LogService.LOG_INFO, "Temporarily setting Thread Context
Classloader to: " + cl);
             Thread.currentThread().setContextClassLoader(cl);
@@ -62,13 +65,22 @@ public class Util {
         }
     }
     
-    private static ClassLoader findContextClassloader(Class<?> cls, Bundle consumerBundle)
{
+    private static ClassLoader findContextClassloader(Bundle consumerBundle, String className,
String methodName, Class<?> clsArg) {
         Activator activator = Activator.activator;
         
-        Collection<Bundle> bundles = new ArrayList<Bundle>(activator.findProviderBundles(cls.getName()));
-        activator.log(LogService.LOG_DEBUG, "Found bundles providing " + cls + ": " + bundles);
+        String requestedClass;
+        if (ServiceLoader.class.getName().equals(className) && "load".equals(methodName))
{
+            requestedClass = clsArg.getName();
+        } else {
+            requestedClass = className;
+        }
+        Collection<Bundle> bundles = new ArrayList<Bundle>(activator.findProviderBundles(requestedClass));
+        activator.log(LogService.LOG_DEBUG, "Found bundles providing " + requestedClass +
": " + bundles);
                 
-        Collection<Bundle> allowedBundles = activator.findConsumerRestrictions(consumerBundle,
0, cls.getName());
+        Map<Pair<Integer, String>, String> args = new HashMap<Pair<Integer,String>,
String>();
+        args.put(new Pair<Integer, String>(1, Class.class.getName()), clsArg.getName());
+        Collection<Bundle> allowedBundles = activator.findConsumerRestrictions(consumerBundle,
className, methodName, args);
+
         if (allowedBundles != null) {
             for (Iterator<Bundle> it = bundles.iterator(); it.hasNext(); ) {
                 if (!allowedBundles.contains(it.next())) {

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFlyTests/src/org/apache/aries/spifly/ClientWeavingHookTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFlyTests/src/org/apache/aries/spifly/ClientWeavingHookTest.java?rev=1052204&r1=1052203&r2=1052204&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFlyTests/src/org/apache/aries/spifly/ClientWeavingHookTest.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFlyTests/src/org/apache/aries/spifly/ClientWeavingHookTest.java
Thu Dec 23 09:21:17 2010
@@ -19,6 +19,7 @@
 package org.apache.aries.spifly;
 
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -32,6 +33,7 @@ import java.util.List;
 
 import org.apache.aries.spifly.api.SpiFlyConstants;
 import org.easymock.EasyMock;
+import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -48,14 +50,23 @@ public class ClientWeavingHookTest {
     public void setUp() {
         Activator.activator = new Activator();
     }
+    
+    @After
+    public void tearDown() {
+        Activator.activator = null;
+    }
         
     @Test
     public void testClientWeavingHookBasicServiveLoaderUsage() throws Exception {
-        Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"));
              
-       
-        Dictionary<String, String> headers = new Hashtable<String, String>();
-        headers.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "true");
-        Bundle consumerBundle = mockConsumerBundle(headers, spiFlyBundle);
+        Dictionary<String, String> consumerHeaders = new Hashtable<String, String>();
+        consumerHeaders.put(SpiFlyConstants.SPI_CONSUMER_HEADER, "true");
+
+        // Register the bundle that provides the SPI implementation.
+        Bundle providerBundle = mockProviderBundle("impl1", 1, "META-INF/services/org.apache.aries.mytest.MySPI");
       
+        Activator.activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle);
+
+        Bundle consumerBundle = mockConsumerBundle(consumerHeaders, providerBundle);
+        Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle,
providerBundle);                      
 
         WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext());
         
@@ -70,11 +81,7 @@ public class ClientWeavingHookTest {
         String di2 = "org.apache.aries.spifly;bundle-version=1.9.4;bundle-symbolic-name=spifly";
         String di = wc.getDynamicImports().get(0);
         Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di) ||
di2.equals(di));        
-                
-        // ok the weaving is done, now prepare the registry for the call
-        Bundle providerBundle = mockProviderBundle("impl1", 1, "META-INF/services/org.apache.aries.mytest.MySPI");
       
-        Activator.activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle);
-        
+                        
         // Invoke the woven class and check that it propertly sets the TCCL so that the 
         // META-INF/services/org.apache.aries.mytest.MySPI file from impl1 is visible.
         Class<?> cls = wc.getDefinedClass();
@@ -340,25 +347,30 @@ public class ClientWeavingHookTest {
         Assert.assertEquals("JAXP implementation from alternative bundle", "org.apache.aries.spifly.impl3.MyAltDocumentBuilderFactory",
result.getName());                        
     }
     
-    private Bundle mockSpiFlyBundle(Bundle ... bundles) {
+    private Bundle mockSpiFlyBundle(Bundle ... bundles) throws Exception {
         return mockSpiFlyBundle("spifly", new Version(1, 0, 0), bundles);
     }
     
-    private Bundle mockSpiFlyBundle(String bsn, Version version, Bundle ... bundles) {
+    private Bundle mockSpiFlyBundle(String bsn, Version version, Bundle ... bundles) throws
Exception {
         Bundle spiFlyBundle = EasyMock.createMock(Bundle.class);
 
         BundleContext spiFlyBundleContext = EasyMock.createMock(BundleContext.class);
-        EasyMock.expect(spiFlyBundleContext.getBundle()).andReturn(spiFlyBundle);
+        EasyMock.expect(spiFlyBundleContext.getBundle()).andReturn(spiFlyBundle).anyTimes();
         List<Bundle> allBundles = new ArrayList<Bundle>(Arrays.asList(bundles));
         allBundles.add(spiFlyBundle);
-        EasyMock.expect(spiFlyBundleContext.getBundles()).andReturn(allBundles.toArray(new
Bundle [] {}));
+        EasyMock.expect(spiFlyBundleContext.getBundles()).andReturn(allBundles.toArray(new
Bundle [] {})).anyTimes();
         EasyMock.replay(spiFlyBundleContext);
 
         EasyMock.expect(spiFlyBundle.getSymbolicName()).andReturn(bsn).anyTimes();
-        EasyMock.expect(spiFlyBundle.getVersion()).andReturn(version);
+        EasyMock.expect(spiFlyBundle.getVersion()).andReturn(version).anyTimes();
         EasyMock.expect(spiFlyBundle.getBundleContext()).andReturn(spiFlyBundleContext).anyTimes();
         EasyMock.replay(spiFlyBundle);
 
+        // Set the bundle context for testing purposes
+        Field bcField = Activator.class.getDeclaredField("bundleContext");
+        bcField.setAccessible(true);
+        bcField.set(Activator.activator, spiFlyBundle.getBundleContext());
+        
         return spiFlyBundle;
     }
 



Mime
View raw message