From 79390f9288d7c2653ab4f761dce3afb14876f6c5 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 12 Sep 2013 14:30:13 -0400
Subject: [PATCH] Connected token database to LDAP.

The TokenDatabase has been reimplemented using LDAPDatabase. The
TokenRecord have been modified to specify the object class and
attribute mappings.

Ticket #652
---
 .../src/org/dogtagpki/server/tps/TPSSubsystem.java | 10 +++++-
 .../dogtagpki/server/tps/token/TokenDatabase.java  | 36 +++++++---------------
 .../dogtagpki/server/tps/token/TokenRecord.java    | 19 +++++++++++-
 3 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
index 34edc58f46b134987333e155d33a11986ea63ad5..630bc23e56f7e587353c973fb31f9fbf119ddac9 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
@@ -32,9 +32,11 @@ import com.netscape.certsrv.authority.IAuthority;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IConfigStore;
 import com.netscape.certsrv.base.ISubsystem;
+import com.netscape.certsrv.dbs.IDBSubsystem;
 import com.netscape.certsrv.logging.ILogger;
 import com.netscape.certsrv.request.IRequestListener;
 import com.netscape.certsrv.request.IRequestQueue;
+import com.netscape.cmscore.dbs.DBSubsystem;
 
 /**
  * @author Endi S. Dewata <edewata@redhat.com>
@@ -54,7 +56,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public AuthenticatorDatabase authenticatorDatabase = new AuthenticatorDatabase();
     public ConnectionDatabase connectionDatabase = new ConnectionDatabase();
     public TPSCertDatabase certDatabase = new TPSCertDatabase();
-    public TokenDatabase tokenDatabase = new TokenDatabase();
+    public TokenDatabase tokenDatabase;
 
     @Override
     public String getId() {
@@ -70,6 +72,12 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public void init(ISubsystem owner, IConfigStore config) throws EBaseException {
         this.owner = owner;
         this.config = config;
+
+        IDBSubsystem dbSubsystem = DBSubsystem.getInstance();
+        IConfigStore cs = CMS.getConfigStore();
+        String tokenDatabaseDN = cs.getString("tokendb.baseDN");
+
+        tokenDatabase = new TokenDatabase(dbSubsystem, tokenDatabaseDN);
     }
 
     @Override
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java
index a4c8e34c5cb289378c51aac94a7121ea4dfe874a..1ee2a4a618265768c3c0198ed439c10fe8cb428e 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java
@@ -20,36 +20,17 @@ package org.dogtagpki.server.tps.token;
 
 import java.util.Date;
 
-import com.netscape.cmscore.dbs.Database;
+import com.netscape.certsrv.base.EBaseException;
+import com.netscape.certsrv.dbs.IDBSubsystem;
+import com.netscape.cmscore.dbs.LDAPDatabase;
 
 /**
- * This class implements in-memory token database. In the future this
- * will be replaced with LDAP database.
- *
  * @author Endi S. Dewata
  */
-public class TokenDatabase extends Database<TokenRecord> {
+public class TokenDatabase extends LDAPDatabase<TokenRecord> {
 
-    public TokenDatabase() {
-        super("Token");
-
-        // add sample records
-        try {
-            TokenRecord record1 = new TokenRecord();
-            record1.setID("token1");
-            record1.setUserID("user1");
-            record1.setStatus("ENABLED");
-            addRecord(record1);
-
-            TokenRecord record2 = new TokenRecord();
-            record2.setID("token2");
-            record2.setUserID("user2");
-            record2.setStatus("DISABLED");
-            addRecord(record2);
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+    public TokenDatabase(IDBSubsystem dbSubsystem, String baseDN) throws EBaseException {
+        super("Token", dbSubsystem, baseDN, TokenRecord.class);
     }
 
     public void addRecord(TokenRecord tokenRecord) throws Exception {
@@ -64,4 +45,9 @@ public class TokenDatabase extends Database<TokenRecord> {
 
         updateRecord(tokenRecord.getID(), tokenRecord);
     }
+
+    @Override
+    public String createDN(String id) {
+        return "cn" + "=" + id + "," + baseDN;
+    }
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java
index ff3af5776809cc51f616c90d56b630d01f175dc2..da40f95b04675b5ee219f89eadf45b1c794bd9d1 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java
@@ -20,10 +20,18 @@ package org.dogtagpki.server.tps.token;
 
 import java.util.Date;
 
+import com.netscape.cmscore.dbs.DBAttribute;
+import com.netscape.cmscore.dbs.DBObjectClasses;
+import com.netscape.cmscore.dbs.DBRecord;
+import com.netscape.cmscore.dbs.DateMapper;
+
 /**
  * @author Endi S. Dewata
  */
-public class TokenRecord {
+@DBObjectClasses({ "top", "tokenRecord", "extensibleObject" })
+public class TokenRecord extends DBRecord {
+
+    private static final long serialVersionUID = 1L;
 
     String id;
     String userID;
@@ -31,9 +39,11 @@ public class TokenRecord {
     String reason;
     String appletID;
     String keyInfo;
+
     Date createTimestamp;
     Date modifyTimestamp;
 
+    @DBAttribute(name="cn")
     public String getID() {
         return id;
     }
@@ -42,6 +52,7 @@ public class TokenRecord {
         this.id = id;
     }
 
+    @DBAttribute(name="tokenUserID")
     public String getUserID() {
         return userID;
     }
@@ -50,6 +61,7 @@ public class TokenRecord {
         this.userID = userID;
     }
 
+    @DBAttribute(name="tokenStatus")
     public String getStatus() {
         return status;
     }
@@ -58,6 +70,7 @@ public class TokenRecord {
         this.status = status;
     }
 
+    @DBAttribute(name="tokenReason")
     public String getReason() {
         return reason;
     }
@@ -66,6 +79,7 @@ public class TokenRecord {
         this.reason = reason;
     }
 
+    @DBAttribute(name="tokenAppletID")
     public String getAppletID() {
         return appletID;
     }
@@ -74,6 +88,7 @@ public class TokenRecord {
         this.appletID = appletID;
     }
 
+    @DBAttribute(name="keyInfo")
     public String getKeyInfo() {
         return keyInfo;
     }
@@ -82,6 +97,7 @@ public class TokenRecord {
         this.keyInfo = keyInfo;
     }
 
+    @DBAttribute(name="dateOfCreate", mapper=DateMapper.class)
     public Date getCreateTimestamp() {
         return createTimestamp;
     }
@@ -90,6 +106,7 @@ public class TokenRecord {
         this.createTimestamp = createTimestamp;
     }
 
+    @DBAttribute(name="dateOfModify", mapper=DateMapper.class)
     public Date getModifyTimestamp() {
         return modifyTimestamp;
     }
-- 
1.8.1.4

