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: MasterMessageStore.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.sql.PreparedStatement;
49  import java.sql.ResultSet;
50  import java.sql.SQLException;
51  import java.util.ArrayList;
52  import java.util.Iterator;
53  import java.util.List;
54  import javax.jms.JMSException;
55  
56  import org.exolab.jms.message.MessageImpl;
57  import org.exolab.jms.persistence.DatabaseService;
58  import org.exolab.jms.persistence.PersistenceException;
59  import org.exolab.jms.persistence.SQLHelper;
60  import org.exolab.jms.tools.migration.Store;
61  import org.exolab.jms.tools.migration.StoreIterator;
62  
63  
64  /***
65   * <code>MasterMessageStore</code> manages a collection of persistent
66   * messages.
67   *
68   * @author <a href="mailto:tma#netspace.net.au">Tim Anderson</a>
69   * @version $Revision: 1.2 $ $Date: 2005/10/20 14:07:03 $
70   */
71  public class MasterMessageStore implements Store {
72  
73      /***
74       * The database service.
75       */
76      private DatabaseService _database;
77  
78  
79      /***
80       * Construct a new <code>MasterDestinationStore</code>.
81       *
82       * @param database the database service
83       */
84      public MasterMessageStore(DatabaseService database) {
85          _database = database;
86      }
87  
88      /***
89       * Export the messages.
90       *
91       * @return an iterator over the collection
92       * @throws JMSException         for any JMS error
93       * @throws PersistenceException for any persistence error
94       */
95      public StoreIterator exportCollection() throws JMSException,
96                                                     PersistenceException {
97          List ids = getMessageIds();
98          return new MessageIterator(ids);
99      }
100 
101     /***
102      * Import messages into the store.
103      *
104      * @param iterator an iterator over the collection
105      * @throws JMSException         for any JMS error
106      * @throws PersistenceException for any persistence error
107      */
108     public void importCollection(StoreIterator iterator)
109             throws JMSException, PersistenceException {
110         Connection connection = _database.getConnection();
111         while (iterator.hasNext()) {
112             MessageImpl message = (MessageImpl) iterator.next();
113             _database.getAdapter().addMessage(connection, message);
114         }
115         _database.commit();
116     }
117 
118     /***
119      * Returns the number of elements in the collection.
120      *
121      * @return the number of elements in the collection
122      * @throws PersistenceException for any persistence error
123      */
124     public int size() throws PersistenceException {
125         return getMessageIds().size();
126     }
127 
128     /***
129      * Returns a list of all message identifiers.
130      *
131      * @return a list of all message identifiers.
132      * @throws PersistenceException for any persistence error
133      */
134     private List getMessageIds() throws PersistenceException {
135         List result = new ArrayList(1000);
136         final String query
137                 = "select messageId from messages order by createTime";
138         Connection connection = _database.getConnection();
139         PreparedStatement select = null;
140         ResultSet set = null;
141 
142         try {
143             select = connection.prepareStatement(query);
144             set = select.executeQuery();
145             while (set.next()) {
146                 result.add(set.getString(1));
147             }
148         } catch (SQLException exception) {
149             throw new PersistenceException("Failed to execute query: " + query,
150                                            exception);
151         } finally {
152             SQLHelper.close(set);
153             SQLHelper.close(select);
154         }
155         _database.commit();
156         return result;
157     }
158 
159 
160     private class MessageIterator implements StoreIterator {
161         private final Iterator _iterator;
162 
163         public MessageIterator(List ids) {
164             _iterator = ids.iterator();
165         }
166 
167         public boolean hasNext() {
168             return _iterator.hasNext();
169         }
170 
171         public Object next() throws PersistenceException {
172             MessageImpl result = null;
173 
174             String id = (String) _iterator.next();
175 
176             Connection connection = _database.getConnection();
177             result = _database.getAdapter().getMessage(connection, id);
178             _database.commit();
179             return result;
180         }
181 
182     }
183 
184 }