1 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 ; 34 import java.util.Properties ; 35 import java.util.Enumeration ; 36 import java.util.Map ; 37 import java.util.Random ; 38 39 import org.objectweb.util.monolog.api.BasicLevel; 40 import org.objectweb.joram.shared.JoramTracing; 41 42 45 public class ClusterDestination extends Destination { 46 47 protected Hashtable cluster = null; 48 49 52 public ClusterDestination() {} 53 54 59 public ClusterDestination(Hashtable 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 cluster) { 67 this.cluster = cluster; 68 } 69 70 public Hashtable getCluster() { 71 return cluster; 72 } 73 74 81 public void addDestination(Destination dest) { 82 String location = System.getProperty("location"); 83 addDestination(location, dest); 84 } 85 86 96 public void addDestination(String location, Destination dest) { 97 if (cluster == null) cluster = new Hashtable (); 98 99 if (location == null) { 100 String destname = dest.getName(); 101 location = "server" + destname.substring(0, destname.indexOf('.')); 102 } 103 cluster.put(location, dest); 104 } 105 106 107 protected Destination getDestination() { 108 if ((cluster != null) && ! cluster.isEmpty()) { 109 Destination dest = null; 110 String location = System.getProperty("location"); 111 if (location == null || location.equals("")) { 112 int idx = new Random ().nextInt(cluster.size()); 113 114 Object key[] = cluster.keySet().toArray(); 115 location = (String ) key[idx]; 116 System.setProperty("location", location); 117 } 118 119 dest = (Destination) cluster.get(location); 120 if (dest == null) { 121 Enumeration e = cluster.elements(); 122 dest = (Destination) e.nextElement(); 123 } 124 return dest; 125 } 126 127 return null; 128 } 129 130 131 public String getName() { 132 return getDestination().getName(); 133 } 134 135 143 147 148 public void toReference(Reference ref) throws NamingException { 149 Map.Entry entries[] = new Map.Entry [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 ) 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 162 public void fromReference(Reference ref) throws NamingException { 163 cluster = new Hashtable (); 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 ) refAddr.getContent(), 170 new Queue((String ) 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 ) refAddr.getContent(), 178 new Topic((String ) ref.get("cluster#" + i + ".destName").getContent())); 179 i++; 180 } 181 } 182 } 183 184 188 public Hashtable code() { 189 Hashtable h = super.code(); 190 h.put("cluster",cluster); 191 return h; 192 } 193 194 public void decode(Hashtable h) { 195 cluster = (Hashtable ) h.get("cluster"); 196 } 197 } 198 | Popular Tags |