Author: vitold
Date: 2009-03-20 08:13:49 -0400 (Fri, 20 Mar 2009)
New Revision: 2120
Added:
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestBean.java
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestDeploymentType.java
ri/trunk/tests/src/test/resources/org/jboss/webbeans/test/unit/xml/parser/namespace
Modified:
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/XmlParser.java
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java
Log:
change enabling deployment types
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-20
00:07:16 UTC (rev 2119)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java 2009-03-20
12:13:49 UTC (rev 2120)
@@ -1,10 +1,14 @@
package org.jboss.webbeans.xml;
+import java.io.File;
+import java.io.FileNotFoundException;
import java.lang.reflect.Modifier;
+import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Scanner;
import java.util.Set;
import javax.inject.DefinitionException;
@@ -17,19 +21,14 @@
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)
@@ -198,7 +197,8 @@
private static boolean isSimpleBean(Element element)
{
- Class<?> beanClass = loadClass(element);
+ String urn = element.getNamespace().getURI();
+ Class<?> beanClass = loadClassByURN(urn, element.getName());
if (!Modifier.isAbstract(beanClass.getModifiers()) &&
beanClass.getTypeParameters().length == 0)
@@ -209,7 +209,8 @@
private static AnnotatedItem<?, ?> receiveSimpleBeanItem(Element element)
{
- Class<?> beanClass = loadClass(element);
+ String urn = element.getNamespace().getURI();
+ Class<?> beanClass = loadClassByURN(urn, element.getName());
if (!Modifier.isStatic(beanClass.getModifiers()) &&
beanClass.isMemberClass())
@@ -227,14 +228,6 @@
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)
{
@@ -265,4 +258,76 @@
{
return
element.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE);
}
+
+ public static Class<?> loadClassByURN(String urn, String className)
+ {
+ List<Class<?>> classes = new ArrayList<Class<?>>();
+ List<String> packages = new ArrayList<String>();
+ File namespaceFile = loadNamespaceFile(urn);
+
+ if(namespaceFile == null)
+ packages.add(urn.replaceFirst(XmlConstants.URN_PREFIX, ""));
+
+ else
+ packages.addAll(parseNamespaceFile(namespaceFile));
+
+ for(String possiblePackage : packages)
+ {
+ String classPath = possiblePackage + "." + className;
+ try
+ {
+ classes.add(Class.forName(classPath));
+ }
+ catch (ClassNotFoundException e)
+ {}
+ }
+
+ if(classes.size() == 0)
+ throw new DefinitionException("Could not find '" + className +
"'according to specified URN '" + urn + "'");
+
+ if(classes.size() == 1)
+ return classes.get(0);
+
+ throw new DefinitionException("There are multiple packages containing a Java
type with the same name '" + className + "'");
+ }
+
+ public static File loadNamespaceFile(String urn)
+ {
+ char separator = '/';
+ String packageName = urn.replaceFirst(XmlConstants.URN_PREFIX, "");
+ String path = packageName.replace('.', separator);
+ String filePath = separator + path + separator + XmlConstants.NAMESPACE_FILE_NAME;
+ URL fileUrl = ParseXmlHelper.class.getResource(filePath);
+
+ if(fileUrl == null)
+ return null;
+
+ File f = new File(fileUrl.getPath());
+
+ return f;
+ }
+
+ public static List<String> parseNamespaceFile(File namespaceFile)
+ {
+ try
+ {
+ List<String> packages = new ArrayList<String>();
+ Scanner fileScanner = new Scanner(namespaceFile);
+ while (fileScanner.hasNextLine() )
+ {
+ String line = fileScanner.nextLine();
+ Scanner lineScanner = new Scanner(line);
+ lineScanner.useDelimiter(XmlConstants.NAMESPACE_FILE_DELIMETER);
+ while(lineScanner.hasNext())
+ {
+ packages.add(lineScanner.next());
+ }
+ }
+ return packages;
+ }
+ catch (FileNotFoundException e)
+ {
+ throw new DefinitionException("Could not find " +
namespaceFile.getAbsolutePath());
+ }
+ }
}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java 2009-03-20
00:07:16 UTC (rev 2119)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java 2009-03-20
12:13:49 UTC (rev 2120)
@@ -1,70 +1,46 @@
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";
+
+ public static final String NAMESPACE_FILE_NAME = "namespace";
+
+ public static final String NAMESPACE_FILE_DELIMETER = " ";
}
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-20 00:07:16
UTC (rev 2119)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java 2009-03-20 12:13:49
UTC (rev 2120)
@@ -13,11 +13,14 @@
import javax.inject.DefinitionException;
import javax.inject.DeploymentException;
import javax.inject.DeploymentType;
+import javax.inject.Production;
+import javax.inject.Standard;
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.log.Log;
import org.jboss.webbeans.log.Logging;
@@ -58,6 +61,9 @@
private void parseForDeploy()
{
+
+ List<Class<? extends Annotation>> deploymentClasses = new
ArrayList<Class<? extends Annotation>>();
+
int counter = 0;
for (URL url : environment.getBeansXmlUrls())
@@ -73,11 +79,17 @@
Element element = (Element) elIterator.next();
if (ParseXmlHelper.isJavaEeNamespace(element) &&
element.getName().equalsIgnoreCase(XmlConstants.DEPLOY))
-
environment.getEnabledDeploymentTypes().addAll(obtainDeploymentTypes(element,
counter++));
+ deploymentClasses.addAll(obtainDeploymentTypes(element, counter++));
}
}
}
+ if(deploymentClasses.size() == 0)
+ {
+ deploymentClasses.add(Standard.class);
+ deploymentClasses.add(Production.class);
+ }
+ environment.getEnabledDeploymentTypes().addAll(deploymentClasses);
}
@SuppressWarnings("unchecked")
@@ -180,18 +192,18 @@
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;
+ String urn = child.getNamespace().getURI();
+ Class<?> deploymentClass = ParseXmlHelper.loadClassByURN(urn,
child.getName());
- 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");
@@ -202,19 +214,4 @@
}
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");
- }
}
Added: ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestBean.java
===================================================================
--- ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestBean.java
(rev 0)
+++
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestBean.java 2009-03-20
12:13:49 UTC (rev 2120)
@@ -0,0 +1,6 @@
+package org.jboss.webbeans.test.unit.xml.beans;
+
+public class TestBean
+{
+
+}
Added:
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestDeploymentType.java
===================================================================
---
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestDeploymentType.java
(rev 0)
+++
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/beans/TestDeploymentType.java 2009-03-20
12:13:49 UTC (rev 2120)
@@ -0,0 +1,9 @@
+package org.jboss.webbeans.test.unit.xml.beans;
+
+import javax.inject.DeploymentType;
+
+@DeploymentType
+public @interface TestDeploymentType
+{
+
+}
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-20
00:07:16 UTC (rev 2119)
+++
ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/xml/parser/XmlParserImplTest.java 2009-03-20
12:13:49 UTC (rev 2120)
@@ -1,7 +1,9 @@
package org.jboss.webbeans.test.unit.xml.parser;
+import java.io.File;
import java.net.URL;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import org.jboss.testharness.impl.packaging.Artifact;
@@ -13,8 +15,12 @@
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.test.unit.xml.beans.TestBean;
+import org.jboss.webbeans.test.unit.xml.beans.TestDeploymentType;
+import org.jboss.webbeans.xml.ParseXmlHelper;
import org.jboss.webbeans.xml.XmlEnvironment;
import org.jboss.webbeans.xml.XmlParser;
+import org.testng.annotations.Test;
@Artifact
@Resources({
@@ -45,10 +51,34 @@
assert parserEnv.getClasses().size() == 1;
}
+
+ //@Test
+ public void testParceNamespaceFile()
+ {
+ String urn = "urn:java:org.jboss.webbeans.test.unit.xml.parser";
+
+ File f = ParseXmlHelper.loadNamespaceFile(urn);
+ List<String> packages = ParseXmlHelper.parseNamespaceFile(f);
+ assert packages.size() == 2;
+ }
+
+ //@Test
+ public void testLoadClassByURN()
+ {
+ String urn = "urn:java:org.jboss.webbeans.test.unit.xml.parser";
+ String beanName = "TestBean";
+ String deploymentTypeName = "TestDeploymentType";
+
+ Class<?> beanClass = ParseXmlHelper.loadClassByURN(urn, beanName);
+ Class<?> deploymentTypeClass = ParseXmlHelper.loadClassByURN(urn,
deploymentTypeName);
+
+ assert beanClass.equals(TestBean.class);
+ assert deploymentTypeClass.equals(TestDeploymentType.class);
+ }
}
/*
- <Beans xmlns="urn:java:ee"
xmlns:myapp="urn:java:org.jboss.webbeans.test.unit.xml.beans"
+ <Beans xmlns="urn:java:ee"
xmlns:myapp="urn:java:org.jboss.webbeans.test.unit.xml.parser"
xmlns:test="urn:java:org.jboss.webbeans.test.unit.xml">
<Deploy>
<Standard />
Added:
ri/trunk/tests/src/test/resources/org/jboss/webbeans/test/unit/xml/parser/namespace
===================================================================
--- ri/trunk/tests/src/test/resources/org/jboss/webbeans/test/unit/xml/parser/namespace
(rev 0)
+++
ri/trunk/tests/src/test/resources/org/jboss/webbeans/test/unit/xml/parser/namespace 2009-03-20
12:13:49 UTC (rev 2120)
@@ -0,0 +1 @@
+org.jboss.webbeans.test.unit.xml.parser.bean
org.jboss.webbeans.test.unit.xml.parser.deploymenttype
\ No newline at end of file