/**
 * Most of the code in the Qalingo project is copyrighted Hoteia and licensed
 * under the Apache License Version 2.0 (release version 0.8.0)
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *                   Copyright (c) Hoteia, 2012-2014
 * http://www.hoteia.com - http://twitter.com/hoteia - contact@hoteia.com
 *
 */
package org.hoteia.qalingo.core.aop.cacheflush;

import java.net.InetAddress;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.JoinPoint.StaticPart;
import org.hoteia.qalingo.core.jms.cacheflush.producer.CacheFlushMessageJms;
import org.hoteia.qalingo.core.jms.cacheflush.producer.CacheFlushMessageProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component(value = "cacheFlushAspect")
public class CacheFlushAspect {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    protected CacheFlushMessageProducer cacheFlushMessageProducer;
    
    @Value("${env.name}")  
    protected String environmentName;
    
    @Value("${env.id}")  
    protected String environmentId;
    
    @Value("${app.name}")  
    protected String applicationName;
    
    public void before(final JoinPoint joinPoint) {
        if(logger.isDebugEnabled()){
            logger.debug("CacheFlushAspect, before");
        }
    }

    public void afterReturning(final StaticPart staticPart, final Object result) {
        if(logger.isDebugEnabled()){
            logger.debug("CacheFlushAspect, afterReturning");
        }
        try {
            final CacheFlushMessageJms cacheFlushMessageJms = new CacheFlushMessageJms();
            cacheFlushMessageJms.setEnvironmentName(environmentName);
            cacheFlushMessageJms.setEnvironmentId(environmentId);
            cacheFlushMessageJms.setApplicationName(applicationName);
            cacheFlushMessageJms.setServerName(InetAddress.getLocalHost().getHostName());
            cacheFlushMessageJms.setServerIp(InetAddress.getLocalHost().getHostAddress());
            cacheFlushMessageProducer.generateAndSendMessages(cacheFlushMessageJms);
            
        } catch (Exception e) {
            logger.error("CacheFlushAspect Target Object error: " + e);
        }
    }
    
}
@@ -419,16 +419,199 @@
 ddress;%0D
+%0Aimport java.util.ArrayList;%0D%0Aimport java.util.Collections;%0D%0Aimport java.util.Comparator;%0D%0Aimport java.util.List;%0D%0A%0D%0Aimport net.sf.ehcache.Cache;%0D%0Aimport net.sf.ehcache.CacheManager;%0D
 %0A%0D%0Aimpor
@@ -1028,24 +1028,87 @@
 ion.Value;%0D%0A
+import org.springframework.cache.ehcache.EhCacheCacheManager;%0D%0A
 import org.s
@@ -1378,32 +1378,110 @@
 roducer;%0D%0A    %0D%0A
+    @Autowired%0D%0A    protected EhCacheCacheManager ehCacheCacheManager;%0D%0A    %0D%0A
     @Value(%22$%7Ben
@@ -2055,24 +2055,179 @@
      try %7B%0D%0A
+            List%3CCache%3E caches = getCaches();%0D%0A            for (Cache cache : caches) %7B%0D%0A                flushCache(cache);%0D%0A            %7D %0D%0A            %0D%0A
             
@@ -2899,24 +2899,24 @@
 r: %22 + e);%0D%0A
-
         %7D%0D%0A 
@@ -2927,9 +2927,979 @@
 %0D%0A    %0D%0A
+    private List%3CCache%3E getCaches() %7B%0D%0A        List%3CCache%3E caches = new ArrayList%3CCache%3E();%0D%0A        for (String cacheName : getCacheManager().getCacheNames()) %7B%0D%0A            caches.add(getCacheManager().getCache(cacheName));%0D%0A        %7D%0D%0A        Collections.sort(caches, new Comparator%3CCache%3E() %7B%0D%0A            @Override%0D%0A            public int compare(Cache o1, Cache o2) %7B%0D%0A                if (o1 != null && o2 != null) %7B%0D%0A                    String order1 = o1.getName();%0D%0A                    String order2 = o2.getName();%0D%0A                    return order1.compareTo(order2);%0D%0A                %7D%0D%0A                return 0;%0D%0A            %7D%0D%0A        %7D);%0D%0A        return caches;%0D%0A    %7D%0D%0A    %0D%0A    private void flushCache(Cache cache) %7B%0D%0A        if(cache != null)%7B%0D%0A            cache.removeAll();%0D%0A            cache.clearStatistics();%0D%0A        %7D%0D%0A    %7D%0D%0A    %0D%0A    public CacheManager getCacheManager() %7B%0D%0A        return ehCacheCacheManager.getCacheManager();%0D%0A    %7D%0D%0A    %0D%0A
 %7D

fix cache