KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > common > ToolWrapperGenerator


1 package org.objectweb.celtix.tools.common;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5
6 import org.objectweb.celtix.configuration.CommandlineConfiguration;
7
8 /**
9  * A generator which wraps external tools.
10  *
11  * @author codea
12  *
13  */

14 public class ToolWrapperGenerator implements Generator {
15
16     protected final String JavaDoc toolClassName;
17     private final ClassLoader JavaDoc 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 JavaDoc theToolClassName) {
27         this(theToolClassName, ToolWrapperGenerator.class.getClassLoader());
28     }
29
30     /**
31      * construct a generator which delegates to the tool specified by
32      * the class <code>theToolClassName<code> and the tool class is
33      * loaded via the specified classloader
34      *
35      * @param theToolClassName class name of the tool to delegate to
36      * @param theClassLoader the classloader to load the tool class
37      */

38     protected ToolWrapperGenerator(String JavaDoc theToolClassName, ClassLoader JavaDoc theClassLoader) {
39         classLoader = theClassLoader;
40         toolClassName = theToolClassName;
41     }
42     
43     /** invoked main method of tool class, passing in required arguments
44      *
45      */

46     public void generate() {
47         try {
48             Class JavaDoc<?> toolClass = Class.forName(toolClassName, true, classLoader);
49             Method JavaDoc mainMethod = toolClass.getMethod("main", String JavaDoc[].class);
50             mainMethod.invoke(null, (Object JavaDoc)getToolArguments());
51         } catch (IllegalAccessException JavaDoc ex) {
52             ex.printStackTrace();
53         } catch (InvocationTargetException JavaDoc ex) {
54             if (ex.getTargetException() != null) {
55                 ex.getTargetException().printStackTrace();
56             }
57         } catch (NoSuchMethodException JavaDoc ex) {
58             ex.printStackTrace();
59         } catch (ClassNotFoundException JavaDoc ex) {
60             ex.printStackTrace();
61         }
62     }
63
64     public String JavaDoc getToolClassName() {
65         return toolClassName;
66     }
67     
68     
69     private String JavaDoc[] getToolArguments() {
70         // build up array of arguments based on the command line
71
// that have been given. For now, just grab the arguments
72
// that were passed in on the command line
73
if (config != null) {
74             return ((ToolConfig)config).getOriginalArgs();
75         } else {
76             return new String JavaDoc[0];
77         }
78     }
79
80     public void setConfiguration(CommandlineConfiguration newConfig) {
81         this.config = newConfig;
82     }
83
84 }
85
86
Popular Tags