/
Main.java
331 lines (290 loc) · 12.7 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.spring;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.VetoCamelContextStartException;
import org.apache.camel.main.MainCommandLineSupport;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* A command line tool for booting up a CamelContext using an optional Spring
* {@link org.springframework.context.ApplicationContext}.
* <p/>
* By placing a file in the {@link #LOCATION_PROPERTIES} directory of any JARs on the classpath, allows this Main class
* to load those additional Spring XML files as Spring {@link org.springframework.context.ApplicationContext} to be
* included.
* <p/>
* Each line in the {@link #LOCATION_PROPERTIES} is a reference to a Spring XML file to include, which by default gets
* loaded from classpath.
*/
public class Main extends MainCommandLineSupport {
public static final String LOCATION_PROPERTIES = "META-INF/camel-spring/location.properties";
protected static Main instance;
private static final Charset UTF8 = StandardCharsets.UTF_8;
private String applicationContextUri = "META-INF/spring/*.xml";
private String fileApplicationContextUri;
private AbstractApplicationContext applicationContext;
private AbstractApplicationContext parentApplicationContext;
private AbstractApplicationContext additionalApplicationContext;
private String parentApplicationContextUri;
private boolean allowMultipleCamelContexts;
public Main() {
// do not run in standalone mode as we let Spring create and manage CamelContext but use this Main to bootstrap
standalone = false;
}
@Override
protected void initOptions() {
super.initOptions();
addOption(new ParameterOption(
"ac", "applicationContext",
"Sets the classpath based spring ApplicationContext", "applicationContext") {
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
setApplicationContextUri(parameter);
}
});
addOption(new ParameterOption(
"fa", "fileApplicationContext",
"Sets the filesystem based spring ApplicationContext", "fileApplicationContext") {
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
setFileApplicationContextUri(parameter);
}
});
}
public static void main(String... args) throws Exception {
Main main = new Main();
instance = main;
main.run(args);
System.exit(main.getExitCode());
}
/**
* Returns the currently executing main
*
* @return the current running instance
*/
public static Main getInstance() {
return instance;
}
// Properties
// -------------------------------------------------------------------------
public AbstractApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(AbstractApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String getApplicationContextUri() {
return applicationContextUri;
}
public void setApplicationContextUri(String applicationContextUri) {
this.applicationContextUri = applicationContextUri;
}
public String getFileApplicationContextUri() {
return fileApplicationContextUri;
}
public void setFileApplicationContextUri(String fileApplicationContextUri) {
this.fileApplicationContextUri = fileApplicationContextUri;
}
public AbstractApplicationContext getParentApplicationContext() {
if (parentApplicationContext == null) {
if (parentApplicationContextUri != null) {
parentApplicationContext = new ClassPathXmlApplicationContext(parentApplicationContextUri);
parentApplicationContext.start();
}
}
return parentApplicationContext;
}
public void setParentApplicationContext(AbstractApplicationContext parentApplicationContext) {
this.parentApplicationContext = parentApplicationContext;
}
public String getParentApplicationContextUri() {
return parentApplicationContextUri;
}
public void setParentApplicationContextUri(String parentApplicationContextUri) {
this.parentApplicationContextUri = parentApplicationContextUri;
}
public boolean isAllowMultipleCamelContexts() {
return allowMultipleCamelContexts;
}
/**
* Enable this to allow multiple CamelContexts to be loaded by this Main class. By default only a single
* CamelContext is allowed.
*/
public void setAllowMultipleCamelContexts(boolean allowMultipleCamelContexts) {
this.allowMultipleCamelContexts = allowMultipleCamelContexts;
}
// Implementation methods
// -------------------------------------------------------------------------
@Override
protected CamelContext createCamelContext() {
Map<String, SpringCamelContext> camels = applicationContext.getBeansOfType(SpringCamelContext.class);
if (camels.size() > 1) {
if (isAllowMultipleCamelContexts()) {
// just grab the first
return camels.values().iterator().next();
}
throw new IllegalArgumentException(
"Multiple CamelContext detected. Set allowMultipleCamelContexts=true to allow multiple CamelContexts");
} else if (camels.size() == 1) {
return camels.values().iterator().next();
}
return null;
}
@Override
protected void doStart() throws Exception {
try {
super.doStart();
if (applicationContext == null) {
applicationContext = createDefaultApplicationContext();
}
// then start any additional after Camel has been started
if (additionalApplicationContext == null) {
additionalApplicationContext = createAdditionalLocationsFromClasspath();
if (additionalApplicationContext != null) {
LOG.debug("Starting Additional ApplicationContext: {}", additionalApplicationContext.getId());
additionalApplicationContext.start();
}
}
LOG.debug("Starting Spring ApplicationContext: {}", applicationContext.getId());
applicationContext.start();
initCamelContext();
} catch (Exception e) {
// if we were veto started then mark as completed
VetoCamelContextStartException veto = ObjectHelper.getException(VetoCamelContextStartException.class, e);
if (veto != null) {
completed();
} else {
throw e;
}
}
}
@Override
protected void doStop() throws Exception {
try {
if (additionalApplicationContext != null) {
LOG.debug("Stopping Additional ApplicationContext: {}", additionalApplicationContext.getId());
additionalApplicationContext.stop();
}
if (applicationContext != null) {
LOG.debug("Stopping Spring ApplicationContext: {}", applicationContext.getId());
applicationContext.stop();
}
IOHelper.close(additionalApplicationContext);
IOHelper.close(applicationContext);
} finally {
super.doStop();
}
}
@Override
protected ProducerTemplate findOrCreateCamelTemplate() {
String[] names = getApplicationContext().getBeanNamesForType(ProducerTemplate.class);
if (names != null && names.length > 0) {
return getApplicationContext().getBean(names[0], ProducerTemplate.class);
}
if (getCamelContext() == null) {
throw new IllegalArgumentException("No CamelContext are available so cannot create a ProducerTemplate!");
}
return getCamelContext().createProducerTemplate();
}
protected AbstractApplicationContext createDefaultApplicationContext() {
ApplicationContext parentContext = getParentApplicationContext();
// file based
if (getFileApplicationContextUri() != null) {
String[] args = getFileApplicationContextUri().split(";");
if (parentContext != null) {
return new FileSystemXmlApplicationContext(args, parentContext);
} else {
return new FileSystemXmlApplicationContext(args);
}
}
// default to classpath based
String[] args = getApplicationContextUri().split(";");
if (parentContext != null) {
return new ClassPathXmlApplicationContext(args, parentContext);
} else {
// okay no application context specified so lets look for either
// classpath xml or annotation based
if (mainConfigurationProperties.getRoutesBuilderClasses() != null) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.register(SpringCamelContext.class);
Set<String> packages = new LinkedHashSet<>();
String[] classes = mainConfigurationProperties.getRoutesBuilderClasses().split(",");
for (String clazz : classes) {
if (clazz.contains(".")) {
String packageName = clazz.substring(0, clazz.lastIndexOf('.'));
packages.add(packageName);
}
}
LOG.info("Using Spring annotation scanning in packages: {}", packages);
ac.scan(packages.toArray(new String[0]));
ac.refresh();
return ac;
} else {
return new ClassPathXmlApplicationContext(args);
}
}
}
protected AbstractApplicationContext createAdditionalLocationsFromClasspath() throws IOException {
Set<String> locations = new LinkedHashSet<>();
findLocations(locations, Main.class.getClassLoader());
if (!locations.isEmpty()) {
LOG.info("Found locations for additional Spring XML files: {}", locations);
String[] locs = locations.toArray(new String[0]);
return new ClassPathXmlApplicationContext(locs);
} else {
return null;
}
}
protected void findLocations(Set<String> locations, ClassLoader classLoader) throws IOException {
Enumeration<URL> resources = classLoader.getResources(LOCATION_PROPERTIES);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
BufferedReader reader = IOHelper.buffered(new InputStreamReader(url.openStream(), UTF8));
try {
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
line = line.trim();
if (line.startsWith("#") || line.isEmpty()) {
continue;
}
locations.add(line);
}
} finally {
IOHelper.close(reader, null, LOG);
}
}
}
}