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: DataLoader.java,v 1.2 2005/11/12 12:52:06 tanderson Exp $
44 */
45 package org.exolab.jms.tools.migration;
46
47 import java.io.InputStream;
48 import java.sql.Connection;
49 import javax.jms.JMSException;
50 import javax.jms.DeliveryMode;
51
52 import org.exolab.jms.client.JmsDestination;
53 import org.exolab.jms.client.JmsQueue;
54 import org.exolab.jms.client.JmsTopic;
55 import org.exolab.jms.config.Configuration;
56 import org.exolab.jms.config.ConfigurationReader;
57 import org.exolab.jms.message.*;
58 import org.exolab.jms.messagemgr.MessageHandle;
59 import org.exolab.jms.messagemgr.PersistentMessageHandle;
60 import org.exolab.jms.persistence.DatabaseService;
61 import org.exolab.jms.persistence.PersistenceAdapter;
62 import org.exolab.jms.persistence.PersistenceException;
63 import org.exolab.jms.service.ServiceException;
64 import org.exolab.jms.tools.db.DBTool;
65 import org.exolab.jms.authentication.User;
66
67
68 /***
69 * Loads up the master database with data, for testing purposes.
70 *
71 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
72 * @version $Revision: 1.2 $ $Date: 2005/11/12 12:52:06 $
73 * @see ExportImportTest
74 */
75 public class DataLoader {
76
77 /***
78 * The database service.
79 */
80 private final DatabaseService _service;
81
82 /***
83 * The persistence adapter.
84 */
85 private final PersistenceAdapter _adapter;
86
87 /***
88 * The connection to use.
89 */
90 private final Connection _connection;
91
92 /***
93 * JMSTimestamp seed.
94 */
95 private long _timestampSeed = 0;
96
97 /***
98 * JMSPriority seed.
99 */
100 private int _prioritySeed = 0;
101
102 private final JmsTopic _topic1 = new JmsTopic("topic1");
103 private final JmsQueue _queue1 = new JmsQueue("queue1");
104
105
106 /***
107 * Construct a new <code>DataLoader</code>.
108 *
109 * @param config the configuration to use
110 * @throws ServiceException for any error
111 */
112 public DataLoader(Configuration config) throws ServiceException {
113 DBTool tool = new DBTool(config);
114 tool.drop();
115 tool.create();
116
117 _service = new DatabaseService(config);
118 _service.start();
119 _adapter = _service.getAdapter();
120 _connection = _service.getConnection();
121 _topic1.setPersistent(true);
122 _queue1.setPersistent(true);
123 }
124
125 /***
126 * Load the database with data.
127 *
128 * @throws JMSException for any JMS error
129 * @throws PersistenceException for any persistence error
130 * @throws ServiceException for any service error
131 */
132 public void load() throws JMSException, PersistenceException,
133 ServiceException{
134 addDestination(_queue1);
135 addDestination(_topic1);
136
137 final String body = "Hello world!";
138 BytesMessageImpl bytes = new BytesMessageImpl();
139 bytes.writeUTF(body);
140
141 MapMessageImpl map = new MapMessageImpl();
142 map.setString("key", body);
143
144 MessageImpl message = new MessageImpl();
145
146 ObjectMessageImpl object = new ObjectMessageImpl();
147 object.setObject(body);
148
149 StreamMessageImpl stream = new StreamMessageImpl();
150 stream.writeString(body);
151
152 TextMessageImpl text = new TextMessageImpl();
153 text.setText(body);
154
155 MessageImpl[] messages = new MessageImpl[]{
156 bytes, map, message, object,
157 stream, text
158 };
159 _adapter.addDurableConsumer(_connection, _topic1.getName(), "sub1");
160
161
162 for (int i = 0; i < messages.length; ++i) {
163 MessageImpl msg = messages[i];
164 addMessage(msg, _queue1);
165 MessageHandle handle = new PersistentMessageHandle(msg,
166 _queue1.getName());
167 _adapter.addMessageHandle(_connection, handle);
168
169 addMessage(msg, _topic1);
170 handle = new PersistentMessageHandle(msg, "sub1");
171 _adapter.addMessageHandle(_connection, handle);
172 }
173
174 addUser("admin", "openjms");
175 addUser("user1", "secret");
176
177 _service.commit();
178 _service.stop();
179 }
180
181 public static void main(String[] args) throws Exception {
182 InputStream stream = DataLoader.class.getResourceAsStream(
183 "/openjmstest.xml");
184 Configuration config = ConfigurationReader.read(stream);
185 DataLoader loader = new DataLoader(config);
186 loader.load();
187 }
188
189 /***
190 * Add a destination to the database.
191 *
192 * @param destination the destination to add
193 * @throws PersistenceException for any error
194 */
195 private void addDestination(JmsDestination destination)
196 throws PersistenceException {
197 boolean queue = false;
198 if (destination instanceof JmsQueue) {
199 queue = true;
200 }
201 _adapter.addDestination(_connection, destination.getName(), queue);
202 }
203
204 /***
205 * Add a message to the database.
206 *
207 * @param message the message to add
208 * @param destination the message's destination
209 * @throws JMSException for any JMS error
210 * @throws PersistenceException for any persistence error
211 */
212 private void addMessage(MessageImpl message, JmsDestination destination)
213 throws JMSException, PersistenceException {
214 message.setJMSMessageID(MessageId.create());
215 message.setJMSDestination(destination);
216 message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
217 message.setJMSTimestamp(_timestampSeed);
218 message.setJMSPriority(_prioritySeed);
219 _adapter.addMessage(_connection, message);
220
221 ++_timestampSeed;
222 _prioritySeed = (_prioritySeed + 1) % 10;
223 }
224
225 /***
226 * Add an user to the database.
227 *
228 * @param user the user name
229 * @param password the user's password
230 * @throws PersistenceException for any error
231 */
232 private void addUser(String user, String password)
233 throws PersistenceException {
234 _adapter.addUser(_connection, new User(user, password));
235 }
236 }