jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r830499 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/config/ main/java/org/apache/jackrabbit/core/security/user/ test/java/org/apache/jackrabbit/core/security/user/
Date Wed, 28 Oct 2009 11:10:28 GMT
Author: jukka
Date: Wed Oct 28 11:10:27 2009
New Revision: 830499

URL: http://svn.apache.org/viewvc?rev=830499&view=rev
Log:
JCR-1438: Replace Config classes with factories

Marcel pointed out some potential caching issues with the Introspector, so let's avoid using
directly.

The code now uses a direct Class.getMethods() call to look up all available property setters
(public setSomething() methods that take a single argument and return nothing).

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserImporter.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java?rev=830499&r1=830498&r2=830499&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
Wed Oct 28 11:10:27 2009
@@ -16,17 +16,14 @@
  */
 package org.apache.jackrabbit.core.config;
 
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
@@ -173,23 +170,21 @@
             Object instance = objectClass.newInstance();
 
             // Set all configured bean properties
-            List<?> names = Collections.list(properties.propertyNames());
-            BeanInfo info = Introspector.getBeanInfo(objectClass, Object.class);
-            for (PropertyDescriptor property : info.getPropertyDescriptors()) {
-                String value = properties.getProperty(property.getName());
-                if (value != null) {
-                    setProperty(instance, property, value);
-                    names.remove(property.getName());
+            Map<String, Method> setters = getSetters(objectClass);
+            Enumeration<?> enumeration = properties.propertyNames();
+            while (enumeration.hasMoreElements()) {
+                String name = enumeration.nextElement().toString();
+                Method setter = setters.get(name);
+                if (setter != null) {
+                    String value = properties.getProperty(name);
+                    setProperty(instance, name, setter, value);
+                } else if (validate) {
+                    throw new ConfigurationException(
+                            "Configured class " + getClassName()
+                            + " does not contain a property named " + name);
                 }
             }
 
-            // Check that no invalid property names were configured
-            if (validate && !names.isEmpty()) {
-                throw new ConfigurationException(
-                        "Configured class " + getClassName()
-                        + " does not contain the properties " + names);
-            }
-
             return (T) instance;
         } catch (ClassNotFoundException e) {
             throw new ConfigurationException(
@@ -203,71 +198,69 @@
             throw new ConfigurationException(
                     "Configured bean implementation class " + getClassName()
                     + " is protected.", e);
-        } catch (IntrospectionException e) {
-            throw new ConfigurationException(
-                    "Configured bean implementation class " + getClassName()
-                    + " can not be introspected", e);
         }
     }
 
-    private void setProperty(
-            Object instance, PropertyDescriptor property, String value)
-            throws ConfigurationException {
-        Method method = property.getWriteMethod();
-        if (method == null) {
-            throw new ConfigurationException(
-                    "Property " + property.getName() + " of class "
-                    + getClassName() + " can not be written"); 
-        }
-
-        Class<?>[] types = method.getParameterTypes();
-        if (types.length != 1) {
-            throw new ConfigurationException(
-                    "Property " + property.getName() + " of class "
-                    + getClassName() + " has an invalid setter");
+    private Map<String, Method> getSetters(Class<?> klass) {
+        Map<String, Method> methods = new HashMap<String, Method>();
+        for (Method method : klass.getMethods()) {
+            String name = method.getName();
+            if (name.startsWith("set") && name.length() > 3
+                    && Modifier.isPublic(method.getModifiers())
+                    && Void.TYPE.equals(method.getReturnType())
+                    && method.getParameterTypes().length == 1) {
+                methods.put(
+                        name.substring(3, 4).toLowerCase() + name.substring(4),
+                        method);
+            }
         }
+        return methods;
+    }
 
-        Class<?> type = types[0];
+    private void setProperty(
+            Object instance, String name, Method setter, String value)
+            throws ConfigurationException {
+        Class<?> type = setter.getParameterTypes()[0];
         try {
-            if (types[0].isAssignableFrom(String.class)
-                || types[0].isAssignableFrom(Object.class)) {
-                method.invoke(instance, value);
-            } else if (types[0].isAssignableFrom(Boolean.TYPE)
-                    || types[0].isAssignableFrom(Boolean.class)) {
-                method.invoke(instance, Boolean.valueOf(value));
-            } else if (types[0].isAssignableFrom(Integer.TYPE)
-                    || types[0].isAssignableFrom(Integer.class)) {
-                method.invoke(instance, Integer.valueOf(value));
-            } else if (types[0].isAssignableFrom(Long.TYPE)
-                    || types[0].isAssignableFrom(Long.class)) {
-                method.invoke(instance, Long.valueOf(value));
-            } else if (types[0].isAssignableFrom(Double.TYPE)
-                    || types[0].isAssignableFrom(Double.class)) {
-                method.invoke(instance, Double.valueOf(value));
+            if (type.isAssignableFrom(String.class)
+                || type.isAssignableFrom(Object.class)) {
+                setter.invoke(instance, value);
+            } else if (type.isAssignableFrom(Boolean.TYPE)
+                    || type.isAssignableFrom(Boolean.class)) {
+                setter.invoke(instance, Boolean.valueOf(value));
+            } else if (type.isAssignableFrom(Integer.TYPE)
+                    || type.isAssignableFrom(Integer.class)) {
+                setter.invoke(instance, Integer.valueOf(value));
+            } else if (type.isAssignableFrom(Long.TYPE)
+                    || type.isAssignableFrom(Long.class)) {
+                setter.invoke(instance, Long.valueOf(value));
+            } else if (type.isAssignableFrom(Double.TYPE)
+                    || type.isAssignableFrom(Double.class)) {
+                setter.invoke(instance, Double.valueOf(value));
             } else {
                 throw new ConfigurationException(
                         "The type (" + type.getName()
-                        + ") of property " + property.getName() + " of class "
+                        + ") of property " + name + " of class "
                         + getClassName() + " is not supported");
             }
         } catch (NumberFormatException e) {
             throw new ConfigurationException(
                     "Invalid number format (" + value + ") for property "
-                    + property.getName() + " of class " + getClassName(), e);
+                    + name + " of class " + getClassName(), e);
         } catch (InvocationTargetException e) {
             throw new ConfigurationException(
-                    "Property " + property.getName() + " of class "
+                    "Property " + name + " of class "
                     + getClassName() + " can not be set to \"" + value + "\"",
                     e);
         } catch (IllegalAccessException e) {
             throw new ConfigurationException(
-                    "The setter of property " + property.getName()
+                    "The setter of property " + name
                     + " of class " + getClassName() + " can not be accessed",
                     e);
         } catch (IllegalArgumentException e) {
             throw new ConfigurationException(
                     "Unable to call the setter of property "
-                    + property.getName() + " of class " + getClassName(), e);
+                    + name + " of class " + getClassName(), e);
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserImporter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserImporter.java?rev=830499&r1=830498&r2=830499&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserImporter.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserImporter.java
Wed Oct 28 11:10:27 2009
@@ -460,22 +460,6 @@
         this.importBehavior = ImportBehavior.valueFromName(importBehaviorStr);
     }
 
-    /**
-     *
-     * @param importBehavior
-     */
-    public void setImportBehavior(int importBehavior) {
-        switch (importBehavior) {
-            case ImportBehavior.IGNORE:
-            case ImportBehavior.ABORT:
-            case ImportBehavior.BESTEFFORT:
-                this.importBehavior = importBehavior;
-                break;
-            default:
-                throw new IllegalArgumentException("Invalid import behavior: " + importBehavior);
-        }
-    }
-
     //--------------------------------------------------------------------------
     /**
      * Inner class used to postpone import of group membership to the very end

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java?rev=830499&r1=830498&r2=830499&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java
Wed Oct 28 11:10:27 2009
@@ -29,6 +29,7 @@
 import org.apache.jackrabbit.core.NodeImpl;
 import org.apache.jackrabbit.core.config.ImportConfig;
 import org.apache.jackrabbit.core.security.principal.PrincipalImpl;
+import org.apache.jackrabbit.core.security.user.UserImporter.ImportBehavior;
 import org.apache.jackrabbit.core.util.ReferenceChangeTracker;
 import org.apache.jackrabbit.core.xml.ProtectedPropertyImporter;
 import org.apache.jackrabbit.core.xml.SessionImporter;
@@ -1041,7 +1042,8 @@
 
         private PseudoConfig(int importBehavior) {
             this.ppi = createImporter();
-            ((UserImporter) ppi).setImportBehavior(importBehavior);
+            ((UserImporter) ppi).setImportBehavior(
+                    ImportBehavior.nameFromValue(importBehavior));
         }
 
         @Override



Mime
View raw message