KickJava   Java API By Example, From Geeks To Geeks.

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


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

24 package org.objectweb.joram.client.jms.admin;
25
26 import javax.naming.*;
27
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Connection JavaDoc;
30
31 import java.util.Hashtable JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Random JavaDoc;
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 /**
44  * A base class for clustered connection factories.
45  */

46 public class ClusterConnectionFactory extends org.objectweb.joram.client.jms.admin.AdministeredObject implements javax.jms.ConnectionFactory JavaDoc {
47
48   protected Hashtable JavaDoc cluster = null;
49
50   /**
51    * Constructs an empty clustered connection factory.
52    */

53   public ClusterConnectionFactory() {}
54
55   /**
56    * Adds a connection factory to the cluster.
57    * The object will be added with a key equals to the location property.
58    * By default, the location value is set to the hostname of corresponding
59    * server.
60    * Be careful, the object should be rebind after modification.
61    *
62    * @param cf the ConnectionFactory
63    */

64   public void addConnectionFactory(ConnectionFactory cf) {
65     String JavaDoc location = System.getProperty("location");
66     addConnectionFactory(location, cf);
67   }
68
69   /**
70    * Adds a connection factory to the cluster with the specified
71    * <code>location</code> key.
72    * Be careful, the object should be rebind after modification.
73    *
74    * @param location the location key
75    * @param cf the ConnectionFactory
76    */

77   public void addConnectionFactory(String JavaDoc location, ConnectionFactory cf) {
78     if (cluster == null) cluster = new Hashtable JavaDoc();
79
80     if (location == null)
81       location = cf.getParameters().getHost();
82     cluster.put(location, cf);
83   }
84
85   /**
86    * Chooses a connection factory from the cluster definition.
87    */

88   protected ConnectionFactory getConnectionFactory() throws JMSException JavaDoc {
89     if ((cluster != null) && ! cluster.isEmpty()) {
90       ConnectionFactory cf = null;
91       String JavaDoc location = System.getProperty("location");
92       if (location == null || location.equals("")) {
93         int idx = new Random JavaDoc().nextInt(cluster.size());
94
95         Object JavaDoc key[] = cluster.keySet().toArray();
96         location = (String JavaDoc) key[idx];
97         System.setProperty("location", location);
98       }
99
100       cf = (ConnectionFactory) cluster.get(location);
101       if (cf == null) {
102         Enumeration JavaDoc e = cluster.elements();
103         cf = (ConnectionFactory) e.nextElement();
104       }
105       return cf;
106     }
107
108     return null;
109   }
110
111   /**
112    * Creates a connection with the default user identity.
113    * It chooses a <code>ConnectionFactory</code> depending of the
114    * location property, then creates the related <code>Connection</code>.
115    *
116    * API method, see javax.jms.ConnectionFactory.
117    *
118    * @exception JMSSecurityException If the default identification is
119    * incorrect.
120    * @exception IllegalStateException If the server is not listening.
121    */

122   public final Connection JavaDoc createConnection() throws JMSException JavaDoc {
123     ConnectionFactory cf = getConnectionFactory();
124     return cf.createConnection();
125   }
126
127   /**
128    * Creates a connection with the specified user identity.
129    * It chooses a <code>ConnectionFactory</code> depending of the
130    * location property, then creates the related <code>Connection</code>.
131    *
132    * API method, see javax.jms.ConnectionFactory.
133    *
134    * @param name the caller's user name
135    * @param password the caller's password
136    *
137    * @exception JMSSecurityException If the user identification is incorrect.
138    * @exception IllegalStateException If the server is not listening.
139    */

140   public final Connection JavaDoc createConnection(String JavaDoc name,
141                                            String JavaDoc password) throws JMSException JavaDoc {
142     ConnectionFactory cf = getConnectionFactory();
143     return cf.createConnection(name, password);
144   }
145
146   /** Returns a String image of the object. */
147   public String JavaDoc toString() {
148     return "ClusterConnectionFactory:" + cluster;
149   }
150
151   /** Sets the naming reference of an administered object. */
152   public void toReference(Reference ref) throws NamingException {
153     Map.Entry JavaDoc entries[] = new Map.Entry JavaDoc [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 JavaDoc) 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   /** Restores the administered object from a naming reference. */
167   public void fromReference(Reference ref) throws NamingException {
168     if (cluster == null) {
169       cluster = new Hashtable JavaDoc();
170     }
171     int i = 0;
172     while (true) {
173       RefAddr refAddr = ref.get("CF#" + i + ".key");
174       if (refAddr == null) break;
175       String JavaDoc key = (String JavaDoc) refAddr.getContent();
176       String JavaDoc classname = (String JavaDoc) ref.get("CF#" + i + ".class").getContent();
177       try {
178         Class JavaDoc 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 JavaDoc exc) {
184         if (JoramTracing.dbgClient.isLoggable(BasicLevel.ERROR))
185           JoramTracing.dbgClient.log(BasicLevel.ERROR, "", exc);
186       }
187       i++;
188     }
189   }
190
191   // AF: The JNDI Soap access should translate the Reference object to
192
// an hashtable.
193

194   /**
195    * Codes a <code>ConnectionFactory</code> as a Hashtable for travelling
196    * through the SOAP protocol.
197    */

198   public Hashtable JavaDoc code() {
199     return null;
200   }
201
202   /**
203    * Implements the <code>decode</code> abstract method defined in the
204    * <code>fr.dyade.aaa.jndi2.soap.SoapObjectItf</code> interface.
205    * <p>
206    * Actual implementation of the method is located in the
207    * tcp and soap sub classes.
208    */

209   public void decode(Hashtable JavaDoc h) {
210     cluster = null;
211   }
212
213 }
214
Popular Tags