Author: pete.muir(a)jboss.org
Date: 2009-03-19 10:44:25 -0400 (Thu, 19 Mar 2009)
New Revision: 2111
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/AnnotatedItemReceiver.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
Removed:
ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/BeansXmlParser.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/dom/
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/AnnotatedItemReceiver.java
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/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironmentImpl.java
Modified:
ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/mock/MockXmlEnvironment.java
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java
Log:
tidyup
Deleted: ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/BeansXmlParser.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/BeansXmlParser.java 2009-03-19
14:34:12 UTC (rev 2110)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/BeansXmlParser.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -1,225 +0,0 @@
-package org.jboss.webbeans.bootstrap;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.annotation.Annotation;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.inject.DefinitionException;
-import javax.inject.DeploymentException;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.jboss.webbeans.resources.spi.ResourceLoader;
-import org.jboss.webbeans.util.dom.NodeListIterable;
-import org.w3c.dom.Attr;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.xml.sax.SAXException;
-
-/**
- * Temporary XML parser to get essential data (like enabled deployment types)
- * until we have a full XML parser/binder
- *
- * @author Pete Muir
- *
- */
-public class BeansXmlParser
-{
-
- private static class DeployElement
- {
- private URL file;
- private Element element;
- private Map<String, String> namespaces;
-
- public DeployElement(URL file, Element element, Map<String, String>
namespaces)
- {
- super();
- this.file = file;
- this.element = element;
- this.namespaces = namespaces;
- }
-
- public URL getFile()
- {
- return file;
- }
-
- public Element getElement()
- {
- return element;
- }
-
- public Map<String, String> getNamespaces()
- {
- return namespaces;
- }
-
- @Override
- public String toString()
- {
- return "File: " + getFile() + "; Node: " + getElement();
- }
-
- }
-
- public static final String EE_NAMESPACE = "urn:java:ee";
-
- private final Iterable<URL> beansXml;
- private final ResourceLoader resourceLoader;
-
- private List<Class<? extends Annotation>> enabledDeploymentTypes;
-
- public List<Class<? extends Annotation>> getEnabledDeploymentTypes()
- {
- return enabledDeploymentTypes;
- }
-
- public BeansXmlParser(ResourceLoader resourceLoader, Iterable<URL> beansXml)
- {
- this.beansXml = beansXml;
- this.resourceLoader = resourceLoader;
- }
-
- public void parse()
- {
- DocumentBuilder documentBuilder;
- try
- {
- documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- }
- catch (ParserConfigurationException e)
- {
- throw new DeploymentException("Error configuring XML parser", e);
- }
- List<DeployElement> deployElements = new ArrayList<DeployElement>();
- for (URL url : beansXml)
- {
- InputStream is;
- boolean fileHasContents;
- try
- {
- is = url.openStream();
- fileHasContents = is.available() > 0;
- }
- catch (IOException e)
- {
- throw new DeploymentException("Error loading beans.xml " +
url.toString(), e);
- }
- if (fileHasContents)
- {
- Document document;
- try
- {
- document = documentBuilder.parse(is);
- document.normalize();
- }
- catch (SAXException e)
- {
- throw new DeploymentException("Error parsing beans.xml " +
url.toString(), e);
- }
- catch (IOException e)
- {
- throw new DeploymentException("Error loading beans.xml " +
url.toString(), e);
- }
- Element beans = document.getDocumentElement();
- Map<String, String> namespaces = new HashMap<String, String>();
- for (int i = 0; i < beans.getAttributes().getLength(); i++)
- {
- Node child = beans.getAttributes().item(i);
- if (child instanceof Attr)
- {
- Attr attr = (Attr) child;
- if (attr.getName().startsWith("xmlns"))
- {
- String namespacePrefix;
- if (attr.getName().length() == 5)
- {
- namespacePrefix = "";
- }
- else
- {
- namespacePrefix = attr.getName().substring(6);
- }
-
- String namespace = attr.getValue();
- namespaces.put(namespacePrefix, namespace);
- }
- }
- }
- for (Node child : new NodeListIterable(beans.getChildNodes()))
- {
- if (child instanceof Element &&
"Deploy".equals(child.getNodeName()))
- {
- deployElements.add(new DeployElement(url, (Element) child,
namespaces));
- }
- }
- }
- }
- if (deployElements.size() > 1)
- {
- throw new DeploymentException("<Deploy> can only be specified once,
but it is specified muliple times " + deployElements);
- }
- else if (deployElements.size() == 1)
- {
- DeployElement deployElement = deployElements.get(0);
- enabledDeploymentTypes = new ArrayList<Class<? extends
Annotation>>();
- for (Node child : new
NodeListIterable(deployElement.getElement().getChildNodes()))
- {
- if (child instanceof Element)
- {
- String className = getAsClassName(child.getNodeName(),
deployElement.getNamespaces(), deployElement.getFile());
- if (className != null)
- {
-
enabledDeploymentTypes.add(resourceLoader.classForName(className).asSubclass(Annotation.class));
- }
- }
- }
- }
- }
-
- private static String getAsClassName(String nodeName, Map<String, String>
namespaces, URL file)
- {
- String namespacePrefix;
- String simpleClassName;
- if (nodeName.contains(":"))
- {
- namespacePrefix = nodeName.substring(0, nodeName.indexOf(":"));
- simpleClassName = nodeName.substring(nodeName.indexOf(":") + 1);
- }
- else
- {
- namespacePrefix = "";
- simpleClassName = nodeName;
- }
- String namespace = namespaces.get(namespacePrefix);
- if (namespace == null)
- {
- throw new DefinitionException("Prefix " + namespacePrefix + " has
no namespace mapped in " + file.getPath());
- }
- String packageName;
- if (namespace.startsWith("urn:java:ee"))
- {
- // Hack for now to return the correct package for this composite package
deployment types
- packageName = "javax.inject";
- }
- else if (namespace.startsWith("urn:java:"))
- {
- packageName = namespace.substring(9);
- }
- else
- {
- // Not a Java package
- return null;
- }
- return packageName + "." + simpleClassName;
- }
-
-}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java 2009-03-19
14:34:12 UTC (rev 2110)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -55,9 +55,9 @@
import org.jboss.webbeans.servlet.HttpSessionManager;
import org.jboss.webbeans.servlet.ServletApiAbstraction;
import org.jboss.webbeans.transaction.spi.TransactionServices;
-import org.jboss.webbeans.util.xml.XmlParserImpl;
import org.jboss.webbeans.xml.XmlEnvironment;
-import org.jboss.webbeans.xml.XmlEnvironmentImpl;
+import org.jboss.webbeans.xml.XmlEnvironment;
+import org.jboss.webbeans.xml.XmlParser;
/**
* Common bootstrapping functionality that is run at application startup and
@@ -156,8 +156,8 @@
// bean is an EJB!
manager.getEjbDescriptorCache().addAll(getServices().get(EjbServices.class).discoverEjbs());
}
- XmlEnvironment xmlEnvironmentImpl = new XmlEnvironmentImpl(getServices());
- XmlParserImpl parser = new XmlParserImpl(xmlEnvironmentImpl);
+ XmlEnvironment xmlEnvironmentImpl = new XmlEnvironment(getServices());
+ XmlParser parser = new XmlParser(xmlEnvironmentImpl);
parser.parse();
List<Class<? extends Annotation>> enabledDeploymentTypes =
xmlEnvironmentImpl.getEnabledDeploymentTypes();
if (enabledDeploymentTypes.size() > 0)
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/mock/MockXmlEnvironment.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/mock/MockXmlEnvironment.java 2009-03-19
14:34:12 UTC (rev 2110)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/mock/MockXmlEnvironment.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -6,9 +6,9 @@
import org.jboss.webbeans.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.webbeans.resources.DefaultResourceLoader;
import org.jboss.webbeans.resources.spi.ResourceLoader;
-import org.jboss.webbeans.xml.XmlEnvironmentImpl;
+import org.jboss.webbeans.xml.XmlEnvironment;
-public class MockXmlEnvironment extends XmlEnvironmentImpl
+public class MockXmlEnvironment extends XmlEnvironment
{
private static final ServiceRegistry services;
Deleted:
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/AnnotatedItemReceiver.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/AnnotatedItemReceiver.java 2009-03-19
14:34:12 UTC (rev 2110)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/AnnotatedItemReceiver.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -1,12 +0,0 @@
-package org.jboss.webbeans.util.xml;
-
-
-import org.dom4j.Element;
-import org.jboss.webbeans.introspector.AnnotatedItem;
-
-public interface AnnotatedItemReceiver
-{
- boolean accept(Element element);
-
- AnnotatedItem<?, ?> receiveAnnotatedItem(Element element);
-}
Deleted: 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
14:34:12 UTC (rev 2110)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -1,268 +0,0 @@
-package org.jboss.webbeans.util.xml;
-
-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 javax.jms.JMSException;
-import javax.jms.Queue;
-import javax.jms.Topic;
-
-import org.dom4j.Element;
-import org.jboss.webbeans.CurrentManager;
-import org.jboss.webbeans.ManagerImpl;
-import org.jboss.webbeans.introspector.AnnotatedItem;
-import org.jboss.webbeans.introspector.jlr.AnnotatedClassImpl;
-import org.jboss.webbeans.resources.DefaultResourceLoader;
-import org.jboss.webbeans.resources.spi.ResourceLoader;
-
-public class ParseXmlHelper
-{
- private static List<AnnotatedItemReceiver> receivers;
-
- private static ResourceLoader resourceLoader;
-
- static
- {
- receivers = initializeReceivers();
- resourceLoader = new DefaultResourceLoader();
- }
-
- public static Set<AnnotatedItem<?, ?>> getBeanItems(List<Element>
beans)
- {
- Set<AnnotatedItem<?, ?>> result = new HashSet<AnnotatedItem<?,
?>>();
-
- for (Element bean : beans)
- result.add(receiveBeanItem(bean));
-
- return result;
- }
-
- private static AnnotatedItem<?, ?> receiveBeanItem(Element element)
- {
- for (AnnotatedItemReceiver receiver : receivers)
- {
- if (receiver.accept(element))
- {
- return receiver.receiveAnnotatedItem(element);
- }
- }
-
- throw new DefinitionException("definition of a bean " + element.getName()
+ " is incorrect");
- }
-
- private static List<AnnotatedItemReceiver> initializeReceivers()
- {
- List<AnnotatedItemReceiver> receivers = new
ArrayList<AnnotatedItemReceiver>();
-
- AnnotatedItemReceiver jmsResourceReceiver = new AnnotatedItemReceiver()
- {
- public boolean accept(Element element)
- {
- return isJMSResource(element);
- }
-
- public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
- {
- return receiveJMSResourceItem(element);
- }
-
- };
- AnnotatedItemReceiver resourceReceiver = new AnnotatedItemReceiver()
- {
- public boolean accept(Element element)
- {
- return isResource(element);
- }
-
- public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
- {
- return receiveResourceItem(element);
- }
-
- };
- AnnotatedItemReceiver sessionBeanReceiver = new AnnotatedItemReceiver()
- {
- public boolean accept(Element element)
- {
- return isSessionBean(element);
- }
-
- public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
- {
- return receiveSessionBeanItem(element);
- }
-
- };
- AnnotatedItemReceiver simpleBeanReceiver = new AnnotatedItemReceiver()
- {
- public boolean accept(Element element)
- {
- return isSimpleBean(element);
- }
-
- public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
- {
- return receiveSimpleBeanItem(element);
- }
-
- };
-
- //order of elements is important
- receivers.add(jmsResourceReceiver);
- receivers.add(resourceReceiver);
- receivers.add(sessionBeanReceiver);
- receivers.add(simpleBeanReceiver);
-
- return receivers;
- }
-
- private static boolean isJMSResource(Element element)
- {
- if (isJavaEeNamespace(element) &&
- (element.getName().equalsIgnoreCase(XmlConstants.TOPIC) ||
- element.getName().equalsIgnoreCase(XmlConstants.QUEUE)))
- return true;
- return false;
- }
-
- private static AnnotatedItem<?, ?> receiveJMSResourceItem(Element element)
- {
- final Element jmsElement = element;
-
- if(jmsElement.getName().equalsIgnoreCase(XmlConstants.QUEUE))
- {
- Queue queue = new Queue()
- {
- public String getQueueName() throws JMSException
- {
- return getJmsResourceName(jmsElement);
- }
- };
-
- return AnnotatedClassImpl.of(queue.getClass());
- }
-
- Topic topic = new Topic()
- {
- public String getTopicName() throws JMSException
- {
- return getJmsResourceName(jmsElement);
- }
- };
-
- return AnnotatedClassImpl.of(topic.getClass());
- }
-
- private static boolean isResource(Element element)
- {
- Iterator<?> elIterator = element.elementIterator();
- while (elIterator.hasNext())
- {
- Element child = (Element) elIterator.next();
- if (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;
- }
-
- private static AnnotatedItem<?, ?> receiveResourceItem(Element element)
- {
- // TODO:
- return null;
- }
-
- private static boolean isSessionBean(Element element)
- {
- ManagerImpl manager = CurrentManager.rootManager();
- if (manager.getEjbDescriptorCache().containsKey(element.getName()) ||
- element.attribute(XmlConstants.EJB_NAME) != null)
- return true;
- return false;
- }
-
- private static AnnotatedItem<?, ?> receiveSessionBeanItem(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 AnnotatedItem<?, ?> receiveSimpleBeanItem(Element element)
- {
- Class<?> beanClass = loadClass(element);
-
- if (!Modifier.isStatic(beanClass.getModifiers()) &&
- 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 AnnotatedClassImpl.of(beanClass);
- }
-
- public static Class<?> loadClass(Element element)
- {
- String beanUri = element.getNamespace().getURI();
- String packageName = beanUri.replaceFirst(XmlConstants.URN_PREFIX, "");
- String classPath = packageName + "." + element.getName();
- return resourceLoader.classForName(classPath);
- }
-
- private static String getJmsResourceName(Element element)
- {
- Iterator<?> elIterator = element.elementIterator();
- while (elIterator.hasNext())
- {
- Element child = (Element) elIterator.next();
- if (isJavaEeNamespace(child) &&
- child.getName().equalsIgnoreCase(XmlConstants.RESOURCE))
- {
- Iterator<?> chIterator = child.elementIterator();
- while(chIterator.hasNext())
- {
- Element chChild = (Element) chIterator.next();
- if (isJavaEeNamespace(chChild) &&
- (chChild.getName().equalsIgnoreCase(XmlConstants.NAME) ||
-
chChild.getName().equalsIgnoreCase(XmlConstants.MAPPED_NAME)))
- {
- return chChild.getName();
- }
- }
- }
- }
- throw new DefinitionException("Incorrect JMSResource declaration for " +
element.getName());
- }
-
- public static boolean isJavaEeNamespace(Element element)
- {
- return
element.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE);
- }
-}
Deleted: 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
14:34:12 UTC (rev 2110)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -1,70 +0,0 @@
-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";
-
- 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 EJB_NAME = "ejbName";
-
- 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:";
-
- public static final String NAME = "name";
-
- public static final String MAPPED_NAME = "mappedName";
-
- public static final String STANDARD = "Standard";
-}
Deleted: 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
14:34:12 UTC (rev 2110)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -1,219 +0,0 @@
-package org.jboss.webbeans.util.xml;
-
-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;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
-import javax.inject.DefinitionException;
-import javax.inject.DeploymentException;
-import javax.inject.DeploymentType;
-
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.Element;
-import org.dom4j.Namespace;
-import org.dom4j.io.SAXReader;
-import org.jboss.webbeans.log.Log;
-import org.jboss.webbeans.log.Logging;
-import org.jboss.webbeans.xml.XmlEnvironment;
-
-public class XmlParserImpl
-{
- private static Log log = Logging.getLog(XmlParserImpl.class);
-
- private final XmlEnvironment environment;
-
- public XmlParserImpl(XmlEnvironment environment)
- {
- this.environment = environment;
- }
-
- public void parse()
- {
- // TODO extremely inefficient, no need to make dom4j parse each beans.xml multiple
times, do as one parse over each document
- parseForBeans();
- parseForDeploy();
- }
-
- private void parseForBeans()
- {
-
- for (URL url : environment.getBeansXmlUrls())
- {
- Document document = createDocument(url);
- if (document != null)
- {
- List<Element> beanElements = findBeans(document);
- // TODO Only pass in classes here
-
//environment.getClasses().addAll(ParseXmlHelper.getBeanItems(beanElements));
- }
- }
-
- }
-
- private void parseForDeploy()
- {
- int counter = 0;
-
- for (URL url : environment.getBeansXmlUrls())
- {
- Document document = createDocument(url);
- if (document != null)
- {
- Element root = document.getRootElement();
-
- Iterator<?> elIterator = root.elementIterator();
- while (elIterator.hasNext())
- {
- Element element = (Element) elIterator.next();
- if (ParseXmlHelper.isJavaEeNamespace(element) &&
- element.getName().equalsIgnoreCase(XmlConstants.DEPLOY))
-
environment.getEnabledDeploymentTypes().addAll(obtainDeploymentTypes(element,
counter++));
- }
- }
- }
-
- }
-
- @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();
- if (xmlStream.available() == 0)
- {
- return null;
- }
- SAXReader reader = new SAXReader();
- Document document = reader.read(xmlStream);
- checkNamespaces(document);
- return document;
- }
- 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)
- {
- List<Element> beans = new ArrayList<Element>();
-
- Element root = document.getRootElement();
-
- Iterator<?> elIterator = root.elementIterator();
- while (elIterator.hasNext())
- {
- Element element = (Element) elIterator.next();
- if (checkBeanElementName(element) &&
- checkBeanElementChildrenNames(element))
- beans.add(element);
- }
-
- return beans;
- }
-
- private boolean checkBeanElementName(Element element)
- {
- if (ParseXmlHelper.isJavaEeNamespace(element) &&
- (element.getName().equalsIgnoreCase(XmlConstants.DEPLOY) ||
- element.getName().equalsIgnoreCase(XmlConstants.INTERCEPTORS) ||
- element.getName().equalsIgnoreCase(XmlConstants.DECORATORS)))
- return false;
- return true;
- }
-
- private boolean checkBeanElementChildrenNames(Element element)
- {
- Iterator<?> elIterator = element.elementIterator();
- while (elIterator.hasNext())
- {
- Element child = (Element) elIterator.next();
- if (ParseXmlHelper.isJavaEeNamespace(child) &&
- (child.getName().equalsIgnoreCase(XmlConstants.BINDING_TYPE) ||
-
child.getName().equalsIgnoreCase(XmlConstants.INTERCEPTOR_BINDING_TYPE) ||
- child.getName().equalsIgnoreCase(XmlConstants.STEREOTYPE)))
- return false;
- }
- 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);
-
- 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");
- }
-}
Copied: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/AnnotatedItemReceiver.java
(from rev 2108,
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/AnnotatedItemReceiver.java)
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/AnnotatedItemReceiver.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/AnnotatedItemReceiver.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -0,0 +1,12 @@
+package org.jboss.webbeans.xml;
+
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedItem;
+
+public interface AnnotatedItemReceiver
+{
+ boolean accept(Element element);
+
+ AnnotatedItem<?, ?> receiveAnnotatedItem(Element element);
+}
Copied: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java (from rev
2108, ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/ParseXmlHelper.java)
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -0,0 +1,268 @@
+package org.jboss.webbeans.xml;
+
+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 javax.jms.JMSException;
+import javax.jms.Queue;
+import javax.jms.Topic;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.introspector.AnnotatedItem;
+import org.jboss.webbeans.introspector.jlr.AnnotatedClassImpl;
+import org.jboss.webbeans.resources.DefaultResourceLoader;
+import org.jboss.webbeans.resources.spi.ResourceLoader;
+
+public class ParseXmlHelper
+{
+ private static List<AnnotatedItemReceiver> receivers;
+
+ private static ResourceLoader resourceLoader;
+
+ static
+ {
+ receivers = initializeReceivers();
+ resourceLoader = new DefaultResourceLoader();
+ }
+
+ public static Set<AnnotatedItem<?, ?>> getBeanItems(List<Element>
beans)
+ {
+ Set<AnnotatedItem<?, ?>> result = new HashSet<AnnotatedItem<?,
?>>();
+
+ for (Element bean : beans)
+ result.add(receiveBeanItem(bean));
+
+ return result;
+ }
+
+ private static AnnotatedItem<?, ?> receiveBeanItem(Element element)
+ {
+ for (AnnotatedItemReceiver receiver : receivers)
+ {
+ if (receiver.accept(element))
+ {
+ return receiver.receiveAnnotatedItem(element);
+ }
+ }
+
+ throw new DefinitionException("definition of a bean " + element.getName()
+ " is incorrect");
+ }
+
+ private static List<AnnotatedItemReceiver> initializeReceivers()
+ {
+ List<AnnotatedItemReceiver> receivers = new
ArrayList<AnnotatedItemReceiver>();
+
+ AnnotatedItemReceiver jmsResourceReceiver = new AnnotatedItemReceiver()
+ {
+ public boolean accept(Element element)
+ {
+ return isJMSResource(element);
+ }
+
+ public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
+ {
+ return receiveJMSResourceItem(element);
+ }
+
+ };
+ AnnotatedItemReceiver resourceReceiver = new AnnotatedItemReceiver()
+ {
+ public boolean accept(Element element)
+ {
+ return isResource(element);
+ }
+
+ public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
+ {
+ return receiveResourceItem(element);
+ }
+
+ };
+ AnnotatedItemReceiver sessionBeanReceiver = new AnnotatedItemReceiver()
+ {
+ public boolean accept(Element element)
+ {
+ return isSessionBean(element);
+ }
+
+ public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
+ {
+ return receiveSessionBeanItem(element);
+ }
+
+ };
+ AnnotatedItemReceiver simpleBeanReceiver = new AnnotatedItemReceiver()
+ {
+ public boolean accept(Element element)
+ {
+ return isSimpleBean(element);
+ }
+
+ public AnnotatedItem<?, ?> receiveAnnotatedItem(Element element)
+ {
+ return receiveSimpleBeanItem(element);
+ }
+
+ };
+
+ //order of elements is important
+ receivers.add(jmsResourceReceiver);
+ receivers.add(resourceReceiver);
+ receivers.add(sessionBeanReceiver);
+ receivers.add(simpleBeanReceiver);
+
+ return receivers;
+ }
+
+ private static boolean isJMSResource(Element element)
+ {
+ if (isJavaEeNamespace(element) &&
+ (element.getName().equalsIgnoreCase(XmlConstants.TOPIC) ||
+ element.getName().equalsIgnoreCase(XmlConstants.QUEUE)))
+ return true;
+ return false;
+ }
+
+ private static AnnotatedItem<?, ?> receiveJMSResourceItem(Element element)
+ {
+ final Element jmsElement = element;
+
+ if(jmsElement.getName().equalsIgnoreCase(XmlConstants.QUEUE))
+ {
+ Queue queue = new Queue()
+ {
+ public String getQueueName() throws JMSException
+ {
+ return getJmsResourceName(jmsElement);
+ }
+ };
+
+ return AnnotatedClassImpl.of(queue.getClass());
+ }
+
+ Topic topic = new Topic()
+ {
+ public String getTopicName() throws JMSException
+ {
+ return getJmsResourceName(jmsElement);
+ }
+ };
+
+ return AnnotatedClassImpl.of(topic.getClass());
+ }
+
+ private static boolean isResource(Element element)
+ {
+ Iterator<?> elIterator = element.elementIterator();
+ while (elIterator.hasNext())
+ {
+ Element child = (Element) elIterator.next();
+ if (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;
+ }
+
+ private static AnnotatedItem<?, ?> receiveResourceItem(Element element)
+ {
+ // TODO:
+ return null;
+ }
+
+ private static boolean isSessionBean(Element element)
+ {
+ ManagerImpl manager = CurrentManager.rootManager();
+ if (manager.getEjbDescriptorCache().containsKey(element.getName()) ||
+ element.attribute(XmlConstants.EJB_NAME) != null)
+ return true;
+ return false;
+ }
+
+ private static AnnotatedItem<?, ?> receiveSessionBeanItem(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 AnnotatedItem<?, ?> receiveSimpleBeanItem(Element element)
+ {
+ Class<?> beanClass = loadClass(element);
+
+ if (!Modifier.isStatic(beanClass.getModifiers()) &&
+ 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 AnnotatedClassImpl.of(beanClass);
+ }
+
+ public static Class<?> loadClass(Element element)
+ {
+ String beanUri = element.getNamespace().getURI();
+ String packageName = beanUri.replaceFirst(XmlConstants.URN_PREFIX, "");
+ String classPath = packageName + "." + element.getName();
+ return resourceLoader.classForName(classPath);
+ }
+
+ private static String getJmsResourceName(Element element)
+ {
+ Iterator<?> elIterator = element.elementIterator();
+ while (elIterator.hasNext())
+ {
+ Element child = (Element) elIterator.next();
+ if (isJavaEeNamespace(child) &&
+ child.getName().equalsIgnoreCase(XmlConstants.RESOURCE))
+ {
+ Iterator<?> chIterator = child.elementIterator();
+ while(chIterator.hasNext())
+ {
+ Element chChild = (Element) chIterator.next();
+ if (isJavaEeNamespace(chChild) &&
+ (chChild.getName().equalsIgnoreCase(XmlConstants.NAME) ||
+
chChild.getName().equalsIgnoreCase(XmlConstants.MAPPED_NAME)))
+ {
+ return chChild.getName();
+ }
+ }
+ }
+ }
+ throw new DefinitionException("Incorrect JMSResource declaration for " +
element.getName());
+ }
+
+ public static boolean isJavaEeNamespace(Element element)
+ {
+ return
element.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE);
+ }
+}
Property changes on:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java (from rev
2108, ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlConstants.java)
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -0,0 +1,70 @@
+package org.jboss.webbeans.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";
+
+ 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 EJB_NAME = "ejbName";
+
+ 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:";
+
+ public static final String NAME = "name";
+
+ public static final String MAPPED_NAME = "mappedName";
+
+ public static final String STANDARD = "Standard";
+}
Property changes on: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java 2009-03-19
14:34:12 UTC (rev 2110)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -1,25 +0,0 @@
-package org.jboss.webbeans.xml;
-
-import java.lang.annotation.Annotation;
-import java.net.URL;
-import java.util.List;
-
-import org.jboss.webbeans.introspector.AnnotatedAnnotation;
-import org.jboss.webbeans.introspector.AnnotatedClass;
-
-public interface XmlEnvironment
-{
-
- public List<AnnotatedClass<?>> getClasses();
-
- public List<AnnotatedAnnotation<?>> getAnnotations();
-
- public Iterable<URL> getBeansXmlUrls();
-
- public <T> AnnotatedClass<? extends T> loadClass(String className,
Class<T> expectedType);
-
- public <T extends Annotation> AnnotatedAnnotation<? extends T>
loadAnnotation(String className, Class<T> expectedType);
-
- public List<Class<? extends Annotation>> getEnabledDeploymentTypes();
-
-}
\ No newline at end of file
Copied: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java (from rev
2110, ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironmentImpl.java)
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironment.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -0,0 +1,64 @@
+package org.jboss.webbeans.xml;
+
+import java.lang.annotation.Annotation;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.webbeans.bootstrap.api.ServiceRegistry;
+import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
+import org.jboss.webbeans.introspector.AnnotatedAnnotation;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.introspector.jlr.AnnotatedAnnotationImpl;
+import org.jboss.webbeans.introspector.jlr.AnnotatedClassImpl;
+import org.jboss.webbeans.resources.spi.ResourceLoader;
+
+public class XmlEnvironment
+{
+
+ private final List<AnnotatedClass<?>> classes;
+ private final List<AnnotatedAnnotation<?>> annotations;
+ private final ServiceRegistry serviceRegistry;
+ private final List<Class<? extends Annotation>> enabledDeploymentTypes;
+ private final Iterable<URL> beansXmlUrls;
+
+ public XmlEnvironment(ServiceRegistry serviceRegistry)
+ {
+ this.classes = new ArrayList<AnnotatedClass<?>>();
+ this.annotations = new ArrayList<AnnotatedAnnotation<?>>();
+ this.enabledDeploymentTypes = new ArrayList<Class<? extends
Annotation>>();
+ this.serviceRegistry = serviceRegistry;
+ this.beansXmlUrls =
serviceRegistry.get(WebBeanDiscovery.class).discoverWebBeansXml();
+ }
+
+ public List<AnnotatedClass<?>> getClasses()
+ {
+ return classes;
+ }
+
+ public List<AnnotatedAnnotation<?>> getAnnotations()
+ {
+ return annotations;
+ }
+
+ public Iterable<URL> getBeansXmlUrls()
+ {
+ return beansXmlUrls;
+ }
+
+ public <T> AnnotatedClass<? extends T> loadClass(String className,
Class<T> expectedType)
+ {
+ return
AnnotatedClassImpl.of(serviceRegistry.get(ResourceLoader.class).classForName(className).asSubclass(expectedType));
+ }
+
+ public <T extends Annotation> AnnotatedAnnotation<? extends T>
loadAnnotation(String className, Class<T> expectedType)
+ {
+ return
AnnotatedAnnotationImpl.of(serviceRegistry.get(ResourceLoader.class).classForName(className).asSubclass(expectedType));
+ }
+
+ public List<Class<? extends Annotation>> getEnabledDeploymentTypes()
+ {
+ return enabledDeploymentTypes;
+ }
+
+}
Deleted: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironmentImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironmentImpl.java 2009-03-19
14:34:12 UTC (rev 2110)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlEnvironmentImpl.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -1,64 +0,0 @@
-package org.jboss.webbeans.xml;
-
-import java.lang.annotation.Annotation;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.jboss.webbeans.bootstrap.api.ServiceRegistry;
-import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
-import org.jboss.webbeans.introspector.AnnotatedAnnotation;
-import org.jboss.webbeans.introspector.AnnotatedClass;
-import org.jboss.webbeans.introspector.jlr.AnnotatedAnnotationImpl;
-import org.jboss.webbeans.introspector.jlr.AnnotatedClassImpl;
-import org.jboss.webbeans.resources.spi.ResourceLoader;
-
-public class XmlEnvironmentImpl implements XmlEnvironment
-{
-
- private final List<AnnotatedClass<?>> classes;
- private final List<AnnotatedAnnotation<?>> annotations;
- private final ServiceRegistry serviceRegistry;
- private final List<Class<? extends Annotation>> enabledDeploymentTypes;
- private final Iterable<URL> beansXmlUrls;
-
- public XmlEnvironmentImpl(ServiceRegistry serviceRegistry)
- {
- this.classes = new ArrayList<AnnotatedClass<?>>();
- this.annotations = new ArrayList<AnnotatedAnnotation<?>>();
- this.enabledDeploymentTypes = new ArrayList<Class<? extends
Annotation>>();
- this.serviceRegistry = serviceRegistry;
- this.beansXmlUrls =
serviceRegistry.get(WebBeanDiscovery.class).discoverWebBeansXml();
- }
-
- public List<AnnotatedClass<?>> getClasses()
- {
- return classes;
- }
-
- public List<AnnotatedAnnotation<?>> getAnnotations()
- {
- return annotations;
- }
-
- public Iterable<URL> getBeansXmlUrls()
- {
- return beansXmlUrls;
- }
-
- public <T> AnnotatedClass<? extends T> loadClass(String className,
Class<T> expectedType)
- {
- return
AnnotatedClassImpl.of(serviceRegistry.get(ResourceLoader.class).classForName(className).asSubclass(expectedType));
- }
-
- public <T extends Annotation> AnnotatedAnnotation<? extends T>
loadAnnotation(String className, Class<T> expectedType)
- {
- return
AnnotatedAnnotationImpl.of(serviceRegistry.get(ResourceLoader.class).classForName(className).asSubclass(expectedType));
- }
-
- public List<Class<? extends Annotation>> getEnabledDeploymentTypes()
- {
- return enabledDeploymentTypes;
- }
-
-}
Copied: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java (from rev 2110,
ri/trunk/impl/src/main/java/org/jboss/webbeans/util/xml/XmlParserImpl.java)
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java 2009-03-19 14:44:25
UTC (rev 2111)
@@ -0,0 +1,218 @@
+package org.jboss.webbeans.xml;
+
+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;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javax.inject.DefinitionException;
+import javax.inject.DeploymentException;
+import javax.inject.DeploymentType;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.Namespace;
+import org.dom4j.io.SAXReader;
+import org.jboss.webbeans.log.Log;
+import org.jboss.webbeans.log.Logging;
+
+public class XmlParser
+{
+ private static Log log = Logging.getLog(XmlParser.class);
+
+ private final XmlEnvironment environment;
+
+ public XmlParser(XmlEnvironment environment)
+ {
+ this.environment = environment;
+ }
+
+ public void parse()
+ {
+ // TODO extremely inefficient, no need to make dom4j parse each beans.xml multiple
times, do as one parse over each document
+ parseForBeans();
+ parseForDeploy();
+ }
+
+ private void parseForBeans()
+ {
+
+ for (URL url : environment.getBeansXmlUrls())
+ {
+ Document document = createDocument(url);
+ if (document != null)
+ {
+ List<Element> beanElements = findBeans(document);
+ // TODO Only pass in classes here
+
//environment.getClasses().addAll(ParseXmlHelper.getBeanItems(beanElements));
+ }
+ }
+
+ }
+
+ private void parseForDeploy()
+ {
+ int counter = 0;
+
+ for (URL url : environment.getBeansXmlUrls())
+ {
+ Document document = createDocument(url);
+ if (document != null)
+ {
+ Element root = document.getRootElement();
+
+ Iterator<?> elIterator = root.elementIterator();
+ while (elIterator.hasNext())
+ {
+ Element element = (Element) elIterator.next();
+ if (ParseXmlHelper.isJavaEeNamespace(element) &&
+ element.getName().equalsIgnoreCase(XmlConstants.DEPLOY))
+
environment.getEnabledDeploymentTypes().addAll(obtainDeploymentTypes(element,
counter++));
+ }
+ }
+ }
+
+ }
+
+ @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();
+ if (xmlStream.available() == 0)
+ {
+ return null;
+ }
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(xmlStream);
+ checkNamespaces(document);
+ return document;
+ }
+ 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)
+ {
+ List<Element> beans = new ArrayList<Element>();
+
+ Element root = document.getRootElement();
+
+ Iterator<?> elIterator = root.elementIterator();
+ while (elIterator.hasNext())
+ {
+ Element element = (Element) elIterator.next();
+ if (checkBeanElementName(element) &&
+ checkBeanElementChildrenNames(element))
+ beans.add(element);
+ }
+
+ return beans;
+ }
+
+ private boolean checkBeanElementName(Element element)
+ {
+ if (ParseXmlHelper.isJavaEeNamespace(element) &&
+ (element.getName().equalsIgnoreCase(XmlConstants.DEPLOY) ||
+ element.getName().equalsIgnoreCase(XmlConstants.INTERCEPTORS) ||
+ element.getName().equalsIgnoreCase(XmlConstants.DECORATORS)))
+ return false;
+ return true;
+ }
+
+ private boolean checkBeanElementChildrenNames(Element element)
+ {
+ Iterator<?> elIterator = element.elementIterator();
+ while (elIterator.hasNext())
+ {
+ Element child = (Element) elIterator.next();
+ if (ParseXmlHelper.isJavaEeNamespace(child) &&
+ (child.getName().equalsIgnoreCase(XmlConstants.BINDING_TYPE) ||
+
child.getName().equalsIgnoreCase(XmlConstants.INTERCEPTOR_BINDING_TYPE) ||
+ child.getName().equalsIgnoreCase(XmlConstants.STEREOTYPE)))
+ return false;
+ }
+ 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);
+
+ 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");
+ }
+}
Property changes on: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
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
14:34:12 UTC (rev 2110)
+++
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java 2009-03-19
14:44:25 UTC (rev 2111)
@@ -13,8 +13,8 @@
import org.jboss.webbeans.mock.MockXmlEnvironment;
import org.jboss.webbeans.test.unit.AbstractWebBeansTest;
import org.jboss.webbeans.test.unit.xml.beans.Order;
-import org.jboss.webbeans.util.xml.XmlParserImpl;
-import org.jboss.webbeans.xml.XmlEnvironmentImpl;
+import org.jboss.webbeans.xml.XmlEnvironment;
+import org.jboss.webbeans.xml.XmlParser;
import org.testng.annotations.Test;
@Artifact
@@ -27,7 +27,7 @@
@Test
public void testParse()
{
- XmlEnvironmentImpl parserEnv = new
MockXmlEnvironment(getResources("beans.xml"));
+ XmlEnvironment parserEnv = new
MockXmlEnvironment(getResources("beans.xml"));
AnnotatedClass<?> aClass =
parserEnv.loadClass("org.jboss.webbeans.test.unit.xml.beans.Order",
Order.class);
Set<URL> xmls = new HashSet<URL>();
@@ -36,7 +36,7 @@
for (URL url : urls)
xmls.add(url);
- XmlParserImpl parser = new XmlParserImpl(parserEnv);
+ XmlParser parser = new XmlParser(parserEnv);
parser.parse();
for (AnnotatedItem<?, ?> aElement : parserEnv.getClasses())