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: PropertyStore.java,v 1.2 2005/10/20 14:07:03 tanderson Exp $
44   */
45  package org.exolab.jms.tools.migration.proxy;
46  
47  import java.sql.Connection;
48  import java.sql.PreparedStatement;
49  import java.sql.SQLException;
50  import java.sql.ResultSet;
51  import java.util.HashMap;
52  import java.util.Map;
53  
54  import org.exolab.jms.persistence.PersistenceException;
55  import org.exolab.jms.persistence.SQLHelper;
56  
57  
58  /***
59   * Stores migration version information.
60   *
61   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62   * @version $Revision: 1.2 $ $Date: 2005/10/20 14:07:03 $
63   */
64  public class PropertyStore implements DBConstants {
65  
66      /***
67       * The properties.
68       */
69      private final Map _properties = new HashMap();
70  
71      /***
72       * The database connection.
73       */
74      private final Connection _connection;
75  
76      /***
77       * Construct a new <code>PropertyStore</code>.
78       *
79       * @param connection the database connection
80       * @throws PersistenceException for any persistence error
81       */
82      public PropertyStore(Connection connection) throws PersistenceException {
83          _connection = connection;
84          init();
85      }
86  
87      /***
88       * Add a property.
89       *
90       * @param name the property name
91       * @param value the property value
92       * @throws PersistenceException for any persistence error
93       */
94      public void add(String name, String value) throws PersistenceException {
95          PreparedStatement insert = null;
96          try {
97              insert = _connection.prepareStatement(
98                      "insert into " + PROPERTIES_TABLE + " values (?, ?)");
99              insert.setString(1, name);
100             insert.setString(2, value);
101             insert.execute();
102 
103             _properties.put(name, value);
104         } catch (SQLException exception) {
105             throw new PersistenceException(
106                     "Failed to insert property, name=" + name
107                     + ", value=" + value, exception);
108         } finally {
109             SQLHelper.close(insert);
110         }
111     }
112 
113     /***
114      * Returns the value of a property.
115      *
116      * @param name the property name
117      * @return the value of the property, or <code>null</code> if it doesn't
118      * exist
119      */
120     public String get(String name) {
121         return (String) _properties.get(name);
122     }
123 
124     /***
125      * Loads the properties from the database.
126      *
127      * @throws PersistenceException for any error
128      */
129     private void init() throws PersistenceException {
130         PreparedStatement select = null;
131         ResultSet set = null;
132         try {
133             select = _connection.prepareStatement(
134                     "select name, value from " + PROPERTIES_TABLE);
135             set = select.executeQuery();
136             while (set.next()) {
137                 String name = set.getString(1);
138                 String value = set.getString(2);
139                 _properties.put(name, value);
140             }
141         } catch (SQLException exception) {
142             throw new PersistenceException("Failed to load properties",
143                                            exception);
144         } finally {
145             SQLHelper.close(set);
146             SQLHelper.close(select);
147         }
148     }
149 }