01: package org.objectweb.celtix.tools.common.toolspec;
02:
03: import java.io.InputStream;
04: import java.lang.reflect.Constructor;
05: import java.util.logging.Level;
06: import java.util.logging.Logger;
07:
08: import org.objectweb.celtix.common.i18n.Message;
09: import org.objectweb.celtix.common.logging.LogUtils;
10: import org.objectweb.celtix.tools.common.ToolException;
11:
12: public final class ToolRunner {
13: private static final Logger LOG = LogUtils
14: .getL7dLogger(ToolRunner.class);
15:
16: private ToolRunner() {
17: // utility class - never constructed
18: }
19:
20: public static void runTool(Class clz, InputStream toolspecStream,
21: boolean validate, String[] args) throws Exception {
22: runTool(clz, toolspecStream, validate, args, true);
23: }
24:
25: public static void runTool(Class clz, InputStream toolspecStream,
26: boolean validate, String[] args, boolean exitOnFinish)
27: throws Exception {
28:
29: if (ToolContainer.class.isAssignableFrom(clz)) {
30:
31: ToolContainer container = null;
32:
33: try {
34: Constructor cons = clz
35: .getConstructor(new Class[] { ToolSpec.class });
36: container = (ToolContainer) cons
37: .newInstance(new Object[] { new ToolSpec(
38: toolspecStream, validate) });
39: } catch (Exception ex) {
40: Message message = new Message(
41: "CLZ_CANNOT_BE_CONSTRUCTED", LOG, clz.getName());
42: LOG.log(Level.SEVERE, message.toString());
43: throw new ToolException(message, ex);
44: }
45:
46: try {
47: container.setCommandLine(args);
48: container.init();
49: container.execute(exitOnFinish);
50: } catch (Exception ex) {
51: throw ex;
52: }
53: } else {
54: Message message = new Message(
55: "CLZ_SHOULD_IMPLEMENT_INTERFACE", LOG, clz
56: .getName());
57: LOG.log(Level.SEVERE, message.toString());
58: throw new ToolException(message);
59: }
60:
61: }
62:
63: }
|