>From 732286e87aadc1bca90232b4af8c29106cbb27eb Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Tue, 15 Jul 2014 02:48:35 -0400 Subject: [PATCH] add LDAPConfigStore class The LDAPConfigStore class is an IConfigStore that reads and writes its configuration to a given attribute and DN in an LDAP database. --- .../com/netscape/cmscore/base/LDAPConfigStore.java | 190 +++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 base/server/cmscore/src/com/netscape/cmscore/base/LDAPConfigStore.java diff --git a/base/server/cmscore/src/com/netscape/cmscore/base/LDAPConfigStore.java b/base/server/cmscore/src/com/netscape/cmscore/base/LDAPConfigStore.java new file mode 100644 index 0000000000000000000000000000000000000000..8ff477bb3cdad02092c56834e043bcd6e0108625 --- /dev/null +++ b/base/server/cmscore/src/com/netscape/cmscore/base/LDAPConfigStore.java @@ -0,0 +1,190 @@ +// --- 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) 2007, 2014 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmscore.base; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.IOException; + +import netscape.ldap.LDAPAttribute; +import netscape.ldap.LDAPAttributeSet; +import netscape.ldap.LDAPConnection; +import netscape.ldap.LDAPEntry; +import netscape.ldap.LDAPException; +import netscape.ldap.LDAPModification; + +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.base.IConfigStore; +import com.netscape.certsrv.ldap.ILdapConnFactory; + +/** + * LDAPConfigStore: + * Extends PropConfigStore with methods to load/save from/to file for + * persistent storage. This is a configuration store agent who + * reads data from an LDAP entry. + *

+ * + * @version $Revision$, $Date$ + * @see PropConfigStore + */ +public class LDAPConfigStore extends PropConfigStore implements IConfigStore { + + private ILdapConnFactory mDbFactory; + private String mDn; + private String mAttr; + private LDAPAttribute[] mAttrs; + private boolean mInDatabase; + + /** + * + */ + private static final long serialVersionUID = 3642124526598175633L; + + /** + * Constructs a file configuration store. + *

+ * + * @param dbFactory Database connection factory + * @param cn Common name of record containing config store + * @param dn Distinguished name of record containing config store + * @param attr Name of attribute containing config store + * @param attrs Set of initial attributes if creating the entry. Should + * contain cn, objectclass and possibly other attributes. + * + * @exception EBaseException failed to create file configuration + */ + public LDAPConfigStore( + ILdapConnFactory dbFactory, + String dn, LDAPAttribute[] attrs, String attr + ) throws EBaseException { + super(null); // top-level store without a name + + mDbFactory = dbFactory; + mDn = dn; + mAttrs = attrs; + mAttr = attr; + + LDAPConnection conn = mDbFactory.getConn(); + + String[] readAttrs = {mAttr}; + try { + LDAPEntry ldapEntry = conn.read(mDn, readAttrs); + + InputStream data = new ByteArrayInputStream( (byte[]) + ldapEntry.getAttribute(mAttr).getByteValues().nextElement()); + load(data); + } + catch (LDAPException e) { + // if there is no such object, we will create it on commit() + if (e.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT) { + throw new EBaseException( + "Error reading LDAPConfigStore '" + + mDn + "': " + e.toString() + ); + } + } + catch (IOException e) { + throw new EBaseException( + "Error reading LDAPConfigStore '" + + mDn + "': " + e.toString() + ); + } + finally { + mDbFactory.returnConn(conn); + } + } + + /** + * Commit the configuration to the database. + * + * All uses of LDAPProfileStore at time of writing call with + * backup=false, so the argument is ignored. + * + * If backup becomes necessary, the constructor should be + * modified to take a String backupAttr, and the existing + * content be copied to that attribute. + * + * @param backup Ignored. + */ + public void commit(boolean createBackup) throws EBaseException { + ByteArrayOutputStream data = new ByteArrayOutputStream(); + save(data, null); + + LDAPAttribute configAttr = new LDAPAttribute(mAttr, data.toByteArray()); + + LDAPConnection conn = mDbFactory.getConn(); + + // first attempt to modify; if modification fails (due + // to no such object), try and add the entry instead. + try { + try { + commitModify(conn, configAttr); + } + catch (LDAPException e) { + if (e.getLDAPResultCode() == LDAPException.NO_SUCH_OBJECT) { + commitAdd(conn, configAttr); + } + else { + throw e; + } + } + } + catch (LDAPException e) { + throw new EBaseException( + "Error writing LDAPConfigStore '" + + mDn + "': " + e.toString() + ); + } + finally { + mDbFactory.returnConn(conn); + } + } + + /** + * Update the record via an LDAPModification. + * + * @param conn LDAP connection. + * @param configAttr Config store attribute. + * @return true on success, false if the entry does not exist. + */ + private void commitModify(LDAPConnection conn, LDAPAttribute configAttr) + throws LDAPException + { + LDAPModification ldapMod = + new LDAPModification(LDAPModification.REPLACE, configAttr); + conn.modify(mDn, ldapMod); + } + + /** + * Add the LDAPEntry via LDAPConnection.add. + * + * @param conn LDAP connection. + * @param configAttr Config store attribute. + * @return true on success, false if the entry already exists. + */ + private void commitAdd(LDAPConnection conn, LDAPAttribute configAttr) + throws LDAPException + { + LDAPAttributeSet attrSet = new LDAPAttributeSet(mAttrs); + attrSet.add(configAttr); + LDAPEntry ldapEntry = new LDAPEntry(mDn, attrSet); + conn.add(ldapEntry); + } +} -- 1.9.3