Mercurial Hosting > nabble
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/model/SystemProperties.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,120 @@ +package nabble.model; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; +import java.util.TreeMap; + +/** + * Holds a Map with strings. + * Basically, this is used to persist keys and values used by the application. + * The cache Map is just a copy of the system table in the database. + * + * @author Hugo Teixeira + */ +public class SystemProperties { + + private static final Logger logger = LoggerFactory.getLogger(SystemProperties.class); + + private static final Map<String, String> cache = new TreeMap<String, String>(); + + static { + load(); + } + + private static void load() { + long start = System.currentTimeMillis(); + try { + Connection con = Db.dbGlobal().getConnection(); + try { + PreparedStatement stmt = con.prepareStatement("select * from system"); + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + String key = rs.getString("key_"); + String value = rs.getString("value"); + cache.put(key, value); + } + } finally { + con.close(); + } + logger.info("System properties loaded in " + (System.currentTimeMillis() - start) + " ms"); + } catch(SQLException e) { + logger.error(SystemProperties.class.toString(), e); + } + } + + public static synchronized void set(String key, String value) { + try { + Connection con = Db.dbGlobal().getConnection(); + try { + if (cache.containsKey(key)) { + PreparedStatement stmt = con.prepareStatement("update system set value=? where key_=?"); + stmt.setString(1, value); + stmt.setString(2, key); + stmt.execute(); + stmt.close(); + } else { + PreparedStatement stmt = con.prepareStatement("insert into system values (?,?)"); + stmt.setString(1, key); + stmt.setString(2, value); + stmt.execute(); + stmt.close(); + } + } finally { + con.close(); + } + cache.put(key, value); + logger.info("System Property added: " + key + " -- " + value); + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + public synchronized static String get(String key) { + return cache.get(key); + } + + public synchronized static int size() { + return cache.size(); + } + + public synchronized static void remove(String key) { + try { + String sql = "delete from system where key_='" + key + "';"; + Connection con = Db.dbGlobal().getConnection(); + try { + PreparedStatement stmt = con.prepareStatement(sql); + stmt.execute(); + } finally { + con.close(); + } + cache.remove(key); + logger.info("System Properties -- Key Removed = " + key); + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + public synchronized static void clear() { + try { + String sql = "delete from system"; + Connection con = Db.dbGlobal().getConnection(); + try { + PreparedStatement stmt = con.prepareStatement(sql); + stmt.execute(); + } finally { + con.close(); + } + cache.clear(); + logger.info("System Properties cleared"); + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + +}