KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > ca > caadmin > CADataBean


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.ejb.ca.caadmin;
15
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.security.cert.X509Certificate JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.HashMap JavaDoc;
20
21 import javax.ejb.CreateException JavaDoc;
22
23 import org.apache.log4j.Logger;
24 import org.ejbca.core.ejb.BaseEntityBean;
25 import org.ejbca.core.model.UpgradeableDataHashMap;
26 import org.ejbca.core.model.ca.caadmin.CA;
27 import org.ejbca.core.model.ca.caadmin.CACacheManager;
28 import org.ejbca.core.model.ca.caadmin.CAInfo;
29 import org.ejbca.core.model.ca.caadmin.IllegalKeyStoreException;
30 import org.ejbca.core.model.ca.caadmin.X509CA;
31 import org.ejbca.util.Base64GetHashMap;
32 import org.ejbca.util.Base64PutHashMap;
33
34
35
36 /** Entity bean should not be used directly, use though Session beans.
37  *
38  * Entity Bean representing a ca instance.
39  * Information stored:
40  * <pre>
41  * caid (Primary key)
42  * name
43  * SubjectDN
44  * type
45  * status
46  * expiretime
47  * data (non searchable data, HashMap stored as XML-String)
48  * </pre>
49  *
50  * @version $Id: CADataBean.java,v 1.14 2007/01/12 09:43:28 anatom Exp $
51  *
52  * @ejb.bean
53  * description="This enterprise bean entity represents a publisher"
54  * display-name="CADataEB"
55  * name="CAData"
56  * jndi-name="CAData"
57  * local-jndi-name="CADataLocal"
58  * view-type="local"
59  * type="CMP"
60  * reentrant="True"
61  * cmp-version="2.x"
62  * transaction-type="Container"
63  * schema="CADataBean"
64  * primkey-field="caId"
65  *
66  * @ejb.pk generate="false"
67  * class="java.lang.Integer"
68  *
69  * @ejb.persistence table-name = "CAData"
70  *
71  * @ejb.env-entry description="Used internally to keystores in database"
72  * name="keyStorePass"
73  * type="java.lang.String"
74  * value="${ca.keystorepass}"
75  *
76  * @ejb.env-entry description="Password for OCSP keystores"
77  * name="OCSPKeyStorePass"
78  * type="java.lang.String"
79  * value="${ca.ocspkeystorepass}"
80  *
81  * @ejb.env-entry description="Password for XKMS keystores"
82  * name="XKMSKeyStorePass"
83  * type="java.lang.String"
84  * value="${ca.xkmskeystorepass}"
85  *
86  * @ejb.env-entry description="Password for CMS keystores"
87  * name="CMSKeyStorePass"
88  * type="java.lang.String"
89  * value="${ca.cmskeystorepass}"
90  *
91  * @ejb.home
92  * generate="local"
93  * local-extends="javax.ejb.EJBLocalHome"
94  * local-class="org.ejbca.core.ejb.ca.caadmin.CADataLocalHome"
95  *
96  * @ejb.interface
97  * generate="local"
98  * local-extends="javax.ejb.EJBLocalObject"
99  * local-class="org.ejbca.core.ejb.ca.caadmin.CADataLocal"
100  *
101  * @ejb.finder
102  * description="findByName"
103  * signature="org.ejbca.core.ejb.ca.caadmin.CADataLocal findByName(java.lang.String name)"
104  * query="SELECT OBJECT(a) from CADataBean a WHERE a.name=?1"
105  *
106  * @ejb.finder
107  * description="findAll"
108  * signature="Collection findAll()"
109  * query="SELECT OBJECT(a) from CADataBean a"
110  *
111  * @ejb.transaction type="Required"
112  *
113  * @jonas.jdbc-mapping
114  * jndi-name="${datasource.jndi-name}"
115  */

116 public abstract class CADataBean extends BaseEntityBean {
117
118     private static final Logger log = Logger.getLogger(CADataBean.class);
119
120     /**
121      * @ejb.pk-field
122      * @ejb.persistence column-name="cAId"
123      * @ejb.interface-method
124     */

125     public abstract Integer JavaDoc getCaId();
126
127     /**
128     */

129     public abstract void setCaId(Integer JavaDoc caid);
130
131     /**
132      * @ejb.persistence column-name="name"
133      * @ejb.interface-method
134      */

135     public abstract String JavaDoc getName();
136
137     /**
138      * @ejb.interface-method
139      */

140     public abstract void setName(String JavaDoc name);
141
142     /**
143      * @ejb.persistence column-name="subjectDN"
144      * @ejb.interface-method
145      */

146     public abstract String JavaDoc getSubjectDN();
147
148     /**
149      */

150     public abstract void setSubjectDN(String JavaDoc subjectdn);
151     
152     /**
153      * @ejb.persistence column-name="status"
154      * @ejb.interface-method
155      */

156     public abstract int getStatus();
157
158     /**
159      * @ejb.interface-method
160      */

161     public abstract void setStatus(int status);
162     
163     /**
164      * @ejb.persistence column-name="expireTime"
165      * @ejb.interface-method
166      */

167     public abstract long getExpireTime();
168
169     /**
170      * @ejb.interface-method
171      */

172     public abstract void setExpireTime(long expiretime);
173     
174     /** When was this CA updated in the database
175      * @ejb.persistence column-name="updateTime"
176      * @ejb.interface-method
177      */

178     public abstract long getUpdateTime();
179
180     /**
181      * @ejb.interface-method
182      */

183     public abstract void setUpdateTime(long updatetime);
184     
185     /**
186      * @ejb.persistence jdbc-type="LONGVARCHAR" column-name="data"
187      */

188     public abstract String JavaDoc getData();
189
190     /**
191      */

192     public abstract void setData(String JavaDoc data);
193     
194     /**
195      * @ejb.interface-method view-type="local"
196      */

197     public Date JavaDoc getUpdateTimeAsDate() {
198         return new Date JavaDoc(getUpdateTime());
199     }
200
201     
202     /**
203      * Method that retrieves the CA from the database.
204      * @throws IllegalKeyStoreException
205      * @ejb.interface-method
206      */

207     public CA getCA() throws java.io.UnsupportedEncodingException JavaDoc, IllegalKeyStoreException {
208         CA ca = null;
209         // First check if we already have a cached instance of the CA
210
ca = CACacheManager.instance().getCA(getCaId().intValue(), this);
211         boolean isUpdated = false;
212         if (ca != null) {
213             long update = ca.getCAInfo().getUpdateTime().getTime();
214             long t = getUpdateTime();
215             //log.debug("updateTime from ca = "+update);
216
//log.debug("updateTime from db = "+t);
217
if (update < t) {
218                 log.debug("CA has been updated in database, need to refresh cache");
219                 isUpdated = true;
220             }
221         }
222         if ( (ca == null) || isUpdated) {
223             log.debug("Re-reading CA from database.");
224             java.beans.XMLDecoder JavaDoc decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(getData().getBytes("UTF8")));
225             HashMap JavaDoc h = (HashMap JavaDoc) decoder.readObject();
226             decoder.close();
227             // Handle Base64 encoded string values
228
HashMap JavaDoc data = new Base64GetHashMap(h);
229             
230             // If CA-data is upgraded we want to save the new data, so we must get the old version before loading the data
231
// and perhaps upgrading
232
float oldversion = ((Float JavaDoc) data.get(UpgradeableDataHashMap.VERSION)).floatValue();
233             switch(((Integer JavaDoc)(data.get(CA.CATYPE))).intValue()){
234                 case CAInfo.CATYPE_X509:
235                     ca = new X509CA(data, getCaId().intValue(), getSubjectDN(), getName(), getStatus(), getUpdateTimeAsDate());
236                     break;
237             }
238             boolean upgradedExtendedService = ca.upgradeExtendedCAServices();
239             // Compare old version with current version and save the data if there has been a change
240
if ( ((ca != null) && (Float.compare(oldversion, ca.getVersion()) != 0))
241                   || upgradedExtendedService) {
242                 // Make sure we upgrade the CAToken as well, if needed
243
ca.getCAToken();
244                 setCA(ca);
245             }
246             // We have to do the same if CAToken was upgraded
247
// Add CA to the cache
248
CACacheManager.instance().addCA(getCaId().intValue(), ca);
249         }
250         return ca;
251     }
252     
253     /**
254      * Method that saves the CA to database.
255      * @ejb.interface-method
256      */

257     public void setCA(CA ca) throws UnsupportedEncodingException JavaDoc {
258         // We must base64 encode string for UTF safety
259
HashMap JavaDoc a = new Base64PutHashMap();
260         a.putAll((HashMap JavaDoc)ca.saveData());
261         
262         java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
263         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
264         encoder.writeObject(a);
265         encoder.close();
266         setData(baos.toString("UTF8"));
267         setUpdateTime(new Date JavaDoc().getTime());
268         // remove the CA from the cache to force an update the next time we load it
269
CACacheManager.instance().removeCA(getCaId().intValue());
270     }
271     
272     //
273
// Fields required by Container
274
//
275

276
277     /**
278      * Entity Bean holding data of a CA.
279      * @param subjectdn
280      * @param name of CA
281      * @param status initial status
282      * @param ca CA to store
283      * @return caid
284      * @ejb.create-method
285      */

286     public Integer JavaDoc ejbCreate(String JavaDoc subjectdn, String JavaDoc name, int status, CA ca) throws CreateException JavaDoc {
287         try {
288             
289             setCaId(new Integer JavaDoc(subjectdn.hashCode()));
290             setName(name);
291             setSubjectDN(subjectdn);
292             setStatus(status);
293             
294             
295             if(ca instanceof X509CA && ca.getCertificateChain().size() != 0){
296                 setExpireTime(((X509Certificate JavaDoc) ca.getCACertificate()).getNotAfter().getTime());
297                 ca.setExpireTime(((X509Certificate JavaDoc) ca.getCACertificate()).getNotAfter());
298             }
299             
300             setCA(ca);
301             
302             log.debug("Created CA "+ name);
303             return new Integer JavaDoc(subjectdn.hashCode());
304         } catch(java.io.UnsupportedEncodingException JavaDoc e) {
305             log.error("CAData caught exception trying to create: ", e);
306             throw new CreateException JavaDoc(e.toString());
307         }
308     }
309
310     public void ejbPostCreate(String JavaDoc subjectdn, String JavaDoc name, int status, CA ca) {
311         // Do nothing. Required.
312
}
313     
314     
315 }
316
Popular Tags