KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > catoken > HardCATokenManager


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.model.ca.catoken;
15
16 import java.util.Collection JavaDoc;
17 import java.util.Hashtable JavaDoc;
18
19 import org.apache.log4j.Logger;
20 import org.ejbca.core.model.InternalResources;
21
22
23 /**
24  * Class managing available Hard CA Tokens and instansiated CA Tokens.
25  * Each HardCaToken plug-in should register itself by using the method register.
26  * The CA keeps a registry of CA tokens created here.
27  *
28  * @version $Id: HardCATokenManager.java,v 1.6 2006/12/13 10:34:09 anatom Exp $
29  *
30  */

31 public class HardCATokenManager {
32     
33     /** Log4j instance for Base */
34     private static transient Logger log = Logger.getLogger(HardCATokenManager.class);
35     /** Internal localization of logs and errors */
36     private static final InternalResources intres = InternalResources.getInstance();
37
38     /** Registry of available hard ca token classes that can be instsiated. */
39     private Hashtable JavaDoc availablehardcatokens = new Hashtable JavaDoc();
40     /** Registry of CATokens associated with a specific CA, kept so CATokens will not
41      * be destroyed when a bean is passivated for example. */

42     private Hashtable JavaDoc caTokenRegistry = new Hashtable JavaDoc();
43
44     /** Implementing the Singleton pattern */
45     private static HardCATokenManager instance = null;
46
47     /**
48      * Static intialization block used to register all plug-in classes to the manager.
49      * All new plug-ins should add a loadClass call with it's classpath to this method.
50      */

51     static {
52         HardCATokenManager.instance().addAvailableHardCAToken("org.ejbca.core.model.ca.catoken.NFastCAToken", "NFastCAToken", false, true);
53         HardCATokenManager.instance().addAvailableHardCAToken("se.primeKey.caToken.card.PrimeCAToken", "PrimeCAToken", false, true);
54         HardCATokenManager.instance().addAvailableHardCAToken("org.ejbca.core.model.ca.catoken.EracomCAToken", "Eracom", false, true);
55         HardCATokenManager.instance().addAvailableHardCAToken("org.ejbca.core.model.ca.catoken.SafeNetLunaCAToken", "SafeNetLunaCAToken", false, true);
56         HardCATokenManager.instance().addAvailableHardCAToken("org.ejbca.core.model.ca.catoken.DummyHardCAToken", "DummyHardCAToken", false, false);
57         HardCATokenManager.instance().addAvailableHardCAToken("org.ejbca.core.model.ca.catoken.HardCATokenSample", "HardCATokenSample", false, false);
58     }
59
60     /** Don't allow external creation of this class, implementing the Singleton pattern.
61      */

62     private HardCATokenManager() {}
63     
64     /** Get the instance of this singleton
65      *
66      */

67     public synchronized static HardCATokenManager instance() {
68         if (instance == null) {
69             instance = new HardCATokenManager();
70         }
71         return instance;
72     }
73     
74     /** Returns a previously registered (using addCAToken) CAToken, or null.
75      *
76      * @param caid the id of the CA whose CAToken you want to fetch.
77      * @return The previously added CAToken or null if the token does not exist in the registry.
78      */

79     public CAToken getCAToken(int caid) {
80         return (CAToken)caTokenRegistry.get(new Integer JavaDoc(caid));
81     }
82     
83     /** Adds a CA token to the token registry. If a token already exists for the given CAid,
84      * the old one is removed and replaced with the new. If the token passed is null, an existing token is removed.
85      *
86      * @param caid the id of the CA whose CAToken you want to fetch.
87      * @param token the token to be added
88      */

89     public synchronized void addCAToken(int caid, CAToken token) {
90         if (caTokenRegistry.containsKey(new Integer JavaDoc(caid))) {
91             caTokenRegistry.remove(new Integer JavaDoc(caid));
92             log.debug("Removed old CA token for CA: "+caid);
93         }
94         if (token != null) {
95             caTokenRegistry.put(new Integer JavaDoc(caid), token);
96             log.debug("Added CA token for CA: "+caid);
97         }
98     }
99     
100     
101     /**
102      * Method registering a HardCAToken plug-in as available to the system.
103      *
104      * @param classpath the classpath of the plug-in
105      * @param name the general name used in adminweb-gui.
106      * @param translateable indicates if the name should be translated in adminweb-gui
107      * @param use indicates if this plug-in should be used.
108      *
109      * @return true if registration went successful, false if the classpath could not be found or the classpath was already registered.
110      */

111     public synchronized boolean addAvailableHardCAToken(String JavaDoc classpath, String JavaDoc name, boolean translateable, boolean use) {
112         boolean retval = false;
113         if (!availablehardcatokens.containsKey(classpath)) {
114             log.debug("HardCATokenManager registering " + classpath);
115             if (loadClass(classpath)) {
116                 // Add to the available tokens
117
availablehardcatokens.put(classpath, new AvailableHardCAToken(classpath, name, translateable, use));
118                 retval = true;
119                 log.debug("Registered " + classpath + " successfully.");
120             } else {
121                 // Normally not an error, since these classes are provided by HSM vendor
122
String JavaDoc msg = intres.getLocalizedMessage("catoken.inforegisterclasspath", classpath);
123                 log.info(msg);
124             }
125         }
126         return retval;
127     }
128     /**
129      * Method loading a class in order to test if it can be instasiated.
130      *
131      * @param classpath
132      */

133     private boolean loadClass(String JavaDoc classpath){
134         try {
135             HardCATokenManager.class.getClassLoader().loadClass(classpath).newInstance();
136         } catch (ClassNotFoundException JavaDoc e) {
137             String JavaDoc msg = intres.getLocalizedMessage("catoken.classnotfound", classpath);
138             log.info(msg);
139             return false;
140         } catch (InstantiationException JavaDoc e) {
141             String JavaDoc msg = intres.getLocalizedMessage("catoken.errorinstansiate", classpath, e.getMessage());
142             log.info(msg);
143             return false;
144         } catch (IllegalAccessException JavaDoc e) {
145             log.error("IllegalAccessException: "+classpath, e);
146             return false;
147         }
148         return true;
149     }
150     
151     /**
152      * Method returning to the system available HardCATokens
153      *
154      * @return a Collection (AvailableHardCAToken) of registrered plug-ins.
155      */

156     public Collection JavaDoc getAvailableHardCATokens(){
157        return availablehardcatokens.values();
158     }
159
160     /**
161      * Method returning to the available hardcatoken with given classpath.
162      *
163      * @return the corresponding AvailableHardCAToken or null of classpath couldn't be found
164      */

165     public AvailableHardCAToken getAvailableHardCAToken(String JavaDoc classpath){
166         if (classpath == null) { return null; }
167         return (AvailableHardCAToken)availablehardcatokens.get(classpath);
168     }
169     
170 }
171
Popular Tags