From 419266d283def510f565a3791319391c3d1018ca Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Wed, 7 Aug 2013 00:00:59 -0400
Subject: [PATCH] Added generic database.

A new generic database class has been added to simplify in-memory
database creation. The token database has been refactored to inherit
this class.

Ticket #652
---
 .../DataCollection.java}                           | 31 ++++-----
 .../netscape/certsrv/token/TokenCollection.java    | 35 ++---------
 .../src/com/netscape/cmscore/dbs/Database.java     | 73 ++++++++++++++++++++++
 .../com/netscape/cmstools/token/TokenFindCLI.java  |  2 +-
 .../org/dogtagpki/tps/token/TokenDatabase.java     | 45 +++----------
 .../java/org/dogtagpki/tps/token/TokenService.java | 20 +++---
 6 files changed, 114 insertions(+), 92 deletions(-)
 copy base/common/src/com/netscape/certsrv/{token/TokenCollection.java => base/DataCollection.java} (71%)
 create mode 100644 base/common/src/com/netscape/cmscore/dbs/Database.java

diff --git a/base/common/src/com/netscape/certsrv/token/TokenCollection.java b/base/common/src/com/netscape/certsrv/base/DataCollection.java
similarity index 71%
copy from base/common/src/com/netscape/certsrv/token/TokenCollection.java
copy to base/common/src/com/netscape/certsrv/base/DataCollection.java
index c5be9977ffdae53a6f19862c02662134aad8d8cc..0237e8fccbc629565bd93768901f7b4845187591 100644
--- a/base/common/src/com/netscape/certsrv/token/TokenCollection.java
+++ b/base/common/src/com/netscape/certsrv/base/DataCollection.java
@@ -16,38 +16,37 @@
 // All rights reserved.
 // --- END COPYRIGHT BLOCK ---
 
-package com.netscape.certsrv.token;
+package com.netscape.certsrv.base;
 
 import java.util.ArrayList;
 import java.util.Collection;
 
 import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlElementRef;
-import javax.xml.bind.annotation.XmlRootElement;
 
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
-
 /**
  * @author Endi S. Dewata
  */
-@XmlRootElement(name="Tokens")
-public class TokenCollection {
+public class DataCollection<E> {
 
-    Collection<TokenData> tokens = new ArrayList<TokenData>();
+    Collection<E> entries = new ArrayList<E>();
     Collection<Link> links = new ArrayList<Link>();
 
-    @XmlElementRef
-    public Collection<TokenData> getTokens() {
-        return tokens;
+    public Collection<E> getEntries() {
+        return entries;
     }
 
-    public void setTokens(Collection<TokenData> tokens) {
-        this.tokens = tokens;
+    public void setEntries(Collection<E> entries) {
+        this.entries = entries;
     }
 
-    public void addToken(TokenData tokenData) {
-        tokens.add(tokenData);
+    public void addEntry(E entry) {
+        entries.add(entry);
+    }
+
+    public void removeEntry(E entry) {
+        entries.remove(entry);
     }
 
     @XmlElement(name="Link")
@@ -62,4 +61,8 @@ public class TokenCollection {
     public void addLink(Link link) {
         links.add(link);
     }
+
+    public void removeLink(Link link) {
+        links.remove(link);
+    }
 }
diff --git a/base/common/src/com/netscape/certsrv/token/TokenCollection.java b/base/common/src/com/netscape/certsrv/token/TokenCollection.java
index c5be9977ffdae53a6f19862c02662134aad8d8cc..e5de21f74637bda4844da082dd3879b25d3c087f 100644
--- a/base/common/src/com/netscape/certsrv/token/TokenCollection.java
+++ b/base/common/src/com/netscape/certsrv/token/TokenCollection.java
@@ -18,48 +18,21 @@
 
 package com.netscape.certsrv.token;
 
-import java.util.ArrayList;
 import java.util.Collection;
 
-import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlElementRef;
 import javax.xml.bind.annotation.XmlRootElement;
 
-import org.jboss.resteasy.plugins.providers.atom.Link;
-
+import com.netscape.certsrv.base.DataCollection;
 
 /**
  * @author Endi S. Dewata
  */
 @XmlRootElement(name="Tokens")
-public class TokenCollection {
-
-    Collection<TokenData> tokens = new ArrayList<TokenData>();
-    Collection<Link> links = new ArrayList<Link>();
+public class TokenCollection extends DataCollection<TokenData> {
 
     @XmlElementRef
-    public Collection<TokenData> getTokens() {
-        return tokens;
-    }
-
-    public void setTokens(Collection<TokenData> tokens) {
-        this.tokens = tokens;
-    }
-
-    public void addToken(TokenData tokenData) {
-        tokens.add(tokenData);
-    }
-
-    @XmlElement(name="Link")
-    public Collection<Link> getLinks() {
-        return links;
-    }
-
-    public void setLink(Collection<Link> links) {
-        this.links = links;
-    }
-
-    public void addLink(Link link) {
-        links.add(link);
+    public Collection<TokenData> getEntries() {
+        return super.getEntries();
     }
 }
diff --git a/base/common/src/com/netscape/cmscore/dbs/Database.java b/base/common/src/com/netscape/cmscore/dbs/Database.java
new file mode 100644
index 0000000000000000000000000000000000000000..5128298e67e69bb5ee2829e25e4dd2408d5fc94e
--- /dev/null
+++ b/base/common/src/com/netscape/cmscore/dbs/Database.java
@@ -0,0 +1,73 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmscore.dbs;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * This class implements in-memory token database. In the future this
+ * will be replaced with LDAP database.
+ *
+ * @author Endi S. Dewata
+ */
+public class Database<E> {
+
+    public final static int DEFAULT_SIZE = 20;
+
+    public String name ;
+    public Map<String, E> records = new LinkedHashMap<String, E>();
+
+    public Database(String name) {
+        this.name = name;
+    }
+
+    public Collection<E> getRecords() throws Exception {
+        return records.values();
+    }
+
+    public E getRecord(String id) throws Exception {
+        if (!records.containsKey(id)) {
+            throw new Exception(name + " " + id + " does not exist.");
+        }
+        return records.get(id);
+    }
+
+    public void addRecord(String id, E record) throws Exception {
+        if (records.containsKey(id)) {
+            throw new Exception(name + " " + id + " already exists.");
+        }
+        records.put(id, record);
+    }
+
+    public void updateRecord(String id, E record) throws Exception {
+        if (!records.containsKey(id)) {
+            throw new Exception(name + " " + id + " does not exist.");
+        }
+        records.put(id, record);
+    }
+
+    public void removeRecord(String id) throws Exception {
+        if (!records.containsKey(id)) {
+            throw new Exception(name + " " + id + " does not exist.");
+        }
+        records.remove(id);
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/token/TokenFindCLI.java b/base/java-tools/src/com/netscape/cmstools/token/TokenFindCLI.java
index a627d13fbeb11074e21284425b3455e3a21bdbd3..b1fa04f47ee63fc5470e2d9871c0b135d0b1c50e 100644
--- a/base/java-tools/src/com/netscape/cmstools/token/TokenFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/token/TokenFindCLI.java
@@ -72,7 +72,7 @@ public class TokenFindCLI extends CLI {
         Integer size = s == null ? null : Integer.valueOf(s);
 
         TokenCollection result = tokenCLI.tokenClient.findTokens(start, size);
-        Collection<TokenData> tokens = result.getTokens();
+        Collection<TokenData> tokens = result.getEntries();
 
         MainCLI.printMessage(tokens.size() + " token(s) matched");
 
diff --git a/base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java b/base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java
index 3db76649f34dd4d4fa88aa1d5fe5365560ef8e5c..7cc5aca78693a65b66fd945c234d04406849ea90 100644
--- a/base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java
+++ b/base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java
@@ -18,10 +18,9 @@
 
 package org.dogtagpki.tps.token;
 
-import java.util.Collection;
 import java.util.Date;
-import java.util.LinkedHashMap;
-import java.util.Map;
+
+import com.netscape.cmscore.dbs.Database;
 
 /**
  * This class implements in-memory token database. In the future this
@@ -29,48 +28,22 @@ import java.util.Map;
  *
  * @author Endi S. Dewata
  */
-public class TokenDatabase {
+public class TokenDatabase extends Database<TokenRecord> {
 
-    public final static int DEFAULT_SIZE = 20;
-
-    Map<String, TokenRecord> tokens = new LinkedHashMap<String, TokenRecord>();
-
-    public Collection<TokenRecord> getTokens() throws Exception {
-        return tokens.values();
+    public TokenDatabase() {
+        super("Token");
     }
 
-    public TokenRecord getToken(String tokenID) throws Exception {
-        if (!tokens.containsKey(tokenID)) {
-            throw new Exception("Token "+ tokenID + " does not exist.");
-        }
-        return tokens.get(tokenID);
-    }
-
-    public void addToken(TokenRecord tokenRecord) throws Exception {
-        if (tokens.containsKey(tokenRecord.getID())) {
-            throw new Exception("Token "+ tokenRecord.getID() + " already exists.");
-        }
-
+    public void addRecord(TokenRecord tokenRecord) throws Exception {
         tokenRecord.setStatus("ENABLED");
         tokenRecord.setCreateTimestamp(new Date());
 
-        tokens.put(tokenRecord.getID(), tokenRecord);
+        addRecord(tokenRecord.getID(), tokenRecord);
     }
 
-    public void updateToken(String tokenID, TokenRecord tokenRecord) throws Exception {
-        if (!tokens.containsKey(tokenRecord.getID())) {
-            throw new Exception("Token "+ tokenRecord.getID() + " does not exist.");
-        }
-
+    public void updateRecord(TokenRecord tokenRecord) throws Exception {
         tokenRecord.setModifyTimestamp(new Date());
 
-        tokens.put(tokenRecord.getID(), tokenRecord);
-    }
-
-    public void removeToken(String tokenID) throws Exception {
-        if (!tokens.containsKey(tokenID)) {
-            throw new Exception("Token "+ tokenID + " does not exist.");
-        }
-        tokens.remove(tokenID);
+        updateRecord(tokenRecord.getID(), tokenRecord);
     }
 }
diff --git a/base/tps/java/org/dogtagpki/tps/token/TokenService.java b/base/tps/java/org/dogtagpki/tps/token/TokenService.java
index bc8b35d59e5c66cee6f7759041cd4d1f3cf85cfb..c2787ebf5e6cfdc61152189df4248c1478ec88b2 100644
--- a/base/tps/java/org/dogtagpki/tps/token/TokenService.java
+++ b/base/tps/java/org/dogtagpki/tps/token/TokenService.java
@@ -100,7 +100,7 @@ public class TokenService extends PKIService implements TokenResource {
             TPSSubsystem subsystem = TPSSubsystem.getInstance();
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            Iterator<TokenRecord> tokens = database.getTokens().iterator();
+            Iterator<TokenRecord> tokens = database.getRecords().iterator();
 
             TokenCollection response = new TokenCollection();
 
@@ -111,7 +111,7 @@ public class TokenService extends PKIService implements TokenResource {
 
             // return entries up to the page size
             for ( ; i<start+size && tokens.hasNext(); i++) {
-                response.addToken(createTokenData(tokens.next()));
+                response.addEntry(createTokenData(tokens.next()));
             }
 
             // count the total entries
@@ -144,7 +144,7 @@ public class TokenService extends PKIService implements TokenResource {
             TPSSubsystem subsystem = TPSSubsystem.getInstance();
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            return createTokenData(database.getToken(tokenID));
+            return createTokenData(database.getRecord(tokenID));
 
         } catch (Exception e) {
             e.printStackTrace();
@@ -161,8 +161,8 @@ public class TokenService extends PKIService implements TokenResource {
             TPSSubsystem subsystem = TPSSubsystem.getInstance();
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            database.addToken(createTokenRecord(tokenData));
-            tokenData = createTokenData(database.getToken(tokenData.getID()));
+            database.addRecord(createTokenRecord(tokenData));
+            tokenData = createTokenData(database.getRecord(tokenData.getID()));
 
             return Response
                     .created(tokenData.getLink().getHref())
@@ -185,11 +185,11 @@ public class TokenService extends PKIService implements TokenResource {
             TPSSubsystem subsystem = TPSSubsystem.getInstance();
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            TokenRecord tokenRecord = database.getToken(tokenID);
+            TokenRecord tokenRecord = database.getRecord(tokenID);
             tokenRecord.setUserID(tokenData.getUserID());
-            database.updateToken(tokenData.getID(), tokenRecord);
+            database.updateRecord(tokenData.getID(), tokenRecord);
 
-            tokenData = createTokenData(database.getToken(tokenID));
+            tokenData = createTokenData(database.getRecord(tokenID));
 
             return Response
                     .ok(tokenData)
@@ -211,7 +211,7 @@ public class TokenService extends PKIService implements TokenResource {
             TPSSubsystem subsystem = TPSSubsystem.getInstance();
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            TokenRecord tokenRecord = database.getToken(tokenID);
+            TokenRecord tokenRecord = database.getRecord(tokenID);
             // TODO: perform modification
 
             TokenData tokenData = createTokenData(tokenRecord);
@@ -235,7 +235,7 @@ public class TokenService extends PKIService implements TokenResource {
         try {
             TPSSubsystem subsystem = TPSSubsystem.getInstance();
             TokenDatabase database = subsystem.getTokenDatabase();
-            database.removeToken(tokenID);
+            database.removeRecord(tokenID);
 
         } catch (Exception e) {
             e.printStackTrace();
-- 
1.8.1.4

