1 21 22 package com.rift.coad.commandline.messageservicecommandlinetool; 24 25 26 import java.io.*; 28 import java.text.SimpleDateFormat ; 29 import java.util.ArrayList ; 30 import java.util.Hashtable ; 31 import java.util.List ; 32 import java.util.Iterator ; 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.rmi.PortableRemoteObject ; 36 37 import com.rift.coad.daemon.messageservice.Message; 39 import com.rift.coad.daemon.messageservice.MessageError; 40 import com.rift.coad.daemon.messageservice.MessageService; 41 import com.rift.coad.daemon.messageservice.MessageServiceException; 42 import com.rift.coad.daemon.messageservice.RPCMessage; 43 import com.rift.coad.daemon.messageservice.TextMessage; 44 45 46 52 public class Main { 53 54 private String url = null; 56 private String host = null; 57 private String username = null; 58 private String password = null; 59 60 61 64 public Main() { 65 66 } 67 68 69 73 public void commandLine(String [] args) { 74 try { 75 String queue = ""; 76 int choice = -1; 77 for (int i = 0; i < args.length; i ++) { 78 if (args[i].equals("-h")) { 79 help(); 80 return; 81 } else if (args[i].equals("-u")) { 82 i++; 83 url = args[i]; 84 } else if (args[i].equals("-U")) { 85 i++; 86 username = args[i]; 87 } else if (args[i].equals("-P")) { 88 i++; 89 password = args[i]; 90 } else if (args[i].equals("-s")) { 91 i ++; 92 host = args[i]; 93 if (host.indexOf(":") == -1) { 94 host += ":2000"; 95 } 96 } else if (args[i].equals("-l")) { 97 choice = 1; 98 } else if (args[i].equals("-lq")) { 99 choice = 2; 100 if (++i >= args.length) { 101 throw new Exception ( 102 "Please supply a queue name when using the lq " + 103 "option."); 104 } 105 queue = args[i]; 106 } else if (args[i].equals("-pq")) { 107 choice = 3; 108 if (++i >= args.length) { 109 throw new Exception ( 110 "Please supply a queue name when using the pq " + 111 "option."); 112 } 113 queue = args[i]; 114 } 115 } 116 if ((url == null) || (username == null) || (password == null) || 118 (host == null) || (choice == -1)) { 119 System.out.println("Invalid data supplied please use the " + 120 "following arguments"); 121 help(); 122 return; 123 } 124 125 MessageService ms = null; 126 List entries = null; 127 switch (choice) { 128 case 0: 129 help(); 130 break; 131 case 1: 132 ms = createConnection(); 133 entries = ms.listNamedQueues(); 134 System.out.println("List of named queues"); 135 for (Iterator iter = entries.iterator(); iter.hasNext();) { 136 System.out.println("\t" + (String )iter.next()); 137 } 138 break; 139 case 2: 140 ms = createConnection(); 141 entries = ms.listMessagesForNamedQueue(queue); 142 System.out.println("Queue entries for : " + queue); 143 for (Iterator iter = entries.iterator(); iter.hasNext();) { 144 Message message = (Message)iter.next(); 145 System.out.println("\tMessage:" + message.getMessageId()); 146 if (message instanceof RPCMessage){ 147 System.out.println("Type: RPC"); 148 System.out.println("\tBody:" + 149 ((RPCMessage)message).getMethodBodyXML()); 150 } else { 151 System.out.println("Type: Text"); 152 System.out.println("\tBody:" + 153 ((TextMessage)message).getTextBody()); 154 } 155 if (message.getMessageType() == Message.POINT_TO_POINT){ 156 System.out.println("\tRouting: Point to Point"); 157 } else if (message.getMessageType() == 158 Message.POINT_TO_SERVICE) { 159 System.out.println("\ttRouting: Point to Service"); 160 } else if (message.getMessageType() == 161 Message.POINT_TO_MULTI_SERVICE) { 162 System.out.println("\ttRouting: Point to Multi " + 163 "Service"); 164 } 165 List errors = message.getErrors(); 166 int errorCount = 1; 167 SimpleDateFormat dateFormat = new SimpleDateFormat ( 168 "yyyy-MM-dd hh:mm:ss"); 169 for (Iterator errorIter = errors.iterator(); 170 errorIter.hasNext();) { 171 MessageError error = (MessageError)errorIter.next(); 172 System.out.println(dateFormat.format( 173 error.getErrorDate()) + " [" + 174 error.getLevel() +"] error :"+ 175 error.getMSG()); 176 } 177 } 178 break; 179 case 3: 180 ms = createConnection(); 181 ms.purgeNamedQueue(queue); 182 break; 183 default: 184 System.out.println("Invalid arguments"); 185 help(); 186 break; 187 } 188 } catch (Exception ex) { 189 System.out.println("Failed to manage the message service : " + 190 ex.getMessage()); 191 ex.printStackTrace(); 192 help(); 193 } 194 } 195 196 197 200 public void help() { 201 System.out.println("Help"); 202 System.out.println("\t-u\tJNDI url of message service."); 203 System.out.println("\t-s\tThe server the coadunation instance runs on."); 204 System.out.println("\t\t\thostname:port"); 205 System.out.println("\t-U\tUsername for connection"); 206 System.out.println("\t-P\tPassword for the connection."); 207 System.out.println("\t-l\tList of the named queues in uses"); 208 System.out.println("\t-lq\tList entries for the named queue"); 209 System.out.println("\t-pq\tPurge the entries from the queue"); 210 } 211 212 213 218 public MessageService createConnection() 219 throws Exception { 220 try { 221 Hashtable env = new Hashtable (); 222 env.put(Context.INITIAL_CONTEXT_FACTORY, 223 "com.rift.coad.client.naming." + 224 "CoadunationInitialContextFactory"); 225 env.put(Context.PROVIDER_URL,host); 226 env.put("com.rift.coad.username",username); 227 env.put("com.rift.coad.password",password); 228 Context ctx = new InitialContext (env); 229 230 Object obj = ctx.lookup(url); 231 MessageService beanInterface = 232 (MessageService) 233 PortableRemoteObject.narrow(obj, 234 MessageService.class); 235 236 if (beanInterface == null) { 237 throw new Exception ("narrow failed."); 238 } else { 239 return beanInterface; 240 } 241 } catch (Exception ex) { 242 System.out.println(ex.getMessage()); 243 ex.printStackTrace(System.out); 244 throw new Exception (ex); 245 } 246 } 247 248 249 252 public static void main(String [] args) throws MessageServiceException { 253 Main mainProgram = new Main(); 254 mainProgram.commandLine(args); 255 } 256 257 258 259 } 260 | Popular Tags |