[jboss-cvs] JBossAS SVN: r97603 - in projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata: plugins/context and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Dec 9 07:36:26 EST 2009


Author: alesj
Date: 2009-12-09 07:36:25 -0500 (Wed, 09 Dec 2009)
New Revision: 97603

Added:
   projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/cache/EmptyCache.java
Modified:
   projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/AbstractMetaDataContext.java
   projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/CachingMetaDataContext.java
   projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/cummulative/CummulativeAnnotationsItem.java
Log:
Refactor JBMDR-62 a bit.

Copied: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/cache/EmptyCache.java (from rev 97600, projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/cache/ConcurrentHashMapCache.java)
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/cache/EmptyCache.java	                        (rev 0)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/cache/EmptyCache.java	2009-12-09 12:36:25 UTC (rev 97603)
@@ -0,0 +1,63 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metadata.plugins.cache;
+
+/**
+ * Empty cache.
+ *
+ * @param <K> the key class
+ * @param <V> the value class
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class EmptyCache<K, V> implements Cache<K, V>
+{
+   private static final Cache EMPTY = new EmptyCache();
+
+   private EmptyCache()
+   {
+   }
+
+   @SuppressWarnings({"unchecked"})
+   public static <K,V> Cache<K, V> emptyCache()
+   {
+      return EMPTY;
+   }
+
+   public V put(K key, V value)
+   {
+      return null;
+   }
+
+   public V get(K key)
+   {
+      return null;
+   }
+
+   public V remove(K key)
+   {
+      return null;
+   }
+
+   public void clear()
+   {
+   }
+}
\ No newline at end of file

Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/AbstractMetaDataContext.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/AbstractMetaDataContext.java	2009-12-09 11:34:37 UTC (rev 97602)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/AbstractMetaDataContext.java	2009-12-09 12:36:25 UTC (rev 97603)
@@ -25,11 +25,12 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
 
+import org.jboss.metadata.plugins.cache.Cache;
+import org.jboss.metadata.plugins.cache.ConcurrentHashMapCache;
+import org.jboss.metadata.plugins.cache.EmptyCache;
 import org.jboss.metadata.spi.context.MetaDataContext;
 import org.jboss.metadata.spi.retrieval.AnnotationItem;
 import org.jboss.metadata.spi.retrieval.AnnotationsItem;
@@ -37,7 +38,6 @@
 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
 import org.jboss.metadata.spi.retrieval.MetaDatasItem;
 import org.jboss.metadata.spi.retrieval.ValidTime;
-import org.jboss.metadata.spi.retrieval.basic.BasicAnnotationsItem;
 import org.jboss.metadata.spi.retrieval.cummulative.CummulativeAnnotationsItem;
 import org.jboss.metadata.spi.retrieval.cummulative.CummulativeMetaDatasItem;
 import org.jboss.metadata.spi.scope.Scope;
@@ -49,6 +49,8 @@
  * AbstractMetaDataContext.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision$
  */
 public class AbstractMetaDataContext implements MetaDataContext
@@ -230,8 +232,7 @@
    
    public AnnotationsItem retrieveAnnotationsAnnotatedWith(Class<? extends Annotation> meta)
    {
-      @SuppressWarnings("unchecked")
-      Map<Class, AnnotationsItem> map = retrieveAnnotationsAnnotatedWithIndex();
+      Cache<Class, AnnotationsItem> map = retrieveAnnotationsAnnotatedWithIndex();
       return map.get(meta);
    }
 
@@ -336,52 +337,88 @@
 
    /**
     * Only match annotations with meta annotation.
+    *
+    * @return index map
     */
-   @SuppressWarnings("unchecked")
-   protected Map<Class, AnnotationsItem> retrieveAnnotationsAnnotatedWithIndex()
+   protected Cache<Class, AnnotationsItem> retrieveAnnotationsAnnotatedWithIndex()
    {
-      Map<Class, AnnotationsItem> result = new HashMap<Class, AnnotationsItem>();
+      Cache<Class, AnnotationsItem> result = EmptyCache.emptyCache(); // use empty
       AnnotationsItem item = retrieveAnnotations();
-      if (item.getAnnotations().length > 0)
+      AnnotationItem<? extends Annotation>[] items = item.getAnnotations();
+      if (items.length > 0)
       {
-         Map<Class<? extends Annotation>, List<AnnotationItem<?>>> annotations = new HashMap<Class<? extends Annotation>, List<AnnotationItem<?>>>();
-         for (AnnotationItem<?> annotation : item.getAnnotations())
+         result = getAnnotationsItemCache(); // create real map/cache
+         for (AnnotationItem<? extends Annotation> annotation : items)
          {
             for (Annotation current : annotation.getAnnotation().annotationType().getAnnotations())
             {
-               List<AnnotationItem<?>> annotationsForMeta = annotations.get(current.annotationType());
-               if (annotationsForMeta == null)
+               Class<? extends Annotation> annotationType = current.annotationType();
+               AnnotationsItem annotationsItem = result.get(annotationType);
+               if (annotationsItem == null)
                {
-                  annotationsForMeta = new ArrayList<AnnotationItem<?>>();
-                  annotations.put(current.annotationType(), annotationsForMeta);
+                  annotationsItem = new ListAnnotationsItem();
+                  result.put(annotationType, annotationsItem);
                }
-               annotationsForMeta.add(annotation);
+               ListAnnotationsItem.class.cast(annotationsItem).add(annotation);
             }
          }
-         
-         for (Map.Entry<Class<? extends Annotation>, List<AnnotationItem<?>>> entry : annotations.entrySet())
-         {
-            AnnotationsItem basic = new AnnotationsAnnotatedWithAnnotationsItem(entry.getValue().toArray(new AnnotationItem<?>[entry.getValue().size()] ));
-            result.put(entry.getKey(), basic);
-         }
-
       }
       return result;
    }
-   
-   static class AnnotationsAnnotatedWithAnnotationsItem extends BasicAnnotationsItem
+
+   protected Cache<Class, AnnotationsItem> getAnnotationsItemCache()
    {
-      private boolean cachable = true;
+      return new ConcurrentHashMapCache<Class, AnnotationsItem>();
+   }
 
-      AnnotationsAnnotatedWithAnnotationsItem(AnnotationItem<? extends Annotation>[] annotationItems)
+   static class ListAnnotationsItem implements AnnotationsItem
+   {
+      private List<AnnotationItem<? extends Annotation>> annotationItems = new ArrayList<AnnotationItem<? extends Annotation>>();
+      private Annotation[] annotations;
+
+      private void add(AnnotationItem<? extends Annotation> annotationItem)
       {
-         super(null, annotationItems);
+         annotationItems.add(annotationItem);
       }
 
-      @Override
+      public Annotation[] getValue()
+      {
+         if (annotations == null)
+         {
+            Annotation[] temp = new Annotation[annotationItems.size()];
+            for (int i = 0; i < temp.length; ++i)
+               temp[i] = annotationItems.get(i).getAnnotation();
+            annotations = temp;
+         }
+         return annotations;
+      }
+
+      @SuppressWarnings({"unchecked"})
+      public AnnotationItem<? extends Annotation>[] getAnnotations()
+      {
+         return annotationItems.toArray(new AnnotationItem[annotationItems.size()]);
+      }
+
       public boolean isCachable()
       {
-         return cachable;
+         for (AnnotationItem<? extends Annotation> item : annotationItems)
+         {
+            if (item.isCachable() == false)
+               return false;
+         }
+
+         return true;
       }
+
+      public boolean isValid()
+      {
+         for (AnnotationItem<? extends Annotation> item : annotationItems)
+         {
+            if (item.isValid() == false)
+               return false;
+         }
+
+         return true;
+      }
    }
 }

Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/CachingMetaDataContext.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/CachingMetaDataContext.java	2009-12-09 11:34:37 UTC (rev 97602)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/context/CachingMetaDataContext.java	2009-12-09 12:36:25 UTC (rev 97603)
@@ -24,7 +24,6 @@
 import java.lang.annotation.Annotation;
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
 
 import org.jboss.metadata.plugins.cache.Cache;
 import org.jboss.metadata.plugins.cache.CacheFactory;
@@ -62,13 +61,8 @@
    /** All meta data */
    private volatile CacheItem<MetaDatasItem> cachedMetaDatasItem;
    
-   /** The annotations indexed by their meta-annotations. The contained map is
-    * of type <Class<?>, AnnotationsItem> */
-   @SuppressWarnings("unchecked")
-   private volatile Cache<String, Map> cachedMetaAnnotations;
-   
-   /** The key into cachedMetaAnnotations for the indexed meta-anotations*/
-   private static final String META_ANNOTATIONS_INDEX_KEY = "MetaAnnotations";
+   /** The annotations indexed by their meta-annotations */
+   private volatile Cache<Class, AnnotationsItem> cachedMetaAnnotations;
 
    /** Cached components */
    private volatile Cache<Signature, MetaDataRetrieval> cachedComponents;
@@ -223,11 +217,13 @@
 
       return result;
    }
-   
+
    @Override
-   @SuppressWarnings("unchecked")
-   protected Map<Class, AnnotationsItem> retrieveAnnotationsAnnotatedWithIndex()
+   public AnnotationsItem retrieveAnnotationsAnnotatedWith(Class<? extends Annotation> meta)
    {
+      if (meta == null)
+         throw new IllegalArgumentException("Null meta annotation");
+
       long newValidTime = getValidTime().getValidTime();
       if (validTime < newValidTime)
       {
@@ -236,32 +232,33 @@
          clearCache(cachedMetaAnnotations);
          validTime = newValidTime;
       }
-      
-      if (cachedMetaAnnotations == null)
-         cachedMetaAnnotations = factory.createCache(String.class, Map.class, getFqn());
-      
-      Map<Class, AnnotationsItem> index = cachedMetaAnnotations.get(META_ANNOTATIONS_INDEX_KEY);
-      if (index != null)
+
+      if (cachedMetaAnnotations != null)
       {
-         for (AnnotationsItem item : index.values())
+         AnnotationsItem items = cachedMetaAnnotations.get(meta);
+         if (items != null)
          {
-            if (item.isValid() == false)
-            {
-               index = null;
-               break;
-            }
+            if (items.isValid())
+               return items;
+
+            clearCache(cachedMetaAnnotations);
          }
       }
-      if (index == null)
-      {
-         index = super.retrieveAnnotationsAnnotatedWithIndex();
-         cachedMetaAnnotations.clear();
-         cachedMetaAnnotations.put(META_ANNOTATIONS_INDEX_KEY, index);
-      }
-         
-      return index; 
+
+      //super call will put the values into cache
+      return super.retrieveAnnotationsAnnotatedWith(meta);
    }
 
+   @Override
+   protected synchronized Cache<Class, AnnotationsItem> getAnnotationsItemCache()
+   {
+      // create cache so we can store the info
+      if (cachedMetaAnnotations == null)
+         cachedMetaAnnotations = factory.createCache(Class.class, AnnotationsItem.class, getFqn());
+
+      return cachedMetaAnnotations;
+   }
+
    public MetaDatasItem retrieveMetaData()
    {
       if (cachedMetaDatasItem == null)

Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/cummulative/CummulativeAnnotationsItem.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/cummulative/CummulativeAnnotationsItem.java	2009-12-09 11:34:37 UTC (rev 97602)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/cummulative/CummulativeAnnotationsItem.java	2009-12-09 12:36:25 UTC (rev 97603)
@@ -100,7 +100,7 @@
       if (validTime < newValidTime)
          valid = false;
       
-      if (valid && items != null)
+      if (valid)
       {
          for (AnnotationItem<? extends Annotation> item : items)
          {




More information about the jboss-cvs-commits mailing list