1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.spring; |
18 |
|
|
19 |
|
import java.util.ArrayList; |
20 |
|
import java.util.Arrays; |
21 |
|
import java.util.LinkedList; |
22 |
|
import java.util.List; |
23 |
|
import java.util.concurrent.CountDownLatch; |
24 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
25 |
|
|
26 |
|
import org.apache.camel.impl.ServiceSupport; |
27 |
|
import org.apache.commons.logging.Log; |
28 |
|
import org.apache.commons.logging.LogFactory; |
29 |
|
|
30 |
|
import org.springframework.context.support.AbstractApplicationContext; |
31 |
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public class Main extends ServiceSupport { |
40 |
0 |
private static final Log LOG = LogFactory.getLog(Main.class); |
41 |
0 |
private String applicationContextUri = "META-INF/spring/*.xml"; |
42 |
|
private AbstractApplicationContext applicationContext; |
43 |
0 |
private List<Option> options = new ArrayList<Option>(); |
44 |
0 |
private CountDownLatch latch = new CountDownLatch(1); |
45 |
0 |
private AtomicBoolean completed = new AtomicBoolean(false); |
46 |
|
|
47 |
0 |
public Main() { |
48 |
0 |
addOption(new Option("h", "help", "Displays the help screen") { |
49 |
0 |
protected void doProcess(String arg, LinkedList<String> remainingArgs) { |
50 |
0 |
showOptions(); |
51 |
0 |
completed(); |
52 |
0 |
} |
53 |
|
}); |
54 |
|
|
55 |
0 |
addOption(new ParameterOption("a", "applicationContext", "Sets the classpath based pring ApplicationContext", "applicationContext") { |
56 |
0 |
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) { |
57 |
0 |
setApplicationContextUri(parameter); |
58 |
0 |
} |
59 |
|
}); |
60 |
0 |
} |
61 |
|
|
62 |
|
public static void main(String[] args) { |
63 |
0 |
Main main = new Main(); |
64 |
0 |
main.run(args); |
65 |
0 |
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
public void run(String[] args) { |
71 |
0 |
parseArguments(args); |
72 |
0 |
run(); |
73 |
0 |
} |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
public void run() { |
79 |
0 |
if (!completed.get()) { |
80 |
|
try { |
81 |
0 |
start(); |
82 |
0 |
waitUntilCompleted(); |
83 |
0 |
stop(); |
84 |
0 |
} catch (Exception e) { |
85 |
0 |
LOG.error("Failed: " + e, e); |
86 |
0 |
} |
87 |
|
} |
88 |
0 |
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
public void completed() { |
94 |
0 |
completed.set(true); |
95 |
0 |
latch.countDown(); |
96 |
0 |
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
public void showOptions() { |
102 |
0 |
System.out.println("Apache Camel Runner takes the following options"); |
103 |
0 |
System.out.println(); |
104 |
|
|
105 |
0 |
for (Option option : options) { |
106 |
0 |
System.out.println(" " + option.getAbbreviation() + " or " + option.getFullName() + " = " + option.getDescription()); |
107 |
0 |
} |
108 |
0 |
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
public void parseArguments(String[] arguments) { |
114 |
0 |
LinkedList<String> args = new LinkedList<String>(Arrays.asList(arguments)); |
115 |
|
|
116 |
0 |
boolean valid = true; |
117 |
0 |
while (!args.isEmpty()) { |
118 |
0 |
String arg = args.removeFirst(); |
119 |
|
|
120 |
0 |
boolean handled = false; |
121 |
0 |
for (Option option : options) { |
122 |
0 |
if (option.processOption(arg, args)) { |
123 |
0 |
handled = true; |
124 |
0 |
break; |
125 |
|
} |
126 |
0 |
} |
127 |
0 |
if (!handled) { |
128 |
0 |
System.out.println("Unknown option: " + arg); |
129 |
0 |
System.out.println(); |
130 |
0 |
valid = false; |
131 |
0 |
break; |
132 |
|
} |
133 |
0 |
} |
134 |
0 |
if (!valid) { |
135 |
0 |
showOptions(); |
136 |
0 |
completed(); |
137 |
|
} |
138 |
0 |
} |
139 |
|
|
140 |
|
public void addOption(Option option) { |
141 |
0 |
options.add(option); |
142 |
0 |
} |
143 |
|
|
144 |
|
public abstract class Option { |
145 |
|
private String abbreviation; |
146 |
|
private String fullName; |
147 |
|
private String description; |
148 |
|
|
149 |
0 |
protected Option(String abbreviation, String fullName, String description) { |
150 |
0 |
this.abbreviation = "-" + abbreviation; |
151 |
0 |
this.fullName = "-" + fullName; |
152 |
0 |
this.description = description; |
153 |
0 |
} |
154 |
|
|
155 |
|
public boolean processOption(String arg, LinkedList<String> remainingArgs) { |
156 |
0 |
if (arg.equalsIgnoreCase(abbreviation) || fullName.startsWith(arg)) { |
157 |
0 |
doProcess(arg, remainingArgs); |
158 |
0 |
return true; |
159 |
|
} |
160 |
0 |
return false; |
161 |
|
} |
162 |
|
|
163 |
|
public String getAbbreviation() { |
164 |
0 |
return abbreviation; |
165 |
|
} |
166 |
|
|
167 |
|
public String getDescription() { |
168 |
0 |
return description; |
169 |
|
} |
170 |
|
|
171 |
|
public String getFullName() { |
172 |
0 |
return fullName; |
173 |
|
} |
174 |
|
|
175 |
|
protected abstract void doProcess(String arg, LinkedList<String> remainingArgs); |
176 |
|
} |
177 |
|
|
178 |
|
public abstract class ParameterOption extends Option { |
179 |
|
private String parameterName; |
180 |
|
|
181 |
0 |
protected ParameterOption(String abbreviation, String fullName, String description, String parameterName) { |
182 |
0 |
super(abbreviation, fullName, description); |
183 |
0 |
this.parameterName = parameterName; |
184 |
0 |
} |
185 |
|
|
186 |
|
protected void doProcess(String arg, LinkedList<String> remainingArgs) { |
187 |
0 |
if (remainingArgs.isEmpty()) { |
188 |
0 |
System.err.println("Expected fileName for "); |
189 |
0 |
showOptions(); |
190 |
0 |
completed(); |
191 |
0 |
} else { |
192 |
0 |
String parameter = remainingArgs.removeFirst(); |
193 |
0 |
doProcess(arg, parameter, remainingArgs); |
194 |
|
} |
195 |
0 |
} |
196 |
|
|
197 |
|
protected abstract void doProcess(String arg, String parameter, LinkedList<String> remainingArgs); |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
public AbstractApplicationContext getApplicationContext() { |
203 |
0 |
return applicationContext; |
204 |
|
} |
205 |
|
|
206 |
|
public void setApplicationContext(AbstractApplicationContext applicationContext) { |
207 |
0 |
this.applicationContext = applicationContext; |
208 |
0 |
} |
209 |
|
|
210 |
|
public String getApplicationContextUri() { |
211 |
0 |
return applicationContextUri; |
212 |
|
} |
213 |
|
|
214 |
|
public void setApplicationContextUri(String applicationContextUri) { |
215 |
0 |
this.applicationContextUri = applicationContextUri; |
216 |
0 |
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
protected void doStart() throws Exception { |
221 |
0 |
LOG.info("Apache Camel " + getVersion() + " starting"); |
222 |
0 |
if (applicationContext == null) { |
223 |
0 |
applicationContext = createDefaultApplicationContext(); |
224 |
|
} |
225 |
0 |
applicationContext.start(); |
226 |
0 |
} |
227 |
|
|
228 |
|
protected AbstractApplicationContext createDefaultApplicationContext() { |
229 |
0 |
return new ClassPathXmlApplicationContext(getApplicationContextUri()); |
230 |
|
} |
231 |
|
|
232 |
|
protected void doStop() throws Exception { |
233 |
0 |
LOG.info("Apache Camel terminating"); |
234 |
|
|
235 |
0 |
if (applicationContext != null) { |
236 |
0 |
applicationContext.close(); |
237 |
|
} |
238 |
0 |
} |
239 |
|
|
240 |
|
protected void waitUntilCompleted() { |
241 |
0 |
while (!completed.get()) { |
242 |
|
try { |
243 |
0 |
latch.await(); |
244 |
0 |
} catch (InterruptedException e) { |
245 |
|
|
246 |
0 |
} |
247 |
0 |
} |
248 |
0 |
} |
249 |
|
|
250 |
|
protected String getVersion() { |
251 |
0 |
Package aPackage = Package.getPackage("org.apache.camel"); |
252 |
0 |
if (aPackage != null) { |
253 |
0 |
String version = aPackage.getImplementationVersion(); |
254 |
0 |
if (version == null) { |
255 |
0 |
version = aPackage.getSpecificationVersion(); |
256 |
0 |
if (version == null) { |
257 |
0 |
version = ""; |
258 |
|
} |
259 |
|
} |
260 |
0 |
return version; |
261 |
|
} |
262 |
0 |
return ""; |
263 |
|
} |
264 |
|
} |