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 package org.exolab.jms.tools.migration.proxy; 44 45 import java.sql.PreparedStatement; 46 import java.sql.ResultSet; 47 import java.sql.SQLException; 48 import java.sql.Connection; 49 import java.util.ArrayList; 50 import java.util.Iterator; 51 import java.util.List; 52 import javax.jms.JMSException; 53 import javax.jms.Message; 54 55 import org.exolab.jms.persistence.PersistenceException; 56 import org.exolab.jms.persistence.SQLHelper; 57 import org.exolab.jms.tools.migration.Store; 58 import org.exolab.jms.tools.migration.StoreIterator; 59 60 61 /*** 62 * Provides persistency for {@link Message} instances. 63 * 64 * @author <a href="mailto:tma#netspace.net.au">Tim Anderson</a> 65 * @version $Revision: 1.1 $ $Date: 2005/09/04 07:07:12 $ 66 */ 67 public class MessageStore implements Store, DBConstants { 68 69 /*** 70 * The destination store. 71 */ 72 private final DestinationStore _destinations; 73 74 /*** 75 * The database connection. 76 */ 77 private final Connection _connection; 78 79 80 /*** 81 * Construct a new <code>MessageStore</code>. 82 * 83 * @param destinations the destination store 84 * @param connection the database connection. 85 */ 86 public MessageStore(DestinationStore destinations, Connection connection) { 87 _destinations = destinations; 88 _connection = connection; 89 } 90 91 /*** 92 * Export the collection. 93 * 94 * @return an iterator over the collection 95 * @throws JMSException for any JMS error 96 * @throws PersistenceException for any persistence error 97 */ 98 public StoreIterator exportCollection() throws JMSException, 99 PersistenceException { 100 List messageIds = getMessageIds(); 101 return new MessageIterator(messageIds); 102 } 103 104 105 /*** 106 * Import a collection. 107 * 108 * @param iterator an iterator over the collection 109 * @throws JMSException for any JMS error 110 * @throws PersistenceException for any persistence error 111 */ 112 public void importCollection(StoreIterator iterator) throws JMSException, 113 PersistenceException { 114 115 while (iterator.hasNext()) { 116 Message message = (Message) iterator.next(); 117 add(message); 118 } 119 } 120 121 /*** 122 * Returns the number of elements in the collection. 123 * 124 * @return the number of elements in the collection 125 * @throws PersistenceException for any persistence error 126 */ 127 public int size() throws PersistenceException { 128 return getMessageIds().size(); 129 } 130 131 /*** 132 * Add a message. 133 * 134 * @param message the message to add 135 * @throws JMSException for any JMS error 136 * @throws PersistenceException for any persistence error 137 */ 138 public synchronized void add(Message message) 139 throws JMSException, PersistenceException { 140 141 MessageHandler handler = MessageHandlerFactory.create( 142 message, _destinations, _connection); 143 144 handler.add(message); 145 } 146 147 /*** 148 * Returns a message for a given identifier. 149 * 150 * @param messageId the identity of the message 151 * @return the message corresponding to <code>messageId</code> or 152 * <code>null</code> if no such message exists 153 * @throws PersistenceException for any persistence error 154 */ 155 public Message get(String messageId) 156 throws JMSException, PersistenceException { 157 Message result = null; 158 PreparedStatement select = null; 159 ResultSet set = null; 160 try { 161 select = _connection.prepareStatement( 162 "select message_type from " + MESSAGE_TABLE 163 + " where message_id = ?"); 164 select.setString(1, messageId); 165 set = select.executeQuery(); 166 if (set.next()) { 167 String type = set.getString("message_type"); 168 String qualifiedType = "javax.jms." + type; 169 MessageHandler handler = MessageHandlerFactory.create( 170 qualifiedType, _destinations, _connection); 171 result = handler.get(messageId); 172 } 173 } catch (SQLException exception) { 174 throw new PersistenceException( 175 "Failed to get message with JMSMessageID=" + messageId, 176 exception); 177 } finally { 178 SQLHelper.close(set); 179 SQLHelper.close(select); 180 } 181 return result; 182 } 183 184 /*** 185 * Returns all message identifiers. 186 * 187 * @return a list of message identifiers 188 * @throws PersistenceException for any persistence error 189 */ 190 public List getMessageIds() throws PersistenceException { 191 ArrayList result = new ArrayList(); 192 193 PreparedStatement select = null; 194 ResultSet set = null; 195 try { 196 select = _connection.prepareStatement( 197 "select message_id from " + MESSAGE_TABLE); 198 199 set = select.executeQuery(); 200 while (set.next()) { 201 String messageId = set.getString("message_id"); 202 result.add(messageId); 203 } 204 } catch (SQLException exception) { 205 throw new PersistenceException("Failed to get message ids", 206 exception); 207 } finally { 208 SQLHelper.close(set); 209 SQLHelper.close(select); 210 } 211 return result; 212 } 213 214 private class MessageIterator implements StoreIterator { 215 216 private Iterator _iterator; 217 218 public MessageIterator(List messageIds) { 219 _iterator = messageIds.iterator(); 220 } 221 222 public boolean hasNext() { 223 return _iterator.hasNext(); 224 } 225 226 public Object next() throws JMSException, PersistenceException { 227 String messageId = (String) _iterator.next(); 228 229 return get(messageId); 230 } 231 232 } 233 234 } 235 236 237