[jboss-svn-commits] JBL Code SVN: r30396 - in labs/jbossbuild/maven-plugins/tags: maven-injection-plugin-1.0.1 and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Nov 30 13:08:45 EST 2009
Author: steve.ebersole at jboss.com
Date: 2009-11-30 13:08:44 -0500 (Mon, 30 Nov 2009)
New Revision: 30396
Added:
labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/
labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/pom.xml
labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java
labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java
Removed:
labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/pom.xml
labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java
labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java
Log:
[maven-release-plugin] copy for tag maven-injection-plugin-1.0.1
Copied: labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1 (from rev 27090, labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin)
Deleted: labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/pom.xml
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/pom.xml 2009-06-23 08:14:50 UTC (rev 27090)
+++ labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/pom.xml 2009-11-30 18:08:44 UTC (rev 30396)
@@ -1,91 +0,0 @@
-<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/maven-v4_0_0.xsd">
-
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.jboss</groupId>
- <artifactId>jboss-parent</artifactId>
- <version>5-beta-1</version>
- </parent>
-
- <groupId>org.jboss.maven.plugins</groupId>
- <artifactId>maven-injection-plugin</artifactId>
- <packaging>maven-plugin</packaging>
- <version>1.0.1-SNAPSHOT</version>
-
- <name>Injection Maven Mojo</name>
- <description>Provides capability to perform bytecode injection as part of build</description>
-
- <organization>
- <name>JBoss, a division of Red Hat, Inc</name>
- <url>http://jboss.org/</url>
- </organization>
-
- <developers>
- <developer>
- <name>Steve Ebersole</name>
- <email>steve at hibernate.org</email>
- <organization>JBoss, a division of Red Hat, Inc</organization>
- <organizationUrl>http://jboss.org</organizationUrl>
- <timezone>-5</timezone>
- </developer>
- </developers>
-
- <licenses>
- <license>
- <name>GNU Lesser General Public License</name>
- <url>http://www.gnu.org/copyleft/lesser.html</url>
- <distribution>repo</distribution>
- </license>
- </licenses>
-
- <scm>
- <connection>scm:svn:http://anonsvn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin</connection>
- <developerConnection>scm:svn:https://svn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin</developerConnection>
- <url>http://anonsvn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin</url>
- </scm>
-
- <dependencies>
- <dependency>
- <groupId>org.apache.maven</groupId>
- <artifactId>maven-plugin-api</artifactId>
- <version>2.0</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.maven</groupId>
- <artifactId>maven-core</artifactId>
- <version>2.0.9</version>
- </dependency>
- <dependency>
- <groupId>javassist</groupId>
- <artifactId>javassist</artifactId>
- <version>3.9.0.GA</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.0.2</version>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-release-plugin</artifactId>
- <configuration>
- <tagBase>https://svn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/tags</tagBase>
- </configuration>
- </plugin>
- </plugins>
- </build>
-</project>
Copied: labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/pom.xml (from rev 30395, labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/pom.xml)
===================================================================
--- labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/pom.xml (rev 0)
+++ labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/pom.xml 2009-11-30 18:08:44 UTC (rev 30396)
@@ -0,0 +1,91 @@
+<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/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.jboss</groupId>
+ <artifactId>jboss-parent</artifactId>
+ <version>5-beta-1</version>
+ </parent>
+
+ <groupId>org.jboss.maven.plugins</groupId>
+ <artifactId>maven-injection-plugin</artifactId>
+ <packaging>maven-plugin</packaging>
+ <version>1.0.1</version>
+
+ <name>Injection Maven Mojo</name>
+ <description>Provides capability to perform bytecode injection as part of build</description>
+
+ <organization>
+ <name>JBoss, a division of Red Hat, Inc</name>
+ <url>http://jboss.org/</url>
+ </organization>
+
+ <developers>
+ <developer>
+ <name>Steve Ebersole</name>
+ <email>steve at hibernate.org</email>
+ <organization>JBoss, a division of Red Hat, Inc</organization>
+ <organizationUrl>http://jboss.org</organizationUrl>
+ <timezone>-5</timezone>
+ </developer>
+ </developers>
+
+ <licenses>
+ <license>
+ <name>GNU Lesser General Public License</name>
+ <url>http://www.gnu.org/copyleft/lesser.html</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+
+ <scm>
+ <connection>scm:svn:http://anonsvn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1</connection>
+ <developerConnection>scm:svn:https://svn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1</developerConnection>
+ <url>http://anonsvn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1</url>
+ </scm>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>2.0.9</version>
+ </dependency>
+ <dependency>
+ <groupId>javassist</groupId>
+ <artifactId>javassist</artifactId>
+ <version>3.9.0.GA</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-release-plugin</artifactId>
+ <configuration>
+ <tagBase>https://svn.jboss.org/repos/labs/labs/jbossbuild/maven-plugins/tags</tagBase>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
Deleted: labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java 2009-06-23 08:14:50 UTC (rev 27090)
+++ labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java 2009-11-30 18:08:44 UTC (rev 30396)
@@ -1,83 +0,0 @@
-package org.jboss.maven.plugins.injection;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.net.URL;
-import java.net.MalformedURLException;
-import java.net.URLClassLoader;
-import java.io.File;
-
-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.maven.artifact.DependencyResolutionRequiredException;
-import org.jboss.maven.plugins.injection.process.InjectionDescriptor;
-import org.jboss.maven.plugins.injection.process.InjectionTarget;
-
-/**
- * Mojo responsible for injecting values into various targets as part of a build process. One example of this
- * is injecting the project version into a class file (bytcode manip) or a text file or etc so that these various
- * targets do not need to be synch'ed manually.
- *
- * @author Steve Ebersole
- */
-public abstract class AbstractInjectionMojo extends AbstractMojo {
- /**
- * The Maven Project Object
- *
- * @parameter expression="${project}"
- * @required
- */
- protected MavenProject project;
-
- protected void prepare() throws MojoExecutionException {
- }
-
- protected abstract List<InjectionDescriptor> getInjectionDescriptors() throws MojoExecutionException;
-
- protected void finishUp() {
- }
-
- public void execute() throws MojoExecutionException, MojoFailureException {
- prepare();
-
- for ( InjectionDescriptor descriptor : getInjectionDescriptors() ) {
- final String injectionValue = descriptor.getValue();
- for ( InjectionTarget target : descriptor.getTargets() ) {
- target.inject( injectionValue );
- }
- }
-
- finishUp();
- }
-
- protected final String resolveExpression(String expression) {
- return expression;
- }
-
- protected final ClassLoader buildProjectCompileClassLoader() throws MojoExecutionException {
- ArrayList<URL> classPathUrls = new ArrayList<URL>();
-
- // we should also add the compile classpath elements...
- for ( String path : projectCompileClasspathElements() ) {
- try {
- getLog().info( "Adding project compile classpath element : " + path );
- classPathUrls.add( new File( path ).toURI().toURL() );
- }
- catch ( MalformedURLException e ) {
- throw new MojoExecutionException( "Unable to build path URL [" + path + "]" );
- }
- }
- return new URLClassLoader( classPathUrls.toArray( new URL[classPathUrls.size()] ), getClass().getClassLoader() );
- }
-
- protected final List<String> projectCompileClasspathElements() throws MojoExecutionException {
- try {
- return ( List<String> ) project.getCompileClasspathElements();
- }
- catch ( DependencyResolutionRequiredException e ) {
- throw new MojoExecutionException( "Call to Project#getCompileClasspathElements required dependency resolution" );
- }
- }
-}
Copied: labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java (from rev 30394, labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java)
===================================================================
--- labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java (rev 0)
+++ labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java 2009-11-30 18:08:44 UTC (rev 30396)
@@ -0,0 +1,84 @@
+package org.jboss.maven.plugins.injection;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.net.URLClassLoader;
+import java.io.File;
+
+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.maven.artifact.DependencyResolutionRequiredException;
+import org.jboss.maven.plugins.injection.process.InjectionDescriptor;
+import org.jboss.maven.plugins.injection.process.InjectionTarget;
+
+/**
+ * Mojo responsible for injecting values into various targets as part of a build process. One example of this
+ * is injecting the project version into a class file (bytcode manip) or a text file or etc so that these various
+ * targets do not need to be synch'ed manually.
+ *
+ * @author Steve Ebersole
+ */
+public abstract class AbstractInjectionMojo extends AbstractMojo {
+ /**
+ * The Maven Project Object
+ *
+ * @parameter expression="${project}"
+ * @required
+ */
+ protected MavenProject project;
+
+ protected void prepare() throws MojoExecutionException {
+ }
+
+ protected abstract List<InjectionDescriptor> getInjectionDescriptors() throws MojoExecutionException;
+
+ protected void finishUp() {
+ }
+
+ public void execute() throws MojoExecutionException, MojoFailureException {
+ prepare();
+
+ for ( InjectionDescriptor descriptor : getInjectionDescriptors() ) {
+ final String injectionValue = descriptor.getValue();
+ for ( InjectionTarget target : descriptor.getTargets() ) {
+ target.inject( injectionValue );
+ }
+ }
+
+ finishUp();
+ }
+
+ protected final String resolveExpression(String expression) {
+ return expression;
+ }
+
+ protected final ClassLoader buildProjectCompileClassLoader() throws MojoExecutionException {
+ ArrayList<URL> classPathUrls = new ArrayList<URL>();
+
+ // we should also add the compile classpath elements...
+ for ( String path : projectCompileClasspathElements() ) {
+ try {
+ getLog().info( "Adding project compile classpath element : " + path );
+ classPathUrls.add( new File( path ).toURI().toURL() );
+ }
+ catch ( MalformedURLException e ) {
+ throw new MojoExecutionException( "Unable to build path URL [" + path + "]" );
+ }
+ }
+ return new URLClassLoader( classPathUrls.toArray( new URL[classPathUrls.size()] ), getClass().getClassLoader() );
+ }
+
+ @SuppressWarnings({ "unchecked" })
+ protected final List<String> projectCompileClasspathElements() throws MojoExecutionException {
+ try {
+ return ( List<String> ) project.getCompileClasspathElements();
+ }
+ catch ( DependencyResolutionRequiredException e ) {
+ throw new MojoExecutionException( "Call to Project#getCompileClasspathElements required dependency resolution" );
+ }
+ }
+}
Deleted: labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java 2009-06-23 08:14:50 UTC (rev 27090)
+++ labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java 2009-11-30 18:08:44 UTC (rev 30396)
@@ -1,211 +0,0 @@
-/*
- * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors. All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this distribution; if not, write to:
- * Free Software Foundation, Inc.
- * 51 Franklin Street, Fifth Floor
- * Boston, MA 02110-1301 USA
- */
-package org.jboss.maven.plugins.injection;
-
-import java.io.BufferedOutputStream;
-import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import javassist.ClassPool;
-import javassist.CtClass;
-import javassist.LoaderClassPath;
-import javassist.CtMethod;
-import javassist.bytecode.ClassFile;
-import javassist.bytecode.ConstantAttribute;
-import javassist.bytecode.FieldInfo;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.jboss.maven.plugins.injection.process.InjectionDescriptor;
-import org.jboss.maven.plugins.injection.process.InjectionTarget;
-
-/**
- * Used to inject resolved expression values into compiled bytecode.
- * <p/>
- * TODO : add checks as to whether the injection is needed to avoid file timestamp changes.
- * Basically we should skip the injection if the class file field is already the injection value...
- *
- * @goal bytecode
- * @phase compile
- * @
- *
- * @author Steve Ebersole
- */
-public class BytecodeInjectionMojo extends AbstractInjectionMojo {
- /**
- * The injections to be performed.
- *
- * @parameter
- * @required
- */
- protected BytecodeInjection[] bytecodeInjections;
-
- private LoaderClassPath loaderClassPath;
- private ClassPool classPool;
-
- @Override
- protected void prepare() throws MojoExecutionException {
- super.prepare();
- loaderClassPath = new LoaderClassPath( buildProjectCompileClassLoader() );
- classPool = new ClassPool();
- classPool.appendClassPath( loaderClassPath );
- }
-
- protected List<InjectionDescriptor> getInjectionDescriptors() throws MojoExecutionException {
- ArrayList<InjectionDescriptor> descriptors = new ArrayList<InjectionDescriptor>();
- for ( BytecodeInjection injection : bytecodeInjections ) {
- descriptors.add( generateDescriptor( injection ) );
- }
- return descriptors;
- }
-
- private InjectionDescriptor generateDescriptor(BytecodeInjection injection) throws MojoExecutionException {
- InjectionDescriptor descriptor = new InjectionDescriptor( resolveExpression( injection.getExpression() ) );
- for ( TargetMember member : injection.getTargetMembers() ) {
- descriptor.getTargets().add( generateBytecodeInjectionTarget( member ) );
- }
- return descriptor;
- }
-
- private InjectionTarget generateBytecodeInjectionTarget(TargetMember member) throws MojoExecutionException {
- if ( member instanceof Constant ) {
- return new ConstantInjectionTarget( ( Constant ) member );
- }
- else if ( member instanceof MethodBodyReturn ) {
- return new MethodBodyReturnReplacementTarget( ( MethodBodyReturn ) member );
- }
- throw new MojoExecutionException( "Unexpected injection member type : " + member );
- }
-
- private abstract class BaseInjectionTarget implements InjectionTarget {
- private final TargetMember targetMember;
-
- public TargetMember getTargetMember() {
- return targetMember;
- }
-
- private final File classFileLocation;
-
- public File getClassFileLocation() {
- return classFileLocation;
- }
-
- private final CtClass ctClass;
-
- public CtClass getCtClass() {
- return ctClass;
- }
-
- protected BaseInjectionTarget(TargetMember targetMember) throws MojoExecutionException {
- this.targetMember = targetMember;
- try {
- classFileLocation = new File( loaderClassPath.find( targetMember.getClassName() ).toURI() );
- ctClass = classPool.get( targetMember.getClassName() );
- }
- catch ( Throwable e ) {
- throw new MojoExecutionException( "Unable to resolve class file path", e );
- }
- }
-
- protected void writeOutChanges() throws MojoExecutionException {
- getLog().info( "writing injection changes back [" + classFileLocation.getAbsolutePath() + "]" );
- ClassFile classFile = ctClass.getClassFile();
- classFile.compact();
- try {
- DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( classFileLocation ) ) );
- try {
-
- classFile.write( out );
- out.flush();
- if ( ! classFileLocation.setLastModified( System.currentTimeMillis() ) ) {
- getLog().info( "Unable to manually update class file timestamp" );
- }
- }
- finally {
- out.close();
- }
- }
- catch ( IOException e ) {
- throw new MojoExecutionException( "Unable to write out modified class file", e );
- }
- }
- }
-
- private class ConstantInjectionTarget extends BaseInjectionTarget {
- private final FieldInfo ctFieldInfo;
-
- private ConstantInjectionTarget(Constant constant) throws MojoExecutionException {
- super( constant );
-
- try {
- ctFieldInfo = getCtClass().getField( constant.getFieldName() ).getFieldInfo();
- }
- catch ( Throwable e ) {
- throw new MojoExecutionException( "Unable to resolve class field [" + constant.getQualifiedName() + "]", e );
- }
- }
-
- public void inject(String value) throws MojoExecutionException {
- ctFieldInfo.addAttribute(
- new ConstantAttribute(
- ctFieldInfo.getConstPool(),
- ctFieldInfo.getConstPool().addStringInfo( value )
- )
- );
-
- writeOutChanges();
- }
- }
-
- private class MethodBodyReturnReplacementTarget extends BaseInjectionTarget {
- private final CtMethod ctMethod;
-
- private MethodBodyReturnReplacementTarget(MethodBodyReturn method) throws MojoExecutionException {
- super( method );
-
- try {
- for ( CtMethod ctMethod : getCtClass().getMethods() ) {
- if ( method.getMethodName().equals( ctMethod.getName() ) ) {
- this.ctMethod = ctMethod;
- return;
- }
- }
- throw new MojoExecutionException( "Could not locate method [" + method.getQualifiedName() + "]" );
- }
- catch ( Throwable e ) {
- throw new MojoExecutionException( "Unable to resolve method [" + method.getQualifiedName() + "]", e );
- }
- }
-
- public void inject(String value) throws MojoExecutionException {
- try {
- ctMethod.setBody( "{return \"" + value + "\";}" );
- }
- catch ( Throwable t ) {
- throw new MojoExecutionException( "Unable to replace method body", t );
- }
- writeOutChanges();
- }
- }
-}
Copied: labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java (from rev 30394, labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java)
===================================================================
--- labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java (rev 0)
+++ labs/jbossbuild/maven-plugins/tags/maven-injection-plugin-1.0.1/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java 2009-11-30 18:08:44 UTC (rev 30396)
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.jboss.maven.plugins.injection;
+
+import java.io.BufferedOutputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.LoaderClassPath;
+import javassist.CtMethod;
+import javassist.bytecode.ClassFile;
+import javassist.bytecode.ConstantAttribute;
+import javassist.bytecode.FieldInfo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.jboss.maven.plugins.injection.process.InjectionDescriptor;
+import org.jboss.maven.plugins.injection.process.InjectionTarget;
+
+/**
+ * Used to inject resolved expression values into compiled bytecode.
+ * <p/>
+ * TODO : add checks as to whether the injection is needed to avoid file timestamp changes.
+ * Basically we should skip the injection if the class file field is already the injection value...
+ *
+ * @goal bytecode
+ * @phase compile
+ *
+ * @author Steve Ebersole
+ */
+public class BytecodeInjectionMojo extends AbstractInjectionMojo {
+ /**
+ * The injections to be performed.
+ *
+ * @parameter
+ * @required
+ */
+ protected BytecodeInjection[] bytecodeInjections;
+
+ private LoaderClassPath loaderClassPath;
+ private ClassPool classPool;
+
+ @Override
+ protected void prepare() throws MojoExecutionException {
+ super.prepare();
+ loaderClassPath = new LoaderClassPath( buildProjectCompileClassLoader() );
+ classPool = new ClassPool();
+ classPool.appendClassPath( loaderClassPath );
+ classPool.appendSystemPath();
+ }
+
+ protected List<InjectionDescriptor> getInjectionDescriptors() throws MojoExecutionException {
+ ArrayList<InjectionDescriptor> descriptors = new ArrayList<InjectionDescriptor>();
+ for ( BytecodeInjection injection : bytecodeInjections ) {
+ descriptors.add( generateDescriptor( injection ) );
+ }
+ return descriptors;
+ }
+
+ private InjectionDescriptor generateDescriptor(BytecodeInjection injection) throws MojoExecutionException {
+ InjectionDescriptor descriptor = new InjectionDescriptor( resolveExpression( injection.getExpression() ) );
+ for ( TargetMember member : injection.getTargetMembers() ) {
+ descriptor.getTargets().add( generateBytecodeInjectionTarget( member ) );
+ }
+ return descriptor;
+ }
+
+ private InjectionTarget generateBytecodeInjectionTarget(TargetMember member) throws MojoExecutionException {
+ if ( member instanceof Constant ) {
+ return new ConstantInjectionTarget( ( Constant ) member );
+ }
+ else if ( member instanceof MethodBodyReturn ) {
+ return new MethodBodyReturnReplacementTarget( ( MethodBodyReturn ) member );
+ }
+ throw new MojoExecutionException( "Unexpected injection member type : " + member );
+ }
+
+ private abstract class BaseInjectionTarget implements InjectionTarget {
+ private final TargetMember targetMember;
+
+ public TargetMember getTargetMember() {
+ return targetMember;
+ }
+
+ private final File classFileLocation;
+
+ public File getClassFileLocation() {
+ return classFileLocation;
+ }
+
+ private final CtClass ctClass;
+
+ public CtClass getCtClass() {
+ return ctClass;
+ }
+
+ protected BaseInjectionTarget(TargetMember targetMember) throws MojoExecutionException {
+ this.targetMember = targetMember;
+ try {
+ classFileLocation = new File( loaderClassPath.find( targetMember.getClassName() ).toURI() );
+ ctClass = classPool.get( targetMember.getClassName() );
+ }
+ catch ( Throwable e ) {
+ throw new MojoExecutionException( "Unable to resolve class file path", e );
+ }
+ }
+
+ protected void writeOutChanges() throws MojoExecutionException {
+ getLog().info( "writing injection changes back [" + classFileLocation.getAbsolutePath() + "]" );
+ ClassFile classFile = ctClass.getClassFile();
+ classFile.compact();
+ try {
+ DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( classFileLocation ) ) );
+ try {
+
+ classFile.write( out );
+ out.flush();
+ if ( ! classFileLocation.setLastModified( System.currentTimeMillis() ) ) {
+ getLog().info( "Unable to manually update class file timestamp" );
+ }
+ }
+ finally {
+ out.close();
+ }
+ }
+ catch ( IOException e ) {
+ throw new MojoExecutionException( "Unable to write out modified class file", e );
+ }
+ }
+ }
+
+ private class ConstantInjectionTarget extends BaseInjectionTarget {
+ private final FieldInfo ctFieldInfo;
+
+ private ConstantInjectionTarget(Constant constant) throws MojoExecutionException {
+ super( constant );
+
+ try {
+ ctFieldInfo = getCtClass().getField( constant.getFieldName() ).getFieldInfo();
+ }
+ catch ( Throwable e ) {
+ throw new MojoExecutionException( "Unable to resolve class field [" + constant.getQualifiedName() + "]", e );
+ }
+ }
+
+ public void inject(String value) throws MojoExecutionException {
+ ctFieldInfo.addAttribute(
+ new ConstantAttribute(
+ ctFieldInfo.getConstPool(),
+ ctFieldInfo.getConstPool().addStringInfo( value )
+ )
+ );
+
+ writeOutChanges();
+ }
+ }
+
+ private class MethodBodyReturnReplacementTarget extends BaseInjectionTarget {
+ private final CtMethod ctMethod;
+
+ private MethodBodyReturnReplacementTarget(MethodBodyReturn method) throws MojoExecutionException {
+ super( method );
+
+ try {
+ for ( CtMethod ctMethod : getCtClass().getMethods() ) {
+ if ( method.getMethodName().equals( ctMethod.getName() ) ) {
+ this.ctMethod = ctMethod;
+ return;
+ }
+ }
+ throw new MojoExecutionException( "Could not locate method [" + method.getQualifiedName() + "]" );
+ }
+ catch ( Throwable e ) {
+ throw new MojoExecutionException( "Unable to resolve method [" + method.getQualifiedName() + "]", e );
+ }
+ }
+
+ public void inject(String value) throws MojoExecutionException {
+ try {
+ ctMethod.setBody( "{return \"" + value + "\";}" );
+ }
+ catch ( Throwable t ) {
+ throw new MojoExecutionException( "Unable to replace method body", t );
+ }
+ writeOutChanges();
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list