KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2007 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 - 2007 France Telecom R&D
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): ScalAgent Distributed Technologies
23  * Contributor(s):
24  */

25 package org.objectweb.joram.client.jms.admin;
26
27 import org.objectweb.joram.client.jms.Destination;
28 import org.objectweb.joram.client.jms.Queue;
29 import org.objectweb.joram.client.jms.Topic;
30
31 import javax.naming.*;
32
33 import java.util.Hashtable JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Random JavaDoc;
38
39 import org.objectweb.util.monolog.api.BasicLevel;
40 import org.objectweb.joram.shared.JoramTracing;
41
42 /**
43  * A base class for clustered destinations.
44  */

45 public class ClusterDestination extends Destination {
46
47   protected Hashtable JavaDoc cluster = null;
48
49   /**
50    * Constructs an empty clustered destination.
51    */

52   public ClusterDestination() {}
53
54   /**
55    * Constructs a cluster destination.
56    *
57    * @param cluster Hashtable of the cluster agent destination.
58    */

59   public ClusterDestination(Hashtable JavaDoc cluster) {
60     this.cluster = cluster;
61     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
62       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
63                                  this + ": cluster = " + cluster);
64   }
65
66   public void setCluster(Hashtable JavaDoc cluster) {
67     this.cluster = cluster;
68   }
69
70   public Hashtable JavaDoc getCluster() {
71     return cluster;
72   }
73
74   /**
75    * Adds a destination to the cluster.
76    * The object will be added with a key equals to the location property.
77    * Be careful, the object should be rebind after modification.
78    *
79    * @param dest the Destination
80    */

81   public void addDestination(Destination dest) {
82     String JavaDoc location = System.getProperty("location");
83     addDestination(location, dest);
84   }
85
86   /**
87    * Adds a destination to the cluster with the specified
88    * <code>location</code> key.
89    * By default, the location value is set to the String server#i, where i is
90    * the server id. of the destination.
91    * Be careful, the object should be rebind after modification.
92    *
93    * @param location the location key
94    * @param dest the Destination
95    */

96   public void addDestination(String JavaDoc location, Destination dest) {
97     if (cluster == null) cluster = new Hashtable JavaDoc();
98
99     if (location == null) {
100       String JavaDoc destname = dest.getName();
101       location = "server" + destname.substring(0, destname.indexOf('.'));
102     }
103     cluster.put(location, dest);
104   }
105
106   /** return the appropriate destination of cluster */
107   protected Destination getDestination() {
108     if ((cluster != null) && ! cluster.isEmpty()) {
109       Destination dest = null;
110       String JavaDoc location = System.getProperty("location");
111       if (location == null || location.equals("")) {
112         int idx = new Random JavaDoc().nextInt(cluster.size());
113
114         Object JavaDoc key[] = cluster.keySet().toArray();
115         location = (String JavaDoc) key[idx];
116         System.setProperty("location", location);
117       }
118
119       dest = (Destination) cluster.get(location);
120       if (dest == null) {
121         Enumeration JavaDoc e = cluster.elements();
122         dest = (Destination) e.nextElement();
123       }
124       return dest;
125     }
126
127     return null;
128   }
129
130   /** Returns the name of the destination. */
131   public String JavaDoc getName() {
132     return getDestination().getName();
133   }
134
135 // /**
136
// * Returns <code>true</code> if the parameter object is a Joram
137
// * cluster destination wrapping the same agent identifier.
138
// */
139
// public boolean equals(Object obj) {
140
// if (! (obj instanceof org.objectweb.joram.client.jms.admin.ClusterDestination))
141
// return false;
142

143 // return (getName().equals(
144
// ((org.objectweb.joram.client.jms.admin.ClusterDestination) obj).getName()));
145
// }
146

147   /** Sets the naming reference of an administered object. */
148   public void toReference(Reference ref) throws NamingException {
149     Map.Entry JavaDoc entries[] = new Map.Entry JavaDoc [cluster.size()];
150     cluster.entrySet().toArray(entries);
151
152     for (int i=0; i<entries.length; i++) {
153       ref.add(new StringRefAddr("cluster#" + i + ".key",
154                                 (String JavaDoc) entries[i].getKey()));
155       Destination dest = (Destination) entries[i].getValue();
156       ref.add(new StringRefAddr("cluster#" + i + ".destName",
157                                 dest.getName()));
158     }
159   }
160
161   /** Restores the administered object from a naming reference. */
162   public void fromReference(Reference ref) throws NamingException {
163     cluster = new Hashtable JavaDoc();
164     int i = 0;
165     if (isQueue()) {
166       while (true) {
167         RefAddr refAddr = ref.get("cluster#" + i + ".key");
168         if (refAddr == null) break;
169         cluster.put((String JavaDoc) refAddr.getContent(),
170                     new Queue((String JavaDoc) ref.get("cluster#" + i + ".destName").getContent()));
171         i++;
172       }
173     } else {
174       while (true) {
175         RefAddr refAddr = ref.get("cluster#" + i + ".key");
176         if (refAddr == null) break;
177         cluster.put((String JavaDoc) refAddr.getContent(),
178                     new Topic((String JavaDoc) ref.get("cluster#" + i + ".destName").getContent()));
179         i++;
180       }
181     }
182   }
183
184   /**
185    * Codes a <code>ClusterDestination</code> as a Hashtable for
186    * travelling through the SOAP protocol.
187    */

188   public Hashtable JavaDoc code() {
189     Hashtable JavaDoc h = super.code();
190     h.put("cluster",cluster);
191     return h;
192   }
193
194   public void decode(Hashtable JavaDoc h) {
195     cluster = (Hashtable JavaDoc) h.get("cluster");
196   }
197 }
198
Popular Tags