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  package org.exolab.jms.tools.migration.proxy;
44  
45  import java.sql.Connection;
46  import java.sql.PreparedStatement;
47  import java.sql.ResultSet;
48  import java.sql.SQLException;
49  import java.util.ArrayList;
50  import java.util.List;
51  import javax.jms.JMSException;
52  
53  import org.exolab.jms.authentication.User;
54  import org.exolab.jms.persistence.PersistenceException;
55  import org.exolab.jms.persistence.SQLHelper;
56  import org.exolab.jms.tools.migration.IteratorAdapter;
57  import org.exolab.jms.tools.migration.Store;
58  import org.exolab.jms.tools.migration.StoreIterator;
59  
60  
61  /***
62   * Provides persistency for {@link User} instances.
63   *
64   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
65   * @version $Revision: 1.1 $ $Date: 2005/10/20 14:07:03 $
66   */
67  public class UserStore implements Store, DBConstants {
68  
69      /***
70       * The database connection.
71       */
72      private final Connection _connection;
73  
74  
75      /***
76       * Construct a new <code>UserStore</code>.
77       *
78       * @param connection the database connection
79       */
80      public UserStore(Connection connection) {
81          _connection = connection;
82      }
83  
84      /***
85       * Export the users.
86       *
87       * @return an iterator over the collection
88       * @throws JMSException         for any JMS error
89       * @throws PersistenceException for any persistence error
90       */
91      public StoreIterator exportCollection() throws JMSException,
92              PersistenceException {
93          List users = getUsers();
94          return new IteratorAdapter(users.iterator());
95      }
96  
97      /***
98       * Import users into the store.
99       *
100      * @param iterator an iterator over the collection
101      * @throws JMSException         for any JMS error
102      * @throws PersistenceException for any persistence error
103      */
104     public void importCollection(StoreIterator iterator) throws JMSException,
105             PersistenceException {
106         while (iterator.hasNext()) {
107             User user = (User) iterator.next();
108             add(user);
109         }
110     }
111 
112     /***
113      * Returns the number of elements in the collection.
114      *
115      * @return the number of elements in the collection
116      * @throws PersistenceException for any persistence error
117      */
118     public int size() throws PersistenceException {
119         return getUsers().size();
120     }
121 
122     /***
123      * Add a new user.
124      *
125      * @param user the user to add
126      * @throws PersistenceException for any persistence error
127      */
128     private void add(User user) throws PersistenceException {
129         PreparedStatement insert = null;
130         try {
131             insert = _connection.prepareStatement(
132                     "insert into " + USER_TABLE + " values (?, ?)");
133 
134             insert.setString(1, user.getUsername());
135             insert.setString(2, user.getPassword());
136             insert.executeUpdate();
137         } catch (SQLException exception) {
138             throw new PersistenceException("Failed to add consumer",
139                                            exception);
140         } finally {
141             SQLHelper.close(insert);
142         }
143     }
144 
145     /***
146      * Returns the users.
147      *
148      * @return a list of {@link User} instances
149      * @throws PersistenceException for any persistence error
150      */
151     private List getUsers() throws PersistenceException {
152         ArrayList result = new ArrayList();
153 
154         PreparedStatement select = null;
155         ResultSet set = null;
156         try {
157             select = _connection.prepareStatement(
158                     "select * from " + USER_TABLE);
159 
160             set = select.executeQuery();
161             while (set.next()) {
162                 String user = set.getString("username");
163                 String password = set.getString("password");
164                 result.add(new User(user, password));
165             }
166         } catch (SQLException exception) {
167             throw new PersistenceException("Failed to retrieve users",
168                                            exception);
169         } finally {
170             SQLHelper.close(set);
171             SQLHelper.close(select);
172         }
173         return result;
174     }
175 
176 }
177 
178 
179