You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2009/06/19 17:16:07 UTC

svn commit: r786541 - in /felix/trunk/framework/src/main/java/org/apache/felix/framework/util: SecureAction.java ldap/Parser.java

Author: rickhall
Date: Fri Jun 19 15:16:05 2009
New Revision: 786541

URL: http://svn.apache.org/viewvc?rev=786541&view=rev
Log:
Added support in LDAP evaluation for creating Comparable/unknown objects
with non-public constructors. (FELIX-1257)

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java?rev=786541&r1=786540&r2=786541&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java Fri Jun 19 15:16:05 2009
@@ -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;
         }

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java?rev=786541&r1=786540&r2=786541&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java Fri Jun 19 15:16:05 2009
@@ -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)
                 {