From 36093a18b0b91cd9b22211765b2805c645749ff9 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Gonzalez Date: Mon, 20 Feb 2006 22:51:50 +0000 Subject: [PATCH] Added test case it0094 for classloading issues in plugins PR: MNG-1898 git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@379268 13f79535-47bb-0310-9956-ffa450edef68 --- maven-core-it/README.txt | 5 +- maven-core-it/it0094/goals.txt | 1 + maven-core-it/it0094/mojo/pom.xml | 65 ++++++++ .../java/org/codehaus/mojo/kodo/Enhance.java | 147 ++++++++++++++++++ maven-core-it/it0094/pom.xml | 15 ++ maven-core-it/it0094/test/pom.xml | 33 ++++ 6 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 maven-core-it/it0094/goals.txt create mode 100644 maven-core-it/it0094/mojo/pom.xml create mode 100644 maven-core-it/it0094/mojo/src/main/java/org/codehaus/mojo/kodo/Enhance.java create mode 100644 maven-core-it/it0094/pom.xml create mode 100644 maven-core-it/it0094/test/pom.xml diff --git a/maven-core-it/README.txt b/maven-core-it/README.txt index c9011036f4..b61c5dbb56 100644 --- a/maven-core-it/README.txt +++ b/maven-core-it/README.txt @@ -255,7 +255,10 @@ it0091: Test that currently demonstrates that properties are not correctly it0092: Test that legacy repositories with legacy snapshots download correctly. it0093: A test that ensures that an exception is thrown when two artifacts -with the same id are present in the reactor. + with the same id are present in the reactor. + +it0094: Test classloading issues with mojos after 2.0 (MNG-1898). + ------------------------------------------------------------------------------- - generated sources diff --git a/maven-core-it/it0094/goals.txt b/maven-core-it/it0094/goals.txt new file mode 100644 index 0000000000..7c32f55981 --- /dev/null +++ b/maven-core-it/it0094/goals.txt @@ -0,0 +1 @@ +install diff --git a/maven-core-it/it0094/mojo/pom.xml b/maven-core-it/it0094/mojo/pom.xml new file mode 100644 index 0000000000..16e51b8cb1 --- /dev/null +++ b/maven-core-it/it0094/mojo/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + org.codehaus.mojo + mojo + 5 + + org.apache.maven.it + maven-core-it0094-mojo + maven-plugin + 1.0-SNAPSHOT + + + + org.apache.maven + maven-plugin-api + 2.0 + + + org.apache.maven + maven-model + 2.0 + + + log4j + log4j + [1.2.9,] + + + org.codehaus.plexus + plexus-utils + 1.0.5 + + + xalan + xalan + 2.5.1 + compile + + + xerces + xercesImpl + 2.5.0 + compile + + + xml-apis + xml-apis + 2.0.0 + compile + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + + + + diff --git a/maven-core-it/it0094/mojo/src/main/java/org/codehaus/mojo/kodo/Enhance.java b/maven-core-it/it0094/mojo/src/main/java/org/codehaus/mojo/kodo/Enhance.java new file mode 100644 index 0000000000..50936066d9 --- /dev/null +++ b/maven-core-it/it0094/mojo/src/main/java/org/codehaus/mojo/kodo/Enhance.java @@ -0,0 +1,147 @@ +/* + * Copyright 2005-2006 Brian Fox (brianefox@gmail.com) + * + * Licensed 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. + */ + +package org.codehaus.mojo.kodo; + +import java.lang.reflect.Field; +import java.net.URL; +import java.net.URLClassLoader; + +import javax.xml.parsers.SAXParserFactory; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.xerces.jaxp.SAXParserFactoryImpl; +import org.codehaus.classworlds.ClassRealm; + +/** + * Goal that enhances persistant classes + * + * @requiresDependancyResolution test + * @goal enhance + * + * @phase compile + */ +public class Enhance + extends AbstractMojo + +{ + public Enhance() + { + super(); + } + + public void execute() + throws MojoExecutionException + { + printClassPath(); + + ClassLoader originalLoader = Thread.currentThread().getContextClassLoader(); + System.out.println( originalLoader.getClass() ); + + setupClassloader(); + originalLoader = Thread.currentThread().getContextClassLoader(); + System.out.println( originalLoader.getClass() ); + + SAXParserFactoryImpl spi = new SAXParserFactoryImpl(); + SAXParserFactory spf = SAXParserFactory.newInstance(); + this.getLog().info( spf.toString() ); + String t = "org/apache/xerces/jaxp/SAXParserFactoryImpl.class"; + this.getLog().info(t); + URL url = originalLoader.getResource(t); + //URL url = spf.getClass().getClassLoader().getResource("javax/xml/parsers/SAXParserFactory.class"); + this.getLog().info("Loaded from: "+url.toString()); + + } + + /** + * Adds nessessary items to the classloader. + * + * @return ClassLoader original Classloader. + * @throws MojoExecutionException + */ + public ClassLoader setupClassloader() + throws MojoExecutionException + { + + URLClassLoader loader = null; + ClassLoader originalLoader = Thread.currentThread().getContextClassLoader(); + this.getLog().info( originalLoader.toString() ); + URL[] urls = new URL[0]; + loader = new URLClassLoader( urls, originalLoader ); + + Thread.currentThread().setContextClassLoader( loader ); + printURLClassPath(); + return originalLoader; + + } + + public void printURLClassPath() + { + //Get the Classloader + ClassLoader sysClassLoader = Thread.currentThread().getContextClassLoader(); + //Get the URLs + URL[] urls = ( (URLClassLoader) sysClassLoader ).getURLs(); + this.getLog().info( "Added to Classpath:" ); + for ( int i = 0; i < urls.length; i++ ) + { + this.getLog().info( urls[i].getFile() ); + } + } + + public void printClassPath() + { + ClassLoader sysClassLoader = Thread.currentThread().getContextClassLoader(); + URL[] urls = null; + Field field; + try + { + + field = sysClassLoader.getClass().getDeclaredField( "realm" ); + field.setAccessible( true ); + ClassRealm realm = (ClassRealm) field.get( sysClassLoader ); + + urls = realm.getConstituents(); + } + catch ( SecurityException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch ( NoSuchFieldException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch ( IllegalArgumentException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch ( IllegalAccessException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + //URL[] urls = ( (URLClassLoader) sysClassLoader ).getURLs(); + this.getLog().info( "Initial Classpath:" ); + for ( int i = 0; i < urls.length; i++ ) + { + this.getLog().info( urls[i].getFile() ); + } + } +} diff --git a/maven-core-it/it0094/pom.xml b/maven-core-it/it0094/pom.xml new file mode 100644 index 0000000000..1e93aae704 --- /dev/null +++ b/maven-core-it/it0094/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + org.apache.maven.it + maven-core-it0094 + pom + 1.0-SNAPSHOT + + + mojo + test + + + diff --git a/maven-core-it/it0094/test/pom.xml b/maven-core-it/it0094/test/pom.xml new file mode 100644 index 0000000000..13b4fd8311 --- /dev/null +++ b/maven-core-it/it0094/test/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + org.apache.maven.it + maven-core-it0094 + 1.0-SNAPSHOT + + maven-core-it0094-test + + + + + + + + org.apache.maven.it + maven-core-it0094-mojo + + + process-classes + process-classes + + enhance + + + + + + + +