1 13 14 package org.ejbca.core.ejb.ca.caadmin; 15 16 import java.io.UnsupportedEncodingException ; 17 import java.security.cert.X509Certificate ; 18 import java.util.Date ; 19 import java.util.HashMap ; 20 21 import javax.ejb.CreateException ; 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 116 public abstract class CADataBean extends BaseEntityBean { 117 118 private static final Logger log = Logger.getLogger(CADataBean.class); 119 120 125 public abstract Integer getCaId(); 126 127 129 public abstract void setCaId(Integer caid); 130 131 135 public abstract String getName(); 136 137 140 public abstract void setName(String name); 141 142 146 public abstract String getSubjectDN(); 147 148 150 public abstract void setSubjectDN(String subjectdn); 151 152 156 public abstract int getStatus(); 157 158 161 public abstract void setStatus(int status); 162 163 167 public abstract long getExpireTime(); 168 169 172 public abstract void setExpireTime(long expiretime); 173 174 178 public abstract long getUpdateTime(); 179 180 183 public abstract void setUpdateTime(long updatetime); 184 185 188 public abstract String getData(); 189 190 192 public abstract void setData(String data); 193 194 197 public Date getUpdateTimeAsDate() { 198 return new Date (getUpdateTime()); 199 } 200 201 202 207 public CA getCA() throws java.io.UnsupportedEncodingException , IllegalKeyStoreException { 208 CA ca = null; 209 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 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 decoder = new java.beans.XMLDecoder (new java.io.ByteArrayInputStream (getData().getBytes("UTF8"))); 225 HashMap h = (HashMap ) decoder.readObject(); 226 decoder.close(); 227 HashMap data = new Base64GetHashMap(h); 229 230 float oldversion = ((Float ) data.get(UpgradeableDataHashMap.VERSION)).floatValue(); 233 switch(((Integer )(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 if ( ((ca != null) && (Float.compare(oldversion, ca.getVersion()) != 0)) 241 || upgradedExtendedService) { 242 ca.getCAToken(); 244 setCA(ca); 245 } 246 CACacheManager.instance().addCA(getCaId().intValue(), ca); 249 } 250 return ca; 251 } 252 253 257 public void setCA(CA ca) throws UnsupportedEncodingException { 258 HashMap a = new Base64PutHashMap(); 260 a.putAll((HashMap )ca.saveData()); 261 262 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 263 java.beans.XMLEncoder encoder = new java.beans.XMLEncoder (baos); 264 encoder.writeObject(a); 265 encoder.close(); 266 setData(baos.toString("UTF8")); 267 setUpdateTime(new Date ().getTime()); 268 CACacheManager.instance().removeCA(getCaId().intValue()); 270 } 271 272 276 277 286 public Integer ejbCreate(String subjectdn, String name, int status, CA ca) throws CreateException { 287 try { 288 289 setCaId(new Integer (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 ) ca.getCACertificate()).getNotAfter().getTime()); 297 ca.setExpireTime(((X509Certificate ) ca.getCACertificate()).getNotAfter()); 298 } 299 300 setCA(ca); 301 302 log.debug("Created CA "+ name); 303 return new Integer (subjectdn.hashCode()); 304 } catch(java.io.UnsupportedEncodingException e) { 305 log.error("CAData caught exception trying to create: ", e); 306 throw new CreateException (e.toString()); 307 } 308 } 309 310 public void ejbPostCreate(String subjectdn, String name, int status, CA ca) { 311 } 313 314 315 } 316 | Popular Tags |