package com.eactive.eai.common.session;

import com.eactive.eai.common.util.Logger;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;
import net.sf.ehcache.event.CacheEventListenerFactory;

import java.util.Properties;

public class LoggingCacheListenerFactory extends CacheEventListenerFactory {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	@Override
	public CacheEventListener createCacheEventListener(Properties properties) {
		return new CacheEventListener(){
			public Object clone() throws CloneNotSupportedException{
				logger.debug("LoggingCacheListenerFactory] Clone obtained");
				return super.clone();
			}

			@Override
			public void dispose() {
				logger.debug("LoggingCacheListenerFactory] disponse the listener");
			}

			@Override
			public void notifyElementEvicted(Ehcache ehcache,
					Element element) {
				logger.debug("LoggingCacheListenerFactory] Element evicted from the cache: "+element.getObjectKey());
			}

			@Override
			public void notifyElementExpired(Ehcache ehcache,
					Element element) {
				logger.debug("LoggingCacheListenerFactory] Element expired in the cache: "+element.getObjectKey());
			}

			@Override
			public void notifyElementPut(Ehcache ehcache, Element element)
					throws CacheException {
				logger.debug("LoggingCacheListenerFactory] Element put into the cache: "+element.getObjectKey());
			}

			@Override
			public void notifyElementRemoved(Ehcache ehcache,
					Element element) throws CacheException {
				logger.debug("LoggingCacheListenerFactory] Element remove from the cache: "+element.getObjectKey());
			}

			@Override
			public void notifyElementUpdated(Ehcache ehcache,
					Element element) throws CacheException {
				logger.debug("LoggingCacheListenerFactory] Element updated in the cache: "+element.getObjectKey());
			}

			@Override
			public void notifyRemoveAll(Ehcache ehcache) {
				logger.debug("LoggingCacheListenerFactory] Element remove all element from the cache");
			}
		};
	}
}
