KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2005 - 2006 ScalAgent Distributed Technologies
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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s): Benoit Pelletier (Bull SA)
22  */

23 package org.objectweb.joram.client.jms.admin;
24
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.ConnectException JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.jms.Destination JavaDoc;
36
37 import fr.dyade.aaa.util.management.MXWrapper;
38 import org.objectweb.joram.client.jms.admin.server.*;
39 import org.objectweb.joram.client.jms.Queue;
40 import org.objectweb.joram.client.jms.Topic;
41 import org.objectweb.joram.shared.JoramTracing;
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 /**
45  *
46  */

47 public class JoramAdmin
48   implements JoramAdminMBean {
49
50   public long timeOut = 1000;
51   public PlatformAdmin platformAdmin;
52   /** <code>true</code> if the underlying a JORAM HA server is defined */
53   static boolean isHa = false;
54
55
56   /**
57    * Path to the file containing a description of the exported administered objects (destination)
58    */

59   private String JavaDoc adminFileExportXML = null;
60
61
62   public JoramAdmin()
63     throws UnknownHostException JavaDoc, ConnectException JavaDoc, AdminException {
64     platformAdmin = new PlatformAdmin();
65     registerMBean();
66   }
67
68   public JoramAdmin(String JavaDoc hostName,
69                     int port,
70                     String JavaDoc name,
71                     String JavaDoc password,
72                     int cnxTimer,
73                     String JavaDoc reliableClass)
74     throws UnknownHostException JavaDoc, ConnectException JavaDoc, AdminException {
75     platformAdmin = new PlatformAdmin(hostName,port,name,password,cnxTimer,reliableClass);
76     registerMBean();
77   }
78
79   public JoramAdmin(String JavaDoc hostName,
80                     int port,
81                     String JavaDoc name,
82                     String JavaDoc password,
83                     int cnxTimer)
84     throws UnknownHostException JavaDoc, ConnectException JavaDoc, AdminException {
85     platformAdmin = new PlatformAdmin(hostName,port,name,password,cnxTimer);
86     registerMBean();
87   }
88
89   public JoramAdmin(String JavaDoc name,
90                     String JavaDoc password)
91     throws ConnectException JavaDoc, AdminException {
92     platformAdmin = new PlatformAdmin(name,password);
93     registerMBean();
94   }
95
96   public JoramAdmin(javax.jms.TopicConnectionFactory JavaDoc cnxFact,
97                     String JavaDoc name,
98                     String JavaDoc password)
99     throws ConnectException JavaDoc, AdminException {
100     platformAdmin = new PlatformAdmin(cnxFact,name,password);
101     registerMBean();
102   }
103
104   private void registerMBean() {
105     try {
106       MXWrapper.registerMBean(this,
107                               "joramClient",
108                               "type=JoramAdmin");
109     } catch (Exception JavaDoc e) {
110       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
111         JoramTracing.dbgClient.log(BasicLevel.DEBUG,
112                                    "registerMBean",e);
113     }
114   }
115
116   private void unregisterMBean() {
117     try {
118       MXWrapper.unregisterMBean("joramClient",
119                                 "type=JoramAdmin");
120     } catch (Exception JavaDoc e) {
121       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
122         JoramTracing.dbgClient.log(BasicLevel.DEBUG,
123                                    "unregisterMBean",e);
124     }
125   }
126
127   public PlatformAdmin getPlatformAdmin() {
128     return platformAdmin;
129   }
130
131   public void exit() {
132     platformAdmin.exit();
133     unregisterMBean();
134   }
135
136   /**
137    * wait before abort a request.
138    */

139   public void setTimeOutToAbortRequest(long timeOut) {
140     this.timeOut = timeOut;
141   }
142
143   /**
144    * wait before abort a request.
145    */

146   public long getTimeOutToAbortRequest() {
147     return timeOut;
148   }
149
150   /**
151    * Sets a given dead message queue as the default DMQ for a given server
152    * (<code>null</code> for unsetting previous DMQ).
153    * <p>
154    * The request fails if the target server does not belong to the platform.
155    *
156    * @param serverId The identifier of the server.
157    * @param dmq The dmq to be set as the default one.
158    *
159    * @exception ConnectException If the connection fails.
160    * @exception AdminException If the request fails.
161    */

162   public void setDefaultDMQ(int serverId, DeadMQueue dmq)
163     throws ConnectException JavaDoc, AdminException {
164     AdminModule.setDefaultDMQ(serverId,dmq);
165   }
166
167   /**
168    * Returns the default dead message queue for a given server, null if not
169    * set.
170    * <p>
171    * The request fails if the target server does not belong to the platform.
172    *
173    * @exception ConnectException If the connection fails.
174    * @exception AdminException If the request fails.
175    */

176   public DeadMQueue getDefaultDMQ(int serverId)
177     throws ConnectException JavaDoc, AdminException {
178     return AdminModule.getDefaultDMQ(serverId);
179   }
180
181   /**
182    * Returns the default dead message queue for the local server, null if not
183    * set.
184    *
185    * @exception ConnectException If the connection fails.
186    * @exception AdminException Never thrown.
187    */

188   public DeadMQueue getDefaultDMQ()
189     throws ConnectException JavaDoc, AdminException {
190     return AdminModule.getDefaultDMQ();
191   }
192
193   /**
194    * Returns the list of all destinations that exist on a given server,
195    * or an empty list if none exist.
196    */

197   public List JavaDoc getDestinations(int serverId) {
198     Vector JavaDoc destinations = new Vector JavaDoc();
199     try {
200       List JavaDoc destList = AdminModule.getDestinations(serverId,timeOut);
201       Iterator JavaDoc destIt = destList.iterator();
202       while (destIt.hasNext()) {
203         org.objectweb.joram.client.jms.Destination dest =
204           (org.objectweb.joram.client.jms.Destination) destIt.next();
205         destinations.add(new String JavaDoc("type=" + dest.getType() +
206                                     ", name=" + dest.getAdminName() +
207                                     ", id=" + dest.getName()));
208       }
209     } catch (Exception JavaDoc exc) {}
210     return destinations;
211   }
212
213   /**
214    * Returns the list of all destinations that exist on the local server,
215    * or an empty list if none exist.
216    */

217   public List JavaDoc getDestinations() {
218     Vector JavaDoc destinations = new Vector JavaDoc();
219
220     List JavaDoc list = platformAdmin.getServersIds();
221     if (list != null) {
222       Iterator JavaDoc it = list.iterator();
223       while (it.hasNext()) {
224         try {
225           Integer JavaDoc sid = (Integer JavaDoc) it.next();
226           List JavaDoc destList = AdminModule.getDestinations(sid.intValue(),timeOut);
227           Iterator JavaDoc destIt = destList.iterator();
228           while (destIt.hasNext()) {
229             org.objectweb.joram.client.jms.Destination dest =
230               (org.objectweb.joram.client.jms.Destination) destIt.next();
231             destinations.add(new String JavaDoc("type=" + dest.getType() +
232                                         ", name=" + dest.getAdminName() +
233                                         ", id=" + dest.getName()));
234           }
235         } catch (Exception JavaDoc exc) {}
236       }
237     }
238     return destinations;
239   }
240
241   /**
242    * Returns the list of all users that exist on a given server, or an empty
243    * list if none exist.
244    */

245   public List JavaDoc getUsers(int serverId) {
246     Vector JavaDoc users = new Vector JavaDoc();
247     try {
248       List JavaDoc userList = AdminModule.getUsers(serverId,timeOut);
249       Iterator JavaDoc userIt = userList.iterator();
250       while (userIt.hasNext()) {
251         User dest = (User) userIt.next();
252         users.add(dest.toString());
253       }
254     } catch (Exception JavaDoc exc) {}
255     return users;
256   }
257
258   /**
259    * Returns the list of all users that exist on the local server, or an empty
260    * list if none exist.
261    */

262   public List JavaDoc getUsers() {
263     Vector JavaDoc users = new Vector JavaDoc();
264
265     List JavaDoc list = platformAdmin.getServersIds();
266     if (list != null) {
267       Iterator JavaDoc it = list.iterator();
268       while (it.hasNext()) {
269         try {
270           Integer JavaDoc sid = (Integer JavaDoc) it.next();
271           List JavaDoc userList = AdminModule.getUsers(sid.intValue(),timeOut);
272           Iterator JavaDoc userIt = userList.iterator();
273           while (userIt.hasNext()) {
274             User dest = (User) userIt.next();
275             users.add(dest.toString());
276           }
277         } catch (Exception JavaDoc exc) {}
278       }
279     }
280     return users;
281   }
282
283   /**
284    * Creates or retrieves a user on the underlying JORAM server.
285    *
286    * @exception AdminException If the creation fails.
287    */

288   public void createUser(String JavaDoc name, String JavaDoc password)
289     throws AdminException {
290     try {
291       User.create(name,password);
292     } catch (ConnectException JavaDoc exc) {
293       throw new AdminException("createUser() failed: admin connection "
294                                + "has been lost.");
295     }
296   }
297
298   /**
299    * Creates or retrieves a user on the underlying JORAM server.
300    *
301    * @exception AdminException If the creation fails.
302    */

303   public void createUser(String JavaDoc name, String JavaDoc password, int serverId)
304     throws AdminException {
305     try {
306       User.create(name,password,serverId);
307     } catch (ConnectException JavaDoc exc) {
308       throw new AdminException("createUser() failed: admin connection "
309                                + "has been lost.");
310     }
311   }
312
313   /**
314    * Creates or retrieves a queue destination on the underlying JORAM server,
315    * (re)binds the corresponding <code>Queue</code> instance.
316    *
317    * @param name The name of the queue.
318    *
319    * @exception AdminException If the creation fails.
320    */

321   public Destination JavaDoc createQueue(String JavaDoc name)
322     throws AdminException {
323     try {
324       return createQueue(platformAdmin.getLocalServerId(),
325                          name,
326                          "org.objectweb.joram.mom.dest.Queue",
327                          null);
328     } catch (ConnectException JavaDoc exc) {
329       throw new AdminException("createQueue() failed: admin connection "
330                                + "has been lost.");
331     }
332   }
333
334   /**
335    * Creates or retrieves a queue destination on the underlying JORAM server,
336    * (re)binds the corresponding <code>Queue</code> instance.
337    *
338    * @param serverId The identifier of the server where deploying the queue.
339    * @param name The name of the queue.
340    *
341    * @exception AdminException If the creation fails.
342    */

343   public Destination JavaDoc createQueue(int serverId, String JavaDoc name)
344     throws AdminException {
345     return createQueue(serverId,
346                        name,
347                        "org.objectweb.joram.mom.dest.Queue",
348                        null);
349   }
350
351   /**
352    * Creates or retrieves a queue destination on the underlying JORAM server,
353    * (re)binds the corresponding <code>Queue</code> instance.
354    *
355    * @param serverId The identifier of the server where deploying the queue.
356    * @param name The name of the queue.
357    * @param className The queue class name.
358    * @param prop The queue properties.
359    *
360    * @exception AdminException If the creation fails.
361    */

362   public Destination JavaDoc createQueue(int serverId,
363                                  String JavaDoc name,
364                                  String JavaDoc className,
365                                  Properties JavaDoc prop)
366     throws AdminException {
367     try {
368       Queue queue = Queue.create(serverId,
369                                  name,
370                                  className,
371                                  prop);
372       return queue;
373     } catch (ConnectException JavaDoc exc) {
374       throw new AdminException("createQueue() failed: admin connection "
375                                + "has been lost.");
376     }
377   }
378
379
380   /**
381    * Creates or retrieves a topic destination on the underlying JORAM server,
382    * (re)binds the corresponding <code>Topic</code> instance.
383    *
384    * @exception AdminException If the creation fails.
385    */

386   public Destination JavaDoc createTopic(String JavaDoc name)
387     throws AdminException {
388     try {
389       return createTopic(platformAdmin.getLocalServerId(),
390                          name,
391                          "org.objectweb.joram.mom.dest.Topic",
392                          null);
393     } catch (ConnectException JavaDoc exc) {
394       throw new AdminException("createTopic() failed: admin connection "
395                                + "has been lost.");
396     }
397   }
398
399   /**
400    * Creates or retrieves a topic destination on the underlying JORAM server,
401    * (re)binds the corresponding <code>Topic</code> instance.
402    *
403    * @param serverId The identifier of the server where deploying the topic.
404    * @param name The name of the topic.
405    *
406    * @exception AdminException If the creation fails.
407    */

408   public Destination JavaDoc createTopic(int serverId, String JavaDoc name)
409     throws AdminException {
410     return createTopic(serverId,
411                        name,
412                        "org.objectweb.joram.mom.dest.Topic",
413                        null);
414   }
415
416   /**
417    * Creates or retrieves a topic destination on the underlying JORAM server,
418    * (re)binds the corresponding <code>Topic</code> instance.
419    *
420    * @param serverId The identifier of the server where deploying the topic.
421    * @param name The name of the topic.
422    * @param className The topic class name.
423    * @param prop The topic properties.
424    *
425    * @exception AdminException If the creation fails.
426    */

427   public Destination JavaDoc createTopic(int serverId,
428                                  String JavaDoc name,
429                                  String JavaDoc className,
430                                  Properties JavaDoc prop)
431     throws AdminException {
432     try {
433       Topic topic = Topic.create(serverId,
434                                  name,
435                                  className,
436                                  prop);
437       return topic;
438     } catch (ConnectException JavaDoc exc) {
439       throw new AdminException("createTopic() failed: admin connection "
440                                + "has been lost.");
441     }
442   }
443
444   public static boolean executeXMLAdmin(String JavaDoc cfgDir,
445                                         String JavaDoc cfgFileName)
446     throws Exception JavaDoc {
447     return AdminModule.executeXMLAdmin(cfgDir, cfgFileName);
448   }
449
450   public static boolean executeXMLAdmin(String JavaDoc path)
451     throws Exception JavaDoc {
452     return AdminModule.executeXMLAdmin(path);
453   }
454
455   /**
456    * Reload the joramAdmin.xml file
457    * @param the path for the joramAdmin file
458    * @throws AdminException if an error occurs
459    */

460   public boolean executeXMLAdminJMX(String JavaDoc path)
461     throws Exception JavaDoc {
462     throw new Exception JavaDoc("Not implemented yet");
463
464   }
465
466
467   /**
468    * Export the repository content to an XML file
469    * - only the destinations objects are retrieved in this version
470    * - xml script format of the admin objects (joramAdmin.xml)
471    * @param exportDir target directory where the export file will be put
472    * @throws AdminException if an error occurs
473    */

474   public void exportRepositoryToFile(String JavaDoc exportDir) throws AdminException {
475
476     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) {
477       JoramTracing.dbgClient.log(BasicLevel.DEBUG, "export repository to " + exportDir.toString());
478     }
479
480     StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
481     int indent = 0;
482     strbuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
483     strbuf.append("<!--\n");
484     strbuf.append(" Exported JMS objects : \n");
485     strbuf.append(" - destinations : Topic/Queue \n");
486     strbuf.append(" The file can be reloaded through the admin interface (joramAdmin.executeXMLAdmin())\n");
487     strbuf.append("-->\n");
488     strbuf.append("<JoramAdmin>\n");
489     indent += 2;
490
491     // Get the srv list
492
List JavaDoc srvList = platformAdmin.getServersIds();
493     if (srvList != null) {
494
495       // For each server
496
Iterator JavaDoc it = srvList.iterator();
497       while (it.hasNext()) {
498         try {
499           Integer JavaDoc sid = (Integer JavaDoc) it.next();
500
501           // Export the JMS destinations
502
List JavaDoc destList = AdminModule.getDestinations(sid.intValue(), timeOut);
503           Iterator JavaDoc destIt = destList.iterator();
504           while (destIt.hasNext()) {
505             org.objectweb.joram.client.jms.Destination dest = (org.objectweb.joram.client.jms.Destination) destIt
506               .next();
507
508             strbuf.append(dest.toXml(indent, sid.intValue()));
509           }
510
511         } catch (Exception JavaDoc exc) {
512           throw new AdminException("exportRepositoryToFile() failed - " + exc);
513         }
514       }
515
516       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) {
517         JoramTracing.dbgClient.log(BasicLevel.DEBUG, "exported objects : \n" + strbuf.toString());
518       }
519     }
520
521     indent -= 2;
522     strbuf.append("</JoramAdmin>");
523
524     // Flush the file in the specified directory
525
File JavaDoc exportFile = null;
526     FileOutputStream JavaDoc fos = null;
527
528     try {
529       exportFile = new File JavaDoc(exportDir, getAdminFileExportXML());
530       fos = new FileOutputStream JavaDoc(exportFile);
531       fos.write(strbuf.toString().getBytes());
532     } catch(Exception JavaDoc ioe) {
533       throw new AdminException("exportRepositoryToFile() failed - " + ioe);
534     } finally {
535       try {
536         exportFile = null;
537         fos.close();
538       } catch (Exception JavaDoc e) {
539         if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) {
540           JoramTracing.dbgClient.log(BasicLevel.DEBUG, "Unable to close the file : " + fos);
541         }
542       }
543       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) {
544         JoramTracing.dbgClient.log(BasicLevel.DEBUG, "File : " + exportDir + "/" + getAdminFileExportXML() + " created");
545       }
546     }
547   }
548
549
550   public String JavaDoc getAdminFileExportXML() {
551     return adminFileExportXML;
552   }
553
554   public void setAdminFileExportXML(String JavaDoc adminFileExportXML) {
555     this.adminFileExportXML = adminFileExportXML;
556   }
557
558
559   public static boolean isHa() {
560     return isHa;
561   }
562
563
564   public static void setHa(boolean isHa) {
565     JoramAdmin.isHa = isHa;
566     AdminModule.setHa(isHa);
567   }
568 }
569
Popular Tags