1 /********************************************************************************
2 *InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 package base.jdbs.cryptography;
21
22 import java.io.File;
23 import java.security.KeyStore;
24 import java.util.Collection;
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27
28 import org.apache.log4j.Logger;
29
30
31 public abstract class KeyRing<K> {
32
33 private static final transient Logger logger = Logger.getLogger(KeyRing.class.getName());
34
35 /***The AsymmetricKeyRings' element collection.**/
36 private Hashtable<Integer, K> element = new Hashtable<Integer , K>();
37
38 /***The current element id.**/
39 private int currentElementId = 0;
40
41 /***
42 * This method computes a new Id for a KeyPair.
43 * @return A new Id for a KeyPair.
44 */
45 protected Integer nextElementId(){
46 return ++currentElementId;
47 }
48
49 /***
50 * This method adds to the KeyRing the target object.
51 * @param id The id associated to the object.
52 * @param object The object to be added.
53 */
54 protected void add(Integer id, K object){
55 this.element.put(id, object);
56 }
57
58 /***
59 * This method removes a stored element from the KeyRing.
60 * @param id The id associated to the element to remove.
61 */
62 protected void remove(Integer id){
63 this.element.remove(id);
64 }
65
66 /***
67 * This method removes all the stored elements from the KeyRing.
68 */
69 protected void removeAll(){
70 Enumeration keys = element.keys();
71 for(;keys.hasMoreElements();)
72 this.element.remove(keys.nextElement());
73 }
74
75 /***
76 * This method retrieves a stored element by id.
77 * @param id The id associated to the element to retrieve.
78 * @return The stored element otherwise null.
79 */
80 protected K get(Integer id){
81 return this.element.get(id);
82 }
83
84 /***
85 * This method returns a collection of all stored elements.
86 * @return A collection of stored elements.
87 */
88 protected Collection<K> getAll(){
89 return this.element.values();
90 }
91
92 /***
93 * This method saves the content of the key ring in a file system location.
94 * @param location The location where the key ring must be stored.
95 * @param password The password associated to the file.
96 */
97 public abstract void store(File location, String password);
98
99 /***
100 * This method retrieves the content of a stored key ring from a file system location.
101 * @param location The location from wich the content of the key ring must be retrieved.
102 * @param password The password associated to the file.
103 * @return The KeyStore associated to the input location, null if the input location isn't a valid key store archive.
104 */
105 public abstract KeyStore retrieve(File location, String password);
106
107 }