KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > admin > AdminModule


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 Bull SA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (Bull SA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  * Benoit Pelletier (Bull SA)
24  */

25 package org.objectweb.joram.client.jms.admin;
26
27 import java.io.Reader JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.net.ConnectException JavaDoc;
35 import java.net.UnknownHostException JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 import javax.jms.*;
43
44 import org.objectweb.joram.client.jms.Destination;
45 import org.objectweb.joram.client.jms.Queue;
46 import org.objectweb.joram.client.jms.Topic;
47 import org.objectweb.joram.client.jms.TopicConnectionFactory;
48 import org.objectweb.joram.client.jms.Message;
49 import org.objectweb.joram.client.jms.ha.local.TopicHALocalConnectionFactory;
50 import org.objectweb.joram.client.jms.ha.tcp.TopicHATcpConnectionFactory;
51 import org.objectweb.joram.client.jms.local.TopicLocalConnectionFactory;
52 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory;
53 import org.objectweb.joram.shared.admin.*;
54
55 import org.objectweb.joram.shared.JoramTracing;
56 import org.objectweb.util.monolog.api.BasicLevel;
57
58 /**
59  * The <code>AdminModule</code> class allows to set an administrator
60  * connection to a given JORAM server, and provides administration and
61  * monitoring methods at a server/platform level.
62  */

63 public class AdminModule {
64   public static final String JavaDoc ADM_NAME_PROPERTY = "JoramAdminXML";
65   public static final String JavaDoc DEFAULT_ADM_NAME = "default";
66
67   public static final String JavaDoc REQUEST_TIMEOUT_PROP =
68       "org.objectweb.joram.client.jms.admin.requestTimeout";
69
70   public static final long DEFAULT_REQUEST_TIMEOUT = 120000;
71
72   /** The identifier of the server the module is connected to. */
73   private static int localServer;
74   /** The host name or IP address this client is connected to. */
75   protected static String JavaDoc localHost;
76   /** The port number of the client connection. */
77   protected static int localPort;
78
79   /** The connection used to link the administrator and the platform. */
80   private static TopicConnection cnx = null;
81
82   /** The requestor for sending the synchronous requests. */
83   private static AdminRequestor requestor;
84
85   /** ObjectMessage sent to the platform. */
86   private static ObjectMessage requestMsg;
87   /** ObjectMessage received from the platform. */
88   private static ObjectMessage replyMsg;
89
90   /** Reply object received from the platform. */
91   protected static AdminReply reply;
92
93   private static int requestCounter;
94
95   private static long requestTimeout =
96       Long.getLong(REQUEST_TIMEOUT_PROP,
97                    DEFAULT_REQUEST_TIMEOUT).longValue();
98
99   /** <code>true</code> if the underlying a JORAM HA server is defined */
100   private static boolean isHa = false;
101
102   /**
103    * This method execute the XML script file that the path is given in
104    * parameter.
105    *
106    * @since 4.3.12
107    */

108   public static void main(String JavaDoc[] args) {
109     try {
110       AdminModule.executeXMLAdmin(args[0]);
111     } catch (Exception JavaDoc exc) {
112       exc.printStackTrace();
113     }
114   }
115
116   /**
117    * Opens a connection dedicated to administering with the Joram server
118    * which parameters are wrapped by a given
119    * <code>TopicConnectionFactory</code>.
120    *
121    * @param cnxFact The TopicConnectionFactory to use for connecting.
122    * @param name Administrator's name.
123    * @param password Administrator's password.
124    *
125    * @exception ConnectException If connecting fails.
126    * @exception AdminException If the administrator identification is
127    * incorrect.
128    */

129   public static void connect(javax.jms.TopicConnectionFactory JavaDoc cnxFact,
130                              String JavaDoc name,
131                              String JavaDoc password)
132     throws ConnectException JavaDoc, AdminException {
133     if (cnx != null)
134       return;
135
136     try {
137       cnx = cnxFact.createTopicConnection(name, password);
138       requestor = new AdminRequestor(cnx);
139
140       cnx.start();
141
142       org.objectweb.joram.client.jms.FactoryParameters params = null;
143
144       if (cnxFact instanceof javax.jms.XATopicConnectionFactory JavaDoc)
145         params = ((org.objectweb.joram.client.jms.XAConnectionFactory)
146                   cnxFact).getParameters();
147       else
148         params = ((org.objectweb.joram.client.jms.ConnectionFactory)
149                   cnxFact).getParameters();
150
151       localHost = params.getHost();
152       localPort = params.getPort();
153
154       // Getting the id of the local server:
155
localServer = requestor.getLocalServerId();
156     } catch (JMSSecurityException exc) {
157       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
158         JoramTracing.dbgClient.log(
159           BasicLevel.DEBUG, "", exc);
160       throw new AdminException(exc.getMessage());
161     } catch (JMSException exc) {
162       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
163         JoramTracing.dbgClient.log(
164           BasicLevel.DEBUG, "", exc);
165       throw new ConnectException JavaDoc("Connecting failed: " + exc);
166     }
167   }
168
169   /**
170    * Opens a TCP connection with the Joram server running on a given host and
171    * listening to a given port.
172    *
173    * @param host The name or IP address of the host the server is running on.
174    * @param port The number of the port the server is listening to.
175    * @param name Administrator's name.
176    * @param password Administrator's password.
177    * @param cnxTimer Timer in seconds during which connecting to the server
178    * is attempted.
179    *
180    * @exception UnknownHostException If the host is invalid.
181    * @exception ConnectException If connecting fails.
182    * @exception AdminException If the administrator identification is
183    * incorrect.
184    */

185   public static void connect(String JavaDoc hostName,
186                              int port,
187                              String JavaDoc name,
188                              String JavaDoc password,
189                              int cnxTimer)
190     throws UnknownHostException JavaDoc, ConnectException JavaDoc, AdminException {
191     connect(hostName,port,name,password,cnxTimer,
192             "org.objectweb.joram.client.jms.tcp.ReliableTcpClient");
193   }
194
195   /**
196    * Opens a TCP connection with the Joram server running on a given host and
197    * listening to a given port.
198    *
199    * @param host The name or IP address of the host the server is running on.
200    * @param port The number of the port the server is listening to.
201    * @param name Administrator's name.
202    * @param password Administrator's password.
203    * @param cnxTimer Timer in seconds during which connecting to the server
204    * is attempted.
205    * @param reliableClass Reliable class name.
206    *
207    * @exception UnknownHostException If the host is invalid.
208    * @exception ConnectException If connecting fails.
209    * @exception AdminException If the administrator identification is
210    * incorrect.
211    */

212   public static void connect(String JavaDoc hostName,
213                              int port,
214                              String JavaDoc name,
215                              String JavaDoc password,
216                              int cnxTimer,
217                              String JavaDoc reliableClass)
218     throws UnknownHostException JavaDoc, ConnectException JavaDoc, AdminException {
219     javax.jms.TopicConnectionFactory JavaDoc cnxFact =null;
220
221     if (isHa) {
222       String JavaDoc urlHa = "hajoram://" + hostName + ":" + port;
223       cnxFact = TopicHATcpConnectionFactory.create(urlHa);
224
225     } else {
226       cnxFact = TopicTcpConnectionFactory.create(hostName, port, reliableClass);
227     }
228
229     ((org.objectweb.joram.client.jms.ConnectionFactory)
230      cnxFact).getParameters().connectingTimer = cnxTimer;
231
232     connect(cnxFact, name, password);
233   }
234
235   /**
236    * Opens a TCP connection with the Joram server running on the default
237    * "locahost" host and listening to the default 16010 port.
238    *
239    * @param name Administrator's name.
240    * @param password Administrator's password.
241    * @param cnxTimer Timer in seconds during which connecting to the server
242    * is attempted.
243    *
244    * @exception UnknownHostException Never thrown.
245    * @exception ConnectException If connecting fails.
246    * @exception AdminException If the administrator identification is
247    * incorrect.
248    */

249   public static void connect(String JavaDoc name, String JavaDoc password, int cnxTimer)
250     throws UnknownHostException JavaDoc, ConnectException JavaDoc, AdminException {
251       connect("localhost", 16010, name, password, cnxTimer);
252     }
253
254   /**
255    * Opens a TCP connection with the Joram server running on the default
256    * "locahost" host and listening to the default 16010 port.
257    *
258    * @param name Administrator's name.
259    * @param password Administrator's password.
260    * @param cnxTimer Timer in seconds during which connecting to the server
261    * is attempted.
262    * @param reliableClass Reliable class name.
263    *
264    * @exception UnknownHostException Never thrown.
265    * @exception ConnectException If connecting fails.
266    * @exception AdminException If the administrator identification is
267    * incorrect.
268    */

269   public static void connect(String JavaDoc name,
270                              String JavaDoc password,
271                              int cnxTimer,
272                              String JavaDoc reliableClass)
273     throws UnknownHostException JavaDoc, ConnectException JavaDoc, AdminException {
274     connect("localhost", 16010, name, password, cnxTimer, reliableClass);
275   }
276
277   /**
278    * Opens a connection with the collocated JORAM server.
279    *
280    * @param name Administrator's name.
281    * @param password Administrator's password.
282    *
283    * @exception ConnectException If connecting fails.
284    * @exception AdminException If the administrator identification is
285    * incorrect.
286    */

287   public static void collocatedConnect(String JavaDoc name, String JavaDoc password)
288          throws ConnectException JavaDoc, AdminException {
289     JoramTracing.dbgClient.log(BasicLevel.DEBUG, "isHa=" + isHa);
290     if (isHa) {
291       connect(TopicHALocalConnectionFactory.create(), name, password);
292     } else {
293       connect(TopicLocalConnectionFactory.create(), name, password);
294     }
295   }
296
297   /** Closes the administration connection. */
298   public static void disconnect() {
299     try {
300       if (cnx == null) return;
301       
302       cnx.close();
303     } catch (JMSException exc) {}
304
305     cnx = null;
306   }
307
308   /**
309    * Stops a given server of the platform.
310    * <p>
311    * The request fails if the target server does not belong to the platform.
312    *
313    * @param serverId Identifier of the server to stop.
314    *
315    * @exception ConnectException If the connection fails.
316    * @exception AdminException If the request fails.
317    */

318   public static void stopServer(int serverId)
319     throws ConnectException JavaDoc, AdminException {
320     try {
321       doRequest(new StopServerRequest(serverId));
322
323       if (serverId == localServer)
324         cnx = null;
325     } catch (ConnectException JavaDoc exc) {
326       // ConnectException is intercepted if stopped server is local server.
327
if (serverId != localServer) throw exc;
328
329       cnx = null;
330     }
331   }
332
333   /**
334    * Stops the platform local server.
335    *
336    * @exception ConnectException If the connection fails.
337    * @exception AdminException Never thrown.
338    */

339   public static void stopServer() throws ConnectException JavaDoc, AdminException {
340     stopServer(localServer);
341   }
342
343   /**
344    * Adds a server to the platform.
345    *
346    * @param serverId Id of the added server
347    * @param hostName Address of the host where the added server is started
348    * @param domainName Name of the domain where the server is added
349    * @param port Listening port of the server in the specified domain
350    * @param serverName Name of the added server
351    *
352    * @exception ConnectException If the connection fails.
353    * @exception AdminException If the request fails.
354    */

355   public static void addServer(int sid,
356                                String JavaDoc hostName,
357                                String JavaDoc domainName,
358                                int port,
359                                String JavaDoc serverName)
360     throws ConnectException JavaDoc, AdminException {
361     addServer(sid,
362               hostName, domainName, port, serverName,
363               null, null);
364   }
365
366   /**
367    * Adds a server to the platform.
368    *
369    * @param serverId Id of the added server
370    * @param hostName Address of the host where the added server is started
371    * @param domainName Name of the domain where the server is added
372    * @param port Listening port of the server in the specified domain
373    * @param serverName Name of the added server
374    * @param serviceNames Names of the service to start within the server
375    * @param args Services' arguments
376    *
377    * @exception ConnectException If the connection fails.
378    * @exception AdminException If the request fails.
379    */

380   public static void addServer(int sid,
381                                String JavaDoc hostName,
382                                String JavaDoc domainName,
383                                int port,
384                                String JavaDoc serverName,
385                                String JavaDoc[] serviceNames,
386                                String JavaDoc[] serviceArgs)
387     throws ConnectException JavaDoc, AdminException {
388     if (serviceNames != null &&
389         serviceArgs != null) {
390       if (serviceNames.length != serviceArgs.length)
391         throw new AdminException(
392           "Same number of service names and arguments expected");
393     } else {
394       if (serviceNames == null) throw new AdminException(
395         "Expected service names");
396       if (serviceArgs == null) throw new AdminException(
397         "Expected service arguments");
398     }
399     doRequest(new AddServerRequest(
400                 sid,
401                 hostName,
402                 domainName,
403                 port,
404                 serverName,
405                 serviceNames,
406                 serviceArgs));
407   }
408
409   /**
410    * Removes a server from the platform.
411    *
412    * @param sid Id of the removed server
413    *
414    * @exception ConnectException If the connection fails.
415    * @exception AdminException If the request fails.
416    */

417   public static void removeServer(int sid)
418     throws ConnectException JavaDoc, AdminException {
419     doRequest(new RemoveServerRequest(sid));
420   }
421
422   /**
423    * Adds a domain to the platform.
424    *
425    * @param domainName Name of the added domain
426    * @param sid Id of the router server that
427    * gives access to the added domain
428    * @param port Listening port in the added domain of the
429    * router server
430    *
431    * @exception ConnectException If the connection fails.
432    * @exception AdminException If the request fails.
433    */

434   public static void addDomain(String JavaDoc domainName,
435                                int sid,
436                                int port)
437     throws ConnectException JavaDoc, AdminException {
438     doRequest(new AddDomainRequest(
439                 domainName,
440                 sid,
441                 port));
442   }
443
444   /**
445    * Removes a domain from the platform.
446    *
447    * @param domainName Name of the added domain
448    *
449    * @exception ConnectException If the connection fails.
450    * @exception AdminException If the request fails.
451    */

452   public static void removeDomain(String JavaDoc domainName)
453     throws ConnectException JavaDoc, AdminException {
454     doRequest(new RemoveDomainRequest(
455                 domainName));
456   }
457
458   /**
459    * Returns the current servers configuration (a3servers.xml).
460    *
461    * @exception ConnectException If the connection fails.
462    * @exception AdminException If the request fails.
463    */

464   public static String JavaDoc getConfiguration()
465     throws ConnectException JavaDoc, AdminException {
466     return doRequest(new GetConfigRequest()).getInfo();
467   }
468
469   /**
470    * Sets a given dead message queue as the default DMQ for a given server
471    * (<code>null</code> for unsetting previous DMQ).
472    * <p>
473    * The request fails if the target server does not belong to the platform.
474    *
475    * @param serverId The identifier of the server.
476    * @param dmq The dmq to be set as the default one.
477    *
478    * @exception ConnectException If the connection fails.
479    * @exception AdminException If the request fails.
480    */

481   public static void setDefaultDMQ(int serverId, DeadMQueue dmq)
482     throws ConnectException JavaDoc, AdminException
483     {
484       doRequest(new SetDefaultDMQ(serverId, dmq.getName()));
485     }
486
487   /**
488    * Sets a given dead message queue as the default DMQ for the local server
489    * (<code>null</code> for unsetting previous DMQ).
490    *
491    * @param dmq The dmq to be set as the default one.
492    *
493    * @exception ConnectException If the connection fails.
494    * @exception AdminException Never thrown.
495    */

496   public static void setDefaultDMQ(DeadMQueue dmq)
497     throws ConnectException JavaDoc, AdminException
498     {
499       setDefaultDMQ(localServer, dmq);
500     }
501
502   /**
503    * Sets a given value as the default threshold for a given server (-1 for
504    * unsetting previous value).
505    * <p>
506    * The request fails if the target server does not belong to the platform.
507    *
508    * @param serverId The identifier of the server.
509    * @param threshold The threshold value to be set.
510    *
511    * @exception ConnectException If the connection fails.
512    * @exception AdminException If the request fails.
513    */

514   public static void setDefaultThreshold(int serverId, int threshold)
515     throws ConnectException JavaDoc, AdminException
516     {
517       doRequest(new SetDefaultThreshold(serverId, threshold));
518     }
519
520   /**
521    * Sets a given value as the default threshold for the local server (-1 for
522    * unsetting previous value).
523    *
524    * @param threshold The threshold value to be set.
525    *
526    * @exception ConnectException If the connection fails.
527    * @exception AdminException Never thrown.
528    */

529   public static void setDefaultThreshold(int threshold)
530     throws ConnectException JavaDoc, AdminException
531     {
532       setDefaultThreshold(localServer, threshold);
533     }
534
535   /**
536    * Returns the list of the platform's servers' identifiers.
537    *
538    * @exception ConnectException If the connection fails.
539    * @exception AdminException Never thrown.
540    */

541   public static List JavaDoc getServersIds() throws ConnectException JavaDoc, AdminException
542     {
543       return getServersIds(null);
544     }
545
546   /**
547    * Returns the list of the servers' identifiers that belong
548    * to the specified domain
549    *
550    * @exception ConnectException If the connection fails.
551    * @exception AdminException Never thrown.
552    */

553   public static List JavaDoc getServersIds(String JavaDoc domainName)
554     throws ConnectException JavaDoc, AdminException {
555     Monitor_GetServersIds request =
556       new Monitor_GetServersIds(
557         AdminModule.getLocalServerId(),
558         domainName);
559     Monitor_GetServersIdsRep reply =
560       (Monitor_GetServersIdsRep) doRequest(request);
561     int[] serverIds = reply.getIds();
562     Vector JavaDoc res = new Vector JavaDoc();
563     for (int i = 0; i < serverIds.length; i++) {
564       res.addElement(new Integer JavaDoc(serverIds[i]));
565     }
566     return res;
567   }
568
569   public static Server[] getServers()
570     throws ConnectException JavaDoc, AdminException {
571     return getServers(null);
572   }
573
574   public static Server[] getServers(String JavaDoc domainName)
575     throws ConnectException JavaDoc, AdminException {
576     Monitor_GetServersIds request =
577       new Monitor_GetServersIds(
578         AdminModule.getLocalServerId(),
579         domainName);
580     Monitor_GetServersIdsRep reply =
581       (Monitor_GetServersIdsRep) doRequest(request);
582     int[] serverIds = reply.getIds();
583     String JavaDoc[] serverNames = reply.getNames();
584     String JavaDoc[] serverHostNames = reply.getHostNames();
585     Server[] servers = new Server[serverIds.length];
586     for (int i = 0; i < serverIds.length; i++) {
587       servers[i] = new Server(serverIds[i],
588                               serverNames[i],
589                               serverHostNames[i]);
590     }
591     return servers;
592   }
593
594   public static Server getLocalServer()
595     throws ConnectException JavaDoc, AdminException {
596     GetLocalServerRep reply = (GetLocalServerRep)doRequest(
597       new GetLocalServer());
598     return new Server(reply.getId(),
599                       reply.getName(),
600                       reply.getHostName());
601   }
602
603   /**
604    * Returns the list of the domain names that
605    * contains the specified server.
606    *
607    * @exception ConnectException If the connection fails.
608    * @exception AdminException Never thrown.
609    */

610   public static String JavaDoc[] getDomainNames(int serverId)
611     throws ConnectException JavaDoc, AdminException {
612     GetDomainNames request =
613       new GetDomainNames(serverId);
614     GetDomainNamesRep reply =
615       (GetDomainNamesRep) doRequest(request);
616     return reply.getDomainNames();
617   }
618
619   /**
620    * Returns the default dead message queue for a given server, null if not
621    * set.
622    * <p>
623    * The request fails if the target server does not belong to the platform.
624    *
625    * @exception ConnectException If the connection fails.
626    * @exception AdminException If the request fails.
627    */

628   public static DeadMQueue getDefaultDMQ(int serverId)
629     throws ConnectException JavaDoc, AdminException
630     {
631       Monitor_GetDMQSettings request = new Monitor_GetDMQSettings(serverId);
632       Monitor_GetDMQSettingsRep reply;
633       reply = (Monitor_GetDMQSettingsRep) doRequest(request);
634
635       if (reply.getDMQName() == null)
636         return null;
637       else
638         return new DeadMQueue(reply.getDMQName());
639     }
640
641   /**
642    * Returns the default dead message queue for the local server, null if not
643    * set.
644    *
645    * @exception ConnectException If the connection fails.
646    * @exception AdminException Never thrown.
647    */

648   public static DeadMQueue getDefaultDMQ()
649     throws ConnectException JavaDoc, AdminException
650     {
651       return getDefaultDMQ(localServer);
652     }
653
654   /**
655    * Returns the default threshold value for a given server, -1 if not set.
656    * <p>
657    * The request fails if the target server does not belong to the platform.
658    *
659    * @exception ConnectException If the connection fails.
660    * @exception AdminException If the request fails.
661    */

662   public static int getDefaultThreshold(int serverId)
663     throws ConnectException JavaDoc, AdminException
664     {
665       Monitor_GetDMQSettings request = new Monitor_GetDMQSettings(serverId);
666       Monitor_GetDMQSettingsRep reply;
667       reply = (Monitor_GetDMQSettingsRep) doRequest(request);
668
669       if (reply.getThreshold() == null)
670         return -1;
671       else
672         return reply.getThreshold().intValue();
673     }
674
675   /**
676    * Returns the default threshold value for the local server, -1 if not set.
677    *
678    * @exception ConnectException If the connection fails.
679    * @exception AdminException Never thrown.
680    */

681   public static int getDefaultThreshold()
682     throws ConnectException JavaDoc, AdminException
683     {
684       return getDefaultThreshold(localServer);
685     }
686
687   /**
688    * Returns the list of all destinations that exist on a given server,
689    * or an empty list if none exist.
690    * <p>
691    * The request fails if the target server does not belong to the platform.
692    *
693    * @exception ConnectException If the admin connection is closed or broken.
694    * @exception AdminException If the request fails.
695    */

696   public static List JavaDoc getDestinations(int serverId)
697     throws ConnectException JavaDoc, AdminException
698     {
699       Monitor_GetDestinations request = new Monitor_GetDestinations(serverId);
700       Monitor_GetDestinationsRep reply =
701         (Monitor_GetDestinationsRep) doRequest(request);
702
703       Vector JavaDoc list = new Vector JavaDoc();
704       String JavaDoc[] ids = reply.getIds();
705       String JavaDoc[] names = reply.getNames();
706       String JavaDoc[] types = reply.getTypes();
707       for (int i = 0; i < types.length; i++) {
708         list.addElement(Destination.newInstance(
709                           ids[i], names[i], types[i]));
710       }
711       return list;
712     }
713
714   /**
715    * Returns the list of all destinations that exist on the local server,
716    * or an empty list if none exist.
717    *
718    * @exception ConnectException If the admin connection is closed or broken.
719    * @exception AdminException Never thrown.
720    */

721   public static List JavaDoc getDestinations() throws ConnectException JavaDoc, AdminException
722     {
723       return getDestinations(localServer);
724     }
725
726   /**
727    * Returns the list of all destinations that exist on a given server,
728    * or an empty list if none exist.
729    * The request is abort after delay.
730    *
731    * @exception ConnectException If the admin connection is closed or broken.
732    * @exception AdminException If the request fails.
733    */

734   public static List JavaDoc getDestinations(int serverId, long delay)
735     throws ConnectException JavaDoc, AdminException {
736
737     Monitor_GetDestinations request = new Monitor_GetDestinations(serverId);
738     Monitor_GetDestinationsRep reply =
739       (Monitor_GetDestinationsRep) doRequest(request,delay);
740
741     Vector JavaDoc list = new Vector JavaDoc();
742     String JavaDoc[] ids = reply.getIds();
743     String JavaDoc[] names = reply.getNames();
744     String JavaDoc[] types = reply.getTypes();
745     for (int i = 0; i < types.length; i++) {
746       list.addElement(Destination.newInstance(
747                         ids[i], names[i], types[i]));
748     }
749     return list;
750   }
751
752   /**
753    * Returns the list of all users that exist on a given server, or an empty
754    * list if none exist.
755    * <p>
756    * The request fails if the target server does not belong to the platform.
757    *
758    * @exception ConnectException If the connection fails.
759    * @exception AdminException If the request fails.
760    */

761   public static List JavaDoc getUsers(int serverId)
762     throws ConnectException JavaDoc, AdminException
763     {
764       Monitor_GetUsers request = new Monitor_GetUsers(serverId);
765       Monitor_GetUsersRep reply = (Monitor_GetUsersRep) doRequest(request);
766
767       Vector JavaDoc list = new Vector JavaDoc();
768       Hashtable JavaDoc users = reply.getUsers();
769       String JavaDoc name;
770       for (Enumeration JavaDoc names = users.keys(); names.hasMoreElements();) {
771         name = (String JavaDoc) names.nextElement();
772         list.add(new User(name, (String JavaDoc) users.get(name)));
773       }
774       return list;
775     }
776
777   /**
778    * Returns the list of all users that exist on a given server, or an empty
779    * list if none exist.
780    * The request is abort after delay.
781    *
782    * @exception ConnectException If the connection fails.
783    * @exception AdminException If the request fails.
784    */

785   public static List JavaDoc getUsers(int serverId, long delay)
786     throws ConnectException JavaDoc, AdminException {
787
788     Monitor_GetUsers request = new Monitor_GetUsers(serverId);
789     Monitor_GetUsersRep reply = (Monitor_GetUsersRep) doRequest(request,delay);
790
791     Vector JavaDoc list = new Vector JavaDoc();
792     Hashtable JavaDoc users = reply.getUsers();
793     String JavaDoc name;
794     for (Enumeration JavaDoc names = users.keys(); names.hasMoreElements();) {
795       name = (String JavaDoc) names.nextElement();
796       list.add(new User(name, (String JavaDoc) users.get(name)));
797     }
798     return list;
799   }
800
801   /**
802    * Returns the list of all users that exist on the local server, or an empty
803    * list if none exist.
804    *
805    * @exception ConnectException If the connection fails.
806    * @exception AdminException Never thrown.
807    */

808   public static List JavaDoc getUsers() throws ConnectException JavaDoc, AdminException
809     {
810       return getUsers(localServer);
811     }
812
813
814   /**
815    * Returns the identifier of the server the module is connected to.
816    *
817    * @exception ConnectException If the admin connection is not established.
818    */

819   public static int getLocalServerId() throws ConnectException JavaDoc
820     {
821       if (cnx == null)
822         throw new ConnectException JavaDoc("Administrator not connected.");
823
824       return localServer;
825     }
826
827   /**
828    * Returns the host name of the server the module is connected to.
829    *
830    * @exception ConnectException If the admin connection is not established.
831    */

832   public static String JavaDoc getLocalHost() throws ConnectException JavaDoc
833     {
834       if (cnx == null)
835         throw new ConnectException JavaDoc("Administrator not connected.");
836
837       return localHost;
838     }
839
840   /**
841    * Returns the port number of the server the module is connected to.
842    *
843    * @exception ConnectException If the admin connection is not established.
844    */

845   public static int getLocalPort() throws ConnectException JavaDoc
846     {
847       if (cnx == null)
848         throw new ConnectException JavaDoc("Administrator not connected.");
849
850       return localPort;
851     }
852
853   /**
854    * Method actually sending an <code>AdminRequest</code> instance to
855    * the platform and getting an <code>AdminReply</code> instance.
856    *
857    * @exception ConnectException If the connection to the platform fails.
858    * @exception AdminException If the platform's reply is invalid, or if
859    * the request failed.
860    */

861   public static AdminReply doRequest(AdminRequest request)
862     throws AdminException, ConnectException JavaDoc {
863     return doRequest(request,requestTimeout);
864   }
865
866   /**
867    * Method actually sending an <code>AdminRequest</code> instance to
868    * the platform and getting an <code>AdminReply</code> instance.
869    *
870    * @exception ConnectException If the connection to the platform fails.
871    * @exception AdminException If the platform's reply is invalid, or if
872    * the request failed.
873    */

874   public static AdminReply doRequest(AdminRequest request, long timeout)
875     throws AdminException, ConnectException JavaDoc {
876     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
877       JoramTracing.dbgClient.log(
878         BasicLevel.DEBUG, "AdminModule.doRequest(" + request + ')');
879
880     if (cnx == null)
881       throw new ConnectException JavaDoc("Admin connection not established.");
882
883     if (timeout < 1)
884       timeout = requestTimeout;
885
886     try {
887       replyMsg = (ObjectMessage) requestor.request(
888         request, timeout);
889       reply = (AdminReply) replyMsg.getObject();
890
891       if (! reply.succeeded()) {
892         switch (reply.getErrorCode()) {
893         case AdminReply.NAME_ALREADY_USED:
894           throw new NameAlreadyUsedException(reply.getInfo());
895         case AdminReply.START_FAILURE:
896           throw new StartFailureException(reply.getInfo());
897         case AdminReply.SERVER_ID_ALREADY_USED:
898           throw new ServerIdAlreadyUsedException(reply.getInfo());
899         case AdminReply.UNKNOWN_SERVER:
900           throw new UnknownServerException(reply.getInfo());
901         default:
902           throw new AdminException(reply.getInfo());
903         }
904       } else {
905         return reply;
906       }
907     } catch (JMSException exc) {
908       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
909         JoramTracing.dbgClient.log(
910           BasicLevel.DEBUG, "", exc);
911       throw new ConnectException JavaDoc("Connection failed: " + exc.getMessage());
912     } catch (ClassCastException JavaDoc exc) {
913       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
914         JoramTracing.dbgClient.log(
915           BasicLevel.DEBUG, "", exc);
916       throw new AdminException("Invalid server reply: " + exc.getMessage());
917     }
918   }
919
920   public static void abortRequest() throws JMSException {
921     if (requestor != null) {
922       requestor.abort();
923     } else throw new JMSException("Not connected");
924   }
925
926   public static class AdminRequestor {
927     private javax.jms.TopicConnection JavaDoc cnx;
928     private javax.jms.TopicSession JavaDoc sess;
929     private javax.jms.Topic JavaDoc topic;
930     private TemporaryTopic tmpTopic;
931     private MessageProducer producer;
932     private MessageConsumer consumer;
933
934     public AdminRequestor(javax.jms.TopicConnection JavaDoc cnx)
935       throws JMSException {
936       this.cnx = cnx;
937       sess = cnx.createTopicSession(
938         false, Session.AUTO_ACKNOWLEDGE);
939       topic = sess.createTopic("#AdminTopic");
940       producer = sess.createProducer(topic);
941       tmpTopic = sess.createTemporaryTopic();
942       consumer = sess.createConsumer(tmpTopic);
943     }
944
945     public javax.jms.Message JavaDoc request(AdminRequest request,
946                                      long timeout) throws JMSException {
947       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
948         JoramTracing.dbgClient.log(
949           BasicLevel.DEBUG,
950           "AdminModule.AdminRequestor.request(" +
951           request + ',' + timeout + ')');
952
953       requestMsg = sess.createObjectMessage(request);
954       requestMsg.setJMSReplyTo(tmpTopic);
955       producer.send(requestMsg);
956       String JavaDoc correlationId = requestMsg.getJMSMessageID();
957       while (true) {
958         javax.jms.Message JavaDoc reply = consumer.receive(timeout);
959         if (reply == null) {
960           throw new JMSException("Interrupted request");
961         } else {
962           if (correlationId.equals(
963                 reply.getJMSCorrelationID())) {
964             return reply;
965           } else {
966             if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
967               JoramTracing.dbgClient.log(
968                 BasicLevel.DEBUG,
969                 "reply id (" + reply.getJMSCorrelationID() +
970                 ") != request id (" + correlationId + ")");
971             continue;
972           }
973         }
974       }
975     }
976
977     public void abort() throws JMSException {
978       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
979         JoramTracing.dbgClient.log(
980           BasicLevel.DEBUG, "AdminModule.AdminRequestor.abort()");
981       consumer.close();
982       consumer = sess.createConsumer(tmpTopic);
983     }
984
985     public int getLocalServerId() {
986       try {
987         String JavaDoc topicName = topic.getTopicName();
988         int ind0 = topicName.indexOf(".");
989         int ind1 = topicName.indexOf(".", ind0 + 1);
990         return Integer.parseInt(topicName.substring(ind0 + 1, ind1));
991       } catch (JMSException exc) {
992         return -1;
993       }
994     }
995
996     public void close() throws JMSException {
997       consumer.close();
998       producer.close();
999       tmpTopic.delete();
1000      sess.close();
1001    }
1002  }
1003
1004  /**
1005   * This method execute the XML script file that the location is given
1006   * in parameter.
1007   *
1008   * @param cfgDir The directory containing the file.
1009   * @param cfgFileName The script filename.
1010   *
1011   * @since 4.3.10
1012   */

1013  public static boolean executeXMLAdmin(String JavaDoc cfgDir,
1014                                        String JavaDoc cfgFileName)
1015    throws Exception JavaDoc {
1016    return executeXMLAdmin(new File JavaDoc(cfgDir, cfgFileName).getPath());
1017  }
1018
1019  /**
1020   * This method execute the XML script file that the pathname is given
1021   * in parameter.
1022   *
1023   * @param path The script pathname.
1024   *
1025   * @since 4.3.10
1026   */

1027  public static boolean executeXMLAdmin(String JavaDoc path) throws Exception JavaDoc {
1028    if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG))
1029      JoramTracing.dbgAdmin.log(BasicLevel.DEBUG,"executeXMLAdmin(" + path + ")");
1030
1031    boolean res = false;
1032    Reader JavaDoc reader = null;
1033
1034    // 1st, search XML configuration file in directory.
1035
File JavaDoc cfgFile = new File JavaDoc(path);
1036    try {
1037      if (!cfgFile.exists() || !cfgFile.isFile() || (cfgFile.length() == 0)) {
1038        throw new IOException JavaDoc();
1039      }
1040      reader = new FileReader JavaDoc(cfgFile);
1041    } catch (IOException JavaDoc exc) {
1042      // configuration file seems not exist, search it from the
1043
// search path used to load classes.
1044
if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG))
1045        JoramTracing.dbgAdmin.log(BasicLevel.DEBUG,
1046                                  "Unable to find Joram Admin configuration file \"" +
1047                                  cfgFile.getPath() + "\".");
1048      reader = null;
1049    }
1050
1051    // 2nd, search XML configuration file in path used to load classes.
1052
if (reader == null) {
1053      ClassLoader JavaDoc classLoader = null;
1054      InputStream JavaDoc is = null;
1055      try {
1056        classLoader = AdminModule.class.getClassLoader();
1057        if (classLoader != null) {
1058          if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG))
1059            JoramTracing.dbgAdmin.log(BasicLevel.DEBUG,
1060                                      "Trying to find [" + path + "] using " +
1061                                      classLoader + " class loader.");
1062          is = classLoader.getResourceAsStream(path);
1063        }
1064      } catch (Throwable JavaDoc t) {
1065        if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG))
1066          JoramTracing.dbgAdmin.log(BasicLevel.DEBUG,
1067                                    "Can't find [" + path + "] using " +
1068                                    classLoader + " class loader.",
1069                                    t);
1070        is = null;
1071      }
1072
1073      if (is == null) {
1074        // Last ditch attempt: get the resource from the class path.
1075
if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG))
1076          JoramTracing.dbgAdmin.log(BasicLevel.DEBUG,
1077                                    "Trying to find [" + path +
1078                                    "] using ClassLoader.getSystemResource().");
1079        is = ClassLoader.getSystemResourceAsStream(path);
1080      }
1081      if (is != null) {
1082        res = executeAdmin(new InputStreamReader JavaDoc(is));
1083      }
1084    } else {
1085      res = executeAdmin(reader);
1086    }
1087
1088    if (!res)
1089      throw new FileNotFoundException JavaDoc("xml Joram Admin configuration file not found.");
1090
1091    return res;
1092  }
1093
1094  public static boolean executeAdmin(Reader JavaDoc reader)
1095    throws Exception JavaDoc {
1096    if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG))
1097      JoramTracing.dbgAdmin.log(BasicLevel.DEBUG, "executeAdmin(" + reader + ")");
1098
1099    String JavaDoc cfgName = System.getProperty(AdminModule.ADM_NAME_PROPERTY,
1100                                        AdminModule.DEFAULT_ADM_NAME);
1101
1102    JoramSaxWrapper wrapper = new JoramSaxWrapper();
1103    return wrapper.parse(reader,cfgName);
1104  }
1105
1106
1107  public static void setHa(boolean isHa) {
1108    AdminModule.isHa = isHa;
1109  }
1110}
1111
Popular Tags