commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r961839 - in /commons/sandbox/at-digester/trunk: ./ src/java/org/apache/commons/digester/annotations/ src/java/org/apache/commons/digester/annotations/handlers/ src/java/org/apache/commons/digester/annotations/internal/ src/test/org/apache/...
Date Thu, 08 Jul 2010 17:06:47 GMT
Author: simonetripodi
Date: Thu Jul  8 17:06:47 2010
New Revision: 961839

URL: http://svn.apache.org/viewvc?rev=961839&view=rev
Log:
changed the API architecture using spi, delegating optionally to clients the issue of caching
handlers

Modified:
    commons/sandbox/at-digester/trunk/   (props changed)
    commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java
    commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
    commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
    commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/internal/InMemoryLRUCache.java
    commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/AbstractAnnotatedPojoTestCase.java
    commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/employee/EmployeeTestCase.java

Propchange: commons/sandbox/at-digester/trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Thu Jul  8 17:06:47 2010
@@ -1 +1,2 @@
 target
+maven-eclipse.xml

Modified: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java?rev=961839&r1=961838&r2=961839&view=diff
==============================================================================
--- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java
(original)
+++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java
Thu Jul  8 17:06:47 2010
@@ -25,17 +25,18 @@ import org.apache.commons.digester.Diges
 import org.apache.commons.digester.Rule;
 import org.apache.commons.digester.RuleSet;
 import org.apache.commons.digester.annotations.DigesterLoaderHandler.DefaultLoaderHandler;
+import org.apache.commons.digester.annotations.internal.DefaultAnnotationRuleProviderFactory;
+import org.apache.commons.digester.annotations.internal.DefaultDigesterLoaderHandlerFactory;
 import org.apache.commons.digester.annotations.internal.InMemoryLRUCache;
 import org.apache.commons.digester.annotations.reflect.MethodArgument;
+import org.apache.commons.digester.annotations.spi.AnnotationRuleProviderFactory;
+import org.apache.commons.digester.annotations.spi.DigesterLoaderHandlerFactory;
 import org.apache.commons.digester.annotations.utils.AnnotationUtils;
 
 /**
  * This class manages the creation of Digester instances analyzing target classes
  * annotated with digester annotations.
  *
- * It avoids iterate the annotations analysis for already analyzed classes,
- * using an in-memory LRU cache.
- *
  * @version $Id$
  * @since 2.1
  */
@@ -45,14 +46,38 @@ public final class DigesterLoader {
      * In-memory LRU cache that stores already analyzed classes and relative
      * {@link RuleSet}.
      */
-    private static final InMemoryLRUCache<Class<?>, FromAnnotationsRuleSet> CACHED_RULESET
=
-        new InMemoryLRUCache<Class<?>, FromAnnotationsRuleSet>();
+    private final InMemoryLRUCache cachedRuleSet = InMemoryLRUCache.getInstance();
+
+    private final AnnotationRuleProviderFactory annotationRuleProviderFactory;
+
+    private final DigesterLoaderHandlerFactory digesterLoaderHandlerFactory;
+
+    /**
+     * 
+     */
+    public DigesterLoader() {
+        this(new DefaultAnnotationRuleProviderFactory(),
+                new DefaultDigesterLoaderHandlerFactory());
+    }
 
     /**
-     * This class can't be instantiated.
+     * Creates a new {@link DigesterLoader} instance.
+     *
+     * @param annotationRuleProviderFactory
+     * @param digesterLoaderHandlerFactory
      */
-    private DigesterLoader() {
-        // do nothing
+    public DigesterLoader(AnnotationRuleProviderFactory annotationRuleProviderFactory,
+            DigesterLoaderHandlerFactory digesterLoaderHandlerFactory) {
+        this.annotationRuleProviderFactory = annotationRuleProviderFactory;
+        this.digesterLoaderHandlerFactory = digesterLoaderHandlerFactory;
+    }
+
+    protected AnnotationRuleProviderFactory getAnnotationRuleProviderFactory() {
+        return annotationRuleProviderFactory;
+    }
+
+    protected DigesterLoaderHandlerFactory getDigesterLoaderHandlerFactory() {
+        return digesterLoaderHandlerFactory;
     }
 
     /**
@@ -62,7 +87,7 @@ public final class DigesterLoader {
      * @param target the class has to be analyzed.
      * @return a new Digester instance.
      */
-    public static Digester createDigester(final Class<?> target) {
+    public Digester createDigester(final Class<?> target) {
         Digester digester = new Digester();
         digester.setClassLoader(target.getClassLoader());
         addRules(target, digester);
@@ -76,7 +101,7 @@ public final class DigesterLoader {
      * @param target the class has to be analyzed.
      * @param digester the Digester instance reference.
      */
-    public static void addRules(final Class<?> target, final Digester digester) {
+    public void addRules(final Class<?> target, final Digester digester) {
         RuleSet ruleSet = getRuleSet(target);
         ruleSet.addRuleInstances(digester);
     }
@@ -91,14 +116,14 @@ public final class DigesterLoader {
      * @param target the class has to be analyzed.
      * @return a new {@link RuleSet}.
      */
-    public static RuleSet getRuleSet(final Class<?> target) {
-        if (CACHED_RULESET.containsKey(target)) {
-            return CACHED_RULESET.get(target);
+    public RuleSet getRuleSet(final Class<?> target) {
+        if (this.cachedRuleSet.containsKey(target)) {
+            return this.cachedRuleSet.get(target);
         }
 
-        FromAnnotationsRuleSet ruleSet = new FromAnnotationsRuleSet();
+        FromAnnotationsRuleSet ruleSet = new FromAnnotationsRuleSet(this);
         addRulesTo(target, ruleSet);
-        CACHED_RULESET.put(target, ruleSet);
+        this.cachedRuleSet.put(target, ruleSet);
 
         return ruleSet;
     }
@@ -110,15 +135,15 @@ public final class DigesterLoader {
      * @param target the class has to be analyzed.
      * @param ruleSet the RuleSet where adding the providers.
      */
-    public static void addRulesTo(final Class<?> target, FromAnnotationsRuleSet ruleSet)
{
+    public void addRulesTo(final Class<?> target, FromAnnotationsRuleSet ruleSet) {
         if (target == Object.class
                 || target.isInterface()
                 || ruleSet.mapsClass(target)) {
             return;
         }
 
-        if (CACHED_RULESET.containsKey(target)) {
-            ruleSet.addRulesProviderFrom(CACHED_RULESET.get(target));
+        if (this.cachedRuleSet.containsKey(target)) {
+            ruleSet.addRulesProviderFrom(this.cachedRuleSet.get(target));
             ruleSet.addMappedClass(target);
             return;
         }
@@ -153,7 +178,7 @@ public final class DigesterLoader {
      * @param element the current element under analysis.
      * @param ruleSet the ruleSet where add providers.
      */
-    private static void handle(AnnotatedElement element, FromAnnotationsRuleSet ruleSet)
{
+    private void handle(AnnotatedElement element, FromAnnotationsRuleSet ruleSet) {
         for (Annotation annotation : element.getAnnotations()) {
             handle(annotation, element, ruleSet);
         }
@@ -167,7 +192,7 @@ public final class DigesterLoader {
      * @param element the current visited element.
      */
     @SuppressWarnings("unchecked")
-    private static <A extends Annotation, E extends AnnotatedElement> void handle(A
annotation, E element, FromAnnotationsRuleSet ruleSet) {
+    private <A extends Annotation, E extends AnnotatedElement> void handle(A annotation,
E element, FromAnnotationsRuleSet ruleSet) {
         Class<?> annotationType = annotation.annotationType();
 
         // check if it is one of the @*.List annotation
@@ -189,15 +214,10 @@ public final class DigesterLoader {
                         annotation,
                         element);
             } else {
-                DigesterLoaderHandler<Annotation,AnnotatedElement> handler = null;
-
-                try {
-                    handler = (DigesterLoaderHandler<Annotation, AnnotatedElement>)
digesterRule.handledBy().newInstance();
-                } catch (Throwable t) {
-                    throw new DigesterLoadingException("Impossible to instance handler of
type '"
-                            + digesterRule.handledBy().getName()
-                            + "', see nested exceptions", t);
-                }
+                Class<? extends DigesterLoaderHandler<Annotation, AnnotatedElement>>
handlerType =
+                    (Class<? extends DigesterLoaderHandler<Annotation, AnnotatedElement>>)
digesterRule.handledBy();
+                DigesterLoaderHandler<Annotation, AnnotatedElement> handler =
+                    this.digesterLoaderHandlerFactory.newInstance(handlerType);
 
                 // run!
                 handler.handle(annotation, element, ruleSet);

Modified: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java?rev=961839&r1=961838&r2=961839&view=diff
==============================================================================
--- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
(original)
+++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/FromAnnotationsRuleSet.java
Thu Jul  8 17:06:47 2010
@@ -51,12 +51,23 @@ public final class FromAnnotationsRuleSe
      */
     private final Set<Class<?>> mappedClasses = new HashSet<Class<?>>();
 
+    private final DigesterLoader digesterLoader;
+
     /**
      * The namespace URI.
      */
     private volatile String namespaceURI;
 
     /**
+     * 
+     *
+     * @param annotationRuleProviderFactory
+     */
+    protected FromAnnotationsRuleSet(DigesterLoader digesterLoader) {
+        this.digesterLoader = digesterLoader;
+    }
+
+    /**
      * {@inheritDoc}
      */
     public void addRuleInstances(Digester digester) {
@@ -75,6 +86,16 @@ public final class FromAnnotationsRuleSe
     }
 
     /**
+     * Analyzes the target class and adds the {@link AnnotationRuleProvider}s to
+     * this {@link FromAnnotationsRuleSet}.
+     *
+     * @param target the class has to be analyzed.
+     */
+    public void addRules(Class<?> target) {
+        this.digesterLoader.addRulesTo(target, this);
+    }
+
+    /**
      * Builds and register an {@link AnnotationRuleProvider} for a specific
      * pattern.
      *
@@ -88,15 +109,10 @@ public final class FromAnnotationsRuleSe
             Class<T> klass,
             A annotation,
             E element) {
-        try {
-            T annotationRuleProvider = klass.newInstance();
-            annotationRuleProvider.init(annotation, element);
-            this.addRuleProvider(pattern, annotationRuleProvider);
-        } catch (Throwable t) {
-            throw new DigesterLoadingException("Impossible to instantiate provider of type
'"
-                    + klass.getName()
-                    + "', see nested exceptions", t);
-        }
+        T annotationRuleProvider =
+            this.digesterLoader.getAnnotationRuleProviderFactory().newInstance(klass);
+        annotationRuleProvider.init(annotation, element);
+        this.addRuleProvider(pattern, annotationRuleProvider);
     }
 
     /**

Modified: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java?rev=961839&r1=961838&r2=961839&view=diff
==============================================================================
--- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
(original)
+++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
Thu Jul  8 17:06:47 2010
@@ -23,7 +23,6 @@ import java.lang.reflect.Modifier;
 import org.apache.commons.digester.Rule;
 import org.apache.commons.digester.annotations.AnnotationRuleProvider;
 import org.apache.commons.digester.annotations.CreationRule;
-import org.apache.commons.digester.annotations.DigesterLoader;
 import org.apache.commons.digester.annotations.DigesterLoaderHandler;
 import org.apache.commons.digester.annotations.DigesterLoadingException;
 import org.apache.commons.digester.annotations.DigesterRule;
@@ -110,7 +109,7 @@ public final class MethodHandler impleme
     private <A extends Annotation> void doHandle(A methodAnnotation, Annotation annotation,
Method method, Class<?> type, FromAnnotationsRuleSet ruleSet) {
         if (annotation.annotationType().isAnnotationPresent(DigesterRule.class)
                 && annotation.annotationType().isAnnotationPresent(CreationRule.class))
{
-            DigesterLoader.addRulesTo(type, ruleSet);
+            ruleSet.addRules(type);
 
             DigesterRule digesterRule = methodAnnotation.annotationType().getAnnotation(DigesterRule.class);
             Class<? extends AnnotationRuleProvider<A, Method, ? extends Rule>>
providerType = (Class<? extends AnnotationRuleProvider<A, Method, ? extends Rule>>)
digesterRule.providedBy();

Modified: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/internal/InMemoryLRUCache.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/internal/InMemoryLRUCache.java?rev=961839&r1=961838&r2=961839&view=diff
==============================================================================
--- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/internal/InMemoryLRUCache.java
(original)
+++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/internal/InMemoryLRUCache.java
Thu Jul  8 17:06:47 2010
@@ -20,13 +20,15 @@ import java.io.Serializable;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.apache.commons.digester.annotations.FromAnnotationsRuleSet;
+
 /**
  * Simple in-memory LRU cache implementation.
  *
  * @version $Id$
  * @since 2.1
  */
-public final class InMemoryLRUCache<K, V> implements Serializable {
+public final class InMemoryLRUCache implements Serializable {
 
     /**
      * This class serialVersionUID.
@@ -34,24 +36,38 @@ public final class InMemoryLRUCache<K, V
     private static final long serialVersionUID = 1L;
 
     /**
+     * The static instance reference.
+     */
+    private final static InMemoryLRUCache INSTANCE = new InMemoryLRUCache();
+
+    /**
+     * Returns the static instance reference.
+     *
+     * @return the static instance reference.
+     */
+    public static InMemoryLRUCache getInstance() {
+        return INSTANCE;
+    }
+
+    /**
      * The fixed cache size.
      */
-    private static final int CACHE_SIZE = 255;
+    private final int cacheSize = 255;
 
     /**
      * The fixed cache load facor.
      */
-    private static final float LOAD_FACTOR = 0.75f;
+    private final float loadFactor = 0.75f;
 
     /**
      * The fixed cache capacity.
      */
-    private static final int CACHE_CAPACITY = (int) Math.ceil(CACHE_SIZE / LOAD_FACTOR) +
1;
+    private final int capacity = (int) Math.ceil(this.cacheSize / this.loadFactor) + 1;
 
     /**
      * The map that implements the LRU cache.
      */
-    private final Map<K, V> data = new LinkedHashMap<K, V>(CACHE_CAPACITY, LOAD_FACTOR)
{
+    private final Map<Class<?>, FromAnnotationsRuleSet> data = new LinkedHashMap<Class<?>,
FromAnnotationsRuleSet>(capacity, loadFactor) {
         /**
          * This class serialVersionUID.
          */
@@ -61,19 +77,26 @@ public final class InMemoryLRUCache<K, V
          * {@inheritDoc}
          */
         @Override
-        protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
-            return size() > CACHE_SIZE;
+        protected boolean removeEldestEntry(Map.Entry<Class<?>, FromAnnotationsRuleSet>
eldest) {
+            return size() > cacheSize;
         }
     };
 
     /**
+     * This class can't be instantiated.
+     */
+    private InMemoryLRUCache() {
+        // do nothing
+    }
+
+    /**
      * Returns true if this cache contains a mapping for the specified key.
      *
      * @param key key whose presence in this map is to be tested.
      * @return true if this map contains a mapping for the specified key, false
      *         otherwise.
      */
-    public boolean containsKey(K key) {
+    public boolean containsKey(Class<?> key) {
         checkKey(key);
         return this.data.containsKey(key);
     }
@@ -88,7 +111,7 @@ public final class InMemoryLRUCache<K, V
      * @return the value to which the specified key is cached, null if this
      *         cache contains no mapping for the key.
      */
-    public V get(K key) {
+    public FromAnnotationsRuleSet get(Class<?> key) {
         checkKey(key);
         return this.data.get(key);
     }
@@ -101,7 +124,7 @@ public final class InMemoryLRUCache<K, V
      * @param key key with which the specified value is to be associated.
      * @param value value to be associated with the specified key.
      */
-    public void put(K key, V value) {
+    public void put(Class<?> key, FromAnnotationsRuleSet value) {
         checkKey(key);
         this.data.put(key, value);
     }
@@ -112,7 +135,7 @@ public final class InMemoryLRUCache<K, V
      * @param <T> the generic key type.
      * @param key the key object.
      */
-    private static <T> void checkKey(T key) {
+    private static void checkKey(Class<?> key) {
         if (key == null) {
             throw new IllegalArgumentException("null keys not supported");
         }

Modified: commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/AbstractAnnotatedPojoTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/AbstractAnnotatedPojoTestCase.java?rev=961839&r1=961838&r2=961839&view=diff
==============================================================================
--- commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/AbstractAnnotatedPojoTestCase.java
(original)
+++ commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/AbstractAnnotatedPojoTestCase.java
Thu Jul  8 17:06:47 2010
@@ -30,6 +30,8 @@ import org.apache.commons.digester.Diges
  */
 public abstract class AbstractAnnotatedPojoTestCase extends TestCase {
 
+    private final DigesterLoader digesterLoader = new DigesterLoader();
+
     /**
      * Loads the digester rules parsing the expected object class, parses the
      * XML and verify the digester produces the same result.
@@ -43,8 +45,7 @@ public abstract class AbstractAnnotatedP
         String resource = clazz.getSimpleName() + ".xml";
         InputStream input = clazz.getResourceAsStream(resource);
 
-        Digester digester = DigesterLoader.createDigester(clazz);
-        digester = DigesterLoader.createDigester(clazz);
+        Digester digester = this.digesterLoader.createDigester(clazz);
         this.decorate(digester);
 
         Object actual = digester.parse(input);
@@ -56,6 +57,10 @@ public abstract class AbstractAnnotatedP
         assertEquals(expected, actual);
     } 
 
+    protected DigesterLoader getDigesterLoader() {
+        return this.digesterLoader;
+    }
+
     protected void decorate(Digester digester) {
         // do nothing
     }

Modified: commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/employee/EmployeeTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/employee/EmployeeTestCase.java?rev=961839&r1=961838&r2=961839&view=diff
==============================================================================
--- commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/employee/EmployeeTestCase.java
(original)
+++ commons/sandbox/at-digester/trunk/src/test/org/apache/commons/digester/annotations/employee/EmployeeTestCase.java
Thu Jul  8 17:06:47 2010
@@ -18,7 +18,6 @@ package org.apache.commons.digester.anno
 
 import org.apache.commons.digester.Digester;
 import org.apache.commons.digester.annotations.AbstractAnnotatedPojoTestCase;
-import org.apache.commons.digester.annotations.DigesterLoader;
 
 /**
  * 
@@ -53,7 +52,7 @@ public final class EmployeeTestCase exte
 
     @Override
     protected void decorate(Digester digester) {
-        DigesterLoader.addRules(Address.class, digester);
+        this.getDigesterLoader().addRules(Address.class, digester);
     }
 
 }



Mime
View raw message