View Javadoc

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