KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > Destination


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

25 package org.objectweb.joram.client.jms;
26
27 import java.net.ConnectException JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ListIterator JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import javax.naming.*;
36
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 import org.objectweb.joram.client.jms.admin.DeadMQueue;
40 import org.objectweb.joram.client.jms.admin.User;
41 import org.objectweb.joram.client.jms.admin.AdministeredObject;
42 import org.objectweb.joram.client.jms.admin.AdminModule;
43 import org.objectweb.joram.client.jms.admin.AdminException;
44 import org.objectweb.joram.client.jms.admin.XmlSerializer;
45 import org.objectweb.joram.shared.admin.*;
46
47 import org.objectweb.util.monolog.api.BasicLevel;
48 import org.objectweb.joram.shared.JoramTracing;
49
50 import fr.dyade.aaa.util.management.MXWrapper;
51
52 /**
53  * Implements the <code>javax.jms.Destination</code> interface and provides
54  * JORAM specific administration and monitoring methods.
55  */

56 public abstract class Destination
57   extends AdministeredObject
58   implements javax.jms.Destination JavaDoc, DestinationMBean {
59   /** Identifier of the agent destination. */
60   protected String JavaDoc agentId;
61
62   /** Name given by the administrator. */
63   protected String JavaDoc adminName;
64
65   private String JavaDoc type;
66
67   // Used by jndi2 SoapObjectHelper
68
public Destination() {}
69
70   public Destination(String JavaDoc type) {
71     this.type = type;
72   }
73
74   protected Destination(String JavaDoc name, String JavaDoc type) {
75     agentId = name;
76     this.type = type;
77   }
78
79   /** Returns the name of the destination. */
80   public String JavaDoc getName() {
81     return agentId;
82   }
83
84   /** Returns the admin name of the destination. */
85   public final String JavaDoc getAdminName() {
86     return adminName;
87   }
88
89   public final String JavaDoc getType() {
90     return type;
91   }
92
93   /**
94    * Returns <code>true</code> if the parameter object is a Joram destination
95    * wrapping the same agent identifier.
96    */

97   public boolean equals(Object JavaDoc obj) {
98     if (! (obj instanceof Destination))
99       return false;
100
101     return (getName().equals(((Destination) obj).getName()));
102   }
103
104   /**
105    * Returns a String image of the queue.
106    *
107    * @return A provider-specific identity values for this queue.
108    */

109   public String JavaDoc toString() {
110     StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
111     strbuf.append(type).append(agentId);
112     if (adminName != null)
113       strbuf.append('(').append(adminName).append(')');
114     return strbuf.toString();
115   }
116
117   /**
118    * Format the destination properties in a XML format
119    * @param indent use this indent for prexifing XML representation.
120    * @param serverId server id hosting the destination object
121    * @return returns a XML view of the queue (admin format)
122    * @throws ConnectException if the server is unreachable
123    * @throws AdminException if an error occurs
124    */

125   public String JavaDoc toXml(int indent, int serverId) throws ConnectException JavaDoc, AdminException {
126     StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
127
128     strbuf.append(XmlSerializer.indent(indent));
129
130     if (getType().equals("queue")) {
131       strbuf.append("<Queue ");
132     } else if (getType().equals("topic")) {
133       strbuf.append("<Topic ");
134     } else {
135       return "";
136     }
137     strbuf.append(XmlSerializer.xmlAttribute(getAdminName(), "name"));
138     strbuf.append(XmlSerializer.xmlAttribute(String.valueOf(serverId), "serverId"));
139     DeadMQueue dmq = getDMQ();
140     if (dmq != null) {
141       strbuf.append(XmlSerializer.xmlAttribute(dmq.getAdminName(), "dmq"));
142       strbuf.append(XmlSerializer.xmlAttribute(String.valueOf(dmq.getThreshold()), "threshold"));
143     }
144
145     strbuf.append(">\n");
146
147     indent+=2;
148
149     if (isFreelyReadable()) {
150       strbuf.append(XmlSerializer.indent(indent));
151       strbuf.append("<freeReader/>\n");
152     }
153
154     if (isFreelyWriteable()) {
155       strbuf.append(XmlSerializer.indent(indent));
156       strbuf.append("<freeWriter/>\n");
157     }
158
159     List JavaDoc readers = getReaders();
160     for (ListIterator JavaDoc iterator = readers.listIterator(); iterator.hasNext(); ) {
161         User user = (User) (iterator.next());
162         strbuf.append(XmlSerializer.indent(indent));
163         strbuf.append("<reader ");
164         strbuf.append(XmlSerializer.xmlAttribute(user.getName(), "user"));
165         strbuf.append("/>\n");
166     }
167
168     List JavaDoc writers = getWriters();
169     for (ListIterator JavaDoc iterator = writers.listIterator(); iterator.hasNext(); ) {
170         User user = (User) (iterator.next());
171         strbuf.append(XmlSerializer.indent(indent));
172         strbuf.append("<writer ");
173         strbuf.append(XmlSerializer.xmlAttribute(user.getName(), "user"));
174         strbuf.append("/>\n");
175     }
176
177
178     strbuf.append(XmlSerializer.indent(indent));
179     strbuf.append("<jndi ");
180     strbuf.append(XmlSerializer.xmlAttribute(getAdminName(), "name"));
181     strbuf.append("/>\n");
182
183     indent-=2;
184
185     strbuf.append(XmlSerializer.indent(indent));
186
187     if (getType().equals("queue")) {
188       strbuf.append("</Queue>\n");
189     } else if (getType().equals("topic")) {
190       strbuf.append("</Topic>\n");
191     }
192
193     return strbuf.toString();
194   }
195
196   /**
197    * Returns <code>true</code> if the destination is a queue.
198    */

199   public boolean isQueue() {
200     return (this instanceof javax.jms.Queue JavaDoc);
201   }
202
203   public static final String JavaDoc QUEUE =
204       "org.objectweb.joram.mom.dest.Queue";
205   public static final String JavaDoc TOPIC =
206       "org.objectweb.joram.mom.dest.Topic";
207   public static final String JavaDoc DEAD_MQUEUE =
208       "org.objectweb.joram.mom.dest.DeadMQueue";
209   public static final String JavaDoc CLUSTER_QUEUE =
210       "org.objectweb.joram.mom.dest.ClusterQueue";
211   public static final String JavaDoc BRIDGE_QUEUE =
212       "org.objectweb.joram.mom.dest.BridgeQueue";
213   public static final String JavaDoc BRIDGE_TOPIC =
214       "org.objectweb.joram.mom.dest.BridgeTopic";
215   public static final String JavaDoc MAIL_QUEUE =
216       "com.scalagent.joram.mom.dest.mail.JavaMailQueue";
217   public static final String JavaDoc MAIL_TOPIC =
218       "com.scalagent.joram.mom.dest.mail.JavaMailTopic";
219   public static final String JavaDoc SCHEDULER_QUEUE =
220       "com.scalagent.joram.mom.dest.scheduler.SchedulerQueue";
221
222   /**
223    * Admin method creating or retrieving a destination with a given name on a
224    * given server, and returning its identifier.
225    * <p>
226    * The request fails if the target server does not belong to the platform,
227    * or if the destination deployement fails server side.
228    *
229    * @param serverId The identifier of the server where deploying the
230    * destination.
231    * @param name The destination name.
232    * @param className Name of the MOM destination class.
233    * @param prop Properties.
234    *
235    * @exception ConnectException If the admin connection is closed or broken.
236    * @exception AdminException If the request fails.
237    */

238   protected static void doCreate(
239     int serverId,
240     String JavaDoc name,
241     String JavaDoc className,
242     Properties JavaDoc props,
243     Destination dest,
244     String JavaDoc expectedType)
245     throws ConnectException JavaDoc, AdminException {
246     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
247       JoramTracing.dbgClient.log(
248         BasicLevel.DEBUG,
249         "Destination.doCreate(" +
250         serverId + ',' + name + ',' +
251         className + ',' + props + ',' +
252         dest + ',' + expectedType + ')');
253
254     CreateDestinationRequest cdr =
255       new CreateDestinationRequest(serverId,
256                                    name,
257                                    className,
258                                    props,
259                                    expectedType);
260     CreateDestinationReply reply =
261       (CreateDestinationReply) AdminModule.doRequest(cdr);
262     dest.agentId = reply.getId();
263     dest.adminName = name;
264     dest.type = reply.getType();
265   }
266
267   /**
268    * Admin method removing this destination from the platform.
269    *
270    * @exception AdminException Never thrown.
271    * @exception ConnectException If the admin connection is closed or broken.
272    * @exception JMSException Never thrown.
273    */

274   public void delete()
275     throws ConnectException JavaDoc, AdminException, javax.jms.JMSException JavaDoc {
276     AdminModule.doRequest(new DeleteDestination(getName()));
277     if (MXWrapper.mxserver != null) {
278       StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
279       buff.append("type=");
280       buff.append(getType());
281       buff.append(",name=");
282       buff.append(getAdminName());
283       try {
284         MXWrapper.unregisterMBean("joramClient",buff.toString());
285       } catch (Exception JavaDoc e) {
286         if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
287           JoramTracing.dbgClient.log(BasicLevel.DEBUG,
288                                      "unregisterMBean",e);
289       }
290     }
291   }
292
293   /**
294    * Admin method setting free reading access to this destination.
295    * <p>
296    * The request fails if this destination is deleted server side.
297    *
298    * @exception ConnectException If the admin connection is closed or broken.
299    * @exception AdminException If the request fails.
300    */

301   public void setFreeReading() throws ConnectException JavaDoc, AdminException
302     {
303       AdminModule.doRequest(new SetReader(null, getName()));
304     }
305
306   /**
307    * Admin method setting free writing access to this destination.
308    * <p>
309    * The request fails if this destination is deleted server side.
310    *
311    * @exception ConnectException If the admin connection is closed or broken.
312    * @exception AdminException If the request fails.
313    */

314   public void setFreeWriting() throws ConnectException JavaDoc, AdminException
315     {
316       AdminModule.doRequest(new SetWriter(null, getName()));
317     }
318
319   /**
320    * Admin method unsetting free reading access to this destination.
321    * <p>
322    * The request fails if this destination is deleted server side.
323    *
324    * @exception ConnectException If the admin connection is closed or broken.
325    * @exception AdminException If the request fails.
326    */

327   public void unsetFreeReading() throws ConnectException JavaDoc, AdminException
328     {
329       AdminModule.doRequest(new UnsetReader(null, getName()));
330     }
331
332   /**
333    * Admin method unsetting free writing access to this destination.
334    * <p>
335    * The request fails if this destination is deleted server side.
336    *
337    * @exception ConnectException If the admin connection is closed or broken.
338    * @exception AdminException If the request fails.
339    */

340   public void unsetFreeWriting() throws ConnectException JavaDoc, AdminException
341     {
342       AdminModule.doRequest(new UnsetWriter(null, getName()));
343     }
344
345   /**
346    * Admin method setting a given user as a reader on this destination.
347    * <p>
348    * The request fails if this destination is deleted server side.
349    *
350    * @param user User to be set as a reader.
351    *
352    * @exception ConnectException If the admin connection is closed or broken.
353    * @exception AdminException If the request fails.
354    */

355   public void setReader(User user) throws ConnectException JavaDoc, AdminException
356     {
357       AdminModule.doRequest(new SetReader(user.getProxyId(), getName()));
358     }
359
360   /** used by MBean jmx */
361   public void addReader(String JavaDoc proxyId)
362     throws ConnectException JavaDoc, AdminException {
363     AdminModule.doRequest(new SetReader(proxyId, getName()));
364   }
365
366   /**
367    * Admin method setting a given user as a writer on this destination.
368    * <p>
369    * The request fails if this destination is deleted server side.
370    *
371    * @param user User to be set as a writer.
372    *
373    * @exception ConnectException If the admin connection is closed or broken.
374    * @exception AdminException If the request fails.
375    */

376   public void setWriter(User user) throws ConnectException JavaDoc, AdminException
377     {
378       AdminModule.doRequest(new SetWriter(user.getProxyId(), getName()));
379     }
380
381   /** used by MBean jmx */
382   public void addWriter(String JavaDoc proxyId)
383     throws ConnectException JavaDoc, AdminException {
384     AdminModule.doRequest(new SetWriter(proxyId, getName()));
385   }
386
387   /**
388    * Admin method unsetting a given user as a reader on this destination.
389    * <p>
390    * The request fails if this destination is deleted server side.
391    *
392    * @param user Reader to be unset.
393    *
394    * @exception ConnectException If the admin connection is closed or broken.
395    * @exception AdminException If the request fails.
396    */

397   public void unsetReader(User user)
398     throws ConnectException JavaDoc, AdminException {
399     AdminModule.doRequest(new UnsetReader(user.getProxyId(), getName()));
400   }
401
402   /** used by MBean jmx */
403   public void removeReader(String JavaDoc proxyId)
404     throws ConnectException JavaDoc, AdminException {
405     AdminModule.doRequest(new UnsetReader(proxyId, getName()));
406   }
407
408   /**
409    * Admin method unsetting a given user as a writer on this destination.
410    * <p>
411    * The request fails if this destination is deleted server side.
412    *
413    * @param user Writer to be unset.
414    *
415    * @exception ConnectException If the admin connection is closed or broken.
416    * @exception AdminException If the request fails.
417    */

418   public void unsetWriter(User user)
419     throws ConnectException JavaDoc, AdminException {
420     AdminModule.doRequest(new UnsetWriter(user.getProxyId(), getName()));
421   }
422
423   /** used by MBean jmx */
424   public void removeWriter(String JavaDoc proxyId)
425     throws ConnectException JavaDoc, AdminException {
426     AdminModule.doRequest(new UnsetWriter(proxyId, getName()));
427   }
428
429   /**
430    * Admin method setting or unsetting a dead message queue for this
431    * destination.
432    * <p>
433    * The request fails if this destination is deleted server side.
434    *
435    * @param dmq The dead message queue to be set (<code>null</code> for
436    * unsetting current DMQ).
437    *
438    * @exception IllegalArgumentException If the DMQ is not a valid
439    * JORAM destination.
440    * @exception ConnectException If the admin connection is closed or broken.
441    * @exception AdminException If the request fails.
442    */

443   public void setDMQ(DeadMQueue dmq) throws ConnectException JavaDoc, AdminException
444     {
445       if (dmq == null)
446         AdminModule.doRequest(new UnsetDestinationDMQ(getName()));
447       else
448         AdminModule.doRequest(new SetDestinationDMQ(getName(), dmq.getName()));
449     }
450
451   /**
452    * Monitoring method returning the list of all users that have a reading
453    * permission on this destination, or an empty list if no specific readers
454    * are set.
455    * <p>
456    * The request fails if the destination is deleted server side.
457    *
458    * @exception ConnectException If the admin connection is closed or broken.
459    * @exception AdminException If the request fails.
460    */

461   public List JavaDoc getReaders() throws ConnectException JavaDoc, AdminException
462     {
463       Monitor_GetReaders request = new Monitor_GetReaders(getName());
464       Monitor_GetUsersRep reply =
465         (Monitor_GetUsersRep) AdminModule.doRequest(request);
466
467       Vector JavaDoc list = new Vector JavaDoc();
468       Hashtable JavaDoc users = reply.getUsers();
469       String JavaDoc name;
470       for (Enumeration JavaDoc names = users.keys(); names.hasMoreElements();) {
471         name = (String JavaDoc) names.nextElement();
472         list.add(new User(name, (String JavaDoc) users.get(name)));
473       }
474       return list;
475     }
476
477   /** used by MBean jmx */
478   public List JavaDoc getReaderList() throws ConnectException JavaDoc, AdminException {
479     Vector JavaDoc list = new Vector JavaDoc();
480     List JavaDoc readers = getReaders();
481     for (ListIterator JavaDoc iterator = readers.listIterator(); iterator.hasNext(); ) {
482       list.add(iterator.next().toString());
483     }
484     return list;
485   }
486
487   /**
488    * Monitoring method returning the list of all users that have a writing
489    * permission on this destination, or an empty list if no specific writers
490    * are set.
491    * <p>
492    * The request fails if the destination is deleted server side.
493    *
494    * @exception ConnectException If the admin connection is closed or broken.
495    * @exception AdminException If the request fails.
496    */

497   public List JavaDoc getWriters() throws ConnectException JavaDoc, AdminException
498     {
499       Monitor_GetWriters request = new Monitor_GetWriters(getName());
500       Monitor_GetUsersRep reply =
501         (Monitor_GetUsersRep) AdminModule.doRequest(request);
502
503       Vector JavaDoc list = new Vector JavaDoc();
504       Hashtable JavaDoc users = reply.getUsers();
505       String JavaDoc name;
506       for (Enumeration JavaDoc names = users.keys(); names.hasMoreElements();) {
507         name = (String JavaDoc) names.nextElement();
508         list.add(new User(name, (String JavaDoc) users.get(name)));
509       }
510       return list;
511     }
512
513   /** used by MBean jmx */
514   public List JavaDoc getWriterList() throws ConnectException JavaDoc, AdminException {
515     Vector JavaDoc list = new Vector JavaDoc();
516     List JavaDoc readers = getWriters();
517     for (ListIterator JavaDoc iterator = readers.listIterator(); iterator.hasNext(); ) {
518       list.add(iterator.next().toString());
519     }
520     return list;
521   }
522
523   /**
524    * Monitoring method returning <code>true</code> if this destination
525    * provides free READ access.
526    * <p>
527    * The request fails if the destination is deleted server side.
528    *
529    * @exception ConnectException If the admin connection is closed or broken.
530    * @exception AdminException If the request fails.
531    */

532   public boolean isFreelyReadable() throws ConnectException JavaDoc, AdminException
533     {
534       Monitor_GetFreeAccess request = new Monitor_GetFreeAccess(getName());
535       Monitor_GetFreeAccessRep reply;
536       reply = (Monitor_GetFreeAccessRep) AdminModule.doRequest(request);
537
538       return reply.getFreeReading();
539     }
540
541   /** used by MBean */
542   public void setFreelyReadable(boolean b)
543     throws ConnectException JavaDoc, AdminException {
544     if (b)
545       setFreeReading();
546     else
547       unsetFreeReading();
548   }
549
550   /**
551    * Monitoring method returning <code>true</code> if this destination
552    * provides free WRITE access.
553    * <p>
554    * The request fails if the destination is deleted server side.
555    *
556    * @exception ConnectException If the admin connection is closed or broken.
557    * @exception AdminException If the request fails.
558    */

559   public boolean isFreelyWriteable() throws ConnectException JavaDoc, AdminException
560     {
561       Monitor_GetFreeAccess request = new Monitor_GetFreeAccess(getName());
562       Monitor_GetFreeAccessRep reply;
563       reply = (Monitor_GetFreeAccessRep) AdminModule.doRequest(request);
564
565       return reply.getFreeWriting();
566     }
567
568   /** used by MBean */
569   public void setFreelyWriteable(boolean b)
570     throws ConnectException JavaDoc, AdminException {
571     if (b)
572       setFreeWriting();
573     else
574       unsetFreeWriting();
575   }
576
577   /**
578    * Monitoring method returning the dead message queue of this destination,
579    * null if not set.
580    * <p>
581    * The request fails if the destination is deleted server side.
582    *
583    * @exception ConnectException If the admin connection is closed or broken.
584    * @exception AdminException If the request fails.
585    */

586   public DeadMQueue getDMQ() throws ConnectException JavaDoc, AdminException
587     {
588       Monitor_GetDMQSettings request = new Monitor_GetDMQSettings(getName());
589       Monitor_GetDMQSettingsRep reply;
590       reply = (Monitor_GetDMQSettingsRep) AdminModule.doRequest(request);
591
592       if (reply.getDMQName() == null) {
593         return null;
594       } else {
595         return new DeadMQueue(reply.getDMQName());
596       }
597     }
598
599   public static Destination newInstance(
600     String JavaDoc id,
601     String JavaDoc name,
602     String JavaDoc type) throws AdminException {
603     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
604       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
605                                  "Destination.newInstance(" +
606                                  id + ',' + name + ',' + type + ')');
607     Destination dest;
608     if (Queue.isQueue(type)) {
609       if (TemporaryQueue.isTemporaryQueue(type)) {
610         dest = new TemporaryQueue(id, null);
611       } else if (DeadMQueue.isDeadMQueue(type)) {
612         dest = new DeadMQueue(id);
613       } else {
614         dest = new Queue(id);
615       }
616     } else if (Topic.isTopic(type)) {
617       if (TemporaryTopic.isTemporaryTopic(type)) {
618         dest = new TemporaryTopic(id, null);
619       } else {
620         dest = new Topic(id);
621       }
622     } else throw new AdminException("Unknown destination type");
623     dest.adminName = name;
624     return dest;
625   }
626
627   public static boolean isAssignableTo(String JavaDoc realType,
628                                        String JavaDoc resultingType) {
629     return realType.startsWith(resultingType);
630   }
631
632   public Hashtable JavaDoc getStatistic()
633     throws ConnectException JavaDoc, AdminException {
634     Monitor_GetStat request =
635       new Monitor_GetStat(agentId);
636     Monitor_GetStatRep reply =
637       (Monitor_GetStatRep) AdminModule.doRequest(request);
638     return reply.getStats();
639   }
640
641   /** Sets the naming reference of a connection factory. */
642   public void toReference(Reference ref) throws NamingException {
643     ref.add(new StringRefAddr("dest.agentId", agentId));
644     ref.add(new StringRefAddr("dest.adminName", adminName));
645   }
646
647   /** Restores the administered object from a naming reference. */
648   public void fromReference(Reference ref) throws NamingException {
649     agentId = (String JavaDoc) ref.get("dest.agentId").getContent();
650     adminName = (String JavaDoc) ref.get("dest.adminName").getContent();
651   }
652
653   /**
654    * Codes a <code>Destination</code> as a Hashtable for travelling through the
655    * SOAP protocol.
656    */

657   public Hashtable JavaDoc code() {
658     Hashtable JavaDoc h = new Hashtable JavaDoc();
659     h.put("agentId", getName());
660     h.put("type", type);
661     return h;
662   }
663
664   public void decode(Hashtable JavaDoc h) {
665     agentId = (String JavaDoc) h.get("agentId");
666     type = (String JavaDoc) h.get("type");
667   }
668 }
669
Popular Tags