Skip to content

Commit

Permalink
Initial work on reading enabled deployment types from XML.
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1525 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Feb 15, 2009
1 parent 9998052 commit 9ae7d3f
Show file tree
Hide file tree
Showing 17 changed files with 447 additions and 37 deletions.
@@ -1,6 +1,7 @@
package org.jboss.webbeans.tck;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;

import javax.inject.manager.Manager;
Expand All @@ -24,7 +25,9 @@ public Manager deploy(List<Class<? extends Annotation>> enabledDeploymentTypes,
{
manager.setEnabledDeploymentTypes(enabledDeploymentTypes);
}
bootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(classes));
MockWebBeanDiscovery discovery = new MockWebBeanDiscovery();
discovery.setWebBeanClasses(Arrays.asList(classes));
bootstrap.setWebBeanDiscovery(discovery);
bootstrap.boot();
return manager;
}
Expand Down
@@ -0,0 +1,205 @@
package org.jboss.webbeans.bootstrap;

import java.io.IOException;
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.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)
{
Document document;
try
{
document = documentBuilder.parse(url.openStream());
document.normalize();
}
catch (SAXException e)
{
throw new DeploymentException("Error parsing beans.xml " + url.toString());
}
catch (IOException e)
{
throw new DeploymentException("Error loading beans.xml " + url.toString());
}
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());
if (className != null)
{
enabledDeploymentTypes.add(resourceLoader.classForName(className).asSubclass(Annotation.class));
}
}
}
}
}

private static String getAsClassName(String nodeName, Map<String, String> namespaces)
{
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);
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;
}

}
Expand Up @@ -17,6 +17,9 @@

package org.jboss.webbeans.bootstrap;

import java.lang.annotation.Annotation;
import java.util.List;

import org.jboss.webbeans.BeanValidator;
import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.ManagerImpl;
Expand All @@ -25,9 +28,9 @@
import org.jboss.webbeans.bootstrap.spi.EjbDiscovery;
import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
import org.jboss.webbeans.conversation.ConversationImpl;
import org.jboss.webbeans.conversation.ServletConversationManager;
import org.jboss.webbeans.conversation.JavaSEConversationTerminator;
import org.jboss.webbeans.conversation.NumericConversationIdGenerator;
import org.jboss.webbeans.conversation.ServletConversationManager;
import org.jboss.webbeans.ejb.spi.EjbResolver;
import org.jboss.webbeans.literal.DeployedLiteral;
import org.jboss.webbeans.literal.InitializedLiteral;
Expand Down Expand Up @@ -126,6 +129,13 @@ public void boot()
// Must populate EJB cache first, as we need it to detect whether a
// bean is an EJB!
getManager().getEjbDescriptorCache().addAll(getEjbDiscovery().discoverEjbs());
BeansXmlParser parser = new BeansXmlParser(getResourceLoader(), getWebBeanDiscovery().discoverWebBeansXml());
parser.parse();
List<Class<? extends Annotation>> enabledDeploymentTypes = parser.getEnabledDeploymentTypes();
if (enabledDeploymentTypes != null)
{
getManager().setEnabledDeploymentTypes(enabledDeploymentTypes);
}
registerBeans(getWebBeanDiscovery().discoverWebBeanClasses());
getManager().fireEvent(getManager(), new InitializedLiteral());
log.info("Web Beans initialized. Validating beans.");
Expand Down
Expand Up @@ -198,10 +198,6 @@ protected void setupContexts()
public void setWebBeanDiscovery(WebBeanDiscovery webBeanDiscovery)
{
this.webBeanDiscovery = webBeanDiscovery;
if (webBeanDiscovery != null)
{
this.ejbDiscovery = new MockEjbDiscovery(webBeanDiscovery.discoverWebBeanClasses());
}
}

@Override
Expand All @@ -224,7 +220,7 @@ public MockNaming getNaming()
@Override
protected EjbDiscovery getEjbDiscovery()
{
return ejbDiscovery;
return new MockEjbDiscovery(webBeanDiscovery.discoverWebBeanClasses());
}

}
Expand Up @@ -17,7 +17,6 @@
package org.jboss.webbeans.mock;

import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;

import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
Expand All @@ -29,22 +28,6 @@ public class MockWebBeanDiscovery implements WebBeanDiscovery

private Iterable<URL> webBeansXmlFiles = new HashSet<URL>();

/**
* Simple constructor that auto discovers EJBs
* @param webBeanClasses
*/
public MockWebBeanDiscovery(Class<?>... webBeanClasses)
{
this(Arrays.asList(webBeanClasses), null);
}

public MockWebBeanDiscovery(Iterable<Class<?>> webBeanClasses, Iterable<URL> webBeansXmlFiles)
{
super();
this.webBeanClasses = webBeanClasses;
this.webBeansXmlFiles = webBeansXmlFiles;
}

public Iterable<Class<?>> discoverWebBeanClasses()
{
return webBeanClasses;
Expand All @@ -54,5 +37,15 @@ public Iterable<URL> discoverWebBeansXml()
{
return webBeansXmlFiles;
}

public void setWebBeanClasses(Iterable<Class<?>> webBeanClasses)
{
this.webBeanClasses = webBeanClasses;
}

public void setWebBeansXmlFiles(Iterable<URL> webBeansXmlFiles)
{
this.webBeansXmlFiles = webBeansXmlFiles;
}

}
Expand Up @@ -17,8 +17,11 @@

package org.jboss.webbeans.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

/**
* An Enumeration -> Iteratble adaptor
Expand All @@ -28,8 +31,8 @@
*/
public class EnumerationIterable<T> implements Iterable<T>
{
// The enumeration-iteartor
private EnumerationIterator<T> iterator;
// The enumeration as a list
private final List<T> list = new ArrayList<T>();

/**
* Constructor
Expand All @@ -38,7 +41,10 @@ public class EnumerationIterable<T> implements Iterable<T>
*/
public EnumerationIterable(Enumeration<T> enumeration)
{
this.iterator = new EnumerationIterator<T>(enumeration);
while (enumeration.hasMoreElements())
{
list.add(enumeration.nextElement());
}
}

/**
Expand All @@ -48,7 +54,7 @@ public EnumerationIterable(Enumeration<T> enumeration)
*/
public Iterator<T> iterator()
{
return iterator;
return Collections.unmodifiableList(list).iterator();
}

}
@@ -0,0 +1,3 @@
package org.jboss.webbeans.util;


@@ -0,0 +1,23 @@
package org.jboss.webbeans.util.dom;

import java.util.Iterator;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NodeListIterable implements Iterable<Node>
{

private final NodeList nodeList;

public NodeListIterable(NodeList nodeList)
{
this.nodeList = nodeList;
}

public Iterator<Node> iterator()
{
return new NodeListIterator(nodeList);
}

}

0 comments on commit 9ae7d3f

Please sign in to comment.