[jboss-cvs] JBossAS SVN: r82251 - in trunk/spring-int: src/main/org/jboss/spring and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Dec 12 17:45:29 EST 2008


Author: alesj
Date: 2008-12-12 17:45:29 -0500 (Fri, 12 Dec 2008)
New Revision: 82251

Added:
   trunk/spring-int/src/main/org/jboss/spring/facade/
   trunk/spring-int/src/main/org/jboss/spring/facade/ControllerBeanFactory.java
   trunk/spring-int/src/main/org/jboss/spring/facade/KernelControllerListableBeanFactory.java
Modified:
   trunk/spring-int/build.xml
Log:
MC facade over Spring's BeanFactory - todo tests.

Modified: trunk/spring-int/build.xml
===================================================================
--- trunk/spring-int/build.xml	2008-12-12 22:16:29 UTC (rev 82250)
+++ trunk/spring-int/build.xml	2008-12-12 22:45:29 UTC (rev 82251)
@@ -102,6 +102,8 @@
       <path refid="jboss.jboss.man.classpath"/>      
       <path refid="jboss.microcontainer.classpath"/>
       <path refid="jboss.jboss.vfs.classpath"/>
+      <path refid="jboss.jboss.reflect.classpath"/>
+      <path refid="jboss.jbossxb.classpath"/>
       <path refid="jboss.jboss.javaee.classpath"/>
       <path refid="jboss.cache.jbosscache.core.classpath"/>
       <path refid="jboss.cache.jbosscache.pojo.classpath"/>

Added: trunk/spring-int/src/main/org/jboss/spring/facade/ControllerBeanFactory.java
===================================================================
--- trunk/spring-int/src/main/org/jboss/spring/facade/ControllerBeanFactory.java	                        (rev 0)
+++ trunk/spring-int/src/main/org/jboss/spring/facade/ControllerBeanFactory.java	2008-12-12 22:45:29 UTC (rev 82251)
@@ -0,0 +1,282 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.spring.facade;
+
+import java.util.Set;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.beans.metadata.spi.BeanMetaData;
+import org.jboss.beans.metadata.spi.ConstructorMetaData;
+import org.jboss.beans.metadata.spi.PropertyMetaData;
+import org.jboss.beans.metadata.spi.ValueMetaData;
+import org.jboss.beans.metadata.spi.builder.BeanMetaDataBuilder;
+import org.jboss.beans.metadata.spi.factory.AbstractBeanFactory;
+import org.jboss.dependency.spi.Controller;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.TypeInfoFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.FatalBeanException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+
+/**
+ * BeanFactory facade over MC's Controller.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class ControllerBeanFactory implements BeanFactory
+{
+   private Controller controller;
+
+   public ControllerBeanFactory(Controller controller)
+   {
+      if (controller == null)
+         throw new IllegalArgumentException("Null controller");
+
+      this.controller = controller;
+   }
+
+   /**
+    * Get the controller context.
+    *
+    * @param name the context name
+    * @return context or null if not found
+    */
+   protected ControllerContext getInstalledContext(String name)
+   {
+      return controller.getInstalledContext(name);
+   }
+
+   /**
+    * Get the controller context.
+    *
+    * @param name  the context name
+    * @param state the state
+    * @return context or null if not found
+    */
+   protected ControllerContext getContext(String name, ControllerState state)
+   {
+      return controller.getContext(name, state);
+   }
+
+   public Object getBean(String name) throws BeansException
+   {
+      ControllerContext context = getInstalledContext(name);
+      if (context == null)
+         throw new NoSuchBeanDefinitionException(name);
+
+      return context.getTarget();
+   }
+
+   @SuppressWarnings("unchecked")
+   public Object getBean(String name, Class clazz) throws BeansException
+   {
+      return getExactBean(name, clazz);
+   }
+
+   /**
+    * Get exact bean.
+    *
+    * @param name the bean name
+    * @param clazz the expected class
+    * @param <T> the exact type
+    * @return exact bean
+    * @throws BeansException for any error
+    */
+   protected <T> T getExactBean(String name, Class<T> clazz) throws BeansException
+   {
+      Object result = getBean(name);
+      if (clazz.isInstance(result) == false)
+         throw new BeanNotOfRequiredTypeException(name, clazz, result.getClass());
+
+      return clazz.cast(result);
+   }
+
+   public Object getBean(String name, Object[] parameters) throws BeansException
+   {
+      AbstractBeanFactory result = getExactBean(name, AbstractBeanFactory.class);
+      ConstructorMetaData cmd = result.getConstructor();
+      BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder("Temp");
+      for (Object parameter : parameters )
+         builder.addConstructorParameter(null, parameter);
+      result.setConstructor(builder.getBeanMetaData().getConstructor());
+      try
+      {
+         return createBean(result);
+      }
+      finally
+      {
+         result.setConstructor(cmd);
+      }
+   }
+
+   public boolean containsBean(String name)
+   {
+      return getInstalledContext(name) != null;
+   }
+
+   public boolean isSingleton(String name) throws NoSuchBeanDefinitionException
+   {
+      return isPrototype(name) == false;
+   }
+
+   public boolean isPrototype(String name) throws NoSuchBeanDefinitionException
+   {
+      Object result = getBean(name);
+      return isPrototype(result);
+   }
+
+   /**
+    * Is the result prototype.
+    *
+    * @param result the result
+    * @return true if prototyle, false otherwise
+    */
+   protected boolean isPrototype(Object result)
+   {
+      return org.jboss.beans.metadata.spi.factory.BeanFactory.class.isInstance(result);
+   }
+
+   /**
+    * Create the bean.
+    *
+    * @param factory the bean factory
+    * @return new bean instance
+    * @throws BeansException for any error
+    */
+   protected Object createBean(Object factory) throws BeansException
+   {
+      try
+      {
+         return org.jboss.beans.metadata.spi.factory.BeanFactory.class.cast(factory).createBean();
+      }
+      catch (Throwable t)
+      {
+         throw new FatalBeanException("Cannot create bean: " + factory, t);
+      }
+   }
+
+   public boolean isTypeMatch(String name, Class clazz) throws NoSuchBeanDefinitionException
+   {
+      return clazz.isInstance(getBean(name));
+   }
+
+   @SuppressWarnings("deprecation")
+   public Class getType(String name) throws NoSuchBeanDefinitionException
+   {
+      ControllerContext context = getContext(name, ControllerState.DESCRIBED);
+      if (context == null)
+         throw new NoSuchBeanDefinitionException(name);
+
+      if (context instanceof KernelControllerContext)
+      {
+         KernelControllerContext kcc = (KernelControllerContext)context;
+         BeanInfo beanInfo = kcc.getBeanInfo();
+         ClassInfo classInfo = beanInfo.getClassInfo();
+         TypeInfoFactory tif = classInfo.getTypeInfoFactory();
+         // it's a bean factory
+         if (tif.getTypeInfo(AbstractBeanFactory.class).isAssignableFrom(classInfo))
+         {
+            return getPrototypeClass(kcc);
+         }
+         else
+         {
+            return classInfo.getType();
+         }
+      }
+      return null;
+   }
+
+   /**
+    * Get prototype class.
+    *
+    * @param kcc the kernel controller context
+    * @return prototype's class
+    */
+   protected Class<?> getPrototypeClass(KernelControllerContext kcc)
+   {
+      BeanMetaData bmd = kcc.getBeanMetaData();
+      Set<PropertyMetaData> properties = bmd.getProperties();
+      for (PropertyMetaData pmd : properties)
+      {
+         if ("bean".equals(pmd.getName()))
+         {
+            ValueMetaData value = pmd.getValue();
+            if (value != null && value.getUnderlyingValue() != null)
+            {
+               String className = value.getUnderlyingValue().toString();
+               return getBeanClass(className, kcc);
+            }
+            else
+            {
+               return null;
+            }
+         }
+      }
+      return null;
+   }
+
+   /**
+    * Get the bean class.
+    *
+    * @param className the class name
+    * @param context the context
+    * @return bean's class
+    * @throws BeansException for any error
+    */
+   protected Class<?> getBeanClass(String className, KernelControllerContext context) throws BeansException
+   {
+      try
+      {
+         ClassLoader cl = context.getClassLoader();
+         return cl.loadClass(className);
+      }
+      catch (Throwable t)
+      {
+         throw new FatalBeanException("Cannot load class: " + className + ", context: " + context, t);
+      }
+   }
+
+   public String[] getAliases(String name)
+   {
+      ControllerContext context = getContext(name, null);
+      if (context == null || context.getAliases() == null)
+      {
+         return new String[]{};
+      }
+      else
+      {
+         Set<Object> aliases = context.getAliases();
+         String[] result = new String[aliases.size()];
+         int i = 0;
+         for (Object alias : aliases)
+         {
+            result[i++] = alias.toString();
+         }
+         return result;
+      }
+   }
+}

Added: trunk/spring-int/src/main/org/jboss/spring/facade/KernelControllerListableBeanFactory.java
===================================================================
--- trunk/spring-int/src/main/org/jboss/spring/facade/KernelControllerListableBeanFactory.java	                        (rev 0)
+++ trunk/spring-int/src/main/org/jboss/spring/facade/KernelControllerListableBeanFactory.java	2008-12-12 22:45:29 UTC (rev 82251)
@@ -0,0 +1,170 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.spring.facade;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.jboss.beans.metadata.spi.factory.AbstractBeanFactory;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.dependency.spi.ControllerStateModel;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.ListableBeanFactory;
+
+/**
+ * ListableBeanFactory facade over MC's KernelController.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class KernelControllerListableBeanFactory extends ControllerBeanFactory implements ListableBeanFactory
+{
+   private KernelController kernelController;
+
+   public KernelControllerListableBeanFactory(KernelController kernelController)
+   {
+      super(kernelController);
+      this.kernelController = kernelController;
+   }
+
+   public KernelControllerListableBeanFactory(Kernel kernel)
+   {
+      super(kernel != null ? kernel.getController() : null);
+      this.kernelController = kernel.getController();
+   }
+
+   public boolean containsBeanDefinition(String name)
+   {
+      return getContext(name, null) != null;
+   }
+
+   public int getBeanDefinitionCount()
+   {
+      int count = 0;
+      ControllerStateModel stateModel = kernelController.getStates();
+      for (ControllerState state : stateModel)
+      {
+         Set<ControllerContext> byState = kernelController.getContextsByState(state);
+         count += byState.size();
+      }
+      return count;
+   }
+
+   public String[] getBeanDefinitionNames()
+   {
+      List<String> result = new ArrayList<String>();
+      ControllerStateModel stateModel = kernelController.getStates();
+      for (ControllerState state : stateModel)
+      {
+         Set<ControllerContext> byState = kernelController.getContextsByState(state);
+         for (ControllerContext context : byState)
+            result.add(context.getName().toString());
+      }
+      return result.toArray(new String[result.size()]);
+   }
+
+   public String[] getBeanNamesForType(Class clazz)
+   {
+      return getBeanNamesForType(clazz, true, true);
+   }
+
+   public String[] getBeanNamesForType(Class clazz, boolean includePrototypes, boolean allowEagerInit)
+   {
+      List<String> result = new ArrayList<String>();
+      Set<KernelControllerContext> contexts = kernelController.getInstantiatedContexts(clazz);
+      if (contexts != null && contexts.isEmpty() == false)
+      {
+         for (KernelControllerContext context : contexts)
+         {
+            result.add(context.getName().toString());
+         }
+      }
+      if (includePrototypes)
+      {
+         Set<KernelControllerContext> factories = kernelController.getInstantiatedContexts(AbstractBeanFactory.class);
+         if (factories != null && factories.isEmpty() == false)
+         {
+            for (KernelControllerContext kcc : factories)
+            {
+               Class<?> prototypeClass = getPrototypeClass(kcc);
+               if (prototypeClass != null)
+               {
+                  if (clazz.isAssignableFrom(prototypeClass))
+                     result.add(kcc.getName().toString());
+               }
+               else if (allowEagerInit)
+               {
+                  Object bean = createBean(kcc.getTarget());
+                  if (clazz.isInstance(bean))
+                     result.add(kcc.getName().toString());
+               }
+            }
+         }
+      }
+      return result.toArray(new String[result.size()]);
+   }
+
+   public Map getBeansOfType(Class clazz) throws BeansException
+   {
+      return getBeansOfType(clazz, true, true);
+   }
+
+   public Map getBeansOfType(Class clazz, boolean includePrototypes, boolean allowEagerInit) throws BeansException
+   {
+      Map<String, Object> result = new HashMap<String, Object>();
+      Set<KernelControllerContext> contexts = kernelController.getContexts(clazz, ControllerState.INSTALLED);
+      for (KernelControllerContext context : contexts)
+      {
+         Object target = context.getTarget();
+         result.put(context.getName().toString(), target);
+      }
+      if (includePrototypes)
+      {
+         Set<KernelControllerContext> factories = kernelController.getInstantiatedContexts(AbstractBeanFactory.class);
+         if (factories != null && factories.isEmpty() == false)
+         {
+            for (KernelControllerContext kcc : factories)
+            {
+               Class<?> prototypeClass = getPrototypeClass(kcc);
+               if (prototypeClass != null)
+               {
+                  if (clazz.isAssignableFrom(prototypeClass))
+                     result.put(kcc.getName().toString(), createBean(kcc.getTarget()));
+               }
+               else if (allowEagerInit)
+               {
+                  Object bean = createBean(kcc.getTarget());
+                  if (clazz.isInstance(bean))
+                     result.put(kcc.getName().toString(), bean);
+               }
+            }
+         }
+      }
+      return result;
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list