001: /*
002: * Timer: The timer class
003: * Copyright (C) 2006-2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * Main.java
020: */
021:
022: // package path
023: package com.rift.coad.commandline.timercommandlinetool;
024:
025: // java imports
026: import com.rift.coad.daemon.timer.TimerEvent;
027: import java.io.Serializable;
028: import java.util.ArrayList;
029: import java.util.Hashtable;
030: import java.util.List;
031: import javax.naming.Context;
032: import javax.naming.InitialContext;
033: import javax.rmi.PortableRemoteObject;
034: import com.rift.coad.daemon.timer.Timer;
035:
036: // log4j
037: import org.apache.log4j.BasicConfigurator;
038:
039: /**
040: *
041: * @author Glynn Chaldecott
042: *
043: * This class is a command line tool for the Timer Daemon and gives a user basic
044: * functionality through the command line.
045: */
046: public class Main {
047: public String[] args = null;
048: public String url = "";
049: public String host = "";
050: public String username = "";
051: public String password = "";
052:
053: /** Creates a new instance of Main */
054: public Main() {
055: }
056:
057: /**
058: * @param args the command line arguments
059: */
060: public static void main(String[] args) {
061: BasicConfigurator.configure();
062: Main mainProgram = new Main();
063: mainProgram.setArgs(args);
064: mainProgram.commandLine();
065: }
066:
067: /**
068: * This method simple sets the public args variable.
069: *
070: * @param args This is the args passed into the main method.
071: */
072: public void setArgs(String[] args) {
073: this .args = args;
074: }
075:
076: /**
077: * This method breaks the arguments down and uses them to perform the
078: * necessary task.
079: */
080: public void commandLine() {
081: try {
082: Timer sb = null;
083: int choice = -1;
084: List service = new ArrayList();
085: String jndi = "";
086: Serializable event = null;
087: boolean recure = false;
088: int minute = -1;
089: int hour = -1;
090: int day = -1;
091: int month = -1;
092: int id = 0;
093:
094: for (int i = 0; i < args.length; i++) {
095: if (args[i].equals("-r")) {
096: choice = 0;
097: } else if (args[i].equals("-l")) {
098: choice = 1;
099: } else if (args[i].equals("-d")) {
100: choice = 2;
101: } else if (args[i].equals("-m")) {
102: i++;
103: month = Integer.parseInt(args[i]);
104: } else if (args[i].equals("-da")) {
105: i++;
106: day = Integer.parseInt(args[i]);
107: } else if (args[i].equals("-ho")) {
108: i++;
109: hour = Integer.parseInt(args[i]);
110: } else if (args[i].equals("-mi")) {
111: i++;
112: minute = Integer.parseInt(args[i]);
113: } else if (args[i].equals("-id")) {
114: i++;
115: id = Integer.parseInt(args[i]);
116: } else if (args[i].equals("-j")) {
117: i++;
118: jndi = args[i];
119: } else if (args[i].equals("-e")) {
120: i++;
121: String temp = "";
122: boolean flag = true;
123: while (flag) {
124: String temp2 = args[i];
125: if (temp2.charAt(0) == '-') {
126: flag = false;
127: i--;
128: } else {
129: temp += temp2;
130: i++;
131: }
132: }
133: event = (Serializable) temp;
134: } else if (args[i].equals("-re")) {
135: i++;
136: String temp = args[i];
137: if (temp.toUpperCase().equals("FALSE")
138: || temp.toUpperCase().equals("F")) {
139: recure = false;
140: } else if (temp.toUpperCase().equals("TRUE")
141: || temp.toUpperCase().equals("T")) {
142: recure = true;
143: }
144: } else if (args[i].equals("-h")) {
145: choice = 3;
146: } else if (args[i].equals("-s")) {
147: i++;
148: host = args[i];
149: if (host.indexOf(":") == -1) {
150: host += ":2000";
151: }
152: } else if (args[i].equals("-u")) {
153: i++;
154: url = args[i];
155: } else if (args[i].equals("-U")) {
156: i++;
157: username = args[i];
158: } else if (args[i].equals("-P")) {
159: i++;
160: password = args[i];
161: }
162: }
163: switch (choice) {
164: case 0:
165: if (!url.equals("") && !host.equals("")) {
166: sb = createConnection();
167: sb.register(jndi, month, day, hour, minute, event,
168: recure);
169: System.out
170: .println("The event has been registered.");
171: } else {
172: System.out.println("Statment missing elements.");
173: help();
174: }
175: break;
176: case 1:
177: if (!url.equals("") && !host.equals("")) {
178: sb = createConnection();
179: TimerEvent[] temp = sb.listEvents();
180: for (int i = 0; i < temp.length; i++) {
181: System.out.println(temp[i].getId() + " "
182: + temp[i].getJndi() + " "
183: + temp[i].getMonth() + "-"
184: + temp[i].getDay() + "-"
185: + temp[i].getHour() + "-"
186: + temp[i].getMinute() + " "
187: + temp[i].getEvent());
188: }
189: } else {
190: System.out.println("Statment missing elements.");
191: help();
192: }
193: break;
194: case 2:
195: if (!url.equals("") && !host.equals("")) {
196: sb = createConnection();
197: sb.deleteEvent(id);
198: System.out.println("The event has been deleted.");
199: } else {
200: System.out.println("Statment missing elements.");
201: help();
202: }
203: break;
204: case 3:
205: help();
206: break;
207: default:
208: System.out.println("There was a problem with the "
209: + "statement");
210: help();
211: break;
212: }
213: } catch (Exception ex) {
214: ex.printStackTrace();
215: }
216: }
217:
218: public void help() {
219: System.out.println("Help");
220: System.out
221: .println("\t-r \tThis is to be used to Register an event.");
222: System.out
223: .println("\t-l \tThis is to be used to retrieve a list of "
224: + "events.");
225: System.out
226: .println("\t-d \tThis is to be used to delete an event.");
227: System.out
228: .println("\t-m \tThis is to be followed by the month.");
229: System.out
230: .println("\t-da \tThis is to be followed by the day.");
231: System.out
232: .println("\t-ho \tThis is to be followed by the hour.");
233: System.out
234: .println("\t-mi \tThis is to be followed by the minute.");
235: System.out
236: .println("\t-id \tThis is to be followed by the event id.");
237: System.out
238: .println("\t-j \tThis is to be followed by the JNDI for "
239: + "the service. This can only be called once in a statment.");
240: System.out
241: .println("\t-s \tThis is to be followed by the host and "
242: + "port on which the Timer Daemon is running and is required.");
243: System.out
244: .println("\t-u \tThis is to be followed by the JNDI for "
245: + "the Timer Daemon and is required.");
246: System.out
247: .println("\t-U \tThis must be followed by the username for "
248: + "the connection and is required.");
249: System.out
250: .println("\t-P \tThis must be followed by the password for "
251: + "the connection and is required.");
252: }
253:
254: /**
255: * This method connects to the Timer Daemon and then returns the Object.
256: *
257: * @return This method returns a connection to the Timer Daemon.
258: */
259: public com.rift.coad.daemon.timer.Timer createConnection()
260: throws Exception {
261: try {
262: Hashtable env = new Hashtable();
263: env.put(Context.INITIAL_CONTEXT_FACTORY,
264: "com.rift.coad.client.naming."
265: + "CoadunationInitialContextFactory");
266: env.put(Context.PROVIDER_URL, host);
267: env.put("com.rift.coad.username", username);
268: env.put("com.rift.coad.password", password);
269: Context ctx = new InitialContext(env);
270:
271: Object obj = ctx.lookup(url);
272: com.rift.coad.daemon.timer.Timer beanInterface = (com.rift.coad.daemon.timer.Timer) PortableRemoteObject
273: .narrow(obj, com.rift.coad.daemon.timer.Timer.class);
274:
275: if (beanInterface == null) {
276: throw new Exception("narrow failed.");
277: } else {
278: return beanInterface;
279: }
280: } catch (Exception ex) {
281: System.out.println(ex.getMessage());
282: ex.printStackTrace(System.out);
283: throw new Exception(ex);
284: }
285: }
286:
287: }
|