commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1069124 - in /commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3: DigesterLoader.java RulesBinderImpl.java rulesbinder/ObjectCreateBuilder.java
Date Wed, 09 Feb 2011 21:38:05 GMT
Author: simonetripodi
Date: Wed Feb  9 21:38:05 2011
New Revision: 1069124

URL: http://svn.apache.org/viewvc?rev=1069124&view=rev
Log:
moved the String type resolution of ObjectCreationRule to binding time rather then execution
time

Modified:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/DigesterLoader.java
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/RulesBinderImpl.java
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/ObjectCreateBuilder.java

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/DigesterLoader.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/DigesterLoader.java?rev=1069124&r1=1069123&r2=1069124&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/DigesterLoader.java
(original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/DigesterLoader.java
Wed Feb  9 21:38:05 2011
@@ -71,7 +71,7 @@ public final class DigesterLoader {
     /**
      * The concrete {@link RulesBinder} implementation.
      */
-    private final RulesBinderImpl rulesBinder = new RulesBinderImpl();
+    private RulesBinderImpl rulesBinder;
 
     /**
      * The URLs of entityValidator that have been registered, keyed by the public
@@ -84,6 +84,10 @@ public final class DigesterLoader {
      */
     private final SAXParserFactory factory = SAXParserFactory.newInstance();
 
+    private final Object $lock = new Object();
+
+    private final Collection<RulesModule> rulesModules;
+
     private boolean useContextClassLoader;
 
     /**
@@ -102,14 +106,7 @@ public final class DigesterLoader {
      * @param rulesModules The modules containing the {@code Rule} binding
      */
     private DigesterLoader(Collection<RulesModule> rulesModules) {
-        for (RulesModule rulesModule : rulesModules) {
-            rulesModule.configure(this.rulesBinder);
-        }
-
-        // check if there were errors while binding rules
-        if (this.rulesBinder.containsErrors()) {
-            throw new DigesterLoadingException(this.rulesBinder.getErrors());
-        }
+        this.rulesModules = rulesModules;
     }
 
     /**
@@ -353,6 +350,15 @@ public final class DigesterLoader {
         ClassLoader classLoader = this.classLoader != null ? this.classLoader :
             (this.useContextClassLoader ? Thread.currentThread().getContextClassLoader()
: this.getClass().getClassLoader());
 
+        synchronized (this.$lock) {
+            if (this.rulesBinder == null) {
+                this.rulesBinder = new RulesBinderImpl(classLoader);
+                for (RulesModule rulesModule : rulesModules) {
+                    rulesModule.configure(this.rulesBinder);
+                }
+            }
+        }
+
         // check if there were errors while binding rules
         if (this.rulesBinder.containsErrors()) {
             throw new DigesterLoadingException(this.rulesBinder.getErrors());

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/RulesBinderImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/RulesBinderImpl.java?rev=1069124&r1=1069123&r2=1069124&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/RulesBinderImpl.java
(original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/RulesBinderImpl.java
Wed Feb  9 21:38:05 2011
@@ -487,17 +487,17 @@ final class RulesBinderImpl implements R
             public ObjectCreateBuilder createObject() {
                 return this.addProvider(new ObjectCreateBuilder() {
 
-                    private String className;
+                    private Class<?> type;
 
                     private String attributeName;
 
                     public ObjectCreateRule get() {
-                        if (this.className == null && this.attributeName == null)
{
-                            throw new DigesterLoadingException("{forPattern(\"%s\").createObject()}
At least one between 'className' or 'attributeName' has to be specified",
+                        if (this.type == null && this.attributeName == null) {
+                            throw new DigesterLoadingException("{forPattern(\"%s\").createObject()}
At least one between 'type' or 'attributeName' has to be specified",
                                     keyPattern);
                         }
 
-                        return setNamespaceAndReturn(new ObjectCreateRule(this.className,
this.attributeName));
+                        return setNamespaceAndReturn(new ObjectCreateRule(this.type, this.attributeName));
                     }
 
                     public LinkedRuleBuilder then() {
@@ -509,19 +509,33 @@ final class RulesBinderImpl implements R
                         return this;
                     }
 
-                    public ObjectCreateBuilder ofType(Class<?> type) {
+                    public <T> ObjectCreateBuilder ofType(Class<T> type) {
                         if (type == null) {
                             addError("{forPattern(\"%s\").createObject().ofType(Class<?>)}
NULL Java type not allowed",
                                     keyPattern);
                             return this;
                         }
 
-                        return this.ofType(type.getName());
+                        this.type = type;
+
+                        return this;
                     }
 
                     public ObjectCreateBuilder ofType(String className) {
-                        this.className = className;
-                        return this;
+                        if (className == null) {
+                            addError("{forPattern(\"%s\").createObject().ofType(String)}
NULL Java type not allowed",
+                                    keyPattern);
+                            return this;
+                        }
+
+                        try {
+                            return this.ofType(classLoader.loadClass(className));
+                        } catch (ClassNotFoundException e) {
+                            addError("{forPattern(\"%s\").createObject().ofType(String)}
class '%s' cannot be load",
+                                    keyPattern,
+                                    className);
+                            return this;
+                        }
                     }
 
                 });
@@ -676,7 +690,7 @@ final class RulesBinderImpl implements R
                                     this.paramTypes[i] = classLoader.loadClass(paramTypeNames[i]);
                                 } catch (ClassNotFoundException e) {
                                     // use the digester log
-                                    addError("{forPattern(\"%s\").callMethod().withParamTypes(%s)}
class %s cannot be load",
+                                    addError("{forPattern(\"%s\").callMethod().withParamTypes(%s)}
class '%s' cannot be load",
                                             keyPattern,
                                             Arrays.toString(paramTypeNames),
                                             paramTypeNames[i]);

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/ObjectCreateBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/ObjectCreateBuilder.java?rev=1069124&r1=1069123&r2=1069124&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/ObjectCreateBuilder.java
(original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/ObjectCreateBuilder.java
Wed Feb  9 21:38:05 2011
@@ -38,7 +38,7 @@ public interface ObjectCreateBuilder ext
      * @param type Java class of the object to be created
      * @return this builder instance
      */
-    ObjectCreateBuilder ofType(Class<?> type);
+    <T> ObjectCreateBuilder ofType(Class<T> type);
 
     /**
      * Allows specify the attribute containing an override class name if it is present.



Mime
View raw message