KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > JiveProperties


1 /**
2  * $RCSfile: JiveProperties.java,v $
3  * $Revision: 1.4 $
4  * $Date: 2005/04/11 21:05:19 $
5  *
6  * Copyright (C) 1999-2004 Jive Software. All rights reserved.
7  *
8  * This software is the proprietary information of Jive Software.
9  * Use is subject to license terms.
10  */

11
12 package org.jivesoftware.util;
13
14 import java.util.*;
15 import java.util.concurrent.ConcurrentHashMap JavaDoc;
16 import java.sql.*;
17
18 import org.jivesoftware.database.DbConnectionManager;
19
20 /**
21  * Retrieves and stores Jive properties. Properties are stored in the database.
22  *
23  * @author Matt Tucker
24  */

25 public class JiveProperties implements Map JavaDoc {
26
27     private static final String JavaDoc LOAD_PROPERTIES = "SELECT name, propValue FROM jiveProperty";
28     private static final String JavaDoc INSERT_PROPERTY = "INSERT INTO jiveProperty(name, propValue) VALUES(?,?)";
29     private static final String JavaDoc UPDATE_PROPERTY = "UPDATE jiveProperty SET propValue=? WHERE name=?";
30     private static final String JavaDoc DELETE_PROPERTY = "DELETE FROM jiveProperty WHERE name LIKE ?";
31
32     private static JiveProperties instance;
33
34     private Map JavaDoc<String JavaDoc, String JavaDoc> properties;
35
36     /**
37      * Returns a singleton instance of JiveProperties.
38      *
39      * @return an instance of JiveProperties.
40      */

41     public static synchronized JiveProperties getInstance() {
42         if (instance == null) {
43             instance = new JiveProperties();
44         }
45         return instance;
46     }
47
48     private JiveProperties() {
49         init();
50     }
51
52     /**
53      * For internal use only. This method allows for the reloading of all properties from the
54      * values in the datatabase. This is required since it's quite possible during the setup
55      * process that a database connection will not be available till after this class is
56      * initialized. Thus, if there are existing properties in the database we will want to reload
57      * this class after the setup process has been completed.
58      */

59     public void init() {
60         if (properties == null) {
61             properties = new ConcurrentHashMap JavaDoc<String JavaDoc, String JavaDoc>();
62         }
63         else {
64             properties.clear();
65         }
66
67         loadProperties();
68     }
69
70     public int size() {
71         return properties.size();
72     }
73
74     public void clear() {
75         throw new UnsupportedOperationException JavaDoc();
76     }
77
78     public boolean isEmpty() {
79         return properties.isEmpty();
80     }
81
82     public boolean containsKey(Object JavaDoc key) {
83         return properties.containsKey(key);
84     }
85
86     public boolean containsValue(Object JavaDoc value) {
87         return properties.containsValue(value);
88     }
89
90     public Collection values() {
91         return Collections.unmodifiableCollection(properties.values());
92     }
93
94     public void putAll(Map JavaDoc t) {
95         for (Iterator i=t.entrySet().iterator(); i.hasNext(); ) {
96             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
97             put(entry.getKey(), entry.getValue());
98         }
99     }
100
101     public Set entrySet() {
102         return Collections.unmodifiableSet(properties.entrySet());
103     }
104
105     public Set keySet() {
106         return Collections.unmodifiableSet(properties.keySet());
107     }
108
109     public Object JavaDoc get(Object JavaDoc key) {
110         return properties.get(key);
111     }
112
113     /**
114      * Return all children property names of a parent property as a Collection
115      * of String objects. For example, given the properties <tt>X.Y.A</tt>,
116      * <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>, then the child properties of
117      * <tt>X.Y</tt> are <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>. The method
118      * is not recursive; ie, it does not return children of children.
119      *
120      * @param parentKey the name of the parent property.
121      * @return all child property names for the given parent.
122      */

123     public Collection<String JavaDoc> getChildrenNames(String JavaDoc parentKey) {
124         Collection<String JavaDoc> results = new HashSet<String JavaDoc>();
125         for (String JavaDoc key : properties.keySet()) {
126             if (key.startsWith(parentKey + ".")) {
127                 if (key.equals(parentKey)) {
128                     continue;
129                 }
130                 int dotIndex = key.indexOf(".", parentKey.length()+1);
131                 if (dotIndex < 1) {
132                     if (!results.contains(key)) {
133                         results.add(key);
134                     }
135                 }
136                 else {
137                     String JavaDoc name = parentKey + key.substring(parentKey.length(), dotIndex);
138                     results.add(name);
139                 }
140             }
141         }
142         return results;
143     }
144
145     /**
146      * Returns all property names as a Collection of String values.
147      *
148      * @return all property names.
149      */

150     public Collection<String JavaDoc> getPropertyNames() {
151         return properties.keySet();
152     }
153
154     public synchronized Object JavaDoc remove(Object JavaDoc key) {
155         Object JavaDoc value = properties.remove(key);
156         // Also remove any children.
157
Collection propNames = getPropertyNames();
158         for (Iterator i=propNames.iterator(); i.hasNext(); ) {
159             String JavaDoc name = (String JavaDoc)i.next();
160             if (name.startsWith((String JavaDoc)key)) {
161                 properties.remove(name);
162             }
163         }
164         deleteProperty((String JavaDoc)key);
165
166         // Generate event.
167
PropertyEventDispatcher.dispatchEvent((String JavaDoc)key,
168                 PropertyEventDispatcher.EventType.property_deleted, Collections.emptyMap());
169
170         return value;
171     }
172
173     public synchronized Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
174         if (key == null || value == null) {
175             throw new NullPointerException JavaDoc("Key or value cannot be null. Key=" +
176                     key + ", value=" + value);
177         }
178         if (!(key instanceof String JavaDoc) || !(value instanceof String JavaDoc)) {
179             throw new IllegalArgumentException JavaDoc("Key and value must be of type String.");
180         }
181         if (((String JavaDoc)key).endsWith(".")) {
182             key = ((String JavaDoc)key).substring(0, ((String JavaDoc)key).length()-1);
183         }
184         key =((String JavaDoc)key).trim();
185         if (properties.containsKey(key)) {
186             if (!properties.get(key).equals(value)) {
187                 updateProperty((String JavaDoc)key, (String JavaDoc)value);
188             }
189         }
190         else {
191             insertProperty((String JavaDoc)key, (String JavaDoc)value);
192         }
193
194         // Generate event.
195
Map JavaDoc params = new HashMap JavaDoc();
196         params.put("value", value);
197         PropertyEventDispatcher.dispatchEvent((String JavaDoc)key,
198                 PropertyEventDispatcher.EventType.property_set, params);
199
200         return properties.put((String JavaDoc)key, (String JavaDoc)value);
201     }
202
203     private void insertProperty(String JavaDoc name, String JavaDoc value) {
204         Connection con = null;
205         PreparedStatement pstmt = null;
206         try {
207             con = DbConnectionManager.getConnection();
208             pstmt = con.prepareStatement(INSERT_PROPERTY);
209             pstmt.setString(1, name);
210             pstmt.setString(2, value);
211             pstmt.executeUpdate();
212         }
213         catch (SQLException e) {
214             Log.error(e);
215         }
216         finally {
217             try { if (pstmt != null) { pstmt.close(); } }
218             catch (Exception JavaDoc e) { Log.error(e); }
219             try { if (con != null) { con.close(); } }
220             catch (Exception JavaDoc e) { Log.error(e); }
221         }
222     }
223
224     private void updateProperty(String JavaDoc name, String JavaDoc value) {
225         Connection con = null;
226         PreparedStatement pstmt = null;
227         try {
228             con = DbConnectionManager.getConnection();
229             pstmt = con.prepareStatement(UPDATE_PROPERTY);
230             pstmt.setString(1, value);
231             pstmt.setString(2, name);
232             pstmt.executeUpdate();
233         }
234         catch (SQLException e) {
235             Log.error(e);
236         }
237         finally {
238             try { if (pstmt != null) { pstmt.close(); } }
239             catch (Exception JavaDoc e) { Log.error(e); }
240             try { if (con != null) { con.close(); } }
241             catch (Exception JavaDoc e) { Log.error(e); }
242         }
243     }
244
245     private void deleteProperty(String JavaDoc name) {
246         Connection con = null;
247         PreparedStatement pstmt = null;
248         try {
249             con = DbConnectionManager.getConnection();
250             pstmt = con.prepareStatement(DELETE_PROPERTY);
251             pstmt.setString(1, name + "%");
252             pstmt.executeUpdate();
253         }
254         catch (SQLException e) {
255             Log.error(e);
256         }
257         finally {
258             try { if (pstmt != null) { pstmt.close(); } }
259             catch (Exception JavaDoc e) { Log.error(e); }
260             try { if (con != null) { con.close(); } }
261             catch (Exception JavaDoc e) { Log.error(e); }
262         }
263     }
264
265     private void loadProperties() {
266         Connection con = null;
267         PreparedStatement pstmt = null;
268         try {
269             con = DbConnectionManager.getConnection();
270             pstmt = con.prepareStatement(LOAD_PROPERTIES);
271             ResultSet rs = pstmt.executeQuery();
272             while (rs.next()) {
273                 String JavaDoc name = rs.getString(1);
274                 String JavaDoc value = rs.getString(2);
275                 properties.put(name, value);
276             }
277             rs.close();
278         }
279         catch (Exception JavaDoc e) {
280             Log.error(e);
281         }
282         finally {
283             try { if (pstmt != null) { pstmt.close(); } }
284             catch (Exception JavaDoc e) { Log.error(e); }
285             try { if (con != null) { con.close(); } }
286             catch (Exception JavaDoc e) { Log.error(e); }
287         }
288     }
289 }
Popular Tags