[jboss-cvs] JBossAS SVN: r73494 - projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon May 19 11:23:16 EDT 2008


Author: alesj
Date: 2008-05-19 11:23:16 -0400 (Mon, 19 May 2008)
New Revision: 73494

Added:
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectAnnotationPlugin.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionDependencyItem.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionResolver.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionValueMetaData.java
Log:
Webbeans kind of injection, attempt #2.
TODO on tests.

Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectAnnotationPlugin.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectAnnotationPlugin.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectAnnotationPlugin.java	2008-05-19 15:23:16 UTC (rev 73494)
@@ -0,0 +1,72 @@
+/*
+* 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.kernel.plugins.annotations.wb;
+
+import java.lang.annotation.Annotation;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.ArrayList;
+
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.beans.metadata.api.annotations.Inject;
+import org.jboss.beans.metadata.spi.ValueMetaData;
+import org.jboss.kernel.plugins.annotations.PropertyAnnotationPlugin;
+
+/**
+ * Web beans kind of inject.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class WBInjectAnnotationPlugin extends PropertyAnnotationPlugin<Inject>
+{
+   private Set<Class<? extends Annotation>> excludedAnnotations = new HashSet<Class<? extends Annotation>>();
+
+   public WBInjectAnnotationPlugin()
+   {
+      super(Inject.class);
+      addExcludedAnnotation(Inject.class);
+   }
+
+   @SuppressWarnings("deprecation")
+   protected ValueMetaData createValueMetaData(PropertyInfo info, Inject inject)
+   {
+      List<Annotation> annotations = new ArrayList<Annotation>();
+      Annotation[] underlyingAnnotations = info.getUnderlyingAnnotations();
+      for (Annotation annotation : underlyingAnnotations)
+      {
+         if (excludedAnnotations.contains(annotation.annotationType()) == false)
+            annotations.add(annotation);
+      }
+      return new WBInjectionValueMetaData(info.getType().getType(), annotations.toArray(new Annotation[annotations.size()]));
+   }
+
+   /**
+    * Add excluded annotations.
+    *
+    * @param annotationClass the excluded annotation's class.
+    */
+   public void addExcludedAnnotation(Class<? extends Annotation> annotationClass)
+   {
+      excludedAnnotations.add(annotationClass);
+   }
+}

Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionDependencyItem.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionDependencyItem.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionDependencyItem.java	2008-05-19 15:23:16 UTC (rev 73494)
@@ -0,0 +1,57 @@
+/*
+* 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.kernel.plugins.annotations.wb;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.dependency.spi.Controller;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.plugins.dependency.ClassDependencyItem;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+
+/**
+ * Web beans injection dependency item.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class WBInjectionDependencyItem extends ClassDependencyItem
+{
+   private Annotation[] annotations;
+
+   public WBInjectionDependencyItem(Object name, ControllerState whenRequired, Class<?> type, Annotation[] annotations)
+   {
+      super(name, type, whenRequired, ControllerState.INSTALLED);
+      this.annotations = annotations;
+   }
+
+   public boolean resolve(Controller controller)
+   {
+      if (controller instanceof KernelController == false)
+         throw new IllegalArgumentException("Can only handle kernel controller: " + controller);
+
+      KernelController kernelController = (KernelController)controller;
+      KernelControllerContext context = WBInjectionResolver.resolve(kernelController, getDemandClass(), annotations);
+      setResolved(context != null);
+      return isResolved();
+   }
+}
\ No newline at end of file

Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionResolver.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionResolver.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionResolver.java	2008-05-19 15:23:16 UTC (rev 73494)
@@ -0,0 +1,88 @@
+/*
+* 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.kernel.plugins.annotations.wb;
+
+import java.lang.annotation.Annotation;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.metadata.spi.MetaData;
+
+/**
+ * Web beans injection resolver.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class WBInjectionResolver
+{
+   /**
+    * Find matching controller context.
+    *
+    * @param controller the kernel controller
+    * @param type the matching type
+    * @param annotations the filter annotations
+    * @return single matching context or null if none or multiple
+    */
+   public static KernelControllerContext resolve(KernelController controller, Class<?> type, Annotation[] annotations)
+   {
+      if (controller == null)
+         throw new IllegalArgumentException("Null controller");
+      if (type == null)
+         throw new IllegalArgumentException("Null type");
+      if (annotations == null)
+         annotations = new Annotation[]{};
+
+      Set<KernelControllerContext> contexts = controller.getContexts(type, ControllerState.INSTALLED);
+      if (contexts != null && contexts.isEmpty() == false)
+      {
+         Set<KernelControllerContext> matchingContexts = new HashSet<KernelControllerContext>();
+         for(KernelControllerContext context : contexts)
+         {
+            boolean match = true;
+            MetaData metaData = context.getScopeInfo().getMetaData();
+            for(Annotation annotation : annotations)
+            {
+               Annotation mdAnnotation = metaData.getAnnotation(annotation.annotationType());
+               if (mdAnnotation == null)
+               {
+                  match = false;
+                  break;
+               }
+            }
+            if (match)
+               matchingContexts.add(context);
+         }
+         int size = matchingContexts.size();
+         if (size != 1)
+            return null;
+         else
+            return matchingContexts.iterator().next();
+      }
+      else
+      {
+         return null;
+      }
+   }
+}
\ No newline at end of file

Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionValueMetaData.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionValueMetaData.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/annotations/wb/WBInjectionValueMetaData.java	2008-05-19 15:23:16 UTC (rev 73494)
@@ -0,0 +1,73 @@
+/*
+* 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.kernel.plugins.annotations.wb;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.beans.metadata.plugins.AbstractValueMetaData;
+import org.jboss.beans.metadata.spi.MetaDataVisitor;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.reflect.spi.TypeInfo;
+
+/**
+ * Web beans injection metadata value.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class WBInjectionValueMetaData extends AbstractValueMetaData
+{
+   /**
+    * The context
+    */
+   protected transient KernelControllerContext context;
+
+   private Class<?> type;
+   private Annotation[] annotations;
+
+   public WBInjectionValueMetaData(Class<?> type, Annotation[] annotations)
+   {
+      this.type = type;
+      this.annotations = annotations;
+   }
+
+   public void initialVisit(MetaDataVisitor visitor)
+   {
+      context = visitor.getControllerContext();
+
+      visitor.addDependency(new WBInjectionDependencyItem(context.getName(), visitor.getContextState(), type, annotations));
+      type = null; // nullify it
+
+      super.initialVisit(visitor);
+   }
+
+   @SuppressWarnings("deprecation")
+   public Object getValue(TypeInfo info, ClassLoader cl) throws Throwable
+   {
+      KernelController controller = context.getKernel().getController();
+      KernelControllerContext result = WBInjectionResolver.resolve(controller, info.getType(), annotations);
+      if (result != null)
+         return result.getTarget();
+      else
+         throw new IllegalArgumentException("Should not be here, dependency not resolved: " + toString());
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list