Author: simonetripodi
Date: Sat Feb 12 00:11:20 2011
New Revision: 1069997

URL: http://svn.apache.org/viewvc?rev=1069997&view=rev
Log:
ObjectCreationFactory type on FactoryCreateRule resolved at binding time, not 
parsing time

Modified:
    
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/FactoryCreateBuilderImpl.java
    
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/LinkedRuleBuilderImpl.java
    
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/FactoryCreateRule.java
    
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/FactoryCreateBuilder.java

Modified: 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/FactoryCreateBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/FactoryCreateBuilderImpl.java?rev=1069997&r1=1069996&r2=1069997&view=diff
==============================================================================
--- 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/FactoryCreateBuilderImpl.java
 (original)
+++ 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/FactoryCreateBuilderImpl.java
 Sat Feb 12 00:11:20 2011
@@ -29,7 +29,9 @@ final class FactoryCreateBuilderImpl
         extends AbstractBackToLinkedRuleBuilder<FactoryCreateRule>
         implements FactoryCreateBuilder {
 
-    private String className;
+    private final ClassLoader classLoader;
+
+    private Class<? extends ObjectCreationFactory<?>> type;
 
     private String attributeName;
 
@@ -40,34 +42,52 @@ final class FactoryCreateBuilderImpl
     public FactoryCreateBuilderImpl(String keyPattern,
             String namespaceURI,
             RulesBinder mainBinder,
-            LinkedRuleBuilderImpl mainBuilder) {
+            LinkedRuleBuilderImpl mainBuilder,
+            ClassLoader classLoader) {
         super(keyPattern, namespaceURI, mainBinder, mainBuilder);
+        this.classLoader = classLoader;
     }
 
     /**
      * {@inheritDoc}
      */
-    public FactoryCreateBuilderImpl ofType(/* @Nullable */String className) {
-        this.className = className;
+    public FactoryCreateBuilder ofType(String className) {
+        if (className == null) {
+            this.reportError("factoryCreate().ofType(Class<?>)", "NULL Java 
type not allowed");
+        }
+
+        try {
+            Class<?> type = this.classLoader.loadClass(className);
+            if (!ObjectCreationFactory.class.isAssignableFrom(type)) {
+                this.reportError("factoryCreate().ofType(Class<?>)", "NULL 
Java type not allowed");
+                return this;
+            }
+
+            this.type = (Class<? extends ObjectCreationFactory<?>>) type;
+        } catch (ClassNotFoundException e) {
+            this.reportError("factoryCreate().ofType(String)", 
String.format("class '%s' cannot be load", className));
+        }
+
         return this;
     }
 
     /**
      * {@inheritDoc}
      */
-    public FactoryCreateBuilderImpl ofType(Class<?> type) {
+    public <T> FactoryCreateBuilder ofType(Class<? extends 
ObjectCreationFactory<T>> type) {
         if (type == null) {
             this.reportError("factoryCreate().ofType(Class<?>)", "NULL Java 
type not allowed");
-            return this;
         }
 
-        return this.ofType(type.getName());
+        this.type = type;
+
+        return this;
     }
 
     /**
      * {@inheritDoc}
      */
-    public <T> FactoryCreateBuilderImpl usingFactory(/* @Nullable 
*/ObjectCreationFactory<T> creationFactory) {
+    public <T> FactoryCreateBuilder usingFactory(/* @Nullable 
*/ObjectCreationFactory<T> creationFactory) {
         this.creationFactory = creationFactory;
         return this;
     }
@@ -75,7 +95,7 @@ final class FactoryCreateBuilderImpl
     /**
      * {@inheritDoc}
      */
-    public FactoryCreateBuilderImpl overriddenByAttribute(/* @Nullable 
*/String attributeName) {
+    public FactoryCreateBuilder overriddenByAttribute(/* @Nullable */String 
attributeName) {
         this.attributeName = attributeName;
         return this;
     }
@@ -83,7 +103,7 @@ final class FactoryCreateBuilderImpl
     /**
      * {@inheritDoc}
      */
-    public FactoryCreateBuilderImpl ignoreCreateExceptions(boolean 
ignoreCreateExceptions) {
+    public FactoryCreateBuilder ignoreCreateExceptions(boolean 
ignoreCreateExceptions) {
         this.ignoreCreateExceptions = ignoreCreateExceptions;
         return this;
     }
@@ -93,12 +113,12 @@ final class FactoryCreateBuilderImpl
      */
     @Override
     protected FactoryCreateRule createRule() {
-        if (className == null && attributeName == null && creationFactory == 
null) {
+        if (type == null && attributeName == null && creationFactory == null) {
             this.reportError("factoryCreate()",
                     "at least one between 'className' ar 'attributeName' or 
'creationFactory' has to be specified");
         }
 
-        return new FactoryCreateRule(this.className, this.attributeName, 
this.creationFactory, this.ignoreCreateExceptions);
+        return new FactoryCreateRule(this.type, this.attributeName, 
this.creationFactory, this.ignoreCreateExceptions);
     }
 
 }

Modified: 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/LinkedRuleBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/LinkedRuleBuilderImpl.java?rev=1069997&r1=1069996&r2=1069997&view=diff
==============================================================================
--- 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/LinkedRuleBuilderImpl.java
 (original)
+++ 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/LinkedRuleBuilderImpl.java
 Sat Feb 12 00:11:20 2011
@@ -111,7 +111,8 @@ final class LinkedRuleBuilderImpl implem
      * {@inheritDoc}
      */
     public FactoryCreateBuilder factoryCreate() {
-        return this.addProvider(new FactoryCreateBuilderImpl(this.keyPattern, 
this.namespaceURI, this.mainBinder, this));
+        return this.addProvider(
+                new FactoryCreateBuilderImpl(this.keyPattern, 
this.namespaceURI, this.mainBinder, this, this.classLoader));
     }
 
     /**

Modified: 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/FactoryCreateRule.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/FactoryCreateRule.java?rev=1069997&r1=1069996&r2=1069997&view=diff
==============================================================================
--- 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/FactoryCreateRule.java
 (original)
+++ 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/FactoryCreateRule.java
 Sat Feb 12 00:11:20 2011
@@ -47,10 +47,10 @@ public class FactoryCreateRule extends R
     private String attributeName = null;
 
     /**
-     * The Java class name of the ObjectCreationFactory to be created.
+     * The Java class of the ObjectCreationFactory to be created.
      * This class must have a no-arguments constructor.
      */
-    private String className = null;
+    private Class<? extends ObjectCreationFactory<?>> type = null;
 
     /**
      * The object creation factory we will use to instantiate objects
@@ -72,8 +72,11 @@ public class FactoryCreateRule extends R
      * @param ignoreCreateExceptions if true, exceptions thrown by the object
      *  creation factory will be ignored.
      */
-    public <T> FactoryCreateRule(String className, String attributeName, 
ObjectCreationFactory<T> creationFactory, boolean ignoreCreateExceptions) {
-        this.className = className;
+    public FactoryCreateRule(Class<? extends ObjectCreationFactory<?>> type,
+            String attributeName,
+            ObjectCreationFactory<?> creationFactory,
+            boolean ignoreCreateExceptions) {
+        this.type = type;
         this.attributeName = attributeName;
         this.creationFactory = creationFactory;
         this.ignoreCreateExceptions = ignoreCreateExceptions;
@@ -169,8 +172,8 @@ public class FactoryCreateRule extends R
      */
     @Override
     public String toString() {
-        return String.format("FactoryCreateRule[className=%s, 
attributeName=%s, creationFactory=%s]",
-                this.className,
+        return String.format("FactoryCreateRule[type=%s, attributeName=%s, 
creationFactory=%s]",
+                this.type != null ? this.type.getName() : "",
                 this.attributeName,
                 this.creationFactory);
     }
@@ -185,19 +188,21 @@ public class FactoryCreateRule extends R
      */
     protected ObjectCreationFactory<?> getFactory(Attributes attributes) 
throws Exception {
         if (this.creationFactory == null) {
-            String realClassName = this.className;
+            Class<?> clazz = this.type;
+
             if (this.attributeName != null) {
                 String value = attributes.getValue(this.attributeName);
                 if (value != null) {
-                    realClassName = value;
+                    clazz = 
this.getDigester().getClassLoader().loadClass(value);
                 }
             }
+
             if (this.getDigester().getLog().isDebugEnabled()) {
                 
this.getDigester().getLog().debug(String.format("[FactoryCreateRule]{%s} New 
factory %s",
                         this.getDigester().getMatch(),
-                        realClassName));
+                        clazz.getName()));
             }
-            Class<?> clazz = 
this.getDigester().getClassLoader().loadClass(realClassName);
+
             this.creationFactory = (ObjectCreationFactory<?>) 
clazz.newInstance();
             this.creationFactory.setDigester(this.getDigester());
         }

Modified: 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/FactoryCreateBuilder.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/FactoryCreateBuilder.java?rev=1069997&r1=1069996&r2=1069997&view=diff
==============================================================================
--- 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/FactoryCreateBuilder.java
 (original)
+++ 
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/FactoryCreateBuilder.java
 Sat Feb 12 00:11:20 2011
@@ -41,7 +41,7 @@ public interface FactoryCreateBuilder ex
      * @param type Java class of the object creation factory class
      * @return this builder instance
      */
-    FactoryCreateBuilder ofType(Class<?> type);
+    <T> FactoryCreateBuilder ofType(Class<? extends ObjectCreationFactory<T>> 
type);
 
     /**
      * Construct a factory create rule using the given, already instantiated, 
{@link ObjectCreationFactory}.


Reply via email to