Author: simonetripodi
Date: Mon Jan 11 18:24:52 2010
New Revision: 897990

URL: http://svn.apache.org/viewvc?rev=897990&view=rev
Log:
added 'handle()' implementation using handlers created at run-time and cached 
into an in-memory LRU cache

Modified:
    
commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java

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=897990&r1=897989&r2=897990&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
 Mon Jan 11 18:24:52 2010
@@ -48,6 +48,13 @@
         new InMemoryLRUCache<Class<?>, FromAnnotationsRuleSet>();
 
     /**
+     * In-memory LRU cache that stores already analyzed instantiated handlers
+     * and relative instance.
+     */
+    private static final InMemoryLRUCache<Class<? extends 
DigesterLoaderHandler<? extends Annotation, ? extends AnnotatedElement>>, 
DigesterLoaderHandler<Annotation, AnnotatedElement>> CACHED_HANDLERS =
+        new InMemoryLRUCache<Class<? extends DigesterLoaderHandler<? extends 
Annotation, ? extends AnnotatedElement>>, 
DigesterLoaderHandler<Annotation,AnnotatedElement>>();
+
+    /**
      * This class can't be instantiated.
      */
     private DigesterLoader() {
@@ -145,8 +152,10 @@
     }
 
     /**
-     * 
-     * @param element
+     * Executes an analysis for each annotation present in the element.
+     *
+     * @param element the current element under analysis.
+     * @param ruleSet the ruleSet where add providers.
      */
     private static void handle(AnnotatedElement element, 
FromAnnotationsRuleSet ruleSet) {
         for (Annotation annotation : element.getAnnotations()) {
@@ -161,6 +170,7 @@
      * @param annotation the current visited annotation.
      * @param element the current visited element.
      */
+    @SuppressWarnings("unchecked")
     private static void handle(Annotation annotation, AnnotatedElement 
element, FromAnnotationsRuleSet ruleSet) {
         Class<?> annotationType = annotation.annotationType();
 
@@ -175,7 +185,25 @@
             }
         } else if (annotationType.isAnnotationPresent(DigesterRule.class)) {
             DigesterRule digesterRule = 
annotationType.getAnnotation(DigesterRule.class);
-            // TODO add missing code
+            DigesterLoaderHandler<Annotation,AnnotatedElement> handler = null;
+
+            // retrieves the handler from the cache if it is present
+            if (CACHED_HANDLERS.containsKey(digesterRule.handledBy())) {
+                handler = CACHED_HANDLERS.get(digesterRule.handledBy());
+            } else {
+                // create the handler and stores it to the cache for future 
reuse
+                try {
+                    handler = (DigesterLoaderHandler<Annotation, 
AnnotatedElement>) digesterRule.handledBy().newInstance();
+                    CACHED_HANDLERS.put(digesterRule.handledBy(), handler);
+                } catch (Throwable t) {
+                    throw new RuntimeException("Impossible to instance handler 
of type '"
+                            + digesterRule.handledBy().getName()
+                            + "', see nested exceptions", t);
+                }
+            }
+
+            // run!
+            handler.handle(annotation, element, ruleSet);
         }
     }
 


Reply via email to