checks added in methods that performs operations on the cache and cacheManager variable changed from local to private static

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/databases-manager-portlet@100896 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Loredana Liccardo 2014-10-22 17:16:37 +00:00
parent 2a9b3e4a2a
commit c65cba2a56
1 changed files with 135 additions and 36 deletions

View File

@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Status;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.MemoryUnit;
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
@ -108,7 +109,9 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
String cachePath = System.getenv("CATALINA_TMPDIR") + "/DBManager";
logger.info("dbmanager-> Creating cache in folder: " + cachePath);
CacheManager cacheManager = CacheManager.create();
// CacheManager cacheManager = CacheManager.create();
cacheManager = CacheManager.create();
if (cacheManager == null) {
logger.error("dbmanager-> Error while starting the servlet. Failed to get the cacheManager. cacheManager null");
@ -2068,18 +2071,44 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
}
private synchronized void insertDataIntoCache(net.sf.ehcache.Element data) {
if (DBCache != null) {
logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath());
if(cacheManager.cacheExists("DBCache")){
if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){
logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE");
DBCache.put(data);
// logger.info("dbmanager-> element added in cache with key: " + data.getKey().toString());
}else{
logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString());
}
}
// if (DBCache != null) {
// DBCache.put(data);
// }
}
private synchronized net.sf.ehcache.Element getDataFromCache(String key) {
net.sf.ehcache.Element data = null;
if (DBCache != null) {
logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath());
if(cacheManager.cacheExists("DBCache")){
if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){
logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE");
data = DBCache.get(key);
// System.out.println("element get from cache with key " + key);
}else{
logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString());
}
}
// if (DBCache != null) {
// data = DBCache.get(key);
// }
return data;
}
@ -2468,9 +2497,23 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
}
private synchronized void refreshCache(String key) throws Exception {
logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath());
if(cacheManager.cacheExists("DBCache")){
if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){
logger.info("dbmanager-> Cache Status:"+ Status.STATUS_ALIVE.toString());
DBCache.remove(key);
// logger.info("dbmanager-> Element with key: " +key+ " removed.");
}else{
logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString());
}
}
// try {
// System.out.println("SIZE CACHE PRE REMOVING: " + DBCache.getSize());
DBCache.remove(key);
// DBCache.remove(key);
// System.out.println("SIZE CACHE POST REMOVING: " + DBCache.getSize());
// } catch (Exception e) {
// TODO: handle exception
@ -2487,6 +2530,12 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
logger.info("dbmanager-> refreshing submitted queries in cache with key:"
+ key);
logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath());
if(cacheManager.cacheExists("DBCache")){
if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){
logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE");
List<String> keysInCache = DBCache.getKeys();
int listSize = keysInCache.size();
List<String> keysToBeRemoved = new ArrayList<>();
@ -2502,6 +2551,27 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
// remove keys
DBCache.removeAll(keysToBeRemoved);
// System.out.println("SubmittedQuery refreshed with key: " + key);
}else{
logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString());
}
}
// List<String> keysInCache = DBCache.getKeys();
// int listSize = keysInCache.size();
// List<String> keysToBeRemoved = new ArrayList<>();
//
// // recover keys list that match the key
// for (int i = 0; i < listSize; i++) {
// if (keysInCache.get(i).startsWith(key)) {
// // System.out.println("data removed with key: " +
// // keysInCache.get(i));
// keysToBeRemoved.add(keysInCache.get(i));
// }
// }
// // remove keys
// DBCache.removeAll(keysToBeRemoved);
// // System.out.println("SubmittedQuery refreshed with key: " + key);
}
private synchronized void refreshSamplingsInCache(
@ -2513,6 +2583,13 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
+ " "
+ keyUsedForSmartSamplingRefresh);
logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath());
if(cacheManager.cacheExists("DBCache")){
if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){
logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE");
List<String> keysInCache = DBCache.getKeys();
int listSize = keysInCache.size();
List<String> keysToBeRemoved = new ArrayList<>();
@ -2530,6 +2607,28 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
// System.out.println("Samplings refreshed with keys: "
// + keyUsedForSamplingsRefresh + " "
// + keyUsedForSmartSamplingRefresh);
}else{
logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString());
}
}
// List<String> keysInCache = DBCache.getKeys();
// int listSize = keysInCache.size();
// List<String> keysToBeRemoved = new ArrayList<>();
//
// // recover keys list that match the key
// for (int i = 0; i < listSize; i++) {
// if ((keysInCache.get(i).startsWith(keyUsedForSamplingsRefresh))
// || (keysInCache.get(i)
// .startsWith(keyUsedForSmartSamplingRefresh))) {
// keysToBeRemoved.add(keysInCache.get(i));
// }
// }
// // remove keys
// DBCache.removeAll(keysToBeRemoved);
// // System.out.println("Samplings refreshed with keys: "
// // + keyUsedForSamplingsRefresh + " "
// // + keyUsedForSmartSamplingRefresh);
}
private String storeResultIntoCSVFile(List<Result> result, String n)