tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1291819 - in /openejb/trunk/maven-plugins/xbean-xml/src: main/java/org/apache/openejb/xbean/xml/ test/java/org/apache/openejb/xbean/xml/ test/resources/
Date Tue, 21 Feb 2012 14:45:43 GMT
Author: rmannibucau
Date: Tue Feb 21 14:45:42 2012
New Revision: 1291819

URL: http://svn.apache.org/viewvc?rev=1291819&view=rev
Log:
managing packages in xbean-xml

Added:
    openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/AbstractXmlAnnotationFinderTest.java
    openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlClassesAnnotationFinderHelperTest.java
      - copied, changed from r1291681, openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlAnnotationFinderHelperTest.java
    openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlPackageAnnotationFinderTest.java
    openejb/trunk/maven-plugins/xbean-xml/src/test/resources/test-package-scan.xml
Removed:
    openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlAnnotationFinderHelperTest.java
Modified:
    openejb/trunk/maven-plugins/xbean-xml/src/main/java/org/apache/openejb/xbean/xml/XMLAnnotationFinderHelper.java

Modified: openejb/trunk/maven-plugins/xbean-xml/src/main/java/org/apache/openejb/xbean/xml/XMLAnnotationFinderHelper.java
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/xbean-xml/src/main/java/org/apache/openejb/xbean/xml/XMLAnnotationFinderHelper.java?rev=1291819&r1=1291818&r2=1291819&view=diff
==============================================================================
--- openejb/trunk/maven-plugins/xbean-xml/src/main/java/org/apache/openejb/xbean/xml/XMLAnnotationFinderHelper.java
(original)
+++ openejb/trunk/maven-plugins/xbean-xml/src/main/java/org/apache/openejb/xbean/xml/XMLAnnotationFinderHelper.java
Tue Feb 21 14:45:42 2012
@@ -4,11 +4,22 @@ import org.apache.xbean.finder.Annotatio
 import org.apache.xbean.finder.IAnnotationFinder;
 import org.apache.xbean.finder.archive.Archive;
 import org.apache.xbean.finder.archive.ClassesArchive;
+import org.apache.xbean.finder.archive.ClasspathArchive;
+import org.apache.xbean.finder.archive.CompositeArchive;
+import org.apache.xbean.finder.archive.FilteredArchive;
+import org.apache.xbean.finder.filter.Filter;
+import org.apache.xbean.finder.filter.FilterList;
+import org.apache.xbean.finder.filter.PackageFilter;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import java.io.BufferedInputStream;
 import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
 
 public final class XMLAnnotationFinderHelper {
     private static final JAXBContext JAXB_CONTEXT;
@@ -16,7 +27,7 @@ public final class XMLAnnotationFinderHe
         try {
             JAXB_CONTEXT = JAXBContext.newInstance(Scan.class);
         } catch (JAXBException e) {
-            throw new RuntimeException(e); // TODO: handle it with a custom exception
+            throw new RuntimeException(e); // shouldn't occur
         }
     }
 
@@ -24,14 +35,33 @@ public final class XMLAnnotationFinderHe
         // no-op
     }
 
-    public static IAnnotationFinder finderFromXml(final InputStream is, final ClassLoader
loader) throws JAXBException {
+    public static IAnnotationFinder finderFromXml(final InputStream is, final ClassLoader
loader, final Iterable<URL> urls, final Class<?> archiveClazz) throws JAXBException
{
         final Scan scan = (Scan) JAXB_CONTEXT.createUnmarshaller().unmarshal(new BufferedInputStream(is));
 
-        // TODO: manage packages
+        final Archive packageArchive = packageArchive(scan.getPackagename(), loader, urls,
archiveClazz);
+        final Archive classesArchive = classesArchive(scan, loader);
 
+        final Archive archive;
+        if (packageArchive != null && classesArchive != null) {
+            archive = new CompositeArchive(classesArchive, packageArchive);
+        } else if (packageArchive != null) {
+            archive = packageArchive;
+        } else {
+            archive = classesArchive;
+        }
+        return new AnnotationFinder(archive); // don't link here
+    }
+
+    public static Archive classesArchive(final Scan scan, final ClassLoader loader) {
         Class<?>[] classes = new Class<?>[scan.getClassname().size()];
         int i = 0;
+        final Set<String> packages = scan.getPackagename();
         for (String clazz : scan.getClassname()) {
+            // skip classes managed by package filtering
+            if (packages != null && clazzInPackage(packages, clazz)) {
+                continue;
+            }
+
             try {
                 classes[i++] = loader.loadClass(clazz);
             } catch (ClassNotFoundException e) {
@@ -45,7 +75,41 @@ public final class XMLAnnotationFinderHe
             classes = updatedClasses;
         }
 
-        final Archive archive = new ClassesArchive(classes);
-        return new AnnotationFinder(archive); // don't link here
+        return new ClassesArchive(classes);
+    }
+
+    public static Archive packageArchive(final Set<String> packagenames, final ClassLoader
loader, final Iterable<URL> urls, final Class<?> archiveClazz) {
+        if (!packagenames.isEmpty()) {
+            Archive rawArchive = null;
+            if (archiveClazz != null) {
+                try {
+                    rawArchive = (Archive) archiveClazz.getConstructor(ClassLoader.class,
Iterable.class).newInstance(loader, urls);
+                } catch (Exception e) {
+                    // ignored
+                }
+            }
+            if (rawArchive == null) {
+                rawArchive = new ClasspathArchive(loader, urls);
+            }
+            return new FilteredArchive(rawArchive, filters(packagenames));
+        }
+        return null;
+    }
+
+    private static boolean clazzInPackage(final Collection<String> packagename, final
String clazz) {
+        for (String str : packagename) {
+            if (clazz.startsWith(str)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static Filter filters(final Set<String> packageNames) {
+        final List<Filter> filters = new ArrayList<Filter>();
+        for (String packageName : packageNames) {
+            filters.add(new PackageFilter(packageName));
+        }
+        return new FilterList(filters);
     }
 }

Added: openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/AbstractXmlAnnotationFinderTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/AbstractXmlAnnotationFinderTest.java?rev=1291819&view=auto
==============================================================================
--- openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/AbstractXmlAnnotationFinderTest.java
(added)
+++ openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/AbstractXmlAnnotationFinderTest.java
Tue Feb 21 14:45:42 2012
@@ -0,0 +1,43 @@
+package org.apache.openejb.xbean.xml;
+
+import org.apache.openejb.xbean.xml.bean.MyAnnotation;
+import org.apache.openejb.xbean.xml.bean.MyBean1;
+import org.apache.openejb.xbean.xml.bean.MyBean2;
+import org.apache.openejb.xbean.xml.bean.MyBean3;
+import org.apache.xbean.finder.IAnnotationFinder;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public abstract class AbstractXmlAnnotationFinderTest {
+    protected abstract IAnnotationFinder finder();
+
+    @Test
+    public void findClass() {
+        final List<Class<?>> myClassAnnotated = finder().findAnnotatedClasses(MyAnnotation.class);
+        assertEquals(1, myClassAnnotated.size());
+        assertEquals(MyBean1.class, myClassAnnotated.iterator().next());
+    }
+
+    @Test
+    public void findMethod() {
+        final List<Method> myMethodAnnotated = finder().findAnnotatedMethods(MyAnnotation.class);
+        assertEquals(1, myMethodAnnotated.size());
+        final Method method = myMethodAnnotated.iterator().next();
+        assertEquals(MyBean2.class, method.getDeclaringClass());
+        assertEquals("aMethod", method.getName());
+    }
+
+    @Test
+    public void findField() {
+        final List<Field> myFieldAnnotated = finder().findAnnotatedFields(MyAnnotation.class);
+        assertEquals(1, myFieldAnnotated.size());
+        final Field field = myFieldAnnotated.iterator().next();
+        assertEquals(MyBean3.class, field.getDeclaringClass());
+        assertEquals("aField", field.getName());
+    }
+}

Copied: openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlClassesAnnotationFinderHelperTest.java
(from r1291681, openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlAnnotationFinderHelperTest.java)
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlClassesAnnotationFinderHelperTest.java?p2=openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlClassesAnnotationFinderHelperTest.java&p1=openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlAnnotationFinderHelperTest.java&r1=1291681&r2=1291819&rev=1291819&view=diff
==============================================================================
--- openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlAnnotationFinderHelperTest.java
(original)
+++ openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlClassesAnnotationFinderHelperTest.java
Tue Feb 21 14:45:42 2012
@@ -1,51 +1,21 @@
 package org.apache.openejb.xbean.xml;
 
-import org.apache.openejb.xbean.xml.bean.MyAnnotation;
-import org.apache.openejb.xbean.xml.bean.MyBean1;
-import org.apache.openejb.xbean.xml.bean.MyBean2;
-import org.apache.openejb.xbean.xml.bean.MyBean3;
 import org.apache.xbean.finder.IAnnotationFinder;
 import org.junit.BeforeClass;
-import org.junit.Test;
 
 import javax.xml.bind.JAXBException;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.List;
 
-import static org.junit.Assert.assertEquals;
-
-public class XmlAnnotationFinderHelperTest {
+public class XmlClassesAnnotationFinderHelperTest extends AbstractXmlAnnotationFinderTest
{
     private static IAnnotationFinder finder;
 
     @BeforeClass
     public static void initFinder() throws JAXBException {
         final ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        finder = XMLAnnotationFinderHelper.finderFromXml(loader.getResourceAsStream("test-scan.xml"),
loader);
-    }
-
-    @Test
-    public void findClass() {
-        final List<Class<?>> myClassAnnotated = finder.findAnnotatedClasses(MyAnnotation.class);
-        assertEquals(1, myClassAnnotated.size());
-        assertEquals(MyBean1.class, myClassAnnotated.iterator().next());
-    }
-
-    @Test
-    public void findMethod() {
-        final List<Method> myMethodAnnotated = finder.findAnnotatedMethods(MyAnnotation.class);
-        assertEquals(1, myMethodAnnotated.size());
-        final Method method = myMethodAnnotated.iterator().next();
-        assertEquals(MyBean2.class, method.getDeclaringClass());
-        assertEquals("aMethod", method.getName());
+        finder = XMLAnnotationFinderHelper.finderFromXml(loader.getResourceAsStream("test-scan.xml"),
loader, null, null);
     }
 
-    @Test
-    public void findField() {
-        final List<Field> myFieldAnnotated = finder.findAnnotatedFields(MyAnnotation.class);
-        assertEquals(1, myFieldAnnotated.size());
-        final Field field = myFieldAnnotated.iterator().next();
-        assertEquals(MyBean3.class, field.getDeclaringClass());
-        assertEquals("aField", field.getName());
+    @Override
+    protected IAnnotationFinder finder() {
+        return finder;
     }
 }

Added: openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlPackageAnnotationFinderTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlPackageAnnotationFinderTest.java?rev=1291819&view=auto
==============================================================================
--- openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlPackageAnnotationFinderTest.java
(added)
+++ openejb/trunk/maven-plugins/xbean-xml/src/test/java/org/apache/openejb/xbean/xml/XmlPackageAnnotationFinderTest.java
Tue Feb 21 14:45:42 2012
@@ -0,0 +1,21 @@
+package org.apache.openejb.xbean.xml;
+
+import org.apache.xbean.finder.IAnnotationFinder;
+import org.junit.BeforeClass;
+
+import javax.xml.bind.JAXBException;
+
+public class XmlPackageAnnotationFinderTest extends AbstractXmlAnnotationFinderTest {
+    private static IAnnotationFinder finder;
+
+    @BeforeClass
+    public static void initFinder() throws JAXBException {
+        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        finder = XMLAnnotationFinderHelper.finderFromXml(loader.getResourceAsStream("test-package-scan.xml"),
loader, null, null);
+    }
+
+    @Override
+    protected IAnnotationFinder finder() {
+        return finder;
+    }
+}

Added: openejb/trunk/maven-plugins/xbean-xml/src/test/resources/test-package-scan.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/xbean-xml/src/test/resources/test-package-scan.xml?rev=1291819&view=auto
==============================================================================
--- openejb/trunk/maven-plugins/xbean-xml/src/test/resources/test-package-scan.xml (added)
+++ openejb/trunk/maven-plugins/xbean-xml/src/test/resources/test-package-scan.xml Tue Feb
21 14:45:42 2012
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<scan>
+  <classes>
+    <class>org.apache.openejb.xbean.xml.bean.MyBean1</class>
+    <class>org.apache.openejb.xbean.xml.bean.MyBean2</class>
+    <class>org.apache.openejb.xbean.xml.bean.MyBean3</class>
+  </classes>
+  <packages/>
+</scan>



Mime
View raw message