Added support in LDAP evaluation for creating Comparable/unknown objects
with non-public constructors. (FELIX-1257)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@786541 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java b/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
index fcf3a4c..9870cfe 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
@@ -25,7 +25,6 @@
import java.util.Hashtable;
import java.util.jar.JarFile;
-import org.apache.felix.framework.ModuleImpl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
@@ -742,6 +741,27 @@
}
}
+ public void setAccesssible(Constructor ctor)
+ {
+ if (System.getSecurityManager() != null)
+ {
+ Actions actions = (Actions) m_actions.get();
+ actions.set(Actions.SET_ACCESSIBLE_ACTION, ctor);
+ try
+ {
+ AccessController.doPrivileged(actions, m_acc);
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw (RuntimeException) e.getException();
+ }
+ }
+ else
+ {
+ ctor.setAccessible(true);
+ }
+ }
+
public Object invoke(Method method, Object target, Object[] params) throws Exception
{
if (System.getSecurityManager() != null)
@@ -961,7 +981,8 @@
public static final int GET_FIELD_ACTION = 31;
public static final int GET_DECLAREDMETHOD_ACTION = 32;
public static final int SET_ACCESSIBLE_ACTION = 33;
- public static final int INVOKE_DIRECTMETHOD_ACTION = 34;
+ public static final int SET_ACCESSIBLE_CTOR_ACTION = 34;
+ public static final int INVOKE_DIRECTMETHOD_ACTION = 35;
private int m_action = -1;
private Object m_arg1 = null;
@@ -1188,6 +1209,10 @@
{
((Method) arg1).setAccessible(true);
}
+ else if (action == SET_ACCESSIBLE_CTOR_ACTION)
+ {
+ ((Constructor) arg1).setAccessible(true);
+ }
return null;
}
diff --git a/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java b/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
index 52c7a71..7d88802 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
@@ -20,12 +20,17 @@
import java.io.IOException;
import java.io.PrintStream;
+import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
+import org.apache.felix.framework.util.SecureAction;
public class Parser
{
+ // Secure action to make object constructors accessible.
+ private static final SecureAction m_secureAction = new SecureAction();
+
//
// Parser contants.
//
@@ -1355,9 +1360,18 @@
}
else
{
- rhsComparable = (Comparable) lhs.getClass()
- .getConstructor(STRING_CLASS)
- .newInstance(new Object[] { rhs });
+ // The constructor may not be public, so we need to make it
+ // accessible in that case.
+ Constructor ctor = lhs.getClass().getConstructor(STRING_CLASS);
+ if (!ctor.isAccessible())
+ {
+ m_secureAction.setAccesssible(ctor);
+ }
+ // We don't invoke the constructor in a privileged block,
+ // since we don't want to elevate the objects privileges.
+ // If the object needs to, it should be doing a privileged
+ // block internally.
+ rhsComparable = (Comparable) ctor.newInstance(new Object[] { rhs });
}
}
catch (Exception ex)
@@ -1441,10 +1455,19 @@
{
try
{
- Object rhsObject = lhsClass
- .getConstructor(STRING_CLASS)
- .newInstance(new Object[] { rhs });
- return lhs.equals(rhsObject);
+ // The constructor may not be public, so we need to make it
+ // accessible in that case.
+ Constructor ctor = lhs.getClass().getConstructor(STRING_CLASS);
+ if (!ctor.isAccessible())
+ {
+ m_secureAction.setAccesssible(ctor);
+ }
+ // We don't invoke the constructor in a privileged block,
+ // since we don't want to elevate the objects privileges.
+ // If the object needs to, it should be doing a privileged
+ // block internally.
+ Object rhsObject = ctor.newInstance(new Object[] { rhs });
+ return lhs.equals(rhsObject);
}
catch (Exception ex)
{