package at.gv.egiz.eidas.specific.connector.storage; import java.util.Date; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import at.gv.egiz.eaaf.core.api.storage.ITransactionStorage; import at.gv.egiz.eaaf.core.exceptions.EAAFException; import at.gv.egiz.eaaf.core.exceptions.EAAFStorageException; public class eIDASCacheTransactionStoreDecorator implements ITransactionStorage{ private static final Logger log = LoggerFactory.getLogger(eIDASCacheTransactionStoreDecorator.class); @Autowired(required=true) private CacheWitheIDASBackend storage; @Override public void changeKey(String oldKey, String newKey, Object value) throws EAAFException { if (containsKey(oldKey)) { TransactionStoreElement el = storage.get(oldKey); el.setKey(newKey); storage.put(newKey, el); storage.remove(oldKey); } else throw new EAAFStorageException("No element in TransactionStorage with key: " + oldKey); } @Override public List clean(Date now, long dataTimeOut) { return storage.clean(now, dataTimeOut); } @Override public boolean containsKey(String key) { if (key != null) { if (storage.get(key) != null) return true; } return false; } @Override public Object get(String key) throws EAAFException { if (key != null && containsKey(key)) { TransactionStoreElement element = storage.get(key); return element.getData(); } else return null; } @Override public T get(String key, Class type) throws EAAFException { return get(key, type, -1); } @Override public T get(String key, Class type, long dataTimeOut) throws EAAFException { if (key != null && containsKey(key)) { TransactionStoreElement value = storage.get(key); if (dataTimeOut > -1) { long now = new Date().getTime(); if (now - value.getCreated().getTime() > dataTimeOut) { log.info("Transaction-Data with key: " + key + " is out of time."); throw new EAAFStorageException("Transaction-Data with key: " + key + " is out of time."); } } if (type.isAssignableFrom(value.getData().getClass())) { return (T) value.getData(); } else log.warn("Can NOT cast '" + value.getClass() + "' to '" + type + "'"); } return null; } @Override public Object getRaw(String key) throws EAAFException { return storage.get(key); } @Override public void put(String key, Object value, int dataTimeOut) throws EAAFException { TransactionStoreElement element = new TransactionStoreElement(); element.setKey(key); element.setData(value); storage.put(key, element); } @Override public void putRaw(String key, Object value) throws EAAFException { if (value instanceof TransactionStoreElement) storage.put(((TransactionStoreElement) value).getKey(), (TransactionStoreElement) value); else log.info(value.getClass().getName() + " is NOT a RAW element of " + ITransactionStorage.class.getName()); } @Override public void remove(String key) { if (containsKey(key)) { log.debug("Remove element with key: " + key + " from " + ITransactionStorage.class.getName()); storage.remove(key); } } }