Author: jochen
Date: Wed Dec 14 12:59:40 2005
New Revision: 356867

URL: http://svn.apache.org/viewcvs?rev=356867&view=rev
Log:
Fixed ClassLoader issues with custom SG factory chains.

Modified:
    
webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeGoal.java

Modified: 
webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeGoal.java
URL: 
http://svn.apache.org/viewcvs/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeGoal.java?rev=356867&r1=356866&r2=356867&view=diff
==============================================================================
--- 
webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeGoal.java
 (original)
+++ 
webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeGoal.java
 Wed Dec 14 12:59:40 2005
@@ -16,7 +16,11 @@
 package org.apache.ws.jaxme.maven.plugins;
 
 import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.maven.model.Resource;
@@ -45,11 +49,20 @@
  * @requiresDependencyResolution test
  */
 public class JaxMeGoal extends AbstractMojo {
+       /** Specifies classpath elements, which should be added to
+        * the plugins classpath.
+        * @parameter expression="${project.compileClasspathElements}"
+        * @required
+        * @readonly
+        */
+       private List classpathElements;
+
        /** This property specifies a set of files, which
         * are referenced from within the XML schema files,
         * but aren't processed as XML schema files for
         * themselves. Specifying files in the "depends" set
         * allows to include them into the uptodate check.
+        * @parameter
         */
        private String[] depends;
 
@@ -84,11 +97,14 @@
         * produces files will also be used for removing old
         * files, if the "removeOldOutput" feature is turned
         * on.
+        * @parameter
         */
        private String[] produces;
 
        /** The Maven project.
         * @parameter expression="${project}"
+        * @required
+        * @readonly
         */
        private MavenProject project;
 
@@ -116,7 +132,7 @@
         * The default changes, if "extension" is set to true,
         * in which case the [EMAIL PROTECTED] JaxMeSchemaReader} is being
         * used.
-        * @parameter 
expression="org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader"
+        * @parameter
         */
        private String schemaReader;
        
@@ -130,6 +146,7 @@
         * generator. Factory chains are modifying the generators
         * behaviour. A good example is the
         * <code>org.apache.ws.jaxme.pm.generator.jdbc.JaxMeJdbcSG</code>.
+        * @parameter
         */
        private String[] sgFactoryChain;
 
@@ -150,6 +167,10 @@
         */
        private boolean validating;
 
+       protected List getClasspathElements() {
+               return classpathElements;
+       }
+
        protected String[] getDepends() {
                return depends;
        }
@@ -208,7 +229,17 @@
        }
 
        private File[] getProducedFiles() {
-               return getFiles(getProduces());
+               String[] prdcs = getProduces();
+               if (prdcs == null) {
+                       String t1 = getSrcTarget();
+                       String t2 = getResourceTarget();
+                       if (t2 == null) {
+                               prdcs = new String[]{t1};
+                       } else {
+                               prdcs = new String[]{t1, t2};
+                       }
+               }
+               return getFiles(prdcs);
        }
 
        private File[] getFiles(String[] pSpec) {
@@ -313,10 +344,11 @@
                }
        }
 
-       private Class getSgFactoryChainClass(String pClass) throws 
MojoFailureException {
+       private Class getSgFactoryChainClass(String pClass)
+                       throws MojoFailureException {
                Class c;
                try {
-                       c = Class.forName(pClass);
+                       c = 
Thread.currentThread().getContextClassLoader().loadClass(pClass);
                } catch (ClassNotFoundException e) {
                        throw new MojoFailureException("The factory chain class 
" + pClass + " was not found.");
                }
@@ -327,9 +359,52 @@
                return c;
        }
 
+       private ClassLoader getClassLoader(ClassLoader pParent) throws 
MojoFailureException {
+               List clElements = getClasspathElements();
+               if (clElements == null  &&  clElements.size() == 0) {
+                       return pParent;
+               }
+               URL[] urls = new URL[clElements.size()];
+               StringBuffer sb = new StringBuffer();
+               for (int i = 0;  i < clElements.size();  i++) {
+                       final String elem = (String) clElements.get(i);
+                       File f = new File(elem);
+                       if (!f.isAbsolute()) {
+                               f = new File(getProject().getBasedir(), elem);
+                       }
+                       try {
+                               urls[i] = f.toURL();
+                       } catch (MalformedURLException e) {
+                               throw new MojoFailureException("Invalid 
classpath element: " + elem);
+                       }
+                       if (i > 0) {
+                               sb.append(File.pathSeparator);
+                       }
+                       sb.append(urls[i]);
+               }
+               getLog().debug("Using classpath " + sb);
+               return new URLClassLoader(urls, pParent);
+       }
+
        private SchemaReader getSchemaReaderInstance() throws 
MojoFailureException, MojoExecutionException {
-               String s = getSchemaReader();
+               final SchemaReader result = newSchemaReaderInstance();
+               getLog().debug("Schema reader class: " + 
result.getClass().getName());
+
+               String[] chains = getSgFactoryChain();
+               if (chains != null) {
+                       for (int i = 0;  i < chains.length;  i++) {
+                               Class c = getSgFactoryChainClass(chains[i]);
+                               getLog().debug("Adding SG Factory chain: " + 
c.getName());
+                               result.addSGFactoryChain(c);
+                       }
+               }
+
+               return result;
+       }
+
+       private SchemaReader newSchemaReaderInstance() throws 
MojoFailureException, MojoExecutionException {
                final SchemaReader result;
+               final String s = getSchemaReader();
                if (s == null  ||  s.length() == 0) {
                        if (isExtension()) {
                                result = new JaxMeSchemaReader();
@@ -339,7 +414,7 @@
                } else {
                        Class c;
                        try {
-                               c = Class.forName(s);
+                               c = 
Thread.currentThread().getContextClassLoader().loadClass(s);
                        } catch (ClassNotFoundException e) {
                                throw new MojoFailureException("The schema 
reader class " + s + " was not found.");
                        }
@@ -358,15 +433,6 @@
                                                + " is not implementing " + 
SchemaReader.class.getName());
                        }
                }
-
-               String[] chains = getSgFactoryChain();
-               if (chains != null) {
-                       for (int i = 0;  i < chains.length;  i++) {
-                               Class c = getSgFactoryChainClass(chains[i]);
-                               result.addSGFactoryChain(c);
-                       }
-               }
-
                return result;
        }
 
@@ -387,6 +453,8 @@
        }
 
        public void execute() throws MojoExecutionException, 
MojoFailureException {
+               ClassLoader oldCl = 
Thread.currentThread().getContextClassLoader();
+               
Thread.currentThread().setContextClassLoader(getClassLoader(SchemaReader.class.getClassLoader()));
                LoggerFactory lf = LoggerAccess.getLoggerFactory();
                try {
                        LoggerAccess.setLoggerFactory(new LoggerFactory(){
@@ -436,6 +504,7 @@
                        resource.setDirectory(getResourceTarget());
                        getProject().addResource(resource);
                } finally {
+                       Thread.currentThread().setContextClassLoader(oldCl);
                        LoggerAccess.setLoggerFactory(lf);
                }
        }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to