package org.cacert.gigi.dbObjects;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
+import org.cacert.gigi.GigiApiException;
import org.cacert.gigi.database.GigiPreparedStatement;
import org.cacert.gigi.database.GigiResultSet;
-public abstract class CertificateOwner implements IdCachable {
+public abstract class CertificateOwner implements IdCachable, Serializable {
+
+ private static final long serialVersionUID = -672580485730247314L;
private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
}
public static synchronized CertificateOwner getById(int id) {
- CertificateOwner u = myCache.get(id);
- if (u == null) {
- try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT *, `users`.`id` AS uid, `organisations`.`id` AS oid FROM `certOwners` LEFT JOIN `users` ON `users`.`id`=`certOwners`.`id` LEFT JOIN `organisations` ON `organisations`.`id` = `certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
- ps.setInt(1, id);
- try (GigiResultSet rs = ps.executeQuery()) {
- if ( !rs.next()) {
- return null;
- }
- if (rs.getString("uid") != null) {
- myCache.put(u = new User(rs));
- } else if (rs.getString("oid") != null) {
- myCache.put(u = new Organisation(rs));
- } else {
- System.err.print("Malformed cert owner: " + id);
- }
- }
+ CertificateOwner cached = myCache.get(id);
+ if (cached != null) {
+ return cached;
+ }
+
+ try (GigiPreparedStatement psU = new GigiPreparedStatement("SELECT *, `users`.`id` AS uid FROM `certOwners` INNER JOIN `users` ON `users`.`id`=`certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
+ psU.setInt(1, id);
+ GigiResultSet rsU = psU.executeQuery();
+ if (rsU.next()) {
+ return myCache.put(new User(rsU));
}
+ } catch (GigiApiException e) {
+ throw new Error(e);
}
- return u;
+
+ try (GigiPreparedStatement psO = new GigiPreparedStatement("SELECT *, `organisations`.`id` AS oid FROM `certOwners` INNER JOIN `organisations` ON `organisations`.`id`=`certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
+ psO.setInt(1, id);
+ GigiResultSet rsO = psO.executeQuery();
+ if (rsO.next()) {
+ return myCache.put(new Organisation(rsO));
+ }
+ } catch (GigiApiException e) {
+ throw new Error(e);
+ }
+
+ System.err.println("Malformed cert owner: " + id);
+ return null;
}
public Domain[] getDomains() {
}
public static CertificateOwner getByEnabledSerial(String serial) {
- try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` WHERE serial=? AND `disablelogin`='0' AND `revoked` is NULL")) {
- prep.setString(1, serial.toLowerCase());
+ try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` INNER JOIN `logincerts` ON `logincerts`.`id`=`certs`.`id` WHERE serial=? AND `revoked` is NULL")) {
+ prep.setString(1, serial);
GigiResultSet res = prep.executeQuery();
if (res.next()) {
return getById(res.getInt(1));
return null;
}
}
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {
+ oos.writeLong(getId());
+ }
+
+ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
+ id = (int) ois.readLong();
+ }
+
+ protected Object readResolve() throws ObjectStreamException {
+ /**
+ * Returning the Object by looking up its ID in the cache.
+ *
+ * @see http://www.javalobby.org/java/forums/t17491.html
+ * @see http://www.jguru.com/faq/view.jsp?EID=44039
+ * @see http://thecodersbreakfast.net/
+ * ?post/2011/05/12/Serialization-and-magic-methods
+ */
+ CertificateOwner co = getById(this.getId());
+
+ if (null == co) {
+ throw new Error("Unknown Certificate Owner");
+ }
+
+ return co;
+ }
+
}