OSDN Git Service

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