1 21 22 package com.rift.coad.commandline.timercommandlinetool; 23 24 import com.rift.coad.daemon.timer.TimerEvent; 25 import java.io.Serializable ; 26 import java.util.ArrayList ; 27 import java.util.Hashtable ; 28 import java.util.List ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.rmi.PortableRemoteObject ; 32 import com.rift.coad.daemon.timer.Timer; 33 34 41 public class Main { 42 public String [] args = null; 43 public String url = ""; 44 public String host = ""; 45 public String username = ""; 46 public String password = ""; 47 48 49 public Main() { 50 } 51 52 55 public static void main(String [] args) { 56 Main mainProgram = new Main(); 57 mainProgram.setArgs(args); 58 mainProgram.commandLine(); 59 } 60 65 public void setArgs(String [] args) { 66 this.args = args; 67 } 68 69 73 public void commandLine() { 74 try { 75 Timer sb = null; 76 int choice = -1; 77 List service = new ArrayList (); 78 String jndi = ""; 79 Serializable 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 temp = ""; 115 boolean flag = true; 116 while (flag) { 117 String 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 ) temp; 127 } else if (args[i].equals("-re")) { 128 i ++; 129 String 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 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 230 public com.rift.coad.daemon.timer.Timer createConnection() 231 throws Exception { 232 try { 233 Hashtable env = new Hashtable (); 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 ctx = new InitialContext (env); 241 242 Object 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 ("narrow failed."); 250 } else { 251 return beanInterface; 252 } 253 } catch (Exception ex) { 254 System.out.println(ex.getMessage()); 255 ex.printStackTrace(System.out); 256 throw new Exception (ex); 257 } 258 } 259 260 } 261 | Popular Tags |