OSDN Git Service

10d09ba68c938a327f0e27a377ea74abbd05f6a9
[hayashilib/hayashi.git] / src / jp / co / areaweb / tools / gui / Command.java
1 package jp.co.areaweb.tools.gui;
2 import java.text.SimpleDateFormat;
3
4 class Command extends Thread {
5         String[] args;          // コマンドパラメータ
6         private String commandName = "";        // コマンド名
7         @SuppressWarnings({ "rawtypes" })
8         private Class cmd;              // 実行対象インスタンス
9
10         /**
11          * コンストラクタ:実行対象のインスタンスを得る
12          * @param cmd
13          */
14         public Command(Class<?> cmd) {
15                 super();
16                 this.cmd = cmd;
17                 this.commandName = cmd.getName();
18                 this.args = new String[0];
19         }
20
21         /**
22          * コマンドパラメータの設定
23          * @param folder
24          */
25         public void setArgs(String[] args) {
26                 this.args = args;
27         }
28
29         public void setCommandName(String name) {
30                 this.commandName = name;
31         }
32         public String getCommandName() {
33                 return this.commandName;
34         }
35
36         @SuppressWarnings("unchecked")
37         public void run() {
38                 System.out.println("[START:"+ (new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss")).format(new java.util.Date()) +"]\t"+ this.commandName);
39                 for (int i=0; i < args.length; i++) {
40                         System.out.println(" args["+ i +"]: "+ this.args[i]);
41                 }
42                 System.out.println();
43
44                 try {
45                 try {
46                         java.lang.reflect.Method method = this.cmd.getMethod("main", new Class[] {String[].class});
47                         method.setAccessible(true);
48                         method.invoke(null, new Object[]{this.args});
49
50                         System.out.println();
51                         System.out.println("[END:"+ (new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss")).format(new java.util.Date()) +"]\t"+ this.commandName);
52                 }
53                 catch (Exception e) {
54                         System.out.println("[ERR!:"+ (new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss")).format(new java.util.Date()) +"]\t"+ this.commandName);
55                         throw e;
56                 }
57         }
58         catch(Exception e) {
59             e.printStackTrace();
60         }
61                 System.out.println();
62     }
63 }