From f38a6081b518657710b6024da536142412cf55be Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Thu, 18 Aug 2016 19:52:14 +0200 Subject: [PATCH] fix: make domain initialization pattern compliant to other dbObject Change-Id: I07ee56f1b63e6da3c5dc11e65be4ccdcbad0aca5 --- src/org/cacert/gigi/dbObjects/Domain.java | 27 +++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/org/cacert/gigi/dbObjects/Domain.java b/src/org/cacert/gigi/dbObjects/Domain.java index eecf3766..043277cf 100644 --- a/src/org/cacert/gigi/dbObjects/Domain.java +++ b/src/org/cacert/gigi/dbObjects/Domain.java @@ -17,18 +17,10 @@ public class Domain implements IdCachable, Verifyable { private int id; - private Domain(int id) { - try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) { - ps.setInt(1, id); - - GigiResultSet rs = ps.executeQuery(); - if ( !rs.next()) { - throw new IllegalArgumentException("Invalid domain id " + id); - } - this.id = id; - owner = CertificateOwner.getById(rs.getInt(1)); - suffix = rs.getString(2); - } + private Domain(GigiResultSet rs, int id) { + this.id = id; + owner = CertificateOwner.getById(rs.getInt(1)); + suffix = rs.getString(2); } public Domain(User actor, CertificateOwner owner, String suffix) throws GigiApiException { @@ -163,10 +155,17 @@ public class Domain implements IdCachable, Verifyable { private static final ObjectCache myCache = new ObjectCache<>(); - public static synchronized Domain getById(int id) throws IllegalArgumentException { + public static synchronized Domain getById(int id) { Domain em = myCache.get(id); if (em == null) { - myCache.put(em = new Domain(id)); + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if ( !rs.next()) { + return null; + } + myCache.put(em = new Domain(rs, id)); + } } return em; } -- 2.39.2