1 24 package org.objectweb.joram.client.jms.admin; 25 26 import javax.naming.*; 27 28 import javax.jms.JMSException ; 29 import javax.jms.Connection ; 30 31 import java.util.Hashtable ; 32 import java.util.Enumeration ; 33 import java.util.Properties ; 34 import java.util.Map ; 35 import java.util.Random ; 36 37 import org.objectweb.joram.client.jms.ConnectionFactory; 38 import org.objectweb.joram.client.jms.admin.AdministeredObject; 39 40 import org.objectweb.util.monolog.api.BasicLevel; 41 import org.objectweb.joram.shared.JoramTracing; 42 43 46 public class ClusterConnectionFactory extends org.objectweb.joram.client.jms.admin.AdministeredObject implements javax.jms.ConnectionFactory { 47 48 protected Hashtable cluster = null; 49 50 53 public ClusterConnectionFactory() {} 54 55 64 public void addConnectionFactory(ConnectionFactory cf) { 65 String location = System.getProperty("location"); 66 addConnectionFactory(location, cf); 67 } 68 69 77 public void addConnectionFactory(String location, ConnectionFactory cf) { 78 if (cluster == null) cluster = new Hashtable (); 79 80 if (location == null) 81 location = cf.getParameters().getHost(); 82 cluster.put(location, cf); 83 } 84 85 88 protected ConnectionFactory getConnectionFactory() throws JMSException { 89 if ((cluster != null) && ! cluster.isEmpty()) { 90 ConnectionFactory cf = null; 91 String location = System.getProperty("location"); 92 if (location == null || location.equals("")) { 93 int idx = new Random ().nextInt(cluster.size()); 94 95 Object key[] = cluster.keySet().toArray(); 96 location = (String ) key[idx]; 97 System.setProperty("location", location); 98 } 99 100 cf = (ConnectionFactory) cluster.get(location); 101 if (cf == null) { 102 Enumeration e = cluster.elements(); 103 cf = (ConnectionFactory) e.nextElement(); 104 } 105 return cf; 106 } 107 108 return null; 109 } 110 111 122 public final Connection createConnection() throws JMSException { 123 ConnectionFactory cf = getConnectionFactory(); 124 return cf.createConnection(); 125 } 126 127 140 public final Connection createConnection(String name, 141 String password) throws JMSException { 142 ConnectionFactory cf = getConnectionFactory(); 143 return cf.createConnection(name, password); 144 } 145 146 147 public String toString() { 148 return "ClusterConnectionFactory:" + cluster; 149 } 150 151 152 public void toReference(Reference ref) throws NamingException { 153 Map.Entry entries[] = new Map.Entry [cluster.size()]; 154 cluster.entrySet().toArray(entries); 155 156 for (int i=0; i<entries.length; i++) { 157 ref.add(new StringRefAddr("CF#" + i + ".key", 158 (String ) entries[i].getKey())); 159 ConnectionFactory cf = (ConnectionFactory) entries[i].getValue(); 160 ref.add(new StringRefAddr("CF#" + i + ".class", 161 cf.getClass().getName())); 162 cf.toReference(ref, "CF#" + i); 163 } 164 } 165 166 167 public void fromReference(Reference ref) throws NamingException { 168 if (cluster == null) { 169 cluster = new Hashtable (); 170 } 171 int i = 0; 172 while (true) { 173 RefAddr refAddr = ref.get("CF#" + i + ".key"); 174 if (refAddr == null) break; 175 String key = (String ) refAddr.getContent(); 176 String classname = (String ) ref.get("CF#" + i + ".class").getContent(); 177 try { 178 Class clazz = Class.forName(classname); 179 ConnectionFactory cf = (ConnectionFactory) clazz.newInstance(); 180 cf.fromReference(ref, "CF#" + i); 181 182 cluster.put(key, cf); 183 } catch (Exception exc) { 184 if (JoramTracing.dbgClient.isLoggable(BasicLevel.ERROR)) 185 JoramTracing.dbgClient.log(BasicLevel.ERROR, "", exc); 186 } 187 i++; 188 } 189 } 190 191 194 198 public Hashtable code() { 199 return null; 200 } 201 202 209 public void decode(Hashtable h) { 210 cluster = null; 211 } 212 213 } 214 | Popular Tags |