]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/AppLauncher.java
Use context classloader
[gpl/argeo-slc.git] / org.argeo.slc.detached / src / main / java / org / argeo / slc / detached / AppLauncher.java
1 package org.argeo.slc.detached;
2
3 import java.lang.reflect.Method;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Properties;
7
8 public class AppLauncher {
9 private Properties systemProperties = new Properties();
10 private String mainClass = null;
11 private List arguments = new ArrayList();
12
13 public void launch() {
14
15 Properties base = System.getProperties();
16 Properties fake = new Properties(base);
17
18 try {
19 if (mainClass == null)
20 throw new DetachedException(
21 "A main class name has to be specified.");
22
23 System.getProperties().putAll(systemProperties);
24
25 ClassLoader cl = Thread.currentThread().getContextClassLoader();
26 Class clss = cl.loadClass(mainClass);
27
28 String[] args = new String[arguments.size()];
29 for (int i = 0; i < arguments.size(); i++) {
30 args[i] = arguments.get(i).toString();
31 }
32
33 Class[] mainArgsClasses = new Class[] { args.getClass() };
34 Object[] mainArgs = { args };
35 Method mainMethod = clss.getMethod("main", mainArgsClasses);
36
37 System.setProperties(fake);
38
39 mainMethod.invoke(null, mainArgs);
40
41 } catch (Exception e) {
42 throw new DetachedException("Unexpected exception while launching "
43 + mainClass, e);
44 } finally {
45 System.setProperties(base);
46 }
47
48 }
49
50 public void setSystemProperties(Properties systemProperties) {
51 this.systemProperties = systemProperties;
52 }
53
54 public void setMainClass(String mainClass) {
55 this.mainClass = mainClass;
56 }
57
58 public void setArguments(List arguments) {
59 this.arguments = arguments;
60 }
61
62 }