001: /*
002: * MessageServiceCommandLine: The message service command line tool.
003: * Copyright (C) 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.messageservicecommandlinetool;
024:
025: // java imports
026: import java.io.*;
027: import java.text.SimpleDateFormat;
028: import java.util.ArrayList;
029: import java.util.Hashtable;
030: import java.util.List;
031: import java.util.Iterator;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.rmi.PortableRemoteObject;
035:
036: // log4j
037: import org.apache.log4j.BasicConfigurator;
038:
039: // message service imports
040: import com.rift.coad.daemon.messageservice.Message;
041: import com.rift.coad.daemon.messageservice.MessageError;
042: import com.rift.coad.daemon.messageservice.MessageServiceManagerMBean;
043: import com.rift.coad.daemon.messageservice.MessageServiceException;
044: import com.rift.coad.daemon.messageservice.RPCMessage;
045: import com.rift.coad.daemon.messageservice.TextMessage;
046:
047: /**
048: * This object is responsible for suppling a command line tool to manage the
049: * message service.
050: *
051: * @author Brett Chaldecott
052: */
053: public class Main {
054:
055: // private member variables
056: private String url = null;
057: private String host = null;
058: private String username = null;
059: private String password = null;
060:
061: /**
062: * The constructor of the main class.
063: */
064: public Main() {
065:
066: }
067:
068: /**
069: * This method breaks the arguments down and uses them to perform the
070: * necessary task.
071: */
072: public void commandLine(String[] args) {
073: try {
074: String queue = "";
075: int choice = -1;
076: for (int i = 0; i < args.length; i++) {
077: if (args[i].equals("-h")) {
078: help();
079: return;
080: } else if (args[i].equals("-u")) {
081: i++;
082: url = args[i];
083: } else if (args[i].equals("-U")) {
084: i++;
085: username = args[i];
086: } else if (args[i].equals("-P")) {
087: i++;
088: password = args[i];
089: } else if (args[i].equals("-s")) {
090: i++;
091: host = args[i];
092: if (host.indexOf(":") == -1) {
093: host += ":2000";
094: }
095: } else if (args[i].equals("-l")) {
096: choice = 1;
097: } else if (args[i].equals("-lq")) {
098: choice = 2;
099: if (++i >= args.length) {
100: throw new Exception(
101: "Please supply a queue name when using the lq "
102: + "option.");
103: }
104: queue = args[i];
105: } else if (args[i].equals("-pq")) {
106: choice = 3;
107: if (++i >= args.length) {
108: throw new Exception(
109: "Please supply a queue name when using the pq "
110: + "option.");
111: }
112: queue = args[i];
113: }
114: }
115: // check the results of the command line parsing.
116: if ((url == null) || (username == null)
117: || (password == null) || (host == null)
118: || (choice == -1)) {
119: System.out
120: .println("Invalid data supplied please use the "
121: + "following arguments");
122: help();
123: return;
124: }
125:
126: MessageServiceManagerMBean ms = null;
127: List entries = null;
128: switch (choice) {
129: case 0:
130: help();
131: break;
132: case 1:
133: ms = createConnection();
134: entries = ms.listNamedQueues();
135: System.out.println("List of named queues ["
136: + entries.size() + "]");
137: for (Iterator iter = entries.iterator(); iter.hasNext();) {
138: System.out.println("\t" + (String) iter.next());
139: }
140:
141: break;
142: case 2:
143: ms = createConnection();
144: entries = ms.listMessagesForNamedQueue(queue);
145: System.out.println("Queue entries for : " + queue);
146: for (Iterator iter = entries.iterator(); iter.hasNext();) {
147: Message message = (Message) iter.next();
148: System.out.println("\tMessage:"
149: + message.getMessageId());
150: if (message instanceof RPCMessage) {
151: System.out.println("Type: RPC");
152: System.out.println("\tBody:"
153: + ((RPCMessage) message)
154: .getMethodBodyXML());
155: } else {
156: System.out.println("Type: Text");
157: System.out
158: .println("\tBody:"
159: + ((TextMessage) message)
160: .getTextBody());
161: }
162: if (message.getMessageType() == Message.POINT_TO_POINT) {
163: System.out.println("\tRouting: Point to Point");
164: } else if (message.getMessageType() == Message.POINT_TO_SERVICE) {
165: System.out
166: .println("\ttRouting: Point to Service");
167: } else if (message.getMessageType() == Message.POINT_TO_MULTI_SERVICE) {
168: System.out
169: .println("\ttRouting: Point to Multi "
170: + "Service");
171: }
172: List errors = message.getErrors();
173: int errorCount = 1;
174: SimpleDateFormat dateFormat = new SimpleDateFormat(
175: "yyyy-MM-dd hh:mm:ss");
176: for (Iterator errorIter = errors.iterator(); errorIter
177: .hasNext();) {
178: MessageError error = (MessageError) errorIter
179: .next();
180: System.out.println(dateFormat.format(error
181: .getErrorDate())
182: + " ["
183: + error.getLevel()
184: + "] error :"
185: + error.getMSG());
186: }
187: }
188: break;
189: case 3:
190: ms = createConnection();
191: ms.purgeNamedQueue(queue);
192: break;
193: default:
194: System.out.println("Invalid arguments");
195: help();
196: break;
197: }
198: } catch (Exception ex) {
199: ex.printStackTrace();
200: System.out
201: .println("Failed to manage the message service : "
202: + ex.getMessage());
203: help();
204: }
205: }
206:
207: /**
208: * This method prints out the help.
209: */
210: public void help() {
211: System.out.println("Help");
212: System.out.println("\t-u\tJNDI url of message service.");
213: System.out
214: .println("\t-s\tThe server the coadunation instance runs on.");
215: System.out.println("\t\t\thostname:port");
216: System.out.println("\t-U\tUsername for connection");
217: System.out.println("\t-P\tPassword for the connection.");
218: System.out.println("\t-l\tList of the named queues in uses");
219: System.out.println("\t-lq\tList entries for the named queue");
220: System.out.println("\t-pq\tPurge the entries from the queue");
221: }
222:
223: /**
224: * This method connects to the Timer Daemon and then returns the Object.
225: *
226: * @return This method returns a connection to the Timer Daemon.
227: */
228: public MessageServiceManagerMBean createConnection()
229: throws Exception {
230: try {
231: Hashtable env = new Hashtable();
232: env.put(Context.INITIAL_CONTEXT_FACTORY,
233: "com.rift.coad.client.naming."
234: + "CoadunationInitialContextFactory");
235: env.put(Context.PROVIDER_URL, host);
236: env.put("com.rift.coad.username", username);
237: env.put("com.rift.coad.password", password);
238: Context ctx = new InitialContext(env);
239:
240: Object obj = ctx.lookup(url);
241: MessageServiceManagerMBean beanInterface = (MessageServiceManagerMBean) PortableRemoteObject
242: .narrow(obj, MessageServiceManagerMBean.class);
243:
244: if (beanInterface == null) {
245: throw new Exception("narrow failed.");
246: } else {
247: return beanInterface;
248: }
249: } catch (Exception ex) {
250: ex.printStackTrace(System.out);
251: System.out
252: .println("Failed to connect to the message service : "
253: + ex.getMessage());
254: System.exit(-1);
255: return null;
256: }
257: }
258:
259: /**
260: * @param args the command line arguments
261: */
262: public static void main(String[] args)
263: throws MessageServiceException {
264: BasicConfigurator.configure();
265: Main mainProgram = new Main();
266: mainProgram.commandLine(args);
267: }
268:
269: }
|