diff options
author | Endi S. Dewata <edewata@redhat.com> | 2013-08-07 00:00:59 -0400 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2013-08-20 14:23:18 -0400 |
commit | 6d99354ce9e2f1250538eb31a8b7e2e788518892 (patch) | |
tree | fc3af79644a1d8ad311502a4e38f7896693c1fba /base/common/src | |
parent | 4a2880f466451d8089409d83474a7339bffffe25 (diff) | |
download | pki-6d99354ce9e2f1250538eb31a8b7e2e788518892.tar.gz pki-6d99354ce9e2f1250538eb31a8b7e2e788518892.tar.xz pki-6d99354ce9e2f1250538eb31a8b7e2e788518892.zip |
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
Diffstat (limited to 'base/common/src')
3 files changed, 145 insertions, 31 deletions
diff --git a/base/common/src/com/netscape/certsrv/base/DataCollection.java b/base/common/src/com/netscape/certsrv/base/DataCollection.java new file mode 100644 index 000000000..0237e8fcc --- /dev/null +++ b/base/common/src/com/netscape/certsrv/base/DataCollection.java @@ -0,0 +1,68 @@ +// --- 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.certsrv.base; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.xml.bind.annotation.XmlElement; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +/** + * @author Endi S. Dewata + */ +public class DataCollection<E> { + + Collection<E> entries = new ArrayList<E>(); + Collection<Link> links = new ArrayList<Link>(); + + public Collection<E> getEntries() { + return entries; + } + + public void setEntries(Collection<E> entries) { + this.entries = entries; + } + + public void addEntry(E entry) { + entries.add(entry); + } + + public void removeEntry(E entry) { + entries.remove(entry); + } + + @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 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 c5be9977f..e5de21f74 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 000000000..5128298e6 --- /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); + } +} |