[jboss-cvs] JBossAS SVN: r71918 - in projects/microcontainer/trunk/kernel/src: main/org/jboss/beans/metadata/spi/builder and 4 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 10 09:34:31 EDT 2008


Author: alesj
Date: 2008-04-10 09:34:31 -0400 (Thu, 10 Apr 2008)
New Revision: 71918

Added:
   projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.xml
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/container/ScopedContainer.java
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.java
Modified:
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/builder/BeanMetaDataBuilderImpl.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/builder/BeanMetaDataBuilder.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PreInstallAction.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ScopedKernelController.java
Log:
BeanContainer scope test.
Caching Scope factories.
New util methods for bean metadata builder.

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/builder/BeanMetaDataBuilderImpl.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/builder/BeanMetaDataBuilderImpl.java	2008-04-10 13:14:48 UTC (rev 71917)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/builder/BeanMetaDataBuilderImpl.java	2008-04-10 13:34:31 UTC (rev 71918)
@@ -47,6 +47,7 @@
 import org.jboss.beans.metadata.plugins.AbstractValueMetaData;
 import org.jboss.beans.metadata.plugins.StringValueMetaData;
 import org.jboss.beans.metadata.plugins.ThisValueMetaData;
+import org.jboss.beans.metadata.plugins.AbstractAnnotationMetaData;
 import org.jboss.beans.metadata.spi.BeanMetaData;
 import org.jboss.beans.metadata.spi.ClassLoaderMetaData;
 import org.jboss.beans.metadata.spi.DemandMetaData;
@@ -54,6 +55,7 @@
 import org.jboss.beans.metadata.spi.PropertyMetaData;
 import org.jboss.beans.metadata.spi.SupplyMetaData;
 import org.jboss.beans.metadata.spi.ValueMetaData;
+import org.jboss.beans.metadata.spi.AnnotationMetaData;
 import org.jboss.beans.metadata.spi.builder.BeanMetaDataBuilder;
 import org.jboss.beans.metadata.spi.builder.ParameterMetaDataBuilder;
 import org.jboss.dependency.spi.Cardinality;
@@ -172,6 +174,47 @@
       return this;
    }
 
+   public BeanMetaDataBuilder addAlias(Object alias)
+   {
+      Set<Object> aliases = beanMetaData.getAliases();
+      if (aliases == null)
+      {
+         aliases = new HashSet<Object>();
+         beanMetaData.setAliases(aliases);
+      }
+      aliases.add(alias);
+      return this;
+   }
+
+   public BeanMetaDataBuilder setAnnotations(Set<String> annotations)
+   {
+      if (annotations != null && annotations.isEmpty() == false)
+      {
+         Set<AnnotationMetaData> amds = new HashSet<AnnotationMetaData>();
+         for (String annotation : annotations)
+         {
+            AbstractAnnotationMetaData amd = new AbstractAnnotationMetaData();
+            amd.setAnnotation(annotation);
+         }
+         beanMetaData.setAnnotations(amds);
+      }
+      return this;
+   }
+
+   public BeanMetaDataBuilder addAnnotation(String annotation)
+   {
+      Set<AnnotationMetaData> annotations = beanMetaData.getAnnotations();
+      if (annotations == null)
+      {
+         annotations = new HashSet<AnnotationMetaData>();
+         beanMetaData.setAnnotations(annotations);
+      }
+      AbstractAnnotationMetaData amd = new AbstractAnnotationMetaData();
+      amd.setAnnotation(annotation);
+      annotations.add(amd);
+      return this;
+   }
+
    public BeanMetaDataBuilder setMode(ControllerMode mode)
    {
       beanMetaData.setMode(mode);

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/builder/BeanMetaDataBuilder.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/builder/BeanMetaDataBuilder.java	2008-04-10 13:14:48 UTC (rev 71917)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/builder/BeanMetaDataBuilder.java	2008-04-10 13:34:31 UTC (rev 71918)
@@ -115,6 +115,30 @@
    public abstract BeanMetaDataBuilder setAliases(Set<Object> aliases);
 
    /**
+    * Add alias.
+    *
+    * @param alias the alias
+    * @return the builder
+    */
+   public abstract BeanMetaDataBuilder addAlias(Object alias);
+
+   /**
+    * Set the annotations
+    *
+    * @param annotations the annotations
+    * @return the builder
+    */
+   public abstract BeanMetaDataBuilder setAnnotations(Set<String> annotations);
+
+   /**
+    * Add alias.
+    *
+    * @param annotation the annotation
+    * @return the builder
+    */
+   public abstract BeanMetaDataBuilder addAnnotation(String annotation);
+
+   /**
     * Set the mode
     * 
     * @param modeString the mode

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PreInstallAction.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PreInstallAction.java	2008-04-10 13:14:48 UTC (rev 71917)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PreInstallAction.java	2008-04-10 13:34:31 UTC (rev 71918)
@@ -22,8 +22,11 @@
 package org.jboss.kernel.plugins.dependency;
 
 import java.lang.annotation.Annotation;
+import java.lang.ref.WeakReference;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Map;
+import java.util.WeakHashMap;
 
 import org.jboss.beans.info.spi.BeanInfo;
 import org.jboss.beans.metadata.spi.BeanMetaData;
@@ -52,6 +55,33 @@
  */
 public class PreInstallAction extends InstallsAwareAction
 {
+   private Map<Class<? extends ScopeFactory<? extends Annotation>>, WeakReference<ScopeFactory>> factories = new WeakHashMap<Class<? extends ScopeFactory<? extends Annotation>>, WeakReference<ScopeFactory>>();
+
+   /**
+    * Get the scope factory.
+    *
+    * @param clazz the class key
+    * @return scope factory
+    * @throws Throwable for any error
+    */
+   protected ScopeFactory getScopeFactory(Class<? extends ScopeFactory<? extends Annotation>> clazz) throws Throwable
+   {
+      ScopeFactory factory = null;
+      WeakReference<ScopeFactory> weak = factories.get(clazz);
+      if (weak != null)
+      {
+         factory = weak.get();
+         if (factory != null)
+            return factory;
+      }
+      if (factory == null)
+      {
+         factory = clazz.newInstance();
+         factories.put(clazz, new WeakReference<ScopeFactory>(factory));
+      }
+      return factory;
+   }
+
    protected void installActionInternal(KernelControllerContext context) throws Throwable
    {
       KernelController controller = (KernelController)context.getController();
@@ -89,7 +119,6 @@
    @SuppressWarnings("unchecked")
    protected ScopeKey getInstallScopeKey(
          KernelControllerContext context,
-         KernelController controller,
          KernelMetaDataRepository repository) throws Throwable
    {
       MetaData retrieval = repository.getMetaData(context);
@@ -104,7 +133,7 @@
                if (annotation.annotationType().isAnnotationPresent(ScopeFactoryLookup.class))
                {
                   ScopeFactoryLookup sfl = annotation.annotationType().getAnnotation(ScopeFactoryLookup.class);
-                  ScopeFactory<Annotation> scf = (ScopeFactory) sfl.value().newInstance();
+                  ScopeFactory<Annotation> scf = getScopeFactory(sfl.value()); 
                   Scope scope = scf.create(annotation);
                   scopes.add(scope);
                }
@@ -122,19 +151,12 @@
    {
       KernelController controller = (KernelController)context.getController();
       KernelMetaDataRepository repository = controller.getKernel().getMetaDataRepository();
-      ScopeKey scopeKey = getInstallScopeKey(context, controller, repository);
+      ScopeKey scopeKey = getInstallScopeKey(context, repository);
       if (scopeKey != null)
       {
          scopeKey.freeze();
          context.getScopeInfo().setInstallScope(scopeKey);
-         // todo - should this be done (repare the current context scope key)
-         //        or where to store this 'deployment' key?
-/*
-                  ScopeKey contextScopeKey = context.getScope();
-                  for (Scope s : scopes)
-                     contextScopeKey.addScope(s);
-*/
-         // find scoped controller
+
          MutableMetaDataRepository mmdr = repository.getMetaDataRepository();
          MetaDataRetrieval mdr = mmdr.getMetaDataRetrieval(scopeKey);
          if (mdr == null)

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ScopedKernelController.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ScopedKernelController.java	2008-04-10 13:14:48 UTC (rev 71917)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ScopedKernelController.java	2008-04-10 13:34:31 UTC (rev 71918)
@@ -135,7 +135,8 @@
 
    protected void install(ControllerContext context, boolean trace) throws Throwable
    {
-      throw new IllegalArgumentException("Should not be called!");
+      // we only allow install at top level
+      getParentController().install(context);
    }
 
    // KernelController methods

Copied: projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.xml (from rev 71877, projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerUsageTestCase_testDependencyInjectionOfBean.xml)
===================================================================
--- projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.xml	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.xml	2008-04-10 13:34:31 UTC (rev 71918)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+  <bean name="CF1" class="org.jboss.test.kernel.deployment.support.container.ScopedContainer">
+    <annotation>@org.jboss.metadata.plugins.scope.InstanceScope("cf1")</annotation>
+    <constructor><parameter><inject bean="jboss.kernel:service=Kernel"/></parameter></constructor>
+    <property name="metaData"><inject fromContext="metadata"/></property>
+  </bean>
+
+  <bean name="CF2" class="org.jboss.test.kernel.deployment.support.container.ScopedContainer">
+    <annotation>@org.jboss.metadata.plugins.scope.InstanceScope("cf2")</annotation>     
+    <constructor><parameter><inject bean="jboss.kernel:service=Kernel"/></parameter></constructor>
+    <property name="metaData"><inject fromContext="metadata"/></property>
+  </bean>
+
+</deployment>

Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/container/ScopedContainer.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/container/ScopedContainer.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/container/ScopedContainer.java	2008-04-10 13:34:31 UTC (rev 71918)
@@ -0,0 +1,83 @@
+/*
+* 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.test.kernel.deployment.support.container;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.beans.metadata.spi.builder.BeanMetaDataBuilder;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.metadata.plugins.scope.InstanceScope;
+import org.jboss.metadata.spi.MetaData;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ScopedContainer
+{
+   private Kernel kernel;
+   private MetaData metaData;
+
+   public ScopedContainer(Kernel kernel)
+   {
+      if (kernel == null)
+         throw new IllegalArgumentException("Null kernel.");
+      this.kernel = kernel;
+   }
+
+   public void setMetaData(MetaData metaData)
+   {
+      this.metaData = metaData;
+   }
+
+   protected String createAnnotation(InstanceScope is)
+   {
+      return "@" + InstanceScope.class.getName() + "(\"" + is.value() + "\")";
+   }
+
+   public List<Object> createBeans(String baseName) throws Throwable
+   {
+      List<Object> result = new ArrayList<Object>();
+
+      KernelController controller = kernel.getController();
+      InstanceScope is = metaData.getAnnotation(InstanceScope.class);
+      String sflAnn = createAnnotation(is);
+
+      // bean context
+      BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder("BeanContext", BaseContext.class.getName());
+      builder.addConstructorParameter(BeanContainer.class.getName(), new BeanContainer<Object>());
+      builder.addAlias(baseName + "$" + "BeanContext");
+      builder.addAnnotation(sflAnn);
+      result.add(controller.install(builder.getBeanMetaData()).getTarget());
+
+      // instance interceptor
+      builder = BeanMetaDataBuilder.createBuilder("InstanceInterceptor", InstanceInterceptor.class.getName());
+      builder.addAlias(baseName + "$" + "InstanceInterceptor");
+      builder.addAnnotation(sflAnn);
+      builder.addInstallWithThis("addInterceptor", "BeanContext");
+      builder.addUninstallWithThis("removeInterceptor", "BeanContext");
+      result.add(controller.install(builder.getBeanMetaData()).getTarget());
+
+      return result;
+   }
+}

Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerScopingTestCase.java	2008-04-10 13:34:31 UTC (rev 71918)
@@ -0,0 +1,64 @@
+/*
+* 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.test.kernel.deployment.test;
+
+import java.util.List;
+
+import org.jboss.test.kernel.deployment.support.container.BaseContext;
+import org.jboss.test.kernel.deployment.support.container.ScopedContainer;
+import org.jboss.test.kernel.deployment.support.container.InstanceInterceptor;
+
+/**
+ * Test controller scopes.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class BeanContainerScopingTestCase extends ScopingDeploymentTest
+{
+   public BeanContainerScopingTestCase(String name) throws Throwable
+   {
+      super(name);
+   }
+
+   public void testControllerScopes() throws Throwable
+   {
+      ScopedContainer sc1 = assertBean("CF1", ScopedContainer.class);
+      List<Object> beans1 = sc1.createBeans("cf_base_1");
+      assertNotNull(beans1);
+      assertEquals(2, beans1.size());
+      BaseContext bc1 = assertBean("cf_base_1$BeanContext", BaseContext.class);
+      InstanceInterceptor ii1 = assertBean("cf_base_1$InstanceInterceptor", InstanceInterceptor.class);
+      assertNotNull(bc1.getInterceptors());
+      assertEquals(1, bc1.getInterceptors().size());
+      assertSame(ii1, bc1.getInterceptors().get(0));
+
+      ScopedContainer sc2 = assertBean("CF2", ScopedContainer.class);
+      List<Object> beans2 = sc2.createBeans("cf_base_2");
+      assertNotNull(beans2);
+      assertEquals(2, beans2.size());
+      BaseContext bc2 = assertBean("cf_base_2$BeanContext", BaseContext.class);
+      InstanceInterceptor ii2 = assertBean("cf_base_2$InstanceInterceptor", InstanceInterceptor.class);
+      assertNotNull(bc2.getInterceptors());
+      assertEquals(1, bc2.getInterceptors().size());
+      assertSame(ii2, bc2.getInterceptors().get(0));
+   }
+}




More information about the jboss-cvs-commits mailing list