diff options
author | Endi S. Dewata <edewata@redhat.com> | 2013-10-07 11:48:54 -0400 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2013-10-25 17:17:39 -0400 |
commit | 2119f1b218e9d68b13496e7042785d9c68753966 (patch) | |
tree | b8c7cf5692723340d8d56e5d8c401acdee059ca5 /base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java | |
parent | 7ca5adf1bd5bc4f9a7c5f2035426b9158007bb28 (diff) | |
download | pki-2119f1b218e9d68b13496e7042785d9c68753966.tar.gz pki-2119f1b218e9d68b13496e7042785d9c68753966.tar.xz pki-2119f1b218e9d68b13496e7042785d9c68753966.zip |
Reorganized server packages.
The tomcat, cms, and cmscore packages have been moved from base/common
into separate folders in base/server so that they can be built separately.
Diffstat (limited to 'base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java')
-rw-r--r-- | base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java b/base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java new file mode 100644 index 000000000..f6ded787b --- /dev/null +++ b/base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java @@ -0,0 +1,171 @@ +package com.netscape.cmscore.dbs; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Enumeration; + +import com.netscape.certsrv.apps.CMS; +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.dbs.IDBAttrMapper; +import com.netscape.certsrv.dbs.IDBObj; +import com.netscape.certsrv.dbs.IDBRegistry; +import com.netscape.certsrv.dbs.IDBSSession; +import com.netscape.certsrv.dbs.IDBSearchResults; +import com.netscape.certsrv.dbs.IDBSubsystem; +import com.netscape.certsrv.dbs.Modification; +import com.netscape.certsrv.dbs.ModificationSet; + +/** + * This class implements LDAP database. + * + * @author Endi S. Dewata + */ +public abstract class LDAPDatabase<E extends IDBObj> extends Database<E> { + + public IDBSubsystem dbSubsystem; + public String baseDN; + public Class<E> recordType; + + public LDAPDatabase(String name, IDBSubsystem dbSubsystem, String baseDN, Class<E> recordType) throws EBaseException { + super(name); + + this.dbSubsystem = dbSubsystem; + this.baseDN = baseDN; + this.recordType = recordType; + + register(recordType); + } + + public IDBAttrMapper createMapper(Class<?> attrType, DBAttribute dbAttribute) throws EBaseException { + String attrName = dbAttribute.value(); + + if (attrType == String.class) { + return new StringMapper(attrName); + + } else if (attrType == Date.class) { + return new DateMapper(attrName); + + } else { + // TODO: add other mappers + throw new EBaseException("Unsupported attribute type: " + attrType); + } + } + + public void register(Class<E> recordType) throws EBaseException { + IDBRegistry dbRegistry = dbSubsystem.getRegistry(); + + // register object classes + DBObjectClasses dbObjectClasses = recordType.getAnnotation(DBObjectClasses.class); + if (dbObjectClasses == null) { + throw new EBaseException("Missing object class mapping in " + recordType.getName()); + } + dbRegistry.registerObjectClass(recordType.getName(), dbObjectClasses.value()); + + // register attributes defined in setters/getters + for (Method method : recordType.getMethods()) { + DBAttribute dbAttribute = method.getAnnotation(DBAttribute.class); + if (dbAttribute == null) continue; + + String name = method.getName(); + if (!name.matches("^set.+") && !name.matches("^get.+")) continue; + + // get attribute name from method name + name = Character.toLowerCase(name.charAt(3)) + name.substring(4); + + Class<?> attrType = method.getReturnType(); + IDBAttrMapper mapper = createMapper(attrType, dbAttribute); + + dbRegistry.registerAttribute(name, mapper); + } + + // register attributes defined in fields + for (Field field : recordType.getFields()) { + DBAttribute dbAttribute = field.getAnnotation(DBAttribute.class); + if (dbAttribute == null) continue; + + String name = field.getName(); + Class<?> attrType = field.getType(); + IDBAttrMapper mapper = createMapper(attrType, dbAttribute); + + dbRegistry.registerAttribute(name, mapper); + } + } + + public abstract String createDN(String id); + public abstract String createFilter(String filter); + + @Override + public Collection<E> getRecords() throws Exception { + CMS.debug("LDAPDatabase: getRecords()"); + + try (IDBSSession session = dbSubsystem.createSession()) { + Collection<E> list = new ArrayList<E>(); + + CMS.debug("LDAPDatabase: searching " + baseDN); + IDBSearchResults results = session.search(baseDN, createFilter(null)); + + while (results.hasMoreElements()) { + @SuppressWarnings("unchecked") + E result = (E)results.nextElement(); + list.add(result); + } + + return list; + } + } + + @SuppressWarnings("unchecked") + @Override + public E getRecord(String id) throws Exception { + CMS.debug("LDAPDatabase: getRecord(\"" + id + "\")"); + try (IDBSSession session = dbSubsystem.createSession()) { + String dn = createDN(id); + CMS.debug("LDAPDatabase: reading " + baseDN); + return (E)session.read(dn); + } + } + + @Override + public void addRecord(String id, E record) throws Exception { + CMS.debug("LDAPDatabase: addRecord(\"" + id + "\")"); + try (IDBSSession session = dbSubsystem.createSession()) { + String dn = createDN(id); + + CMS.debug("LDAPDatabase: adding " + dn); + session.add(dn, record); + } + } + + @Override + public void updateRecord(String id, E record) throws Exception { + CMS.debug("LDAPDatabase: updateRecord(\"" + id + "\")"); + try (IDBSSession session = dbSubsystem.createSession()) { + String dn = createDN(id); + + ModificationSet mods = new ModificationSet(); + for (Enumeration<String> names = record.getSerializableAttrNames(); names.hasMoreElements(); ) { + String name = names.nextElement(); + Object value = record.get(name); + mods.add(name, Modification.MOD_REPLACE, value); + } + + CMS.debug("LDAPDatabase: modifying " + dn); + session.modify(dn, mods); + } + } + + @Override + public void removeRecord(String id) throws Exception { + CMS.debug("LDAPDatabase: removeRecord(\"" + id + "\")"); + try (IDBSSession session = dbSubsystem.createSession()) { + String dn = createDN(id); + + CMS.debug("LDAPDatabase: removing " + dn); + session.delete(dn); + } + } + +} |