aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n..@apache.org
Subject svn commit: r1240631 - in /aries/trunk/jndi/jndi-core/src: main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java main/java/org/apache/aries/jndi/ObjectFactoryHelper.java test/java/org/apache/aries/jndi/ObjectFactoryTest.java
Date Sat, 04 Feb 2012 22:29:00 GMT
Author: not
Date: Sat Feb  4 22:28:17 2012
New Revision: 1240631

URL: http://svn.apache.org/viewvc?rev=1240631&view=rev
Log:
ARIES-823 Suppress calls to ObjectFactorys that can't cope with a non Reference being passed
to getObjectInstance.

Modified:
    aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
    aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
    aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java

Modified: aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
URL: http://svn.apache.org/viewvc/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java?rev=1240631&r1=1240630&r2=1240631&view=diff
==============================================================================
--- aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
(original)
+++ aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
Sat Feb  4 22:28:17 2012
@@ -77,14 +77,14 @@ public class DirObjectFactoryHelper exte
             }
         }
 
-		// Extra, non-standard, bonus step. If javax.naming.OBJECT_FACTORIES is set as 
-		// a property in the environment, use its value to construct additional object factories.

-		// Added under Aries-822, with reference 
-		// to https://www.osgi.org/bugzilla/show_bug.cgi?id=138 
-		if (result == null || result == obj) {
-			result = getObjectInstanceViaContextDotObjectFactories(obj, name, nameCtx, environment);
-		} 
-		
+        // Extra, non-standard, bonus step. If javax.naming.OBJECT_FACTORIES is set as 
+        // a property in the environment, use its value to construct additional object factories.

+        // Added under Aries-822, with reference 
+        // to https://www.osgi.org/bugzilla/show_bug.cgi?id=138 
+        if (result == null || result == obj) {
+            result = getObjectInstanceViaContextDotObjectFactories(obj, name, nameCtx, environment);
+        } 
+        
         return (result == null) ? obj : result;
     }
 
@@ -94,27 +94,30 @@ public class DirObjectFactoryHelper exte
                                                          Hashtable<?, ?> environment,
                                                          Attributes attrs) 
         throws Exception {
-    	
+        
         Object result = null;
         ServiceReference[] refs = Utils.getReferencesPrivileged(callerContext, DirObjectFactory.class);
         if (refs != null) {
-        	Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
-        	for (ServiceReference ref : refs) {
-        		DirObjectFactory factory = (DirObjectFactory) Utils.getServicePrivileged(callerContext,
ref);
-
-        		try {
-        			result = factory.getObjectInstance(obj, name, nameCtx, environment, attrs);
-        		} finally {
-        			callerContext.ungetService(ref);
-        		}
-
-        		// if the result comes back and is not null and not the reference
-        		// object then we should return the result, so break out of the
-        		// loop we are in.
-        		if (result != null && result != obj) {
-        			break;
-        		}
-        	}
+            Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
+            for (ServiceReference ref : refs) {
+              
+                if (canCallObjectFactory(obj, ref)) {
+                    DirObjectFactory factory = (DirObjectFactory) Utils.getServicePrivileged(callerContext,
ref);
+    
+                    try {
+                        result = factory.getObjectInstance(obj, name, nameCtx, environment,
attrs);
+                    } finally {
+                        callerContext.ungetService(ref);
+                    }
+    
+                    // if the result comes back and is not null and not the reference
+                    // object then we should return the result, so break out of the
+                    // loop we are in.
+                    if (result != null && result != obj) {
+                        break;
+                    }
+                }
+            }
         }
 
         if (result == null) {
@@ -124,6 +127,19 @@ public class DirObjectFactoryHelper exte
         return (result == null) ? obj : result;
     }
 
+    private boolean canCallObjectFactory(Object obj, ServiceReference ref)
+    {
+      if (obj instanceof Reference) return true;
+      
+      Object prop = ref.getProperty("aries.object.factory.requires.reference");
+      
+      if (prop == null) return true;
+      
+      if (prop instanceof Boolean) return !!!(Boolean) prop; // if set to true we don't call.
+      
+      return true;
+    }
+
     private Object getObjectInstanceUsingClassName(Object reference,
                                                    String className,
                                                    Object obj,
@@ -137,8 +153,8 @@ public class DirObjectFactoryHelper exte
         Object result = null;
         
         if (tuple.second != null) {
-        	try {
-        		result = ((DirObjectFactory) tuple.second).getObjectInstance(reference, name, nameCtx,
environment, attrs);
+            try {
+                result = ((DirObjectFactory) tuple.second).getObjectInstance(reference, name,
nameCtx, environment, attrs);
             } finally {
                 defaultContext.ungetService(tuple.first);
             }
@@ -156,20 +172,20 @@ public class DirObjectFactoryHelper exte
         ObjectFactory factory = null;
         ServiceReference[] refs = Utils.getReferencesPrivileged(callerContext, ObjectFactoryBuilder.class);
         if (refs != null) {
-        	Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
-        	for (ServiceReference ref : refs) {
-        		ObjectFactoryBuilder builder = (ObjectFactoryBuilder) Utils.getServicePrivileged(callerContext,
ref);
-        		try {
-        			factory = builder.createObjectFactory(obj, environment);
-        		} catch (NamingException e) {
-        			// TODO: log it
-        		} finally {
-        			callerContext.ungetService(ref);
-        		}
-        		if (factory != null) {
-        			break;
-        		}
-        	}
+            Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
+            for (ServiceReference ref : refs) {
+                ObjectFactoryBuilder builder = (ObjectFactoryBuilder) Utils.getServicePrivileged(callerContext,
ref);
+                try {
+                    factory = builder.createObjectFactory(obj, environment);
+                } catch (NamingException e) {
+                    // TODO: log it
+                } finally {
+                    callerContext.ungetService(ref);
+                }
+                if (factory != null) {
+                    break;
+                }
+            }
         }
 
         Object result = null;

Modified: aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
URL: http://svn.apache.org/viewvc/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java?rev=1240631&r1=1240630&r2=1240631&view=diff
==============================================================================
--- aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
(original)
+++ aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
Sat Feb  4 22:28:17 2012
@@ -161,6 +161,7 @@ public class ObjectFactoryHelper impleme
             Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
             
             for (ServiceReference ref : refs) {
+              if (canCallObjectFactory(obj, ref)) {
                 ObjectFactory factory = (ObjectFactory) Utils.getServicePrivileged(callerContext,
ref);
 
                 try {
@@ -177,12 +178,26 @@ public class ObjectFactoryHelper impleme
                 if (result != null && result != obj) {
                     break;
                 }
+              }
             }
         }
 
         return (result == null) ? obj : result;
     }
 
+    private boolean canCallObjectFactory(Object obj, ServiceReference ref)
+    {
+      if (obj instanceof Reference) return true;
+      
+      Object prop = ref.getProperty("aries.object.factory.requires.reference");
+      
+      if (prop == null) return true;
+      
+      if (prop instanceof Boolean) return !!!(Boolean) prop; // if set to true we don't call.
+      
+      return true;
+    }
+
     protected static String getUrlScheme(String name) {
         String scheme = name;   
         int index = name.indexOf(':');

Modified: aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java?rev=1240631&r1=1240630&r2=1240631&view=diff
==============================================================================
--- aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
(original)
+++ aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
Sat Feb  4 22:28:17 2012
@@ -26,16 +26,12 @@ import java.util.Hashtable;
 import java.util.Properties;
 
 import javax.naming.Context;
-import javax.naming.InitialContext;
 import javax.naming.Name;
-import javax.naming.NamingException;
 import javax.naming.Reference;
 import javax.naming.StringRefAddr;
 import javax.naming.spi.NamingManager;
 import javax.naming.spi.ObjectFactory;
 
-import junit.framework.Assert;
-
 import org.apache.aries.jndi.startup.Activator;
 import org.apache.aries.jndi.urls.URLObjectFactoryFinder;
 import org.apache.aries.mocks.BundleContextMock;
@@ -185,23 +181,39 @@ public class ObjectFactoryTest
     assertSame("The naming manager should have returned the reference object", ref, obj);
   }
   
+  @Test
+  public void testFactoriesThatDoUnsafeCastsAreIgnored() throws Exception {
+    Hashtable<String, Object> props = new Hashtable<String, Object>();
+    props.put("aries.object.factory.requires.reference", Boolean.TRUE);
+    bc.registerService(ObjectFactory.class.getName(), new ObjectFactory() {
+      
+      public Object getObjectInstance(Object arg0, Name arg1, Context arg2, Hashtable<?,
?> arg3)
+          throws Exception
+      {
+        return (Reference)arg0;
+      }
+    }, props);
+
+    NamingManager.getObjectInstance("Some dummy data", null, null, env);
+  }
+  
   public static class DummyObjectFactory implements ObjectFactory {
 
-		public Object getObjectInstance(Object obj, Name name, Context nameCtx,
-				Hashtable<?, ?> environment) throws Exception {
-			// TODO Auto-generated method stub
-			return new String ("pass");
-		}
+        public Object getObjectInstance(Object obj, Name name, Context nameCtx,
+                Hashtable<?, ?> environment) throws Exception {
+            // TODO Auto-generated method stub
+            return new String ("pass");
+        }
   }
-	  
+      
   @Test
   public void testContextDotObjectFactories() throws Exception { 
-	  env.put(Context.OBJECT_FACTORIES, "org.apache.aries.jndi.ObjectFactoryTest$DummyObjectFactory");
-	  Reference ref = new Reference("anything");
-	  Object obj = NamingManager.getObjectInstance(ref, null, null, env);
-	  assertTrue (obj instanceof String);
-	  assertEquals ((String)obj, "pass");
-	  env.remove(Context.OBJECT_FACTORIES);
+      env.put(Context.OBJECT_FACTORIES, "org.apache.aries.jndi.ObjectFactoryTest$DummyObjectFactory");
+      Reference ref = new Reference("anything");
+      Object obj = NamingManager.getObjectInstance(ref, null, null, env);
+      assertTrue (obj instanceof String);
+      assertEquals ((String)obj, "pass");
+      env.remove(Context.OBJECT_FACTORIES);
   }
 
 }
\ No newline at end of file



Mime
View raw message