1 package org_scala_tools_maven_executions;
2
3 import java.io.File;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.net.URLClassLoader;
7 import java.util.ArrayList;
8
9 import org.apache.maven.plugin.AbstractMojo;
10 import org.codehaus.plexus.util.StringUtils;
11
12
13
14
15
16
17
18
19 public class JavaMainCallerInProcess extends JavaMainCallerSupport {
20
21 private ClassLoader _cl = null;
22
23 public JavaMainCallerInProcess(AbstractMojo requester, String mainClassName, String classpath, String[] jvmArgs, String[] args) throws Exception {
24 super(requester, mainClassName, "", jvmArgs, args);
25
26
27 ArrayList<URL> urls = new ArrayList<URL>();
28 for(String path : classpath.split(File.pathSeparator)) {
29 try {
30 urls.add(new File(path).toURI().toURL());
31 } catch (MalformedURLException e) {
32
33 requester.getLog().error(e);
34 }
35 }
36 _cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), (ClassLoader)null);
37 }
38
39
40
41 @Override
42 public void addJvmArgs(String... args) {
43
44 if (args != null) {
45 for (String arg : args) {
46 requester.getLog().warn("jvmArgs are ignored when run in process :" + arg);
47 }
48 }
49 }
50
51 public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
52 try {
53 runInternal(displayCmd);
54 return true;
55 } catch (Exception e) {
56 if(throwFailure) {
57 throw e;
58 }
59 return false;
60 }
61 }
62
63
64
65
66 public SpawnMonitor spawn(final boolean displayCmd) throws Exception {
67 final Thread t = new Thread() {
68 @Override
69 public void run() {
70 try {
71 runInternal(displayCmd);
72 } catch (Exception e) {
73
74 }
75 }
76 };
77 t.start();
78 return new SpawnMonitor() {
79 public boolean isRunning() throws Exception {
80 return t.isAlive();
81 }
82 };
83 }
84
85
86 private void runInternal(boolean displayCmd) throws Exception {
87 String[] argArray = args.toArray(new String[args.size()]);
88 if(displayCmd) {
89 requester.getLog().info("cmd : " + mainClassName + "(" + StringUtils.join(argArray, ",")+")");
90 }
91 MainHelper.runMain(mainClassName, args, _cl);
92 }
93
94
95
96 public void redirectToLog() {
97 requester.getLog().warn("redirection to log is not supported for 'inProcess' mode");
98 }
99
100
101 }