>From fc39af6f9d884ec3c1609c5ab249397e8b68a78a 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 | 114 +++++++++++++++++++++ 1 file changed, 114 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..7d08fe9d3b75ceb954f3bd515eec5076d0af2db9 --- /dev/null +++ b/base/server/cmscore/src/com/netscape/cmscore/base/LDAPConfigStore.java @@ -0,0 +1,114 @@ +// --- 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 java.util.Map; + +import netscape.ldap.LDAPAttribute; +import netscape.ldap.LDAPConnection; +import netscape.ldap.LDAPEntry; +import netscape.ldap.LDAPModification; + +import com.netscape.certsrv.apps.CMS; +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.base.IConfigStore; +import com.netscape.certsrv.ldap.ILdapConnFactory; +import com.netscape.cmsutil.util.Utils; + +/** + * 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 static final long serialVersionUID = 3642124526598175633L; + + /** + * Constructs a file configuration store. + *

+ * + * @param dbFactory Database connection factory + * @param dn Distinguished name of record containing config store + * @param attr Name of attribute containing config store + * + * @exception EBaseException failed to create file configuration + */ + public LDAPConfigStore(ILdapConnFactory dbFactory, String dn, String attr) + throws EBaseException { + super(null); // top-level store without a name + + mDbFactory = dbFactory; + mDn = dn; + mAttr = attr; + + LDAPConnection conn = mDbFactory.getConn(); + + String[] attrs = {mAttr}; + LDAPEntry ldapEntry = conn.read(mDn, attrs); + InputStream data = new ByteArrayInputStream( (byte[]) + ldapEntry.getAttribute(mAttr).getByteValues().nextElement()); + load(data); + + conn.disconnect(); + } + + /** + * The original config file is copied to + * .. + * Commits the current properties to the configuration file. + *

+ * + * @param backup + */ + public void commit(boolean createBackup) throws EBaseException { + if (createBackup) { + // TODO back it up, perhaps to a user-specified attribute, + // or `mAttr + "_backup"`. + } + + LDAPConnection conn = mDbFactory.getConn(); + + ByteArrayOutputStream data = new ByteArrayOutputStream(); + save(data, null); + + LDAPAttribute ldapAttr = new LDAPAttribute(mAttr, data.toByteArray()); + LDAPModification ldapMod = + new LDAPModification(LDAPModification.REPLACE, ldapAttr); + conn.modify(mDn, ldapMod); + + conn.disconnect(); + } +} -- 1.9.3