[webbeans-commits] Webbeans SVN: r2218 - in ri/trunk/impl/src/main/java/org/jboss/webbeans/xml: checker and 6 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Thu Mar 26 13:36:33 EDT 2009


Author: vitold
Date: 2009-03-26 13:36:33 -0400 (Thu, 26 Mar 2009)
New Revision: 2218

Added:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/BeanElementChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/JmsResourceElementChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/NotSimpleBeanElementChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/ResourceElementChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SessionBeanElementChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SimpleBeanElementChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/impl/
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/impl/BeanElementCheckerImpl.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/BeanChildrenChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/NotSimpleBeanChildrenChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/SimpleBeanChildrenChecker.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/impl/
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/impl/BeanChildrenCheckerImpl.java
Removed:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/
Modified:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
Log:
changes with bean element validation

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java	2009-03-26 17:24:45 UTC (rev 2217)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -101,10 +101,6 @@
       throw new DefinitionException("There are multiple packages containing a Java type with the same name '" + className + "'");
    }
 
-   public static void checkProduces(Element element, AnnotatedClass<?> beanClass)
-   {
-   }
-
    public static void checkRootAttributes(Element root, Map<String, Set<String>> packagesMap, XmlEnvironment environment)
    {
       Iterator<?> rootAttrIterator = root.attributeIterator();

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java	2009-03-26 17:24:45 UTC (rev 2217)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -28,12 +28,13 @@
 import org.jboss.webbeans.introspector.AnnotatedField;
 import org.jboss.webbeans.log.Log;
 import org.jboss.webbeans.log.Logging;
-import org.jboss.webbeans.xml.check.BeanType;
-import org.jboss.webbeans.xml.check.BeanTypeObtainer;
-import org.jboss.webbeans.xml.check.JmsResourceTypeObtainer;
-import org.jboss.webbeans.xml.check.ResourceTypeObtainer;
-import org.jboss.webbeans.xml.check.SessionBeanTypeObtainer;
-import org.jboss.webbeans.xml.check.SimpleBeanTypeObtainer;
+import org.jboss.webbeans.xml.checker.bean.BeanElementChecker;
+import org.jboss.webbeans.xml.checker.bean.ext.JmsResourceElementChecker;
+import org.jboss.webbeans.xml.checker.bean.ext.ResourceElementChecker;
+import org.jboss.webbeans.xml.checker.bean.ext.SessionBeanElementChecker;
+import org.jboss.webbeans.xml.checker.bean.ext.SimpleBeanElementChecker;
+import org.jboss.webbeans.xml.checker.beanchildren.ext.NotSimpleBeanChildrenChecker;
+import org.jboss.webbeans.xml.checker.beanchildren.ext.SimpleBeanChildrenChecker;
 
 public class XmlParser
 {
@@ -41,7 +42,7 @@
    
    private final XmlEnvironment environment;
    
-   private List<BeanTypeObtainer> beanTypeObtainers = new ArrayList<BeanTypeObtainer>();
+   private List<BeanElementChecker> beanElementCheckers = new ArrayList<BeanElementChecker>();
    
    private boolean haveAnyDeployElement = false;
    
@@ -49,11 +50,7 @@
    
    public XmlParser(XmlEnvironment environment)
    {
-      this.environment = environment;
-      this.beanTypeObtainers.add(new JmsResourceTypeObtainer());
-      this.beanTypeObtainers.add(new ResourceTypeObtainer());
-      this.beanTypeObtainers.add(new SessionBeanTypeObtainer());
-      this.beanTypeObtainers.add(new SimpleBeanTypeObtainer());
+      this.environment = environment;      
    }
    
    public void parse()
@@ -206,22 +203,29 @@
    }
    
    private void checkBeanElement(Element beanElement, AnnotatedClass<?> beanClass)
-   {//TODO: not finished
-      BeanType beanType = BeanType.SIMPLE_BEAN;
-      for(BeanTypeObtainer beanTypeObtainer : beanTypeObtainers)
+   {
+      beanElementCheckers.add(new JmsResourceElementChecker(new NotSimpleBeanChildrenChecker(environment, packagesMap)));
+      beanElementCheckers.add(new ResourceElementChecker(new NotSimpleBeanChildrenChecker(environment, packagesMap)));
+      beanElementCheckers.add(new SessionBeanElementChecker(new NotSimpleBeanChildrenChecker(environment, packagesMap)));
+      beanElementCheckers.add(new SimpleBeanElementChecker(new SimpleBeanChildrenChecker(environment, packagesMap)));
+      
+      boolean isValidType = false;
+      for(BeanElementChecker beanElementChecker : beanElementCheckers)
       {
-         if(beanTypeObtainer.accept(beanElement, beanClass))
+         if(beanElementChecker.accept(beanElement, beanClass))
          {
-            beanType = beanTypeObtainer.obtainType(beanElement, beanClass);
+            beanElementChecker.checkBeanElement(beanElement, beanClass);
+            isValidType = true;
             break;
          }
       }
       
-      
+      if(!isValidType)
+         throw new DefinitionException("Can't determine type of bean element <" + beanElement.getName() + ">");
    }
    
    private void checkProduces(Element beanElement, AnnotatedClass<?> beanClass)
-   {
+   {//TODO: will refactor
       Iterator<?> beanIterator = beanElement.elementIterator();
       while(beanIterator.hasNext())
       {

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/BeanElementChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/BeanElementChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/BeanElementChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,11 @@
+package org.jboss.webbeans.xml.checker.bean;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+
+public interface BeanElementChecker
+{
+   boolean accept(Element beanElement, AnnotatedClass<?> beanClass);
+   
+   void checkBeanElement(Element beanElement, AnnotatedClass<?> beanClass);
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/JmsResourceElementChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/JmsResourceElementChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/JmsResourceElementChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,24 @@
+package org.jboss.webbeans.xml.checker.bean.ext;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+import org.jboss.webbeans.xml.checker.beanchildren.BeanChildrenChecker;
+
+public class JmsResourceElementChecker extends NotSimpleBeanElementChecker
+{
+   public JmsResourceElementChecker(BeanChildrenChecker childrenChecker)
+   {
+      super(childrenChecker);
+   }
+
+   public boolean accept(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      if (ParseXmlHelper.isJavaEeNamespace(beanElement) && 
+            (beanElement.getName().equalsIgnoreCase(XmlConstants.TOPIC) || 
+                  beanElement.getName().equalsIgnoreCase(XmlConstants.QUEUE)))
+         return true;
+      return false;
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/NotSimpleBeanElementChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/NotSimpleBeanElementChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/NotSimpleBeanElementChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,19 @@
+package org.jboss.webbeans.xml.checker.bean.ext;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.checker.bean.impl.BeanElementCheckerImpl;
+import org.jboss.webbeans.xml.checker.beanchildren.BeanChildrenChecker;
+
+public abstract class NotSimpleBeanElementChecker extends BeanElementCheckerImpl
+{
+   public NotSimpleBeanElementChecker(BeanChildrenChecker childrenChecker)
+   {
+      super(childrenChecker);
+   }
+   
+   public void checkElementDeclaration(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      // There is nothing to validate      
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/ResourceElementChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/ResourceElementChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/ResourceElementChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,39 @@
+package org.jboss.webbeans.xml.checker.bean.ext;
+
+import java.util.Iterator;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+import org.jboss.webbeans.xml.checker.beanchildren.BeanChildrenChecker;
+
+public class ResourceElementChecker extends NotSimpleBeanElementChecker
+{
+   public ResourceElementChecker(BeanChildrenChecker childrenChecker)
+   {
+      super(childrenChecker);
+   }
+
+   public boolean accept(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      if (ParseXmlHelper.isJavaEeNamespace(beanElement) && 
+            (beanElement.getName().equalsIgnoreCase(XmlConstants.TOPIC) || 
+                  beanElement.getName().equalsIgnoreCase(XmlConstants.QUEUE)))
+         return false;
+      
+      Iterator<?> elIterator = beanElement.elementIterator();
+      while (elIterator.hasNext())
+      {
+         Element child = (Element) elIterator.next();
+         if (ParseXmlHelper.isJavaEeNamespace(child) && 
+               (child.getName().equalsIgnoreCase(XmlConstants.RESOURCE) || 
+                     child.getName().equalsIgnoreCase(XmlConstants.PERSISTENCE_CONTEXT) || 
+                     child.getName().equalsIgnoreCase(XmlConstants.PERSISTENCE_UNIT) || 
+                     child.getName().equalsIgnoreCase(XmlConstants.EJB) || 
+                     child.getName().equalsIgnoreCase(XmlConstants.WEB_SERVICE_REF)))
+            return true;
+      }
+      return false;
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SessionBeanElementChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SessionBeanElementChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SessionBeanElementChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,25 @@
+package org.jboss.webbeans.xml.checker.bean.ext;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.XmlConstants;
+import org.jboss.webbeans.xml.checker.beanchildren.BeanChildrenChecker;
+
+public class SessionBeanElementChecker extends NotSimpleBeanElementChecker
+{
+   public SessionBeanElementChecker(BeanChildrenChecker childrenChecker)
+   {
+      super(childrenChecker);
+   }
+
+   public boolean accept(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      ManagerImpl manager = CurrentManager.rootManager();
+      if (manager.getEjbDescriptorCache().containsKey(beanElement.getName()) ||
+            beanElement.attribute(XmlConstants.EJB_NAME) != null)
+         return true;
+      return false;
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SimpleBeanElementChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SimpleBeanElementChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/ext/SimpleBeanElementChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,51 @@
+package org.jboss.webbeans.xml.checker.bean.ext;
+
+import javax.decorator.Decorator;
+import javax.inject.DefinitionException;
+import javax.interceptor.Interceptor;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+import org.jboss.webbeans.xml.checker.bean.impl.BeanElementCheckerImpl;
+import org.jboss.webbeans.xml.checker.beanchildren.BeanChildrenChecker;
+
+public class SimpleBeanElementChecker extends BeanElementCheckerImpl
+{
+   public SimpleBeanElementChecker(BeanChildrenChecker childrenChecker)
+   {
+      super(childrenChecker);
+   }
+
+   public boolean accept(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      ManagerImpl manager = CurrentManager.rootManager();
+      boolean isSessionBean = manager.getEjbDescriptorCache().containsKey(beanElement.getName()) || 
+                                          beanElement.attribute(XmlConstants.EJB_NAME) != null;
+      
+      if (!beanClass.isAbstract() && !isSessionBean && !beanClass.isParameterizedType())
+         return true;
+
+      return false;
+   }
+
+   public void checkElementDeclaration(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      if(beanClass.isNonStaticMemberClass())
+         throw new DefinitionException("Bean class '" + beanClass.getName() + "' of a simple bean <" + beanElement.getName() + 
+               "> is a non static member class");
+      
+      if(beanClass.getRawType().isAnnotationPresent(Interceptor.class) && 
+            ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.INTERCEPTOR).size() != 1)
+         throw new DefinitionException("A simple bean defined in XML as <" + beanElement.getName() +  "> has a bean class '" + 
+               beanClass.getName() + "' annotated @Interceptor and is not declared as an interceptor in XML");
+      
+      if(beanClass.getRawType().isAnnotationPresent(Decorator.class) && 
+            ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.DECORATOR).size() != 1)
+         throw new DefinitionException("A simple bean defined in XML as <" + beanElement.getName() +  "> has a bean class '" + 
+               beanClass.getName() + "' annotated @Decorator and is not declared as an decorator in XML");
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/impl/BeanElementCheckerImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/impl/BeanElementCheckerImpl.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/bean/impl/BeanElementCheckerImpl.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,27 @@
+package org.jboss.webbeans.xml.checker.bean.impl;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.checker.bean.BeanElementChecker;
+import org.jboss.webbeans.xml.checker.beanchildren.BeanChildrenChecker;
+
+
+public abstract class BeanElementCheckerImpl implements BeanElementChecker
+{
+   private final BeanChildrenChecker childrenChecker;
+   
+   public BeanElementCheckerImpl(BeanChildrenChecker childrenChecker)
+   {
+      this.childrenChecker = childrenChecker;
+   }
+   
+   public abstract boolean accept(Element beanElement, AnnotatedClass<?> beanClass);
+
+   public abstract void checkElementDeclaration(Element beanElement, AnnotatedClass<?> beanClass);
+   
+   public void checkBeanElement(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      checkElementDeclaration(beanElement, beanClass);
+      childrenChecker.checkChildren(beanElement, beanClass);
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/BeanChildrenChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/BeanChildrenChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/BeanChildrenChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,9 @@
+package org.jboss.webbeans.xml.checker.beanchildren;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+
+public interface BeanChildrenChecker
+{   
+   void checkChildren(Element beanElement, AnnotatedClass<?> beanClass);
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/NotSimpleBeanChildrenChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/NotSimpleBeanChildrenChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/NotSimpleBeanChildrenChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,46 @@
+package org.jboss.webbeans.xml.checker.beanchildren.ext;
+
+import java.util.Map;
+import java.util.Set;
+
+import javax.inject.DefinitionException;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+import org.jboss.webbeans.xml.XmlEnvironment;
+import org.jboss.webbeans.xml.checker.beanchildren.impl.BeanChildrenCheckerImpl;
+
+public class NotSimpleBeanChildrenChecker extends BeanChildrenCheckerImpl
+{
+   public NotSimpleBeanChildrenChecker(XmlEnvironment environment, Map<String, Set<String>> packagesMap)
+   {
+      super(environment, packagesMap);
+   }
+
+   public void checkForInterceptorChild(Element beanElement)
+   {
+      if(ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.INTERCEPTOR).size() > 1)
+         throw new DefinitionException("Not a simple bean '" + beanElement.getName() + "' contains direct child <" + 
+               XmlConstants.INTERCEPTOR + ">");            
+   }
+   
+   public void checkForDecoratorChild(Element beanElement)
+   {
+      if(ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.DECORATOR).size() > 1)
+         throw new DefinitionException("Not a simple bean '" + beanElement.getName() + "' contains direct child <" + 
+               XmlConstants.DECORATOR + ">");
+   }
+   
+   public void checkChildForInterceptorType(Element beanChildElement)
+   {
+      throw new DefinitionException("Declaration of not a simple bean '" + beanChildElement.getParent().getName() + 
+         "' contains a child <" + beanChildElement.getName() + "> which type is javax.interceptor.Interceptor");
+   }
+   
+   public void checkChildForDecoratorType(Element beanChildElement)
+   {
+      throw new DefinitionException("Declaration of not a simple bean '" + beanChildElement.getParent().getName() + 
+         "' contains a child <" + beanChildElement.getName() + "> which type is javax.decorator.Decorator");
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/SimpleBeanChildrenChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/SimpleBeanChildrenChecker.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/ext/SimpleBeanChildrenChecker.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,50 @@
+package org.jboss.webbeans.xml.checker.beanchildren.ext;
+
+import java.util.Map;
+import java.util.Set;
+
+import javax.inject.DefinitionException;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+import org.jboss.webbeans.xml.XmlEnvironment;
+import org.jboss.webbeans.xml.checker.beanchildren.impl.BeanChildrenCheckerImpl;
+
+public class SimpleBeanChildrenChecker extends BeanChildrenCheckerImpl
+{
+   public SimpleBeanChildrenChecker(XmlEnvironment environment, Map<String, Set<String>> packagesMap)
+   {
+      super(environment, packagesMap);
+   }
+
+   public void checkForDecoratorChild(Element beanElement)
+   {
+      if(ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.INTERCEPTOR).size() > 1)
+         throw new DefinitionException("A simple bean element <" + beanElement.getName() + "> has more than one direct child <" + 
+               XmlConstants.INTERCEPTOR + ">");            
+   }
+   
+   public void checkForInterceptorChild(Element beanElement)
+   {
+      if(ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.DECORATOR).size() > 1)
+         throw new DefinitionException("A simple bean element <" + beanElement.getName() + "> has more than one direct child <" + 
+               XmlConstants.DECORATOR + ">");
+   }
+   
+   public void checkChildForInterceptorType(Element beanChildElement)
+   {
+      if(haveBeanInterceptorDeclaration)
+         throw new DefinitionException("There is second element of interceptor type <" + beanChildElement.getName() + 
+               "> in bean '" + beanChildElement.getParent().getName() + "'");
+         haveBeanInterceptorDeclaration = true;
+   }
+   
+   public void checkChildForDecoratorType(Element beanChildElement)
+   {
+      if(haveBeanDecoratorDeclaration)
+         throw new DefinitionException("There is second element of decorator type <" + beanChildElement.getName() + 
+               "> in bean '" + beanChildElement.getParent().getName() + "'");
+         haveBeanDecoratorDeclaration = true;
+   }
+}

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/impl/BeanChildrenCheckerImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/impl/BeanChildrenCheckerImpl.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/checker/beanchildren/impl/BeanChildrenCheckerImpl.java	2009-03-26 17:36:33 UTC (rev 2218)
@@ -0,0 +1,154 @@
+package org.jboss.webbeans.xml.checker.beanchildren.impl;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.Named;
+import javax.annotation.Resource;
+import javax.annotation.Stereotype;
+import javax.context.ScopeType;
+import javax.decorator.Decorator;
+import javax.ejb.EJB;
+import javax.inject.BindingType;
+import javax.inject.DefinitionException;
+import javax.inject.DeploymentType;
+import javax.inject.Realizes;
+import javax.inject.Specializes;
+import javax.interceptor.Interceptor;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+import javax.xml.ws.WebServiceRef;
+
+import org.dom4j.Element;
+import org.dom4j.Namespace;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+import org.jboss.webbeans.xml.XmlEnvironment;
+import org.jboss.webbeans.xml.checker.beanchildren.BeanChildrenChecker;
+
+public abstract class BeanChildrenCheckerImpl implements BeanChildrenChecker
+{
+   private final XmlEnvironment environment;
+   
+   private final Map<String, Set<String>> packagesMap;
+   
+   private boolean haveBeanDeploymentTypeDeclaration = false;
+   
+   private boolean haveBeanScopeTypeDeclaration = false;
+   
+   protected boolean haveBeanInterceptorDeclaration = false;
+   
+   protected boolean haveBeanDecoratorDeclaration = false;
+         
+   public abstract void checkForInterceptorChild(Element beanElement);
+   
+   public abstract void checkForDecoratorChild(Element beanElement);
+   
+   public abstract void checkChildForInterceptorType(Element beanElement);
+   
+   public abstract void checkChildForDecoratorType(Element beanElement);
+   
+   public BeanChildrenCheckerImpl(XmlEnvironment environment, Map<String, Set<String>> packagesMap)
+   {
+      this.environment = environment;
+      this.packagesMap = packagesMap;
+   }
+   
+   public void checkChildren(Element beanElement, AnnotatedClass<?> beanClass)
+   {
+      checkForInterceptorChild(beanElement);
+      checkForDecoratorChild(beanElement);
+      
+      haveBeanDeploymentTypeDeclaration = false;
+      haveBeanScopeTypeDeclaration = false;
+      haveBeanInterceptorDeclaration = ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.INTERCEPTOR).size() > 0;
+      haveBeanDecoratorDeclaration = ParseXmlHelper.findElementsInEeNamespace(beanElement, XmlConstants.DECORATOR).size() > 0;
+      
+      Iterator<?> beanIterator = beanElement.elementIterator();
+      while(beanIterator.hasNext())
+      {
+         Element beanChildElement = (Element)beanIterator.next();
+         checkBeanChild(beanChildElement);
+      }
+   }
+   
+   private void checkBeanChild(Element beanChildElement)
+   {
+      //TODO: not finished
+      try
+      {
+         AnnotatedClass<?> beanChildClass = ParseXmlHelper.loadElementClass(beanChildElement, Object.class, environment, packagesMap);
+         Class<?> beanChildType = beanChildClass.getRawType();
+         boolean isJavaClass = !beanChildType.isEnum() && !beanChildType.isPrimitive() && !beanChildType.isInterface();
+         boolean isInterface = beanChildType.isInterface() && !beanChildType.isAnnotation();
+         if(beanChildType.isAnnotation())
+         {
+            //bean child element declaring type-level metadata
+            checkAnnotationChild(beanChildElement, beanChildClass);
+            return;
+         }
+         if(isJavaClass || isInterface)
+         {
+            //TODO: bean child element declaring a parameter of the bean constructor
+            return;
+         }
+         throw new DefinitionException();//bean child element can't be interpreted as a Java class or interface or Java Annotation type
+      }
+      catch(DefinitionException e)
+      {
+         Element beanElement = beanChildElement.getParent();
+         Namespace beanNamespace = beanElement.getNamespace();
+         Namespace beanChildNamespace = beanChildElement.getNamespace();
+         if(beanChildNamespace.equals(beanNamespace))
+         {
+            //TODO: bean child element declaring a method or field of the bean.
+            return;
+         }
+         throw new DefinitionException("Can't determine type of element <" + beanChildElement.getName() + "> in bean '" + 
+               beanElement.getName() + "'");
+      }
+   }
+   
+   private void checkAnnotationChild(Element beanChildElement, AnnotatedClass<?> beanChildClass)
+   {
+      if(beanChildClass.isAnnotationPresent(DeploymentType.class))
+      {
+         if(haveBeanDeploymentTypeDeclaration)
+            throw new DefinitionException("Only one deployment type declaration allowed for bean '" + 
+                  beanChildElement.getParent().getName() + "'");
+         haveBeanDeploymentTypeDeclaration = true;
+         return;
+      }
+      if(beanChildClass.isAnnotationPresent(ScopeType.class))
+      {
+         if(haveBeanScopeTypeDeclaration)
+            throw new DefinitionException("Only one scope type declaration allowed for bean '" + 
+                  beanChildElement.getParent().getName() + "'");
+         haveBeanScopeTypeDeclaration = true;
+         return;
+      }
+      if(beanChildClass.isAnnotationPresent(Interceptor.class))
+      {
+         checkChildForInterceptorType(beanChildElement);
+         return;
+      }
+      if(beanChildClass.isAnnotationPresent(Decorator.class))
+      {
+         checkChildForDecoratorType(beanChildElement);
+         return;
+      }
+      //TODO: add interceptor binding type
+      if(beanChildClass.isAnnotationPresent(BindingType.class) || beanChildClass.isAnnotationPresent(Stereotype.class) || 
+            beanChildClass.isAnnotationPresent(Named.class) || beanChildClass.isAnnotationPresent(Specializes.class) ||  
+            beanChildClass.isAnnotationPresent(Realizes.class) || beanChildClass.isAnnotationPresent(Resource.class) || 
+            beanChildClass.isAnnotationPresent(EJB.class) || beanChildClass.isAnnotationPresent(WebServiceRef.class) || 
+            beanChildClass.isAnnotationPresent(PersistenceContext.class) || beanChildClass.isAnnotationPresent(PersistenceUnit.class))
+         return;
+      
+      throw new DefinitionException("Can't determine annotation type of <" + beanChildElement.getName() + "> element in bean '" + 
+            beanChildElement.getParent().getName() + "'");
+   }
+
+}




More information about the weld-commits mailing list