1 13 14 package org.ejbca.core.model.ca.certextensions; 15 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Properties ; 22 23 import org.apache.log4j.Logger; 24 import org.ejbca.core.model.InternalResources; 25 26 38 39 public class CertificateExtensionFactory { 40 41 private static Logger log = Logger.getLogger(CertificateExtensionFactory.class); 42 private static final InternalResources intres = InternalResources.getInstance(); 43 44 private static CertificateExtensionFactory instance = null; 45 46 private static String PROPERTY_ID = "id"; 47 private static String PROPERTY_OID = ".oid"; 48 private static String PROPERTY_CLASSPATH = ".classpath"; 49 private static String PROPERTY_DISPLAYNAME = ".displayname"; 50 private static String PROPERTY_USED = ".used"; 51 private static String PROPERTY_TRANSLATABLE = ".translatable"; 52 private static String PROPERTY_CRITICAL = ".critical"; 53 54 private ArrayList availableCertificateExtensions = new ArrayList (); 55 private HashMap certificateExtensions = new HashMap (); 56 57 private CertificateExtensionFactory(){} 58 59 60 63 static CertificateExtensionFactory getInstance(Properties props){ 64 if(instance == null){ 65 instance = parseConfiguration(props); 66 } 67 68 return instance; 69 } 70 71 76 public static CertificateExtensionFactory getInstance(){ 77 if(instance == null){ 78 instance = parseConfiguration(null); 79 } 80 81 return instance; 82 } 83 84 88 public List getAvailableCertificateExtensions(){ 89 return availableCertificateExtensions; 90 } 91 92 98 public CertificateExtension getCertificateExtensions(Integer id){ 99 CertificateExtension ret = (CertificateExtension) certificateExtensions.get(id); 100 if (ret == null) { 101 log.error("No extension configured for id: "+id+". Returning null."); 103 } 104 return ret; 105 } 106 107 114 private static CertificateExtensionFactory parseConfiguration(Properties props) { 115 116 CertificateExtensionFactory retval = new CertificateExtensionFactory(); 117 try{ 118 if(props == null){ 119 props = new Properties (); 120 InputStream is = null; 121 try { 122 is = CertificateExtensionFactory.class.getResourceAsStream("/certextensions.properties"); 123 if(is != null){ 124 props.load(is); 125 }else{ 126 log.error("Certificate Extension configuration file not found"); 127 } 128 } finally { 129 if (is != null) is.close(); 130 } 131 } 132 133 for(int i=1;i<255;i++){ 134 if(props.get("id" + i +".oid")!=null){ 135 log.debug("found " + props.get("id" + i +".oid")); 136 retval.addCertificateExtension(props,i); 137 }else{ 138 break; 139 } 140 } 141 log.debug("Nr of availableCeritficateExtensions: " + retval.availableCertificateExtensions.size()); 142 }catch(IOException e){ 143 log.error(intres.getLocalizedMessage("certext.errorparsingproperty"),e); 144 } catch (CertificateExtentionConfigurationException e) { 145 log.error(e.getMessage(),e); 146 } 147 148 return retval; 149 } 150 151 152 private void addCertificateExtension(Properties props, int id) throws CertificateExtentionConfigurationException { 153 try{ 154 String oid = props.getProperty(PROPERTY_ID + id + PROPERTY_OID); 155 String classPath = props.getProperty(PROPERTY_ID + id + PROPERTY_CLASSPATH); 156 String displayName = props.getProperty(PROPERTY_ID + id + PROPERTY_DISPLAYNAME); 157 log.debug(PROPERTY_ID + id + PROPERTY_USED + ":" + props.getProperty(PROPERTY_ID + id + PROPERTY_USED)); 158 boolean used = props.getProperty(PROPERTY_ID + id + PROPERTY_USED).trim().equalsIgnoreCase("TRUE"); 159 boolean translatable = props.getProperty(PROPERTY_ID + id + PROPERTY_TRANSLATABLE).trim().equalsIgnoreCase("TRUE"); 160 boolean critical = props.getProperty(PROPERTY_ID + id + PROPERTY_CRITICAL).trim().equalsIgnoreCase("TRUE"); 161 log.debug(id + ", " + used + ", " +oid + ", " +critical+ ", " +translatable + ", " + displayName); 162 if(used){ 163 if(oid != null && classPath != null && displayName != null){ 164 AvailableCertificateExtension availableCertificateExtension = new AvailableCertificateExtension(id,oid.trim(),displayName.trim(),translatable); 165 Class implClass = Class.forName(classPath); 166 CertificateExtension certificateExtension = (CertificateExtension) implClass.newInstance(); 167 certificateExtension.init(id, oid.trim(), critical, props); 168 availableCertificateExtensions.add(availableCertificateExtension); 169 certificateExtensions.put(new Integer (id), certificateExtension); 170 171 }else{ 172 throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.certextmissconfigured",new Integer (id))); 173 } 174 } 175 176 }catch(Exception e){ 177 throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.certextmissconfigured",new Integer (id)),e); 178 } 179 } 180 181 } 182 | Popular Tags |