* @return CA
* @throws java.io.UnsupportedEncodingException
* @throws IllegalKeyStoreException
*/
private final CA readAndUpgradeCAInternal() throws java.io.UnsupportedEncodingException, IllegalKeyStoreException {
CA ca = null;
// First check if we already have a cached instance of the CA
ca = CACacheManager.instance().getAndUpdateCA(getCaId().intValue(), getStatus(), getExpireTime(), getName(), getSubjectDN());
boolean isUpdated = false;
if (ca != null) {
if (log.isDebugEnabled()) {
log.debug("Found CA ('"+ca.getName()+"', "+getCaId().intValue()+") in cache.");
}
final long update = ca.getCAInfo().getUpdateTime().getTime();
final long t = getUpdateTime();
//log.debug("updateTime from ca = "+update);
//log.debug("updateTime from db = "+t);
if (update < t) {
if (log.isDebugEnabled()) {
log.debug("CA '"+ca.getName()+"' has been updated in database, need to refresh cache");
}
isUpdated = true;
}
}
if ( (ca == null) || isUpdated) {
if (log.isDebugEnabled()) {
log.debug("Re-reading CA from database: "+getCaId().intValue());
}
final java.beans.XMLDecoder decoder = new java.beans.XMLDecoder(new java.io.ByteArrayInputStream(getData().getBytes("UTF8")));
final HashMap h = (HashMap) decoder.readObject();
decoder.close();
// Handle Base64 encoded string values
final HashMap<Object, Object> data = new Base64GetHashMap(h);
// If CA-data is upgraded we want to save the new data, so we must get the old version before loading the data
// and perhaps upgrading
final float oldversion = ((Float) data.get(UpgradeableDataHashMap.VERSION)).floatValue();
switch(((Integer)(data.get(CA.CATYPE))).intValue()){
case CAInfo.CATYPE_X509:
ca = new X509CA(data, getCaId().intValue(), getSubjectDN(), getName(), getStatus(), getUpdateTimeAsDate(), new Date(getExpireTime()));
break;
case CAInfo.CATYPE_CVC:
ca = new CVCCA(data, getCaId().intValue(), getSubjectDN(), getName(), getStatus(), getUpdateTimeAsDate());
break;
}
final boolean upgradedExtendedService = ca.upgradeExtendedCAServices();
// Compare old version with current version and save the data if there has been a change
if ( ((ca != null) && (Float.compare(oldversion, ca.getVersion()) != 0)) || upgradedExtendedService) {
// Make sure we upgrade the CAToken as well, if needed
ca.getCAToken();
setCA(ca);
log.debug("Stored upgraded CA ('"+ca.getName()+"', "+getCaId().intValue()+") with version "+ca.getVersion());
}
// We have to do the same if CAToken was upgraded
// Add CA to the cache
CACacheManager.instance().addCA(getCaId().intValue(), ca);
}