From 8059f2f157a133c5e1073b0b3f5f2eeb9db74e88 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Fri, 5 Jun 2015 19:02:25 +0200 Subject: [PATCH] fix: Properly handle initialization of profiles and finalize them when done --- .../gigi/dbObjects/CertificateProfile.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/org/cacert/gigi/dbObjects/CertificateProfile.java b/src/org/cacert/gigi/dbObjects/CertificateProfile.java index d9a9e34f..5ac1f761 100644 --- a/src/org/cacert/gigi/dbObjects/CertificateProfile.java +++ b/src/org/cacert/gigi/dbObjects/CertificateProfile.java @@ -22,9 +22,9 @@ public class CertificateProfile implements IdCachable { private final String visibleName; - private static final HashMap byName = new HashMap<>(); + private static final Map byName; - private static final HashMap byId = new HashMap<>(); + private static final Map byId; private final Map pt; @@ -176,13 +176,17 @@ public class CertificateProfile implements IdCachable { } static { + final HashMap myName = new HashMap(); + final HashMap myId = new HashMap(); + for (File f : new File("config/profiles").listFiles()) { Properties p = new Properties(); try { p.load(new FileInputStream(f)); } catch (IOException e) { - e.printStackTrace(); + throw new Error("Unable to load profile from " + f.getName(), e); } + String[] parts = f.getName().split("\\.")[0].split("-", 2); GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT keyname, include, requires, name FROM `profiles` WHERE id=?"); ps.setInt(1, Integer.parseInt(parts[0])); @@ -207,16 +211,18 @@ public class CertificateProfile implements IdCachable { insert.setInt(5, Integer.parseInt(parts[0])); insert.execute(); } - } + GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, keyname, name, requires, include FROM `profiles`"); GigiResultSet rs = ps.executeQuery(); while (rs.next()) { CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"), rs.getString("requires"), rs.getString("include")); - byId.put(cp.getId(), cp); - byName.put(cp.getKeyName(), cp); + myId.put(cp.getId(), cp); + myName.put(cp.getKeyName(), cp); } + byName = Collections.unmodifiableMap(myName); + byId = Collections.unmodifiableMap(myId); } public static CertificateProfile getById(int id) { -- 2.39.2