You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2011/03/31 12:26:03 UTC

svn commit: r1087238 - /tuscany/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java

Author: antelder
Date: Thu Mar 31 10:26:02 2011
New Revision: 1087238

URL: http://svn.apache.org/viewvc?rev=1087238&view=rev
Log:
Add support for invoking a main method instead of running the Shell. That enables using 'mvn tuscany:run' instead of 'mvn exec:java' to run a Tuscany sample that runs a regular Java SE class using Tuscany

Modified:
    tuscany/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java

Modified: tuscany/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java?rev=1087238&r1=1087237&r2=1087238&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java (original)
+++ tuscany/sca-java-2.x/trunk/maven/maven-tuscany-plugin/src/main/java/org/apache/tuscany/maven/plugin/TuscanyRunMojo.java Thu Mar 31 10:26:02 2011
@@ -19,7 +19,10 @@
 package org.apache.tuscany.maven.plugin;
 
 import java.io.File;
+import java.lang.reflect.Method;
 import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -98,7 +101,25 @@ public class TuscanyRunMojo extends Abst
      */
     private String[] contributions;
 
+    /**
+     * @parameter expression="${mainClass}"
+     */
+    private String mainClass;
+
+    /**
+     * @parameter expression="${arguments}"
+     */
+    private String[] arguments;
+
     public void execute() throws MojoExecutionException, MojoFailureException {
+        if (mainClass != null) {
+            executeMainMethod();
+        } else {
+            executeShell();
+        }
+    }
+
+    private void executeShell() throws MojoExecutionException {
         getLog().info("Starting Tuscany Shell...");
 
         List<String> contributionList = new ArrayList<String>();
@@ -165,4 +186,28 @@ public class TuscanyRunMojo extends Abst
             throw new MojoExecutionException("", e);
         }
     }
+
+    public void executeMainMethod() throws MojoExecutionException, MojoFailureException {
+        getLog().info("Invoking " + mainClass + " class main method...");
+        
+        if (arguments == null) {
+            arguments = new String[0];
+        }
+
+        try {
+            Method main = getMainClassLoader().loadClass(mainClass).getMethod("main", new Class[] {String[].class});
+            main.invoke(main, new Object[] {arguments});
+        } catch (NoSuchMethodException e) {
+            throw new MojoExecutionException("The specified mainClass doesn't contain a main method with appropriate signature", e);
+        } catch (Exception e) {
+            throw new MojoExecutionException("exception invoking main method", e);
+        }
+    }
+
+    private ClassLoader getMainClassLoader() throws MalformedURLException {
+        ClassLoader parent = Thread.currentThread().getContextClassLoader();
+        URL thisProject = new File( project.getBuild().getOutputDirectory()).toURI().toURL(); 
+        return new URLClassLoader(new URL[]{thisProject}, parent );
+    }
+
 }