Mercurial Hosting > nabble
comparison src/nabble/model/SystemProperties.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.model; | |
2 | |
3 import org.slf4j.Logger; | |
4 import org.slf4j.LoggerFactory; | |
5 | |
6 import java.sql.Connection; | |
7 import java.sql.PreparedStatement; | |
8 import java.sql.ResultSet; | |
9 import java.sql.SQLException; | |
10 import java.util.Map; | |
11 import java.util.TreeMap; | |
12 | |
13 /** | |
14 * Holds a Map with strings. | |
15 * Basically, this is used to persist keys and values used by the application. | |
16 * The cache Map is just a copy of the system table in the database. | |
17 * | |
18 * @author Hugo Teixeira | |
19 */ | |
20 public class SystemProperties { | |
21 | |
22 private static final Logger logger = LoggerFactory.getLogger(SystemProperties.class); | |
23 | |
24 private static final Map<String, String> cache = new TreeMap<String, String>(); | |
25 | |
26 static { | |
27 load(); | |
28 } | |
29 | |
30 private static void load() { | |
31 long start = System.currentTimeMillis(); | |
32 try { | |
33 Connection con = Db.dbGlobal().getConnection(); | |
34 try { | |
35 PreparedStatement stmt = con.prepareStatement("select * from system"); | |
36 ResultSet rs = stmt.executeQuery(); | |
37 while (rs.next()) { | |
38 String key = rs.getString("key_"); | |
39 String value = rs.getString("value"); | |
40 cache.put(key, value); | |
41 } | |
42 } finally { | |
43 con.close(); | |
44 } | |
45 logger.info("System properties loaded in " + (System.currentTimeMillis() - start) + " ms"); | |
46 } catch(SQLException e) { | |
47 logger.error(SystemProperties.class.toString(), e); | |
48 } | |
49 } | |
50 | |
51 public static synchronized void set(String key, String value) { | |
52 try { | |
53 Connection con = Db.dbGlobal().getConnection(); | |
54 try { | |
55 if (cache.containsKey(key)) { | |
56 PreparedStatement stmt = con.prepareStatement("update system set value=? where key_=?"); | |
57 stmt.setString(1, value); | |
58 stmt.setString(2, key); | |
59 stmt.execute(); | |
60 stmt.close(); | |
61 } else { | |
62 PreparedStatement stmt = con.prepareStatement("insert into system values (?,?)"); | |
63 stmt.setString(1, key); | |
64 stmt.setString(2, value); | |
65 stmt.execute(); | |
66 stmt.close(); | |
67 } | |
68 } finally { | |
69 con.close(); | |
70 } | |
71 cache.put(key, value); | |
72 logger.info("System Property added: " + key + " -- " + value); | |
73 } catch(SQLException e) { | |
74 throw new RuntimeException(e); | |
75 } | |
76 } | |
77 | |
78 public synchronized static String get(String key) { | |
79 return cache.get(key); | |
80 } | |
81 | |
82 public synchronized static int size() { | |
83 return cache.size(); | |
84 } | |
85 | |
86 public synchronized static void remove(String key) { | |
87 try { | |
88 String sql = "delete from system where key_='" + key + "';"; | |
89 Connection con = Db.dbGlobal().getConnection(); | |
90 try { | |
91 PreparedStatement stmt = con.prepareStatement(sql); | |
92 stmt.execute(); | |
93 } finally { | |
94 con.close(); | |
95 } | |
96 cache.remove(key); | |
97 logger.info("System Properties -- Key Removed = " + key); | |
98 } catch(SQLException e) { | |
99 throw new RuntimeException(e); | |
100 } | |
101 } | |
102 | |
103 public synchronized static void clear() { | |
104 try { | |
105 String sql = "delete from system"; | |
106 Connection con = Db.dbGlobal().getConnection(); | |
107 try { | |
108 PreparedStatement stmt = con.prepareStatement(sql); | |
109 stmt.execute(); | |
110 } finally { | |
111 con.close(); | |
112 } | |
113 cache.clear(); | |
114 logger.info("System Properties cleared"); | |
115 } catch(SQLException e) { | |
116 throw new RuntimeException(e); | |
117 } | |
118 } | |
119 | |
120 } |