You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by sb...@apache.org on 2020/10/07 06:49:12 UTC
[aries] branch trunk updated: [jndi][ARIES-2000] Swallow exceptions
when trying every ObjectFactory from NamingManager.getObjectInstance()
This is an automated email from the ASF dual-hosted git repository.
sbratton pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/aries.git
The following commit(s) were added to refs/heads/trunk by this push:
new 34a60ab [jndi][ARIES-2000] Swallow exceptions when trying every ObjectFactory from NamingManager.getObjectInstance()
new eb912a8 Merge pull request #113 from chackoj/ARIES-2000
34a60ab is described below
commit 34a60abeb4e574c3e0cf0f61d4955ae07f48e027
Author: Joe Chacko <14...@users.noreply.github.com>
AuthorDate: Tue Sep 15 11:12:20 2020 +0100
[jndi][ARIES-2000] Swallow exceptions when trying every ObjectFactory from NamingManager.getObjectInstance()
---
.../org/apache/aries/jndi/ObjectFactoryHelper.java | 28 ++++++++++++++--------
.../org/apache/aries/jndi/ObjectFactoryTest.java | 14 ++++++++++-
2 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java b/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
index 9f82ddf..9146640 100644
--- a/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
+++ b/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
@@ -27,13 +27,13 @@ import javax.naming.directory.Attributes;
import javax.naming.spi.DirObjectFactory;
import javax.naming.spi.ObjectFactory;
import javax.naming.spi.ObjectFactoryBuilder;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
+import static org.osgi.service.jndi.JNDIConstants.JNDI_URLSCHEME;
+
public class ObjectFactoryHelper implements ObjectFactory {
private static final Logger logger = Logger.getLogger(ObjectFactoryHelper.class.getName());
@@ -120,13 +120,15 @@ public class ObjectFactoryHelper implements ObjectFactory {
if (canCallObjectFactory(obj, ref)) {
ObjectFactory factory = Activator.getService(callerContext, ref);
if (factory != null) {
- Object result = getObjectFromFactory(obj, name, nameCtx, environment, attrs, factory);
- // 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) {
- return result;
- }
+ try {
+ Object result = getObjectFromFactory(obj, name, nameCtx, environment, attrs, factory);
+ // 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) {
+ return result;
+ }
+ } catch (Exception ignored) {} // only care about factories that CAN transform this object
}
}
}
@@ -136,7 +138,13 @@ public class ObjectFactoryHelper implements ObjectFactory {
private boolean canCallObjectFactory(Object obj, ServiceReference ref) {
if (obj instanceof Reference) return true;
Object prop = ref.getProperty("aries.object.factory.requires.reference");
- return (prop == null) || !(prop instanceof Boolean) || !(Boolean) prop;
+ // if the ObjectFactory needs a reference, then give up straight away
+ if (Boolean.TRUE.equals(prop)) return false;
+ // ObjectFactory services with an osgi.jndi.url.scheme property are only for converting URLs
+ if (ref.getProperty(JNDI_URLSCHEME) != null) return false;
+ // TODO: ObjectFactory services registered with their implementation class names are for References that specify the ObjectFactory class
+ // We've eliminated all other possibilities, so this factory IS callable.
+ return true;
}
private Object getObjectInstanceUsingClassName(Object reference,
diff --git a/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java b/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
index 378a4bf..f2aae06 100644
--- a/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
+++ b/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
@@ -198,6 +198,19 @@ public class ObjectFactoryTest {
env.remove(Context.OBJECT_FACTORIES);
}
+ @Test
+ public void testObjectFactoryThatThrowsIsIgnored() throws Exception {
+ final ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
+ bc.registerService(ObjectFactory.class.getName(), factory, null);
+ final Skeleton skeleton = Skeleton.getSkeleton(factory);
+ final MethodCall getObjectInstance = new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class);
+ skeleton.setThrows(getObjectInstance, new Exception());
+ Object original = new Object(){};
+ Object processed = NamingManager.getObjectInstance(original, null, null, env);
+ skeleton.assertCalled(getObjectInstance);
+ assertEquals("The original object should be returned despite the object factory throwing an exception", original, processed);
+ }
+
public static class DummyObjectFactory implements ObjectFactory {
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
@@ -206,5 +219,4 @@ public class ObjectFactoryTest {
return new String("pass");
}
}
-
}
\ No newline at end of file