KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > certextensions > CertificateExtensionFactory


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.certextensions;
15
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.apache.log4j.Logger;
24 import org.ejbca.core.model.InternalResources;
25
26 /**
27  * Class parsing the src/java/certextensions.properties file
28  * and maintains a set of available extensions to the system.
29  *
30  * It is also responsible for creating the actual CertificateExtensions
31  * used in certificate generation.
32  *
33  *
34  * @author Philip Vendil 2007 jan 5
35  *
36  * @version $Id: CertificateExtensionFactory.java,v 1.2.2.2 2007/03/14 09:34:52 anatom Exp $
37  */

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 JavaDoc PROPERTY_ID = "id";
47     private static String JavaDoc PROPERTY_OID = ".oid";
48     private static String JavaDoc PROPERTY_CLASSPATH = ".classpath";
49     private static String JavaDoc PROPERTY_DISPLAYNAME = ".displayname";
50     private static String JavaDoc PROPERTY_USED = ".used";
51     private static String JavaDoc PROPERTY_TRANSLATABLE = ".translatable";
52     private static String JavaDoc PROPERTY_CRITICAL = ".critical";
53     
54     private ArrayList JavaDoc availableCertificateExtensions = new ArrayList JavaDoc();
55     private HashMap JavaDoc certificateExtensions = new HashMap JavaDoc();
56     
57     private CertificateExtensionFactory(){}
58     
59     
60     /**
61      * Special Method that should only be used from test scripts.
62      */

63     static CertificateExtensionFactory getInstance(Properties JavaDoc props){
64         if(instance == null){
65             instance = parseConfiguration(props);
66         }
67         
68         return instance;
69     }
70     
71     /**
72      * Method used to get the instance of the factory.
73      * If it is the first time the method is called will
74      * the configuration file be parsed.
75      */

76     public static CertificateExtensionFactory getInstance(){
77         if(instance == null){
78             instance = parseConfiguration(null);
79         }
80         
81         return instance;
82     }
83     
84     /**
85      * Method returning a list of all of (AvailableCertificateExtensions)
86      * to be used in the 'Edit Certificate Profile' page
87      */

88     public List JavaDoc getAvailableCertificateExtensions(){
89         return availableCertificateExtensions;
90     }
91     
92     /**
93      * Method returning the instance of the CertificateExtension
94      * given its Id
95      *
96      * @returns null if the CertificateExtension doesn't exist
97      */

98     public CertificateExtension getCertificateExtensions(Integer JavaDoc id){
99         CertificateExtension ret = (CertificateExtension) certificateExtensions.get(id);
100         if (ret == null) {
101             // In EJBCA 3.5 this is a localized message
102
log.error("No extension configured for id: "+id+". Returning null.");
103         }
104         return ret;
105     }
106
107     /**
108      * Method reponsible to read the configuration file.
109      * and parse it into AvailableCertificateExtentions and
110      * also generate instances of the actual CertificateExtensions.
111      * @param props2
112      *
113      */

114     private static CertificateExtensionFactory parseConfiguration(Properties JavaDoc props) {
115         
116         CertificateExtensionFactory retval = new CertificateExtensionFactory();
117         try{
118             if(props == null){
119                 props = new Properties JavaDoc();
120                 InputStream JavaDoc 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 JavaDoc 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 JavaDoc props, int id) throws CertificateExtentionConfigurationException {
153         try{
154             String JavaDoc oid = props.getProperty(PROPERTY_ID + id + PROPERTY_OID);
155             String JavaDoc classPath = props.getProperty(PROPERTY_ID + id + PROPERTY_CLASSPATH);
156             String JavaDoc 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 JavaDoc 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 JavaDoc(id), certificateExtension);
170
171                 }else{
172                     throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.certextmissconfigured",new Integer JavaDoc(id)));
173                 }
174             }
175             
176         }catch(Exception JavaDoc e){
177             throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.certextmissconfigured",new Integer JavaDoc(id)),e);
178         }
179     }
180     
181 }
182
Popular Tags