1 13 14 package org.ejbca.core.model.ca.catoken; 15 16 import java.util.Collection ; 17 import java.util.Hashtable ; 18 19 import org.apache.log4j.Logger; 20 import org.ejbca.core.model.InternalResources; 21 22 23 31 public class HardCATokenManager { 32 33 34 private static transient Logger log = Logger.getLogger(HardCATokenManager.class); 35 36 private static final InternalResources intres = InternalResources.getInstance(); 37 38 39 private Hashtable availablehardcatokens = new Hashtable (); 40 42 private Hashtable caTokenRegistry = new Hashtable (); 43 44 45 private static HardCATokenManager instance = null; 46 47 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 62 private HardCATokenManager() {} 63 64 67 public synchronized static HardCATokenManager instance() { 68 if (instance == null) { 69 instance = new HardCATokenManager(); 70 } 71 return instance; 72 } 73 74 79 public CAToken getCAToken(int caid) { 80 return (CAToken)caTokenRegistry.get(new Integer (caid)); 81 } 82 83 89 public synchronized void addCAToken(int caid, CAToken token) { 90 if (caTokenRegistry.containsKey(new Integer (caid))) { 91 caTokenRegistry.remove(new Integer (caid)); 92 log.debug("Removed old CA token for CA: "+caid); 93 } 94 if (token != null) { 95 caTokenRegistry.put(new Integer (caid), token); 96 log.debug("Added CA token for CA: "+caid); 97 } 98 } 99 100 101 111 public synchronized boolean addAvailableHardCAToken(String classpath, String 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 availablehardcatokens.put(classpath, new AvailableHardCAToken(classpath, name, translateable, use)); 118 retval = true; 119 log.debug("Registered " + classpath + " successfully."); 120 } else { 121 String msg = intres.getLocalizedMessage("catoken.inforegisterclasspath", classpath); 123 log.info(msg); 124 } 125 } 126 return retval; 127 } 128 133 private boolean loadClass(String classpath){ 134 try { 135 HardCATokenManager.class.getClassLoader().loadClass(classpath).newInstance(); 136 } catch (ClassNotFoundException e) { 137 String msg = intres.getLocalizedMessage("catoken.classnotfound", classpath); 138 log.info(msg); 139 return false; 140 } catch (InstantiationException e) { 141 String msg = intres.getLocalizedMessage("catoken.errorinstansiate", classpath, e.getMessage()); 142 log.info(msg); 143 return false; 144 } catch (IllegalAccessException e) { 145 log.error("IllegalAccessException: "+classpath, e); 146 return false; 147 } 148 return true; 149 } 150 151 156 public Collection getAvailableHardCATokens(){ 157 return availablehardcatokens.values(); 158 } 159 160 165 public AvailableHardCAToken getAvailableHardCAToken(String classpath){ 166 if (classpath == null) { return null; } 167 return (AvailableHardCAToken)availablehardcatokens.get(classpath); 168 } 169 170 } 171 | Popular Tags |