KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > commandline > timercommandlinetool > Main


1 /*
2  * Timer: The timer class
3  * Copyright (C) 2006-2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Main.java
20  */

21
22 package com.rift.coad.commandline.timercommandlinetool;
23
24 import com.rift.coad.daemon.timer.TimerEvent;
25 import java.io.Serializable JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32 import com.rift.coad.daemon.timer.Timer;
33
34 /**
35  *
36  * @author Glynn Chaldecott
37  *
38  * This class is a command line tool for the Timer Daemon and gives a user basic
39  * functionality through the command line.
40  */

41 public class Main {
42     public String JavaDoc[] args = null;
43     public String JavaDoc url = "";
44     public String JavaDoc host = "";
45     public String JavaDoc username = "";
46     public String JavaDoc password = "";
47     
48     /** Creates a new instance of Main */
49     public Main() {
50     }
51     
52     /**
53      * @param args the command line arguments
54      */

55     public static void main(String JavaDoc[] args) {
56         Main mainProgram = new Main();
57         mainProgram.setArgs(args);
58         mainProgram.commandLine();
59     }
60     /**
61      * This method simple sets the public args variable.
62      *
63      * @param args This is the args passed into the main method.
64      */

65     public void setArgs(String JavaDoc[] args) {
66         this.args = args;
67     }
68     
69     /**
70      * This method breaks the arguments down and uses them to perform the
71      * necessary task.
72      */

73     public void commandLine() {
74         try {
75             Timer sb = null;
76             int choice = -1;
77             List JavaDoc service = new ArrayList JavaDoc();
78             String JavaDoc jndi = "";
79             Serializable JavaDoc event = null;
80             boolean recure = false;
81             int minute = -1;
82             int hour = -1;
83             int day = -1;
84             int month = -1;
85             int id = 0;
86             
87             for (int i = 0; i < args.length; i ++) {
88                 if (args[i].equals("-r")) {
89                     choice = 0;
90                 } else if (args[i].equals("-l")) {
91                     choice = 1;
92                 } else if (args[i].equals("-d")) {
93                     choice = 2;
94                 } else if (args[i].equals("-m")) {
95                     i ++;
96                     month = Integer.parseInt(args[i]);
97                 } else if (args[i].equals("-da")) {
98                     i ++;
99                     day = Integer.parseInt(args[i]);
100                 } else if (args[i].equals("-ho")) {
101                     i ++;
102                     hour = Integer.parseInt(args[i]);
103                 } else if (args[i].equals("-mi")) {
104                     i ++;
105                     minute = Integer.parseInt(args[i]);
106                 } else if (args[i].equals("-id")) {
107                     i ++;
108                     id = Integer.parseInt(args[i]);
109                 } else if (args[i].equals("-j")) {
110                     i ++;
111                     jndi = args[i];
112                 } else if (args[i].equals("-e")) {
113                     i ++;
114                     String JavaDoc temp = "";
115                     boolean flag = true;
116                     while (flag) {
117                         String JavaDoc temp2 = args[i];
118                         if (temp2.charAt(0) == '-') {
119                             flag = false;
120                             i --;
121                         } else {
122                             temp += temp2;
123                             i ++;
124                         }
125                     }
126                     event = (Serializable JavaDoc) temp;
127                 } else if (args[i].equals("-re")) {
128                     i ++;
129                     String JavaDoc temp = args[i];
130                     if (temp.toUpperCase().equals("FALSE") ||
131                             temp.toUpperCase().equals("F")) {
132                         recure = false;
133                     } else if (temp.toUpperCase().equals("TRUE") ||
134                             temp.toUpperCase().equals("T")) {
135                         recure = true;
136                     }
137                 } else if (args[i].equals("-h")) {
138                     choice = 3;
139                 } else if (args[i].equals("-s")) {
140                     i ++;
141                     host = args[i];
142                     if (host.indexOf(":") == -1) {
143                         host += ":2000";
144                     }
145                 } else if (args[i].equals("-u")) {
146                     i ++;
147                     url = args[i];
148                 } else if (args[i].equals("-U")) {
149                     i ++;
150                     username = args[i];
151                 } else if (args[i].equals("-P")) {
152                     i ++;
153                     password = args[i];
154                 }
155             }
156             switch (choice) {
157                 case 0: if (!url.equals("") && !host.equals("")) {
158                             sb = createConnection();
159                             sb.register(jndi,month,day,hour,minute,event,recure);
160                             System.out.println("The event has been registered.");
161                         } else {
162                             System.out.println("Statment missing elements.");
163                             help();
164                         }
165                         break;
166                 case 1: if (!url.equals("") && !host.equals("")) {
167                             sb = createConnection();
168                             TimerEvent[] temp = sb.listEvents();
169                             for (int i = 0; i < temp.length; i ++) {
170                                 System.out.println(temp[i].getId() + " "
171                                         + temp[i].getJndi() + " "
172                                         + temp[i].getMonth() + "-"
173                                         + temp[i].getDay() + "-"
174                                         + temp[i].getHour() + "-"
175                                         + temp[i].getMinute() + " "
176                                         + temp[i].getEvent());
177                             }
178                         } else {
179                             System.out.println("Statment missing elements.");
180                             help();
181                         }
182                         break;
183                 case 2: if (!url.equals("") && !host.equals("")) {
184                             sb = createConnection();
185                             sb.deleteEvent(id);
186                             System.out.println("The event has been deleted.");
187                         } else {
188                             System.out.println("Statment missing elements.");
189                             help();
190                         }
191                         break;
192                 case 3: help();
193                         break;
194                 default: System.out.println("There was a problem with the " +
195                         "statement"); break;
196             }
197         } catch (Exception JavaDoc ex) {
198             ex.printStackTrace();
199         }
200     }
201     
202     public void help() {
203         System.out.println("Help");
204         System.out.println("\t-r \tThis is to be used to Register an event.");
205         System.out.println("\t-l \tThis is to be used to retrieve a list of " +
206                 "events.");
207         System.out.println("\t-d \tThis is to be used to delete an event.");
208         System.out.println("\t-m \tThis is to be followed by the month.");
209         System.out.println("\t-da \tThis is to be followed by the day.");
210         System.out.println("\t-ho \tThis is to be followed by the hour.");
211         System.out.println("\t-mi \tThis is to be followed by the minute.");
212         System.out.println("\t-id \tThis is to be followed by the event id.");
213         System.out.println("\t-j \tThis is to be followed by the JNDI for " +
214                 "the service. This can only be called once in a statment.");
215         System.out.println("\t-s \tThis is to be followed by the host and " +
216                 "port on which the Timer Daemon is running and is required.");
217         System.out.println("\t-u \tThis is to be followed by the JNDI for " +
218                 "the Timer Daemon and is required.");
219         System.out.println("\t-U \tThis must be followed by the username for " +
220                 "the connection and is required.");
221         System.out.println("\t-P \tThis must be followed by the password for " +
222                 "the connection and is required.");
223     }
224     
225     /**
226      * This method connects to the Timer Daemon and then returns the Object.
227      *
228      * @return This method returns a connection to the Timer Daemon.
229      */

230     public com.rift.coad.daemon.timer.Timer createConnection()
231             throws Exception JavaDoc {
232         try {
233             Hashtable JavaDoc env = new Hashtable JavaDoc();
234             env.put(Context.INITIAL_CONTEXT_FACTORY,
235                     "com.rift.coad.client.naming." +
236                     "CoadunationInitialContextFactory");
237             env.put(Context.PROVIDER_URL,host);
238             env.put("com.rift.coad.username",username);
239             env.put("com.rift.coad.password",password);
240             Context JavaDoc ctx = new InitialContext JavaDoc(env);
241             
242             Object JavaDoc obj = ctx.lookup(url);
243             com.rift.coad.daemon.timer.Timer beanInterface =
244                     (com.rift.coad.daemon.timer.Timer)
245                     PortableRemoteObject.narrow(obj,
246                     com.rift.coad.daemon.timer.Timer.class);
247             
248             if (beanInterface == null) {
249                 throw new Exception JavaDoc("narrow failed.");
250             } else {
251                 return beanInterface;
252             }
253         } catch (Exception JavaDoc ex) {
254             System.out.println(ex.getMessage());
255             ex.printStackTrace(System.out);
256             throw new Exception JavaDoc(ex);
257         }
258     }
259     
260 }
261
Popular Tags