Author: vitold
Date: 2009-03-19 03:47:47 -0400 (Thu, 19 Mar 2009)
New Revision: 2105
Modified:
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java
Log:
add in XmlParserImpl.java method parseForDeploy(Set<URL> xmls) which adds to current
manager enabled deployments types
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java 2009-03-19
01:19:23 UTC (rev 2104)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java 2009-03-19
07:47:47 UTC (rev 2105)
@@ -228,7 +228,7 @@
return AnnotatedClassImpl.of(beanClass);
}
- private static Class<?> loadClass(Element element)
+ public static Class<?> loadClass(Element element)
{
String beanUri = element.getNamespace().getURI();
String packageName = beanUri.replaceFirst(XmlConstants.URN_PREFIX, "");
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java 2009-03-19
01:19:23 UTC (rev 2104)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java 2009-03-19
07:47:47 UTC (rev 2105)
@@ -1,5 +1,33 @@
package org.jboss.webbeans.util.xml;
+enum JavaEePackage
+{
+ JAVA_LANG("java.lang"),
+ JAVA_UTIL("java.util"),
+ JAVAX_ANNOTATION("javax.annotation"),
+ JAVAX_INJECT("javax.inject"),
+ JAVAX_CONTEXT("javax.context"),
+ JAVAX_INTERCEPTOR("javax.interceptor"),
+ JAVAX_DECORATOR("javax.decorator"),
+ JAVAX_EVENT("javax.event"),
+ JAVAX_EJB("javax.ejb"),
+ JAVAX_PERSISTENCE("javax.persistence"),
+ JAVAX_XML_WS("javax.xml.ws"),
+ JAVAX_JMS("javax.jms"),
+ JAVAX_SQL("javax.sql");
+
+ private String packageName;
+
+ JavaEePackage(String name) {
+ packageName = name;
+ }
+
+ @Override
+ public String toString() {
+ return packageName;
+ }
+}
+
public class XmlConstants
{
public static final String JAVA_EE_NAMESPACE = "urn:java:ee";
@@ -37,4 +65,6 @@
public static final String NAME = "name";
public static final String MAPPED_NAME = "mappedName";
+
+ public static final String STANDARD = "Standard";
}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java 2009-03-19
01:19:23 UTC (rev 2104)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java 2009-03-19
07:47:47 UTC (rev 2105)
@@ -2,6 +2,7 @@
import java.io.IOException;
import java.io.InputStream;
+import java.lang.annotation.Annotation;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
@@ -9,45 +10,104 @@
import java.util.List;
import java.util.Set;
+import javax.inject.DefinitionException;
+import javax.inject.DeploymentException;
+import javax.inject.DeploymentType;
+import javax.inject.Production;
+import javax.inject.Standard;
+
+import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
+import org.dom4j.Namespace;
+import org.dom4j.QName;
import org.dom4j.io.SAXReader;
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedItem;
-import org.jboss.webbeans.log.Log;
-import org.jboss.webbeans.log.Logging;
public class XmlParserImpl // implements XmlParser
{
-
- private static Log log = Logging.getLog(XmlParserImpl.class);
-
- public Set<AnnotatedItem<?, ?>> parse(Set<URL> xmls)
+ private static Logger log = Logger.getLogger(XmlParserImpl.class);
+
+ public Set<AnnotatedItem<?, ?>> parseForBeans(Set<URL> xmls)
{
Set<AnnotatedItem<?, ?>> result = new HashSet<AnnotatedItem<?,
?>>();
for (URL url : xmls)
{
- try
+ Document document = createDocument(url);
+ List<Element> beanElements = findBeans(document);
+ result.addAll(ParseXmlHelper.getBeanItems(beanElements));
+ }
+ return result;
+ }
+
+ public void parseForDeploy(Set<URL> xmls)
+ {
+ List<Class<? extends Annotation>> deploymentClasses = new
ArrayList<Class<? extends Annotation>>();
+ int counter = 0;
+
+ for (URL url : xmls)
+ {
+ Document document = createDocument(url);
+ Element root = document.getRootElement();
+
+ Iterator<?> elIterator = root.elementIterator();
+ while (elIterator.hasNext())
{
- InputStream xmlStream;
+ Element element = (Element) elIterator.next();
+ if (ParseXmlHelper.isJavaEeNamespace(element) &&
+ element.getName().equalsIgnoreCase(XmlConstants.DEPLOY))
+ deploymentClasses.addAll(obtainDeploymentTypes(element, counter++));
+ }
+ }
+
+ if(deploymentClasses.size() == 0)
+ {
+ deploymentClasses.add(Standard.class);
+ deploymentClasses.add(Production.class);
+ }
- xmlStream = url.openStream();
- SAXReader reader = new SAXReader();
- Document document = reader.read(xmlStream);
- List<Element> beanElements = findBeans(document);
- result.addAll(ParseXmlHelper.getBeanItems(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);
- }
+ ManagerImpl manager = CurrentManager.rootManager();
+ manager.setEnabledDeploymentTypes(deploymentClasses);
+ }
+
+ @SuppressWarnings("unchecked")
+ public boolean checkNamespaces(Document document)
+ {
+ //TODO: not finished
+ Element root = document.getRootElement();
+ List<Namespace> declaredNamespaces = root.declaredNamespaces();
+
+ return true;
+ }
+
+ private Document createDocument(URL url)
+ {
+ try
+ {
+ InputStream xmlStream;
+
+ xmlStream = url.openStream();
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(xmlStream);
+ checkNamespaces(document);
+ return document;
}
- return result;
+ catch (IOException e)
+ {
+ String message = "Can not open stream for " + url;
+ log.debug(message, e);
+ throw new DeploymentException(message, e);
+ }
+ catch (DocumentException e)
+ {
+ String message = "Error during the processing of a DOM4J document for
" + url;
+ log.debug(message, e);
+ throw new DeploymentException(message, e);
+ }
}
private List<Element> findBeans(Document document)
@@ -60,14 +120,15 @@
while (elIterator.hasNext())
{
Element element = (Element) elIterator.next();
- if (checkElementName(element) && checkElementChildrenNames(element))
+ if (checkBeanElementName(element) &&
+ checkBeanElementChildrenNames(element))
beans.add(element);
}
return beans;
}
- private boolean checkElementName(Element element)
+ private boolean checkBeanElementName(Element element)
{
if (ParseXmlHelper.isJavaEeNamespace(element) &&
(element.getName().equalsIgnoreCase(XmlConstants.DEPLOY) ||
@@ -77,7 +138,7 @@
return true;
}
- private boolean checkElementChildrenNames(Element element)
+ private boolean checkBeanElementChildrenNames(Element element)
{
Iterator<?> elIterator = element.elementIterator();
while (elIterator.hasNext())
@@ -91,4 +152,61 @@
}
return true;
}
+
+ @SuppressWarnings("unchecked")
+ private List<Class<? extends Annotation>> obtainDeploymentTypes(Element
element, int counter)
+ {
+ if (counter > 1)
+ throw new DefinitionException("<Deploy> element is specified more
than once");
+
+ List<Element> deployElements = element.elements();
+ Set<Element> deployElementsSet = new HashSet<Element>(deployElements);
+ if(deployElements.size() - deployElementsSet.size() != 0)
+ throw new DefinitionException("The same deployment type is declared more
than once");
+
+ String standardName = XmlConstants.STANDARD;
+ String standardPrefix = "";
+ String standardUri = XmlConstants.JAVA_EE_NAMESPACE;
+ Namespace standardNamespace = new Namespace(standardPrefix, standardUri);
+ QName qName = new QName(standardName, standardNamespace);
+ Element standardElement = element.element(qName);
+ if (standardElement == null)
+ throw new DeploymentException("The @Standard deployment type must be
declared");
+
+ List<Class<? extends Annotation>> deploymentClasses = new
ArrayList<Class<? extends Annotation>>();
+ List<Element> children = element.elements();
+ for (Element child : children)
+ {
+ Class<?> deploymentClass;
+
+ if (ParseXmlHelper.isJavaEeNamespace(child))
+ deploymentClass = loadJavaEeDeploymentType(child);
+ else
+ deploymentClass = ParseXmlHelper.loadClass(child);
+
+ if(!deploymentClass.isAnnotation())
+ throw new DeploymentException("<Deploy> child " +
element.getName() + " must be a Java annotation type");
+
+ if(deploymentClass.getAnnotation(DeploymentType.class) == null)
+ throw new DefinitionException("<Deploy> child " +
element.getName() + " must be a deployment type");
+
+ deploymentClasses.add(deploymentClass.asSubclass(Annotation.class));
+ }
+ return deploymentClasses;
+ }
+
+ private Class<?> loadJavaEeDeploymentType(Element element)
+ {
+ for(JavaEePackage possiblePackage : JavaEePackage.values())
+ {
+ String className = possiblePackage + "." + element.getName();
+ try
+ {
+ return Class.forName(className);
+ }
+ catch (ClassNotFoundException e)
+ {}
+ }
+ throw new DefinitionException("Could not find " + element.getName() +
"in the Java EE namespace");
+ }
}
Modified:
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java
===================================================================
---
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java 2009-03-19
01:19:23 UTC (rev 2104)
+++
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java 2009-03-19
07:47:47 UTC (rev 2105)
@@ -34,11 +34,11 @@
Iterable<URL> urls = getResources("user-defined-beans.xml");
for (URL url : urls)
- xmls.add(url);
-
+ xmls.add(url);
+
XmlParserImpl parser = new XmlParserImpl();
- Set<AnnotatedItem<?, ?>> aSet = parser.parse(xmls);
-
+ Set<AnnotatedItem<?, ?>> aSet = parser.parseForBeans(xmls);
+
for (AnnotatedItem<?, ?> aElement : aSet)
{
assert aElement.equals(aClass);