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