KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * MessageServiceCommandLine: The message service command line tool.
3  * Copyright (C) 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 path
23
package com.rift.coad.commandline.messageservicecommandlinetool;
24
25
26 // java imports
27
import java.io.*;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36
37 // message service imports
38
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 /**
47  * This object is responsible for suppling a command line tool to manage the
48  * message service.
49  *
50  * @author Brett Chaldecott
51  */

52 public class Main {
53     
54     // private member variables
55
private String JavaDoc url = null;
56     private String JavaDoc host = null;
57     private String JavaDoc username = null;
58     private String JavaDoc password = null;
59     
60     
61     /**
62      * The constructor of the main class.
63      */

64     public Main() {
65         
66     }
67     
68     
69     /**
70      * This method breaks the arguments down and uses them to perform the
71      * necessary task.
72      */

73     public void commandLine(String JavaDoc[] args) {
74         try {
75             String JavaDoc 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 JavaDoc(
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 JavaDoc(
110                                 "Please supply a queue name when using the pq " +
111                                 "option.");
112                     }
113                     queue = args[i];
114                 }
115             }
116             // check the results of the command line parsing.
117
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 JavaDoc 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 JavaDoc iter = entries.iterator(); iter.hasNext();) {
136                         System.out.println("\t" + (String JavaDoc)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 JavaDoc 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 JavaDoc errors = message.getErrors();
166                         int errorCount = 1;
167                         SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc(
168                                 "yyyy-MM-dd hh:mm:ss");
169                         for (Iterator JavaDoc 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 JavaDoc ex) {
189             System.out.println("Failed to manage the message service : " +
190                     ex.getMessage());
191             ex.printStackTrace();
192             help();
193         }
194     }
195     
196     
197     /**
198      * This method prints out the help.
199      */

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     /**
214      * This method connects to the Timer Daemon and then returns the Object.
215      *
216      * @return This method returns a connection to the Timer Daemon.
217      */

218     public MessageService createConnection()
219     throws Exception JavaDoc {
220         try {
221             Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc ctx = new InitialContext JavaDoc(env);
229             
230             Object JavaDoc 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 JavaDoc("narrow failed.");
238             } else {
239                 return beanInterface;
240             }
241         } catch (Exception JavaDoc ex) {
242             System.out.println(ex.getMessage());
243             ex.printStackTrace(System.out);
244             throw new Exception JavaDoc(ex);
245         }
246     }
247     
248     
249     /**
250      * @param args the command line arguments
251      */

252     public static void main(String JavaDoc[] args) throws MessageServiceException {
253         Main mainProgram = new Main();
254         mainProgram.commandLine(args);
255     }
256     
257     
258     
259 }
260
Popular Tags