01: package org.objectweb.celtix.tools.common;
02:
03: import java.lang.reflect.InvocationTargetException;
04: import java.lang.reflect.Method;
05:
06: import org.objectweb.celtix.configuration.CommandlineConfiguration;
07:
08: /**
09: * A generator which wraps external tools.
10: *
11: * @author codea
12: *
13: */
14: public class ToolWrapperGenerator implements Generator {
15:
16: protected final String toolClassName;
17: private final ClassLoader classLoader;
18: private CommandlineConfiguration config;
19:
20: /**
21: * construct a generator which delegates to the tool specified by
22: * the class <code>theToolClassName<code>
23: *
24: * @param theToolClassName class name of the tool to delegate to
25: */
26: protected ToolWrapperGenerator(String theToolClassName) {
27: this (theToolClassName, ToolWrapperGenerator.class
28: .getClassLoader());
29: }
30:
31: /**
32: * construct a generator which delegates to the tool specified by
33: * the class <code>theToolClassName<code> and the tool class is
34: * loaded via the specified classloader
35: *
36: * @param theToolClassName class name of the tool to delegate to
37: * @param theClassLoader the classloader to load the tool class
38: */
39: protected ToolWrapperGenerator(String theToolClassName,
40: ClassLoader theClassLoader) {
41: classLoader = theClassLoader;
42: toolClassName = theToolClassName;
43: }
44:
45: /** invoked main method of tool class, passing in required arguments
46: *
47: */
48: public void generate() {
49: try {
50: Class<?> toolClass = Class.forName(toolClassName, true,
51: classLoader);
52: Method mainMethod = toolClass.getMethod("main",
53: String[].class);
54: mainMethod.invoke(null, (Object) getToolArguments());
55: } catch (IllegalAccessException ex) {
56: ex.printStackTrace();
57: } catch (InvocationTargetException ex) {
58: if (ex.getTargetException() != null) {
59: ex.getTargetException().printStackTrace();
60: }
61: } catch (NoSuchMethodException ex) {
62: ex.printStackTrace();
63: } catch (ClassNotFoundException ex) {
64: ex.printStackTrace();
65: }
66: }
67:
68: public String getToolClassName() {
69: return toolClassName;
70: }
71:
72: private String[] getToolArguments() {
73: // build up array of arguments based on the command line
74: // that have been given. For now, just grab the arguments
75: // that were passed in on the command line
76: if (config != null) {
77: return ((ToolConfig) config).getOriginalArgs();
78: } else {
79: return new String[0];
80: }
81: }
82:
83: public void setConfiguration(CommandlineConfiguration newConfig) {
84: this.config = newConfig;
85: }
86:
87: }
|