1 11 12 package org.jivesoftware.util; 13 14 import java.util.*; 15 import java.util.concurrent.ConcurrentHashMap ; 16 import java.sql.*; 17 18 import org.jivesoftware.database.DbConnectionManager; 19 20 25 public class JiveProperties implements Map { 26 27 private static final String LOAD_PROPERTIES = "SELECT name, propValue FROM jiveProperty"; 28 private static final String INSERT_PROPERTY = "INSERT INTO jiveProperty(name, propValue) VALUES(?,?)"; 29 private static final String UPDATE_PROPERTY = "UPDATE jiveProperty SET propValue=? WHERE name=?"; 30 private static final String DELETE_PROPERTY = "DELETE FROM jiveProperty WHERE name LIKE ?"; 31 32 private static JiveProperties instance; 33 34 private Map <String , String > properties; 35 36 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 59 public void init() { 60 if (properties == null) { 61 properties = new ConcurrentHashMap <String , String >(); 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 (); 76 } 77 78 public boolean isEmpty() { 79 return properties.isEmpty(); 80 } 81 82 public boolean containsKey(Object key) { 83 return properties.containsKey(key); 84 } 85 86 public boolean containsValue(Object 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 t) { 95 for (Iterator i=t.entrySet().iterator(); i.hasNext(); ) { 96 Map.Entry entry = (Map.Entry )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 get(Object key) { 110 return properties.get(key); 111 } 112 113 123 public Collection<String > getChildrenNames(String parentKey) { 124 Collection<String > results = new HashSet<String >(); 125 for (String 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 name = parentKey + key.substring(parentKey.length(), dotIndex); 138 results.add(name); 139 } 140 } 141 } 142 return results; 143 } 144 145 150 public Collection<String > getPropertyNames() { 151 return properties.keySet(); 152 } 153 154 public synchronized Object remove(Object key) { 155 Object value = properties.remove(key); 156 Collection propNames = getPropertyNames(); 158 for (Iterator i=propNames.iterator(); i.hasNext(); ) { 159 String name = (String )i.next(); 160 if (name.startsWith((String )key)) { 161 properties.remove(name); 162 } 163 } 164 deleteProperty((String )key); 165 166 PropertyEventDispatcher.dispatchEvent((String )key, 168 PropertyEventDispatcher.EventType.property_deleted, Collections.emptyMap()); 169 170 return value; 171 } 172 173 public synchronized Object put(Object key, Object value) { 174 if (key == null || value == null) { 175 throw new NullPointerException ("Key or value cannot be null. Key=" + 176 key + ", value=" + value); 177 } 178 if (!(key instanceof String ) || !(value instanceof String )) { 179 throw new IllegalArgumentException ("Key and value must be of type String."); 180 } 181 if (((String )key).endsWith(".")) { 182 key = ((String )key).substring(0, ((String )key).length()-1); 183 } 184 key =((String )key).trim(); 185 if (properties.containsKey(key)) { 186 if (!properties.get(key).equals(value)) { 187 updateProperty((String )key, (String )value); 188 } 189 } 190 else { 191 insertProperty((String )key, (String )value); 192 } 193 194 Map params = new HashMap (); 196 params.put("value", value); 197 PropertyEventDispatcher.dispatchEvent((String )key, 198 PropertyEventDispatcher.EventType.property_set, params); 199 200 return properties.put((String )key, (String )value); 201 } 202 203 private void insertProperty(String name, String 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 e) { Log.error(e); } 219 try { if (con != null) { con.close(); } } 220 catch (Exception e) { Log.error(e); } 221 } 222 } 223 224 private void updateProperty(String name, String 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 e) { Log.error(e); } 240 try { if (con != null) { con.close(); } } 241 catch (Exception e) { Log.error(e); } 242 } 243 } 244 245 private void deleteProperty(String 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 e) { Log.error(e); } 260 try { if (con != null) { con.close(); } } 261 catch (Exception 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 name = rs.getString(1); 274 String value = rs.getString(2); 275 properties.put(name, value); 276 } 277 rs.close(); 278 } 279 catch (Exception e) { 280 Log.error(e); 281 } 282 finally { 283 try { if (pstmt != null) { pstmt.close(); } } 284 catch (Exception e) { Log.error(e); } 285 try { if (con != null) { con.close(); } } 286 catch (Exception e) { Log.error(e); } 287 } 288 } 289 } | Popular Tags |