openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1533462 - in /openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven: AbstractOpenJpaEnhancerMojo.java AbstractOpenJpaMojo.java OpenJpaSchemaMojo.java OpenJpaSqlMojo.java
Date Fri, 18 Oct 2013 14:11:54 GMT
Author: struberg
Date: Fri Oct 18 14:11:53 2013
New Revision: 1533462

URL: http://svn.apache.org/r1533462
Log:
OPENJPA-2409 enable persistenceXmlFile parameter to handle classpath resources as well.

contributed by rmannibucau, txs!
applied with small changes and doc improvements

Modified:
    openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaEnhancerMojo.java
    openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java
    openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSchemaMojo.java
    openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSqlMojo.java

Modified: openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaEnhancerMojo.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaEnhancerMojo.java?rev=1533462&r1=1533461&r2=1533462&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaEnhancerMojo.java
(original)
+++ openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaEnhancerMojo.java
Fri Oct 18 14:11:53 2013
@@ -100,7 +100,7 @@ public abstract class AbstractOpenJpaEnh
      *
      * @return populated Options
      */
-    protected Options getOptions() {
+    protected Options getOptions() throws MojoExecutionException {
         // options
         Options opts = createOptions();
 

Modified: openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java?rev=1533462&r1=1533461&r2=1533462&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java
(original)
+++ openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java
Fri Oct 18 14:11:53 2013
@@ -28,6 +28,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
@@ -41,6 +42,16 @@ import org.codehaus.plexus.util.FileUtil
  */
 public abstract class AbstractOpenJpaMojo extends AbstractMojo 
 {
+    /**
+     * The working directory for putting persistence.xml and
+     * other stuff into if we need to.
+     *
+     *
+     * @parameter expression="${openjpa.workdir}"
+     *            default-value="${project.build.directory}/openjpa-work"
+     * @required
+     */
+    protected File workDir;
 
     /**
      * Location where <code>persistence-enabled</code> classes are located.
@@ -79,8 +90,9 @@ public abstract class AbstractOpenJpaMoj
     /**
      * Used if a non-default file location for the persistence.xml should be used
      * If not specified, the default one in META-INF/persistence.xml will be used.
-     * Please note that this is not a resource location but a file path!
-     *  
+     * Since openjpa-2.3.0 this can also be a resource location. In prior releases
+     * it was only possible to specify a file location.
+     *
      * @parameter
      */
     private String persistenceXmlFile;
@@ -206,7 +218,7 @@ public abstract class AbstractOpenJpaMoj
      * Get the options for the various OpenJPA tools.
      * @return populated Options
      */
-    protected abstract Options getOptions();
+    protected abstract Options getOptions() throws MojoExecutionException;
     
     /**
      * <p>Determine if the mojo execution should get skipped.</p>
@@ -240,7 +252,7 @@ public abstract class AbstractOpenJpaMoj
      * This function will usually get called by {@link #getOptions()}
      * @return the Options filled with the initial values
      */
-    protected Options createOptions()
+    protected Options createOptions() throws MojoExecutionException
     {
         Options opts = new Options();
         if ( toolProperties != null )
@@ -250,8 +262,18 @@ public abstract class AbstractOpenJpaMoj
         
         if ( persistenceXmlFile != null )
         {
+            fixPersistenceXmlIfNeeded(Thread.currentThread().getContextClassLoader());
             opts.put( OPTION_PROPERTIES_FILE, persistenceXmlFile );
-            getLog().debug( "using special persistence XML file: " + persistenceXmlFile );
+            getLog().debug("using special persistence XML file: " + persistenceXmlFile);
+        }
+        else if (!new File(classes, "META-INF/persistence.xml").exists())
+        { // use default but try from classpath
+            persistenceXmlFile = "META-INF/persistence.xml";
+            if (!fixPersistenceXmlIfNeeded(Thread.currentThread().getContextClassLoader()))
{
+                persistenceXmlFile = null;
+            } else {
+                opts.put( OPTION_PROPERTIES_FILE, persistenceXmlFile );
+            }
         }
 
         if ( connectionDriverName != null )
@@ -266,7 +288,71 @@ public abstract class AbstractOpenJpaMoj
 
         return opts;
     }
-    
+
+    /**
+     *
+     *
+     * @param loader
+     * @return
+     * @throws MojoExecutionException
+     */
+    private boolean fixPersistenceXmlIfNeeded(final ClassLoader loader) throws MojoExecutionException
+    {
+        return !new File(persistenceXmlFile).exists() &&
+                (findPersistenceXmlFromLoader(loader)
+                    || findPersistenceXmlInArtifacts(project.getCompileArtifacts())
+                    || findPersistenceXmlInArtifacts(project.getRuntimeArtifacts()));
+    }
+
+    private boolean findPersistenceXmlFromLoader(final ClassLoader loader) throws MojoExecutionException
{
+        final URL url = loader.getResource(persistenceXmlFile);
+        if (url != null) // copy file to be sure to set persistenceXmlFile to a file
+        {
+            final File tmpPersistenceXml = new File(workDir,
+                    "persistence" + System.currentTimeMillis() + ".xml");
+            if (!tmpPersistenceXml.getParentFile().exists() && !tmpPersistenceXml.getParentFile().mkdirs())
+            {
+                throw new MojoExecutionException("Can't create "
+                        + tmpPersistenceXml.getParentFile().getAbsolutePath());
+            }
+
+            try
+            {
+                FileUtils.copyURLToFile(url, tmpPersistenceXml);
+            }
+            catch (final IOException e)
+            {
+                throw new MojoExecutionException(e.getMessage(), e);
+            }
+
+            persistenceXmlFile = tmpPersistenceXml.getAbsolutePath();
+            return true;
+        }
+        return false;
+    }
+
+    private boolean findPersistenceXmlInArtifacts(final List<Artifact> artifacts) throws
MojoExecutionException {
+        for (final Artifact artifact : artifacts) {
+            final File file = artifact.getFile();
+            if (file != null && file.exists())
+            {
+                try // find the persistence.xml using a fake classloader to not need to play
with URLs
+                {
+                    if (findPersistenceXmlFromLoader(new URLClassLoader(new URL[] { file.toURI().toURL()
},
+                                                                ClassLoader.getSystemClassLoader())))
+                    {
+                        return true;
+                    }
+                }
+                catch (final MalformedURLException e)
+                {
+                    // no-op
+                }
+            }
+        }
+        return false;
+    }
+
     /**
      * This will prepare the current ClassLoader and add all jars and local
      * classpaths (e.g. target/classes) needed by the OpenJPA task.

Modified: openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSchemaMojo.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSchemaMojo.java?rev=1533462&r1=1533461&r2=1533462&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSchemaMojo.java
(original)
+++ openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSchemaMojo.java
Fri Oct 18 14:11:53 2013
@@ -21,6 +21,7 @@ package org.apache.openjpa.tools.maven;
 
 import java.io.File;
 
+import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.openjpa.lib.util.Options;
 
 /**
@@ -72,7 +73,7 @@ public class OpenJpaSchemaMojo extends A
     /**
      * @return Options filled with all necessary plugin parameters
      */
-    protected Options getOptions() {
+    protected Options getOptions() throws MojoExecutionException {
         // options
         Options opts = createOptions();
 

Modified: openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSqlMojo.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSqlMojo.java?rev=1533462&r1=1533461&r2=1533462&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSqlMojo.java
(original)
+++ openjpa/branches/2.3.x/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/OpenJpaSqlMojo.java
Fri Oct 18 14:11:53 2013
@@ -21,6 +21,7 @@ package org.apache.openjpa.tools.maven;
 
 import java.io.File;
 
+import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.openjpa.lib.util.Options;
 
 /**
@@ -86,7 +87,7 @@ public class OpenJpaSqlMojo extends Abst
     /**
      * @return Options filled with all necessary plugin parameters
      */
-    protected Options getOptions() {
+    protected Options getOptions() throws MojoExecutionException {
         // options
         Options opts = createOptions();
 



Mime
View raw message