View Javadoc

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 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: MasterConsumerStore.java,v 1.2 2005/10/20 14:07:03 tanderson Exp $
44   */
45  package org.exolab.jms.tools.migration.master;
46  
47  import java.sql.Connection;
48  import java.util.Collection;
49  import java.util.Enumeration;
50  import java.util.HashMap;
51  import java.util.Iterator;
52  import java.util.NoSuchElementException;
53  import java.util.Vector;
54  import javax.jms.JMSException;
55  
56  import org.exolab.jms.client.JmsDestination;
57  import org.exolab.jms.client.JmsQueue;
58  import org.exolab.jms.client.JmsTopic;
59  import org.exolab.jms.message.MessageImpl;
60  import org.exolab.jms.messagemgr.MessageHandle;
61  import org.exolab.jms.messagemgr.PersistentMessageHandle;
62  import org.exolab.jms.persistence.DatabaseService;
63  import org.exolab.jms.persistence.PersistenceAdapter;
64  import org.exolab.jms.persistence.PersistenceException;
65  import org.exolab.jms.tools.migration.Store;
66  import org.exolab.jms.tools.migration.StoreIterator;
67  import org.exolab.jms.tools.migration.proxy.Consumer;
68  import org.exolab.jms.tools.migration.proxy.MessageState;
69  import org.exolab.jms.tools.migration.proxy.Subscription;
70  
71  
72  /***
73   * <code>MasterConsumerStore</code> manages a collection of persistent
74   * consumers.
75   *
76   * @author <a href="mailto:tma#netspace.net.au">Tim Anderson</a>
77   * @version $Revision: 1.2 $ $Date: 2005/10/20 14:07:03 $
78   */
79  public class MasterConsumerStore implements Store {
80  
81      /***
82       * The database service.
83       */
84      private DatabaseService _database;
85  
86  
87      /***
88       * Construct a new <code>MasterConsumerStore</code>.
89       *
90       * @param database the database service
91       */
92      public MasterConsumerStore(DatabaseService database) {
93          _database = database;
94      }
95  
96      /***
97       * Export the consumers.
98       *
99       * @return an iterator over the collection
100      * @throws JMSException         for any JMS error
101      * @throws PersistenceException for any persistence error
102      */
103     public StoreIterator exportCollection() throws JMSException,
104                                                    PersistenceException {
105         Collection consumers = getConsumers();
106         return new ConsumerIterator(consumers);
107     }
108 
109     /***
110      * Import consumers into the store.
111      *
112      * @param iterator an iterator over the collection
113      * @throws JMSException         for any JMS error
114      * @throws PersistenceException for any persistence error
115      */
116     public void importCollection(StoreIterator iterator) throws JMSException,
117                                                                 PersistenceException {
118         while (iterator.hasNext()) {
119             Consumer consumer = (Consumer) iterator.next();
120             add(consumer);
121         }
122     }
123 
124     /***
125      * Returns the number of elements in the collection.
126      *
127      * @return the number of elements in the collection
128      * @throws PersistenceException for any persistence error
129      */
130     public int size() throws PersistenceException {
131         return getConsumers().size();
132     }
133 
134     /***
135      * Returns the list of consumers.
136      *
137      * @return a list of {@link Consumer} instances.
138      * @throws PersistenceException for any PersistenceError
139      */
140     private Collection getConsumers() throws PersistenceException {
141         Enumeration destinations;
142 
143         // Need to jump through some hoops to get a list of all consumers
144         Connection connection = _database.getConnection();
145         destinations = _database.getAdapter().getAllDestinations(connection);
146 
147         HashMap consumers = new HashMap();
148 
149         while (destinations.hasMoreElements()) {
150             JmsDestination destination =
151                     (JmsDestination) destinations.nextElement();
152             if (destination instanceof JmsTopic) {
153                 Enumeration names = _database.getAdapter().getDurableConsumers(
154                         connection, destination.getName());
155                 while (names.hasMoreElements()) {
156                     String name = (String) names.nextElement();
157                     Consumer consumer = (Consumer) consumers.get(name);
158                     if (consumer == null) {
159                         consumer = new Consumer(name, null);
160                         consumers.put(name, consumer);
161                     }
162                     Subscription subscription = getSubscription(name,
163                                                                 destination);
164                     consumer.addSubscription(subscription);
165                 }
166             } else {
167                 final String name = destination.getName();
168                 Consumer consumer = (Consumer) consumers.get(name);
169                 if (consumer == null) {
170                     consumer = new Consumer((JmsQueue) destination);
171                     consumers.put(name, consumer);
172                 }
173                 Subscription subscription = getSubscription(name, destination);
174                 consumer.addSubscription(subscription);
175             }
176         }
177         _database.commit();
178         return consumers.values();
179     }
180 
181     private Subscription getSubscription(String name,
182                                          JmsDestination destination)
183             throws PersistenceException {
184 
185         Subscription result = new Subscription(destination);
186         Connection connection = _database.getConnection();
187 
188         Vector handles = _database.getAdapter().getMessageHandles(connection,
189                                                                   destination,
190                                                                   name);
191         Iterator iterator = handles.iterator();
192         while (iterator.hasNext()) {
193             MessageHandle handle = (MessageHandle) iterator.next();
194             String id = handle.getMessageId();
195             result.addMessage(id, handle.getDelivered());
196         }
197         return result;
198     }
199 
200     private void add(Consumer consumer) throws JMSException,
201                                                PersistenceException {
202 
203         Iterator iterator = consumer.getSubscriptions().iterator();
204         while (iterator.hasNext()) {
205             Subscription subscription = (Subscription) iterator.next();
206             add(consumer, subscription);
207         }
208     }
209 
210     private void add(Consumer consumer, Subscription subscription)
211             throws JMSException, PersistenceException {
212 
213         String name = consumer.getName();
214         JmsDestination destination = subscription.getDestination();
215         Iterator iterator = subscription.getMessages().iterator();
216         PersistenceAdapter adapter = _database.getAdapter();
217         Connection connection = _database.getConnection();
218 
219         if (!consumer.isQueueConsumer()) {
220             adapter.addDurableConsumer(connection, destination.getName(), name);
221 
222         }
223 
224         while (iterator.hasNext()) {
225             MessageState state = (MessageState) iterator.next();
226             MessageImpl message = adapter.getMessage(connection,
227                                                      state.getMessageId());
228             PersistentMessageHandle handle =
229                     new PersistentMessageHandle(message.getJMSMessageID(),
230                                                 message.getJMSPriority(),
231                                                 message.getAcceptedTime(),
232                                                 message.getSequenceNumber(),
233                                                 message.getJMSExpiration(),
234                                                 destination,
235                                                 name);
236             handle.setDelivered(state.getDelivered());
237             handle.add();
238         }
239         _database.commit();
240     }
241 
242     private static class ConsumerIterator implements StoreIterator {
243 
244         /***
245          * The iterator over the consumer collection.
246          */
247         private final Iterator _iterator;
248 
249         /***
250          * Construct a new <code>ConsumerIterator</code>.
251          *
252          * @param consumers a collection of {@link Consumer} instances.
253          */
254         public ConsumerIterator(Collection consumers) {
255             _iterator = consumers.iterator();
256         }
257 
258         /***
259          * Returns <tt>true</tt> if the iterator has more elements.
260          *
261          * @return <tt>true</tt> if the iterator has more elements.
262          */
263         public boolean hasNext() {
264             return _iterator.hasNext();
265         }
266 
267         /***
268          * Returns the next element in the interation.
269          *
270          * @return the next element in the iteration.
271          * @throws PersistenceException   for any persistence error
272          * @throws NoSuchElementException iteration has no more elements.
273          */
274         public Object next() throws PersistenceException {
275             return _iterator.next();
276         }
277     }
278 
279 }