Skip to content

Commit

Permalink
m0smithgh-28 added exec service to run a main method
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew O. Smith committed Jan 14, 2015
1 parent bdc35ed commit 7347289
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=2.0.8-SNAPSHOT
version=2.1.0-SNAPSHOT
profile=empty
ossrhUsername=m0smith
ossrhPassword=entered-during-uploadArchive-and-release
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class MalabarServer {
params['parser']);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/exec/', new JsonHandlerFactory(config).build({params ->
def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.exec(params["repo"], pm, params["class"], params["arg"]);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/tags/', new JsonHandlerFactory(config).build({params ->
def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,33 @@ public class MavenProjectHandler {
ex.getStackTrace()];
}

def fixArgs(args) {
if(args == null) return new String[0];
if(args instanceof String) { def r = new String[1]; r[0] = args; return r;};
return args as String[];
}



/**
* Load and execute the class. Assumes the class has been compiled/parsed already.
*/
def exec(repo, pom, clazzName, args) {
log.fine "Start Exec of " + clazzName;

try{

def cached = lookInCache( pom, { fecthProjectInfo(repo, pom)});

def cl = cached['classLoader'];
def clazz = Class.forName(clazzName, true, cl);
clazz.main(fixArgs(args));

} catch (Exception ex){
ex.printStackTrace();
}
}

/**
* Parse the script on disk. Return errors as a list
*/
Expand Down Expand Up @@ -484,5 +511,3 @@ public class MavenProjectsCreator {


//:load file:/c:/Users/lpmsmith/projects/malabar-mode-jar/src/main/groovy/com/software_ninja/malabar/D.groovy


40 changes: 40 additions & 0 deletions src/test/groovy/com/software_ninja/malabar/project/TestExec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

package com.software_ninja.malabar.project;

import org.junit.Test;
import org.junit.Before;

import static org.junit.Assert.*

import com.software_ninja.malabar.MalabarUtil;
import com.software_ninja.malabar.project.MavenProjectHandler;


public class TestExecImpl {

private Map config;
private MavenProjectHandler mph;
private String defaultRepo = System.getProperty("user.home") + "/.m2/repository";

@Before
public void init() {
config = [ cache : [:] ];
mph = new MavenProjectHandler(config);
}

@Test
public void testExec() {
String simple = 'src/test/resources/projects/simple/';
String scriptIn = simple + '/src/test/java/com/software_ninja/test/project/App.java';
String pm = simple + "pom.xml";

String clazzName = "com.software_ninja.test.project.App";
println 'http://localhost:4429/exec/?pm=' + pm +'&class=' + clazzName + "&repo=" + defaultRepo;
mph.parse(defaultRepo, pm, scriptIn, null, "java");
mph.exec (defaultRepo, pm, clazzName, null);
mph.exec (defaultRepo, pm, clazzName, ["a", "b", "c"] as String[]);
}

}


Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.software_ninja.malabar.project;

import static net.java.quickcheck.generator.PrimitiveGenerators.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
System.out.println( "Hello World! with " + args.length + " args" );
}


public boolean getTrue() {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.software_ninja.test.project;

/**
* Hello world!
*
*/

class PreParserTarget{}

interface PreInterface {}

public class ParserTargetWithError
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}


public boolean getTrue() {
return true;
}

PreInterface pi = new PreInterface(){};

class Inner {}

static class StaticInner{ x=;}

}

0 comments on commit 7347289

Please sign in to comment.