aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mnutt...@apache.org
Subject svn commit: r1240069 - 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 Fri, 03 Feb 2012 10:39:09 GMT
Author: mnuttall
Date: Fri Feb  3 10:39:09 2012
New Revision: 1240069

URL: http://svn.apache.org/viewvc?rev=1240069&view=rev
Log:
Aries-822: Add support for Context.OBJECT_FACTORIES to jndi.core

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=1240069&r1=1240068&r2=1240069&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
Fri Feb  3 10:39:09 2012
@@ -77,6 +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);
+		} 
+		
         return (result == null) ? obj : result;
     }
 

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=1240069&r1=1240068&r2=1240069&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
Fri Feb  3 10:39:09 2012
@@ -23,6 +23,8 @@ import java.security.PrivilegedAction;
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.naming.Context;
 import javax.naming.Name;
@@ -43,6 +45,7 @@ public class ObjectFactoryHelper impleme
     
     protected BundleContext defaultContext;
     protected BundleContext callerContext;
+    private static final Logger logger = Logger.getLogger(ObjectFactoryHelper.class.getName());
 
     public ObjectFactoryHelper(BundleContext defaultContext, BundleContext callerContext)
{
         this.defaultContext = defaultContext;
@@ -58,6 +61,8 @@ public class ObjectFactoryHelper impleme
         if (obj instanceof Referenceable) {
             obj = ((Referenceable) obj).getReference();
         }
+        
+        logger.log(Level.FINE, "obj = " + obj);
 
         Object result = obj;
 
@@ -74,22 +79,75 @@ public class ObjectFactoryHelper impleme
                 result = getObjectInstanceUsingRefAddress(ref.getAll(), obj, name, nameCtx,
environment);
             }
         }
+        
+		logger.log(Level.FINE, "Step 4: result = " + result);
 
         // Step 5 - if we still don't have a resolved object goto the object factory builds
in the SR.
         if (result == null || result == obj) {
             result = getObjectInstanceUsingObjectFactoryBuilders(obj, name, nameCtx, environment);
         }
 
-        // Step 6 - finally as a last ditch effort attempt to use all the registered ObjectFactories
in the SR.
+		logger.log(Level.FINE, "Step 5: result = " + result);
+
+        // Step 6 - Attempt to use all the registered ObjectFactories in the SR.
         if (result == null || result == obj) {                
             if ((obj instanceof Reference && ((Reference) obj).getFactoryClassName()
== null) ||
                 !(obj instanceof Reference)) {
                 result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment);
             }
         }
+ 
+		logger.log(Level.FINE, "Step 6: result = " + result);
+
+		// Extra, non-standard, bonus step 7. 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);
+		} 
+		
+		logger.log(Level.FINE, "Step 7: result = " + result);
 
         return (result == null) ? obj : result;
     }
+    
+    /*
+     * Attempt to obtain an Object instance via the java.naming.factory.object property
+     */
+    protected Object getObjectInstanceViaContextDotObjectFactories(Object obj,
+            Name name,
+            Context nameCtx,
+            Hashtable<?, ?> environment) throws Exception
+    {
+    	Object result = null;
+    	String factories = (String) environment.get(Context.OBJECT_FACTORIES);
+		if (factories != null && factories.length() > 0) {
+			String[] candidates = factories.split(":");
+			ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
{
+				public ClassLoader run() {
+					return Thread.currentThread().getContextClassLoader();
+				}
+			});
+			for (String cand : candidates) {
+				ObjectFactory factory = null;
+				try {
+					@SuppressWarnings("unchecked")
+					Class<ObjectFactory> clz = (Class<ObjectFactory>) cl.loadClass(cand);
+					factory = clz.newInstance();
+				} catch (Exception e) {
+					logger.log(Level.FINE, "Exception instantiating factory: " + e);
+					continue;
+				}
+				logger.log(Level.FINE, "cand=" + cand + " factory=" + factory);
+				if (factory != null) {
+					result = factory.getObjectInstance(obj, name, nameCtx, environment);
+				}
+				if (result != null && result != obj) break;
+			}
+		}
+		return (result == null) ? obj : result;
+    }
 
     protected Object getObjectInstanceUsingObjectFactories(Object obj,
                                                            Name name,

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=1240069&r1=1240068&r2=1240069&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
Fri Feb  3 10:39:09 2012
@@ -20,17 +20,22 @@ package org.apache.aries.jndi;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 
 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;
@@ -179,4 +184,24 @@ public class ObjectFactoryTest
 
     assertSame("The naming manager should have returned the reference object", ref, obj);
   }
+  
+  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");
+		}
+  }
+	  
+  @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);
+  }
+
 }
\ No newline at end of file



Mime
View raw message