You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2012/02/20 22:14:24 UTC

svn commit: r1291445 - in /openejb/trunk/maven-plugins: ./ spi-helper-maven-plugin/ spi-helper-maven-plugin/src/ spi-helper-maven-plugin/src/main/ spi-helper-maven-plugin/src/main/java/ spi-helper-maven-plugin/src/main/java/org/ spi-helper-maven-plugin...

Author: rmannibucau
Date: Mon Feb 20 21:14:23 2012
New Revision: 1291445

URL: http://svn.apache.org/viewvc?rev=1291445&view=rev
Log:
adding started work on spi-helper-maven-plugin

Added:
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/pom.xml
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/ExtendedAnnotationFinder.java
    openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/SpiMojo.java
Modified:
    openejb/trunk/maven-plugins/pom.xml

Modified: openejb/trunk/maven-plugins/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/pom.xml?rev=1291445&r1=1291444&r2=1291445&view=diff
==============================================================================
--- openejb/trunk/maven-plugins/pom.xml (original)
+++ openejb/trunk/maven-plugins/pom.xml Mon Feb 20 21:14:23 2012
@@ -39,6 +39,7 @@
     <module>dd-maven-plugin</module>
     <module>openejb-embedded-maven-plugin</module>
     <module>maven-util</module>
+    <module>spi-helper-maven-plugin</module>
   </modules>
 
   <dependencyManagement>
@@ -73,6 +74,11 @@
         <artifactId>openejb-core</artifactId>
         <version>${openejb.version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.apache.xbean</groupId>
+        <artifactId>xbean-finder-shaded</artifactId>
+        <version>3.10-SNAPSHOT</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 

Added: openejb/trunk/maven-plugins/spi-helper-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/spi-helper-maven-plugin/pom.xml?rev=1291445&view=auto
==============================================================================
--- openejb/trunk/maven-plugins/spi-helper-maven-plugin/pom.xml (added)
+++ openejb/trunk/maven-plugins/spi-helper-maven-plugin/pom.xml Mon Feb 20 21:14:23 2012
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>maven-plugins-parent</artifactId>
+    <groupId>org.apache.openejb</groupId>
+    <version>0.0.1-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>spi-helper-maven-plugin</artifactId>
+  <packaging>maven-plugin</packaging>
+  <name>OpenEJB Maven Plugins :: SPI Maven Plugin</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-project</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.xbean</groupId>
+      <artifactId>xbean-finder-shaded</artifactId>
+    </dependency>
+  </dependencies>
+
+  <properties>
+    <maven-plugin.prefix>spi-helper</maven-plugin.prefix>
+  </properties>
+</project>

Added: openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/ExtendedAnnotationFinder.java
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/ExtendedAnnotationFinder.java?rev=1291445&view=auto
==============================================================================
--- openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/ExtendedAnnotationFinder.java (added)
+++ openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/ExtendedAnnotationFinder.java Mon Feb 20 21:14:23 2012
@@ -0,0 +1,54 @@
+package org.apache.openejb.maven.plugin.spi;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import org.apache.xbean.finder.AnnotationFinder;
+import org.apache.xbean.finder.archive.Archive;
+
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class ExtendedAnnotationFinder extends org.apache.xbean.finder.AnnotationFinder {
+    private static Field annotatedField;
+
+    static { // low cost way to get info
+        try {
+            annotatedField = ExtendedAnnotationFinder.class.getSuperclass().getDeclaredField("annotated");
+            annotatedField.setAccessible(true);
+        } catch (NoSuchFieldException e) {
+            // shouldn't occur
+        }
+    }
+
+    public ExtendedAnnotationFinder(final Archive archive) {
+        super(archive);
+    }
+
+    public Map<String, List<AnnotationFinder.Info>> getAnnotated() {
+        try {
+            return (Map<String, List<AnnotationFinder.Info>>) annotatedField.get(this);
+        } catch (Exception e) {
+            return Collections.emptyMap();
+        }
+    }
+}

Added: openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/SpiMojo.java
URL: http://svn.apache.org/viewvc/openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/SpiMojo.java?rev=1291445&view=auto
==============================================================================
--- openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/SpiMojo.java (added)
+++ openejb/trunk/maven-plugins/spi-helper-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/spi/SpiMojo.java Mon Feb 20 21:14:23 2012
@@ -0,0 +1,172 @@
+package org.apache.openejb.maven.plugin.spi;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.apache.xbean.finder.AnnotationFinder;
+import org.apache.xbean.finder.archive.Archive;
+import org.apache.xbean.finder.archive.FileArchive;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElementWrapper;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.io.File;
+import java.io.FileWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @goal generate
+ * @phase compile
+ */
+public class SpiMojo extends AbstractMojo {
+    /**
+     * @parameter default-value="${project.build.outputDirectory}"
+     * @required
+     */
+    private File module;
+
+    /**
+     * @parameter default-value="${project}"
+     * @required
+     * @readonly
+     */
+    private MavenProject project;
+
+    /**
+     * @parameter
+     */
+    private List<String> annotations;
+
+    /**
+     * @parameter expression="${spi.output}" default-value="META-INF/scan.xml"
+     */
+    private String outputFilename;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        try {
+            final Archive archive = new FileArchive(createClassLoader(), module);
+            final ExtendedAnnotationFinder finder = new ExtendedAnnotationFinder(archive);
+            finder.link();
+            final Map<String, List<AnnotationFinder.Info>> info = finder.getAnnotated();
+
+            final Scan scan = new Scan();
+            for (Map.Entry<String, List<AnnotationFinder.Info>> entry : info.entrySet()) {
+                final Annotation annotation = new Annotation();
+                annotation.name = entry.getKey();
+
+                if (annotations != null && !annotations.isEmpty()) {
+                    if (!annotations.contains(annotation.name)) {
+                        continue;
+                    }
+                } else if (annotation.name.startsWith("java.lang")) {
+                    continue;
+                }
+
+                scan.annotation.add(annotation);
+
+                for (AnnotationFinder.Info value : entry.getValue()) {
+                    if (value instanceof AnnotationFinder.ClassInfo) {
+                        final Class clazz = new Class();
+                        clazz.name = value.getName();
+                        annotation.classes.add(clazz);
+                    } else if (value instanceof AnnotationFinder.FieldInfo) {
+                        final Field field = new Field();
+                        field.classname = ((AnnotationFinder.FieldInfo) value).getDeclaringClass().getName();
+                        field.name = value.getName();
+                        annotation.fields.add(field);
+                    } else if (value instanceof AnnotationFinder.MethodInfo) {
+                        final Method method = new Method();
+                        method.classname = ((AnnotationFinder.MethodInfo) value).getDeclaringClass().getName();
+                        method.name = value.getName();
+                        annotation.methods.add(method);
+                    }
+                }                
+            }
+
+            File output = new File(outputFilename);
+            if (!output.isAbsolute()) {
+                output = new File(module, outputFilename);
+            }
+            if (!output.getParentFile().mkdirs()) {
+                getLog().error("can't create " + output.getParent());
+                return;
+            }
+
+            final JAXBContext context = JAXBContext.newInstance(Scan.class);
+            final FileWriter writer = new FileWriter(output);
+            try {
+                context.createMarshaller().marshal(scan, writer);
+            } finally {
+                writer.close();
+            }
+
+            getLog().info("generated " + output.getPath());
+        } catch (Exception e) {
+            getLog().error(e);
+        }
+    }
+
+    private ClassLoader createClassLoader() {
+        final List<URL> urls = new ArrayList<URL>();
+        for (Artifact artifact : (Set<Artifact>) project.getArtifacts()) {
+            try {
+                urls.add(artifact.getFile().toURI().toURL());
+            } catch (MalformedURLException e) {
+                getLog().warn("can't use artifact " + artifact.toString());
+            }
+        }
+        if (module.exists()) {
+            try {
+                urls.add(module.toURI().toURL());
+            } catch (MalformedURLException e) {
+                getLog().warn("can't use path " + module.getPath());
+            }
+        } else {
+            getLog().warn("can't find " + module.getPath());
+        }
+        return new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread.currentThread().getContextClassLoader());
+    }
+
+    @XmlRootElement
+    public static class Scan {
+        @XmlElementWrapper(name = "annotations")
+        public List<Annotation> annotation = new ArrayList<Annotation>();
+    }
+
+    public static class Annotation {
+        @XmlAttribute
+        public String name;
+        public List<Class> classes = new ArrayList<Class>();
+        public List<Method> methods = new ArrayList<Method>();
+        public List<Field> fields = new ArrayList<Field>();
+    }
+
+    public static class Method {
+        @XmlAttribute
+        public String classname;
+        @XmlAttribute
+        public String name;
+    }
+
+    public static class Field {
+        @XmlAttribute
+        public String classname;
+        @XmlAttribute
+        public String name;
+    }
+
+    public static class Class {
+        @XmlAttribute
+        public String name;
+    }
+}