KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > persistence > Destinations


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  */

43 package org.exolab.jms.persistence;
44
45 import java.sql.Connection JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Vector JavaDoc;
51
52 import org.exolab.jms.client.JmsDestination;
53 import org.exolab.jms.client.JmsQueue;
54 import org.exolab.jms.client.JmsTopic;
55
56
57 /**
58  * This class provides persistency for JmsDestination objects
59  * in an RDBMS database
60  *
61  * @version $Revision: 1.3 $ $Date: 2005/06/09 14:39:51 $
62  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
63  * @see org.exolab.jms.client.JmsDestination
64  * @see org.exolab.jms.persistence.RDBMSAdapter
65  **/

66 class Destinations {
67
68     /**
69      * Cache of all destinations indexed on names
70      */

71     private HashMap JavaDoc _destinations;
72
73     /**
74      * Cache of all destinations indexed on identity
75      */

76     private HashMap JavaDoc _ids;
77
78     /**
79      * Singleton instance of this class
80      */

81     private static Destinations _instance;
82
83     /**
84      * This is used to synchronize the creation of the singleton
85      */

86     private static final Object JavaDoc _block = new Object JavaDoc();
87
88     /**
89      * This is the name of the destination id generator, which uniquely created
90      * identities for destinations
91      */

92     private static final String JavaDoc DESTINATION_ID_SEED = "destinationId";
93
94     /**
95      * Constructor
96      *
97      * @throws PersistenceException - if constructor fails
98      */

99     private Destinations()
100         throws PersistenceException {
101
102         _destinations = new HashMap JavaDoc();
103         _ids = new HashMap JavaDoc();
104     }
105
106     /**
107      * Returns the singleton instance.
108      *
109      * Note that initialise() must have been invoked first for this
110      * to return a valid instance.
111      *
112      * @return Destinations the singleton instance
113      */

114     public static Destinations instance() {
115         return _instance;
116     }
117
118     /**
119      * Initialise the singleton instance
120      *
121      * @param connection - the connection to use
122      * @return Destinations - the singleton instance
123      * @throws PersistenceException - if initialisation fails
124      */

125     public static Destinations initialise(Connection JavaDoc connection)
126         throws PersistenceException {
127
128         if (_instance == null) {
129             synchronized (_block) {
130                 if (_instance == null) {
131                     _instance = new Destinations();
132                     _instance.load(connection);
133                 }
134             }
135         }
136         return _instance;
137     }
138
139     /**
140      * Add a new destination to the database. This method will also assign
141      * it a unique identity.
142      *
143      * @param connection - the connection to use.
144      * @param destination - the destination to add
145      * @throws PersistenceException - if the destination cannot be added
146      */

147     public synchronized void add(Connection JavaDoc connection,
148                                  JmsDestination destination)
149         throws PersistenceException {
150
151         PreparedStatement JavaDoc insert = null;
152         try {
153             long Id = SeedGenerator.instance().next(connection,
154                 DESTINATION_ID_SEED);
155             boolean isQueue = (destination instanceof JmsQueue);
156
157             insert = connection.prepareStatement(
158                 "insert into destinations (name, isqueue, destinationid) "
159                 + "values (?, ?, ?)");
160             insert.setString(1, destination.getName());
161             insert.setBoolean(2, isQueue);
162             insert.setLong(3, Id);
163             insert.executeUpdate();
164             cache(destination, Id);
165         } catch (Exception JavaDoc error) {
166             throw new PersistenceException("Destinations.add failed with " +
167                 error.toString());
168         } finally {
169             SQLHelper.close(insert);
170         }
171     }
172
173     /**
174      * Remove a destination from the database.
175      * This removes all associated consumers, and messages.
176      *
177      * @param connection - the connection to use
178      * @param destination - the destination
179      * @return boolean - <tt>true</tt> if it was removed
180      * @throws PersistenceException - if the request fails
181      */

182     public synchronized boolean remove(Connection JavaDoc connection,
183                                        JmsDestination destination)
184         throws PersistenceException {
185
186         boolean success = false;
187         PreparedStatement JavaDoc deleteDestinations = null;
188         PreparedStatement JavaDoc deleteMessages = null;
189         PreparedStatement JavaDoc deleteConsumers = null;
190         PreparedStatement JavaDoc deleteMessageHandles = null;
191
192         Pair pair = (Pair) _destinations.get(destination.getName());
193         if (pair != null) {
194             try {
195                 deleteDestinations = connection.prepareStatement(
196                     "delete from destinations where name=?");
197                 deleteDestinations.setString(1, destination.getName());
198
199                 deleteMessages = connection.prepareStatement(
200                     "delete from messages where destinationId=?");
201                 deleteMessages.setLong(1, pair.Id);
202
203                 deleteMessageHandles = connection.prepareStatement(
204                     "delete from message_handles where destinationId=?");
205                 deleteMessageHandles.setLong(1, pair.Id);
206
207                 deleteConsumers = connection.prepareStatement(
208                     "delete from consumers where destinationId=?");
209                 deleteConsumers.setLong(1, pair.Id);
210
211
212                 deleteDestinations.executeUpdate();
213                 deleteMessages.executeUpdate();
214                 deleteMessageHandles.executeUpdate();
215                 deleteConsumers.executeUpdate();
216
217                 Consumers.instance().removeCached(pair.Id);
218                 _destinations.remove(destination.getName());
219                 _ids.remove(new Long JavaDoc(pair.Id));
220                 success = true;
221             } catch (Exception JavaDoc error) {
222                 throw new PersistenceException("Destinations.remove failed " +
223                     error.toString());
224             } finally {
225                 SQLHelper.close(deleteDestinations);
226                 SQLHelper.close(deleteMessages);
227                 SQLHelper.close(deleteConsumers);
228                 SQLHelper.close(deleteMessageHandles);
229             }
230         }
231
232         return success;
233     }
234
235     /**
236      * Returns the destination associated with name
237      *
238      * @param name - the name of the destination
239      * @return JmsDestination - the destination, or null
240      */

241     public synchronized JmsDestination get(String JavaDoc name) {
242         Pair pair = (Pair) _destinations.get(name);
243         return (pair != null) ? pair.destination : null;
244     }
245
246     /**
247      * Returns the destination associated with Id
248      *
249      * @param id - the destination Id
250      * @return JmsDestination - the destination or null
251      */

252     public synchronized JmsDestination get(long id) {
253         Pair pair = (Pair) _ids.get(new Long JavaDoc(id));
254         return (pair != null) ? pair.destination : null;
255     }
256
257     /**
258      * Returns the Id for a given destination name
259      *
260      * @param name - the destination name
261      * @return long - the destination Id, or 0
262      */

263     public synchronized long getId(String JavaDoc name) {
264         Pair pair = (Pair) _destinations.get(name);
265         return (pair != null) ? pair.Id : 0;
266     }
267
268     /**
269      * Returns the list of destination names
270      *
271      * @return Vector - list of destination names
272      */

273     public synchronized Vector JavaDoc getNames() {
274         // return a Vector for legacy reasons.
275
Vector JavaDoc result = new Vector JavaDoc(_destinations.size());
276         Iterator JavaDoc iter = _destinations.keySet().iterator();
277         while (iter.hasNext()) {
278             result.add((String JavaDoc) iter.next());
279         }
280
281         return result;
282     }
283
284     /**
285      * Returns the list of destination objects
286      *
287      * @return Vector - list of destination objects
288      */

289     public synchronized Vector JavaDoc getDestinations() {
290         // return a Vector for legacy reasons.
291
Vector JavaDoc result = new Vector JavaDoc(_destinations.size());
292         Iterator JavaDoc iter = _destinations.values().iterator();
293         while (iter.hasNext()) {
294             result.add(((Pair) iter.next()).destination);
295         }
296
297         return result;
298     }
299
300     /**
301      * Deallocates resources owned or referenced by the instance
302      */

303     public synchronized void close() {
304         _destinations.clear();
305         _destinations = null;
306
307         _ids.clear();
308         _ids = null;
309
310         _instance = null;
311     }
312
313     /**
314      * Load all the destinations in memory. It uses the transaction service
315      * and the database service to access the appropriate resources.
316      *
317      * @param connection - the connection to use
318      * @throws PersistenceException - problems loading the destinations
319      */

320     private void load(Connection JavaDoc connection)
321         throws PersistenceException {
322
323         PreparedStatement JavaDoc select = null;
324         ResultSet JavaDoc set = null;
325         try {
326             select = connection.prepareStatement(
327                 "select name, isqueue, destinationid from destinations");
328
329             set = select.executeQuery();
330             String JavaDoc name = null;
331             boolean isQueue = false;
332             JmsDestination destination = null;
333             long Id = 0;
334             while (set.next()) {
335                 name = set.getString(1);
336                 isQueue = set.getBoolean(2);
337                 destination = (isQueue)
338                     ? (JmsDestination) new JmsQueue(name)
339                     : (JmsDestination) new JmsTopic(name);
340                 Id = set.getLong(3);
341                 destination.setPersistent(true);
342                 cache(destination, Id);
343             }
344         } catch (Exception JavaDoc exception) {
345             throw new PersistenceException("Failed to load destinations",
346                                            exception);
347         } finally {
348             SQLHelper.close(set);
349             SQLHelper.close(select);
350         }
351     }
352
353     /**
354      * This method is used to cache a destination
355      *
356      * @param destination - the destination to cache
357      * @param Id - the destination identity
358      */

359     private void cache(JmsDestination destination, long Id) {
360         Pair pair = new Pair(destination, Id);
361
362         _destinations.put(destination.getName(), pair);
363         _ids.put(new Long JavaDoc(Id), pair);
364     }
365
366
367     /**
368      * This private static class holds the name and identity of the
369      * destination
370      */

371     private static class Pair {
372
373         public Pair(JmsDestination destination, long Id) {
374             this.destination = destination;
375             this.Id = Id;
376         }
377
378         public JmsDestination destination;
379         public long Id;
380     }
381 }
382
Popular Tags