1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.spring.util; |
18 |
|
|
19 |
|
import java.lang.reflect.InvocationTargetException; |
20 |
|
import java.lang.reflect.Method; |
21 |
|
import java.lang.reflect.Modifier; |
22 |
|
import java.util.Arrays; |
23 |
|
|
24 |
|
import org.apache.commons.logging.Log; |
25 |
|
import org.apache.commons.logging.LogFactory; |
26 |
|
|
27 |
|
import org.springframework.beans.factory.InitializingBean; |
28 |
|
|
29 |
|
import static org.apache.camel.util.ObjectHelper.name; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
0 |
public class MainRunner implements InitializingBean, Runnable { |
39 |
0 |
private static final Log LOG = LogFactory.getLog(MainRunner.class); |
40 |
|
|
41 |
|
private Class main; |
42 |
0 |
private String[] args = {}; |
43 |
0 |
private boolean asyncRun = true; |
44 |
|
private long delay; |
45 |
|
|
46 |
|
public String toString() { |
47 |
0 |
return "MainRunner(" + name(main) + " " + Arrays.asList(getArgs()) + ")"; |
48 |
|
} |
49 |
|
|
50 |
|
public void run() { |
51 |
|
try { |
52 |
0 |
runMethodWithoutCatchingExceptions(); |
53 |
0 |
} catch (NoSuchMethodException e) { |
54 |
0 |
LOG.error("Class: " + name(main) + " does not have a main method: " + e, e); |
55 |
0 |
} catch (IllegalAccessException e) { |
56 |
0 |
LOG.error("Failed to run: " + this + ". Reason: " + e, e); |
57 |
0 |
} catch (InvocationTargetException e) { |
58 |
0 |
Throwable throwable = e.getTargetException(); |
59 |
0 |
LOG.error("Failed to run: " + this + ". Reason: " + throwable, throwable); |
60 |
0 |
} |
61 |
0 |
} |
62 |
|
|
63 |
|
public void runMethodWithoutCatchingExceptions() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
64 |
0 |
if (delay > 0) { |
65 |
|
try { |
66 |
0 |
Thread.sleep(delay); |
67 |
0 |
} catch (InterruptedException e) { |
68 |
0 |
LOG.info("Caught: " + e, e); |
69 |
0 |
} |
70 |
|
} |
71 |
0 |
Method method = main.getMethod("main", String[].class); |
72 |
0 |
if (!Modifier.isStatic(method.getModifiers())) { |
73 |
0 |
throw new IllegalArgumentException("The main method is not static!: " + method); |
74 |
|
} |
75 |
0 |
Object[] arguments = {getArgs()}; |
76 |
0 |
method.invoke(null, arguments); |
77 |
0 |
} |
78 |
|
|
79 |
|
public String[] getArgs() { |
80 |
0 |
return args; |
81 |
|
} |
82 |
|
|
83 |
|
public void setArgs(String[] args) { |
84 |
0 |
this.args = args; |
85 |
0 |
} |
86 |
|
|
87 |
|
public boolean isAsyncRun() { |
88 |
0 |
return asyncRun; |
89 |
|
} |
90 |
|
|
91 |
|
public void setAsyncRun(boolean asyncRun) { |
92 |
0 |
this.asyncRun = asyncRun; |
93 |
0 |
} |
94 |
|
|
95 |
|
public Class getMain() { |
96 |
0 |
return main; |
97 |
|
} |
98 |
|
|
99 |
|
public void setMain(Class main) { |
100 |
0 |
this.main = main; |
101 |
0 |
} |
102 |
|
|
103 |
|
public long getDelay() { |
104 |
0 |
return delay; |
105 |
|
} |
106 |
|
|
107 |
|
public void setDelay(long delay) { |
108 |
0 |
this.delay = delay; |
109 |
0 |
} |
110 |
|
|
111 |
|
public void afterPropertiesSet() throws Exception { |
112 |
0 |
if (main == null) { |
113 |
0 |
throw new IllegalArgumentException("You must specify a main class!"); |
114 |
|
} |
115 |
0 |
if (isAsyncRun()) { |
116 |
0 |
Thread thread = new Thread(this, "Thread for: " + this); |
117 |
0 |
thread.start(); |
118 |
0 |
} else { |
119 |
0 |
runMethodWithoutCatchingExceptions(); |
120 |
|
} |
121 |
0 |
} |
122 |
|
} |