View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.xtcl.Context;
4   
5   /***
6    * Executes an executable Java class (i.e. with main method)
7    * 
8    * @author tomp
9    */
10  public class JavaCommand extends AbstractCommand {
11  	
12      /*** the Java class name */
13      protected String className;
14  
15      /*** the Java class arguments */
16      protected String[] args;
17  
18      /***
19       * sets the args
20       * 
21       * @param r the Java class args
22       */
23      public void setArgs(String[] r) {
24          args = r;
25      }
26  
27      /***
28       * the executed Java class name
29       * 
30       * @param r the executed Java class name
31       */
32      public void setClassName(String r) {
33          className = r;
34      }
35  
36      /***
37       * Executes the commmand, i.e. the Java class with args
38       * 
39       */
40      public int execute(Context c) throws Exception {
41          int retCode = 0;
42  
43          try {
44              Class toBeExecuted = Class.forName(className);
45              java.lang.reflect.Method main = toBeExecuted.getMethod("main",
46                      new Class[] { new String[0].getClass() });
47  
48              main.invoke(null, new Object[] { args });
49              
50          } catch (Exception e) {
51              e.printStackTrace();
52              retCode = 1;
53          }
54  
55          return done(c, retCode);
56      }
57  
58      /***
59       * @return "JAVA " + className + "(" + listArray(args) + ")";
60       */
61      public String toString() {
62          return "JAVA " + className + "(" + listArray(args) + ")";
63      }
64  }
65  
66  /*
67   * The contents of this file are subject to the Mozilla Public License Version
68   * 1.1 (the "License"); you may not use this file except in compliance with the
69   * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
70   * Software distributed under the License is distributed on an "AS IS" basis,
71   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
72   * the specific language governing rights and limitations under the License. The
73   * Original Code is: all this file. The Initial Developer of the Original Code
74   * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
75   */