[webbeans-commits] Webbeans SVN: r2031 - in ri/tags/webbeans-core-1.0.0.BETA1: src/main/java/org/jboss/webbeans/util and 7 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Mon Mar 16 06:57:55 EDT 2009


Author: vitold
Date: 2009-03-16 06:57:55 -0400 (Mon, 16 Mar 2009)
New Revision: 2031

Added:
   ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/
   ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ClassReceiver.java
   ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java
   ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java
   ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java
   ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/com/
   ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/com/mydomain/
   ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/com/mydomain/myapp/
   ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/com/mydomain/myapp/Order.java
   ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/org/jboss/webbeans/test/unit/xml/XmlParserImplTest.java
Modified:
   ri/tags/webbeans-core-1.0.0.BETA1/pom.xml
   ri/tags/webbeans-core-1.0.0.BETA1/src/test/resources/org/jboss/webbeans/test/unit/xml/user-defined-beans.xml
Log:
add helper class for parsing beans.xml, test class for it, modify user-defined-beans.xml

Modified: ri/tags/webbeans-core-1.0.0.BETA1/pom.xml
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/pom.xml	2009-03-16 10:57:48 UTC (rev 2030)
+++ ri/tags/webbeans-core-1.0.0.BETA1/pom.xml	2009-03-16 10:57:55 UTC (rev 2031)
@@ -10,7 +10,7 @@
    <version>1.0.0.BETA1</version>
    <name>Web Beans Core</name>
    <dependencies>
-
+      
       <dependency>
          <groupId>org.jboss.webbeans</groupId>
          <artifactId>jsr299-api</artifactId>
@@ -82,8 +82,14 @@
          <groupId>javassist</groupId>
          <artifactId>javassist</artifactId>
       </dependency>
-
+      
       <dependency>
+         <groupId>dom4j</groupId>
+         <artifactId>dom4j</artifactId>
+         <version>1.6.1</version>
+      </dependency>
+      
+      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
       </dependency>

Added: ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ClassReceiver.java
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ClassReceiver.java	                        (rev 0)
+++ ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ClassReceiver.java	2009-03-16 10:57:55 UTC (rev 2031)
@@ -0,0 +1,12 @@
+package org.jboss.webbeans.util.xml;
+
+import java.lang.reflect.AnnotatedElement;
+
+import org.dom4j.Element;
+
+public interface ClassReceiver
+{
+   boolean accept(Element element);
+
+   AnnotatedElement reciveClass(Element element);
+}

Added: ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java	                        (rev 0)
+++ ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java	2009-03-16 10:57:55 UTC (rev 2031)
@@ -0,0 +1,209 @@
+package org.jboss.webbeans.util.xml;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javax.inject.DefinitionException;
+
+import org.dom4j.Element;
+
+public class ParseXmlHelper
+{
+   private static List<ClassReceiver> receivers;
+
+   static
+   {
+      receivers = initializeReceivers();
+   }
+
+   public static Set<AnnotatedElement> getBeanClasses(List<Element> beans)
+   {
+      Set<AnnotatedElement> result = new HashSet<AnnotatedElement>();
+
+      for (Element bean : beans)
+         result.add(reciveBeanClass(bean));
+
+      return result;
+   }
+
+   private static AnnotatedElement reciveBeanClass(Element element)
+   {
+      for (ClassReceiver receiver : receivers)
+      {
+         if (receiver.accept(element))
+         {
+            return receiver.reciveClass(element);
+         }
+      }
+
+      throw new DefinitionException("definition of a bean " + element.getName() + " is incorrect");
+   }
+
+   private static List<ClassReceiver> initializeReceivers()
+   {
+      List<ClassReceiver> receivers = new ArrayList<ClassReceiver>();
+
+      ClassReceiver jmsResourceClassReceiver = new ClassReceiver()
+      {
+         public boolean accept(Element element)
+         {
+            return isJMSResource(element);
+         }
+
+         public AnnotatedElement reciveClass(Element element)
+         {
+            return reciveJMSResourceClass(element);
+         }
+
+      };
+      ClassReceiver resourceClassReceiver = new ClassReceiver()
+      {
+         public boolean accept(Element element)
+         {
+            return isResource(element);
+         }
+
+         public AnnotatedElement reciveClass(Element element)
+         {
+            return reciveResourceClass(element);
+         }
+
+      };
+      ClassReceiver sessionBeanClassReceiver = new ClassReceiver()
+      {
+         public boolean accept(Element element)
+         {
+            return isSessionBean(element);
+         }
+
+         public AnnotatedElement reciveClass(Element element)
+         {
+            return reciveSessionBeanClass(element);
+         }
+
+      };
+      ClassReceiver simpleBeanClassReceiver = new ClassReceiver()
+      {
+         public boolean accept(Element element)
+         {
+            return isSimpleBean(element);
+         }
+
+         public AnnotatedElement reciveClass(Element element)
+         {
+            return reciveSimpleBeanClass(element);
+         }
+
+      };
+
+      receivers.add(jmsResourceClassReceiver);
+      receivers.add(resourceClassReceiver);
+      receivers.add(sessionBeanClassReceiver);
+      receivers.add(simpleBeanClassReceiver);
+
+      return receivers;
+   }
+
+   private static boolean isResource(Element element)
+   {
+      Iterator<?> elIterator = element.elementIterator();
+      while (elIterator.hasNext())
+      {
+         Element child = (Element) elIterator.next();
+         if (child.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE) && 
+               (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;
+   }
+
+   private static AnnotatedElement reciveResourceClass(Element element)
+   {
+      // TODO:
+      return null;
+   }
+
+   private static boolean isJMSResource(Element element)
+   {
+      if (element.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE) && 
+            (element.getName().equalsIgnoreCase(XmlConstants.TOPIC) || 
+                  element.getName().equalsIgnoreCase(XmlConstants.QUEUE)))
+         return true;
+      return false;
+   }
+
+   private static AnnotatedElement reciveJMSResourceClass(Element element)
+   {
+      // TODO:
+      return null;
+   }
+
+   private static boolean isSimpleBean(Element element)
+   {
+      Class<?> beanClass = loadClass(element);
+
+      if (!Modifier.isAbstract(beanClass.getModifiers()) && 
+            beanClass.getTypeParameters().length == 0)
+         return true;
+
+      return false;
+   }
+
+   private static AnnotatedElement reciveSimpleBeanClass(Element element)
+   {
+      Class<?> beanClass = loadClass(element);
+
+      if (beanClass.isMemberClass())
+         throw new DefinitionException("class " + beanClass + " is a non-static inner class");
+
+      // if (beanClass.getTypeParameters().length > 0)
+      // throw new DefinitionException("class " + beanClass +
+      // " is a parameterized type");
+
+      // TODO:
+      // boolean isDecorator = false;
+      // if (Modifier.isAbstract(beanClass.getModifiers()) && !isDecorator)
+      // throw new DefinitionException("class " + beanClass +
+      // " is an abstract and not Decorator");
+
+      return beanClass;
+   }
+
+   private static boolean isSessionBean(Element element)
+   {
+      // TODO:
+      return false;
+   }
+
+   private static AnnotatedElement reciveSessionBeanClass(Element element)
+   {
+      // TODO:
+      return null;
+   }
+
+   private static Class<?> loadClass(Element element)
+   {
+      String beanUri = element.getNamespace().getURI();
+      String packageName = beanUri.replaceFirst(XmlConstants.URN_PREFIX, "");
+      String classPath = packageName + "." + element.getName();
+
+      try
+      {
+         return Class.forName(classPath);
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw new DefinitionException("class " + classPath + " not found");
+      }
+
+   }
+}

Added: ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java	                        (rev 0)
+++ ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java	2009-03-16 10:57:55 UTC (rev 2031)
@@ -0,0 +1,34 @@
+package org.jboss.webbeans.util.xml;
+
+public class XmlConstants
+{
+   public static final String JAVA_EE_NAMESPACE = "urn:java:ee";
+   
+   public static final String DEPLOY = "Deploy";
+   
+   public static final String INTERCEPTORS = "Interceptors";
+   
+   public static final String DECORATORS = "Decorators";
+   
+   public static final String BINDING_TYPE = "BindingType";
+   
+   public static final String INTERCEPTOR_BINDING_TYPE = "InterceptorBindingType";
+   
+   public static final String STEREOTYPE = "Stereotype";
+   
+   public static final String RESOURCE = "Resource";
+   
+   public static final String PERSISTENCE_CONTEXT = "PersistenceContext";
+   
+   public static final String PERSISTENCE_UNIT = "PersistenceUnit";
+   
+   public static final String EJB = "EJB";
+   
+   public static final String WEB_SERVICE_REF = "WebServiceRef";
+   
+   public static final String TOPIC = "Topic";
+   
+   public static final String QUEUE = "Queue";
+   
+   public static final String URN_PREFIX = "urn:java:";
+}

Added: ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java	                        (rev 0)
+++ ri/tags/webbeans-core-1.0.0.BETA1/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java	2009-03-16 10:57:55 UTC (rev 2031)
@@ -0,0 +1,93 @@
+package org.jboss.webbeans.util.xml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.AnnotatedElement;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+
+public class XmlParserImpl // implements XmlParser
+{
+
+   private static Logger log = Logger.getLogger(XmlParserImpl.class);
+   
+   public Set<AnnotatedElement> parse(Set<URL> xmls)
+   {
+      Set<AnnotatedElement> result = new HashSet<AnnotatedElement>();
+
+      for (URL url : xmls)
+      {
+         try
+         {
+            InputStream xmlStream;
+
+            xmlStream = url.openStream();
+            SAXReader reader = new SAXReader();
+            Document document = reader.read(xmlStream);
+            List<Element> beanElements = findBeans(document);
+            result.addAll(ParseXmlHelper.getBeanClasses(beanElements));
+         }
+         catch (IOException e)
+         {
+            log.debug("Can not open stream for " + url, e);
+         }
+         catch (DocumentException e)
+         {
+            log.debug("Error during the processing of a DOM4J document for " + url, e);
+         }
+      }
+      return result;
+   }
+
+   private List<Element> findBeans(Document document)
+   {
+      List<Element> beans = new ArrayList<Element>();
+
+      Element root = document.getRootElement();
+
+      Iterator<?> elIterator = root.elementIterator();
+      while (elIterator.hasNext())
+      {
+         Element element = (Element) elIterator.next();
+         if (checkElementName(element) && checkElementChildrenNames(element))
+            beans.add(element);
+      }
+
+      return beans;
+   }   
+
+   private boolean checkElementName(Element element)
+   {
+      if (element.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE) && 
+            (element.getName().equalsIgnoreCase(XmlConstants.DEPLOY) || 
+                  element.getName().equalsIgnoreCase(XmlConstants.INTERCEPTORS) || 
+                  element.getName().equalsIgnoreCase(XmlConstants.DECORATORS)))
+         return false;
+      return true;
+   }
+
+   private boolean checkElementChildrenNames(Element element)
+   {
+      Iterator<?> elIterator = element.elementIterator();
+      while (elIterator.hasNext())
+      {
+         Element child = (Element) elIterator.next();
+         if (child.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE) && 
+               (child.getName().equalsIgnoreCase(XmlConstants.BINDING_TYPE) || 
+                     child.getName().equalsIgnoreCase(XmlConstants.INTERCEPTOR_BINDING_TYPE) || 
+                     child.getName().equalsIgnoreCase(XmlConstants.STEREOTYPE)))
+            return false;
+      }
+      return true;
+   }
+}

Added: ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/com/mydomain/myapp/Order.java
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/com/mydomain/myapp/Order.java	                        (rev 0)
+++ ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/com/mydomain/myapp/Order.java	2009-03-16 10:57:55 UTC (rev 2031)
@@ -0,0 +1,6 @@
+package com.mydomain.myapp;
+
+public class Order
+{
+
+}

Added: ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/org/jboss/webbeans/test/unit/xml/XmlParserImplTest.java
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/org/jboss/webbeans/test/unit/xml/XmlParserImplTest.java	                        (rev 0)
+++ ri/tags/webbeans-core-1.0.0.BETA1/src/test/java/org/jboss/webbeans/test/unit/xml/XmlParserImplTest.java	2009-03-16 10:57:55 UTC (rev 2031)
@@ -0,0 +1,56 @@
+package org.jboss.webbeans.test.unit.xml;
+
+import java.lang.reflect.AnnotatedElement;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.jboss.webbeans.test.unit.AbstractTest;
+import org.jboss.webbeans.util.xml.XmlParserImpl;
+//import org.jboss.webbeans.tck.impl.SpecAssertion;
+import org.testng.annotations.Test;
+
+import com.mydomain.myapp.Order;
+
+public class XmlParserImplTest extends AbstractTest
+{
+   @Test
+   public void testParse()
+   {
+      Set<URL> xmls = new HashSet<URL>();
+      Iterable<URL> urls = getResources("user-defined-beans.xml");
+
+      for (URL url : urls)
+         xmls.add(url);
+
+      XmlParserImpl parser = new XmlParserImpl();
+      Set<AnnotatedElement> aSet = parser.parse(xmls);
+
+      for(AnnotatedElement aElement : aSet){
+    	  assert aElement.equals(Order.class);
+      }
+      assert aSet.size() == 1;
+   }
+}
+
+/*
+<Beans xmlns="urn:java:ee" xmlns:myapp="urn:java:com.mydomain.myapp"
+xmlns:test="urn:java:org.jboss.webbeans.test.unit.xml">
+<Deploy>
+	<Standard />
+	<Production />
+	<test:AnotherDeploymentType />
+</Deploy>
+<myapp:Order>
+	<ConversationScoped />
+	<myapp:PaymentProcessor>
+		<myapp:Asynchronous />
+	</myapp:PaymentProcessor>
+	<myapp:User />
+</myapp:Order>
+<myapp:Login>
+	<ConversationScoped />
+	<BindingType />
+</myapp:Login>
+</Beans>
+*/

Modified: ri/tags/webbeans-core-1.0.0.BETA1/src/test/resources/org/jboss/webbeans/test/unit/xml/user-defined-beans.xml
===================================================================
--- ri/tags/webbeans-core-1.0.0.BETA1/src/test/resources/org/jboss/webbeans/test/unit/xml/user-defined-beans.xml	2009-03-16 10:57:48 UTC (rev 2030)
+++ ri/tags/webbeans-core-1.0.0.BETA1/src/test/resources/org/jboss/webbeans/test/unit/xml/user-defined-beans.xml	2009-03-16 10:57:55 UTC (rev 2031)
@@ -1,8 +1,19 @@
-<Beans xmlns="urn:java:ee"
-       xmlns:test="urn:java:org.jboss.webbeans.test.unit.xml"> 
-   <Deploy>
-      <Standard />
-      <Production />
-      <test:AnotherDeploymentType />
-   </Deploy>
-</Beans>
+<Beans xmlns="urn:java:ee" xmlns:myapp="urn:java:com.mydomain.myapp"
+	xmlns:test="urn:java:org.jboss.webbeans.test.unit.xml">
+	<Deploy>
+		<Standard />
+		<Production />
+		<test:AnotherDeploymentType />
+	</Deploy>
+	<myapp:Order>
+		<ConversationScoped />
+		<myapp:PaymentProcessor>
+			<myapp:Asynchronous />
+		</myapp:PaymentProcessor>
+		<myapp:User />
+	</myapp:Order>
+	<myapp:Login>
+		<ConversationScoped />
+		<BindingType />
+	</myapp:Login>
+</Beans>
\ No newline at end of file




More information about the weld-commits mailing list