[jboss-cvs] JBossAS SVN: r98075 - in projects/jboss-mdr/trunk/src: main/java/org/jboss/metadata/api and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Dec 21 07:30:55 EST 2009


Author: kabir.khan at jboss.com
Date: 2009-12-21 07:30:55 -0500 (Mon, 21 Dec 2009)
New Revision: 98075

Added:
   projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/api/
   projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/api/MetaDataUtils.java
   projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/context/cache/test/MetaDataUtilsCachedMetaDataTestCase.java
Modified:
   projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/MetaDataRetrievalToMetaDataBridge.java
Log:
[JBMDR-63] Util method to rework a MetaData instance to contain a caching context

Added: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/api/MetaDataUtils.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/api/MetaDataUtils.java	                        (rev 0)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/api/MetaDataUtils.java	2009-12-21 12:30:55 UTC (rev 98075)
@@ -0,0 +1,81 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.api;
+
+import org.jboss.logging.Logger;
+import org.jboss.metadata.plugins.context.CachingMetaDataContext;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.metadata.spi.context.MetaDataContext;
+import org.jboss.metadata.spi.retrieval.MetaDataRetrievalToMetaDataBridge;
+
+/**
+ * Utilities for MDR metadata
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class MetaDataUtils
+{
+   private final static Logger log = Logger.getLogger(MetaDataUtils.class);
+   
+   /**
+    * Takes a MetaData and reworks the internal MetaDataContext to be caching.
+    * If the metadata structure cannot be determined, a warning is logged, but no error is thrown.
+    * 
+    * @param metaData The meta data we want to cache
+    * @return a cached meta data, or the original parameter if adding caching was not possible
+    */
+   public static MetaData createCachedMetaData(MetaData metaData)
+   {
+      if (metaData.getClass() != MetaDataRetrievalToMetaDataBridge.class)
+      {
+         log.warn("MetaDataUtils.createCachedMetaData(): meta data is not a MetaDataRetrievalToMetaDataBridge, not adding caching");
+         return metaData;
+      }
+       
+      if (((MetaDataRetrievalToMetaDataBridge)metaData).getMetaDataRetrieval() instanceof MetaDataContext == false)
+      {
+         log.warn("MetaDataUtils.createCachedMetaData(): Could not determine structure of meta data, not adding caching");
+         return metaData;
+      }
+      
+      MetaDataContext ctx = (MetaDataContext)((MetaDataRetrievalToMetaDataBridge)metaData).getMetaDataRetrieval();
+      return new MetaDataRetrievalToMetaDataBridge(createCachedMetaDataContext(ctx));
+   }
+
+   /**
+    * Creates a cached meta data context for the passed in context. All the parent contexts
+    * will also be cached
+    * 
+    * @param ctx the meta data context we want to cache
+    * @param the cached meta data context
+    */
+   private static MetaDataContext createCachedMetaDataContext(MetaDataContext ctx)
+   {
+      MetaDataContext parent = ctx.getParent();
+      if (parent != null)
+      {
+         parent = createCachedMetaDataContext(parent);
+      }
+      return new CachingMetaDataContext(parent, ctx.getLocalRetrievals());
+   }
+}

Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/MetaDataRetrievalToMetaDataBridge.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/MetaDataRetrievalToMetaDataBridge.java	2009-12-21 10:43:18 UTC (rev 98074)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/spi/retrieval/MetaDataRetrievalToMetaDataBridge.java	2009-12-21 12:30:55 UTC (rev 98075)
@@ -214,7 +214,7 @@
     * 
     * @return the retrieval
     */
-   protected MetaDataRetrieval getMetaDataRetrieval()
+   public MetaDataRetrieval getMetaDataRetrieval()
    {
       return retrieval;
    }

Added: projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/context/cache/test/MetaDataUtilsCachedMetaDataTestCase.java
===================================================================
--- projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/context/cache/test/MetaDataUtilsCachedMetaDataTestCase.java	                        (rev 0)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/context/cache/test/MetaDataUtilsCachedMetaDataTestCase.java	2009-12-21 12:30:55 UTC (rev 98075)
@@ -0,0 +1,146 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.test.metadata.context.cache.test;
+
+import java.lang.annotation.Annotation;
+import java.util.List;
+
+import org.jboss.metadata.api.MetaDataUtils;
+import org.jboss.metadata.plugins.context.AbstractMetaDataContext;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.metadata.spi.context.MetaDataContext;
+import org.jboss.metadata.spi.retrieval.AnnotationItem;
+import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
+import org.jboss.metadata.spi.retrieval.basic.BasicAnnotationItem;
+import org.jboss.metadata.spi.retrieval.basic.BasicAnnotationsItem;
+import org.jboss.test.metadata.context.AbstractMetaDataContextTest;
+import org.jboss.test.metadata.context.cache.support.TestMetaDataLoader;
+import org.jboss.test.metadata.shared.support.TestAnnotation;
+import org.jboss.test.metadata.shared.support.TestAnnotation1;
+import org.jboss.test.metadata.shared.support.TestAnnotation1Impl;
+import org.jboss.test.metadata.shared.support.TestAnnotation2;
+import org.jboss.test.metadata.shared.support.TestAnnotation2Impl;
+import org.jboss.test.metadata.shared.support.TestAnnotationImpl;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class MetaDataUtilsCachedMetaDataTestCase extends AbstractMetaDataContextTest
+{
+   public MetaDataUtilsCachedMetaDataTestCase(String name)
+   {
+      super(name);
+   }
+
+   @Override
+   protected MetaDataContext createContext(MetaDataContext parent, List<MetaDataRetrieval> retrievals)
+   {
+      return new AbstractMetaDataContext(parent, retrievals);
+   }
+
+   @Override
+   protected MetaDataRetrieval createRetrieval()
+   {
+      return new TestMetaDataLoader();
+   }
+   
+   protected TestMetaDataLoader getFirstParent()
+   {
+      return (TestMetaDataLoader) firstParent;
+   }
+   
+   protected TestMetaDataLoader getSecondParent()
+   {
+      return (TestMetaDataLoader) secondParent;
+   }
+   
+   protected TestMetaDataLoader getFirstChild()
+   {
+      return (TestMetaDataLoader) firstChild;
+   }
+   
+   protected TestMetaDataLoader getSecondChild()
+   {
+      return (TestMetaDataLoader) secondChild;
+   }
+
+   @SuppressWarnings("unchecked")
+   protected <T extends Annotation> void setAnnotations(TestMetaDataLoader loader, T annotation, Class<T> type)
+   {
+      AnnotationItem[] items = { new BasicAnnotationItem<T>(loader, annotation) };
+      BasicAnnotationsItem item = new BasicAnnotationsItem(loader, items);
+      loader.setAnnotationsItem(item);
+   }
+
+   protected MetaData createTestContext()
+   {
+      MetaData md = super.createTestContext();
+
+      setAnnotations(getFirstParent(), new TestAnnotationImpl(), TestAnnotation.class);
+      setAnnotations(getFirstChild(), new TestAnnotation1Impl(), TestAnnotation1.class);
+      setAnnotations(getSecondChild(), new TestAnnotation2Impl(), TestAnnotation2.class);
+      
+      return md;
+   }
+
+   public void testCachedAnnotations() throws Throwable
+   {
+      MetaData metaData = createTestContext();
+      
+      Annotation[] anns = metaData.getAnnotations();
+      assertNotNull(anns);
+      assertEquals(3, anns.length);
+
+      assertTrue(getFirstParent().isRetrieved());
+      assertTrue(getSecondParent().isRetrieved());
+      assertTrue(getFirstChild().isRetrieved());
+      assertTrue(getSecondChild().isRetrieved());
+      
+      anns = metaData.getAnnotations();
+      assertNotNull(anns);
+      assertEquals(3, anns.length);
+
+      assertTrue(getFirstParent().isRetrieved());
+      assertTrue(getSecondParent().isRetrieved());
+      assertTrue(getFirstChild().isRetrieved());
+      assertTrue(getSecondChild().isRetrieved());
+
+      MetaData cached = MetaDataUtils.createCachedMetaData(metaData);
+      anns = cached.getAnnotations();
+      assertNotNull(anns);
+      assertEquals(3, anns.length);
+
+      assertTrue(getFirstParent().isRetrieved());
+      assertTrue(getSecondParent().isRetrieved());
+      assertTrue(getFirstChild().isRetrieved());
+      assertTrue(getSecondChild().isRetrieved());
+      
+      cached = MetaDataUtils.createCachedMetaData(metaData);
+      assertFalse(getFirstParent().isRetrieved());
+      assertFalse(getSecondParent().isRetrieved());
+      assertFalse(getFirstChild().isRetrieved());
+      assertFalse(getSecondChild().isRetrieved());
+   }
+   
+}




More information about the jboss-cvs-commits mailing list