bug fixed in servlet related to the cache management. Methods init and destroy modified.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/databases-manager-portlet@100226 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Loredana Liccardo 2014-09-26 14:56:42 +00:00
parent 38d0c898f3
commit 87d240b885
1 changed files with 168 additions and 96 deletions

View File

@ -22,8 +22,13 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import net.didion.jwnl.data.Exc;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache; import net.sf.ehcache.Ehcache;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.MemoryUnit;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.apache.regexp.RE; import org.apache.regexp.RE;
import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.ASLSession;
@ -81,7 +86,8 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
.getLogger(GWTdbManagerServiceImpl.class); .getLogger(GWTdbManagerServiceImpl.class);
// private CacheManager cacheManager; // private CacheManager cacheManager;
private static Ehcache employeeCache; private static Cache employeeCache;
private static CacheManager cacheManager;
public static List<String> listAlgorithms; public static List<String> listAlgorithms;
public GWTdbManagerServiceImpl() throws Exception { public GWTdbManagerServiceImpl() throws Exception {
@ -92,45 +98,93 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
super.init(); super.init();
// create cache
try { try {
// System.out.println("*** CACHE NOT EXISTING");
URL url = getClass().getResource("/encache.xml");
CacheManager cacheManager = CacheManager.newInstance(url);
// getcache
employeeCache = cacheManager.getEhcache("DBCache");
// String path = System.getenv("CATALINA_TMPDIR");
// CacheConfiguration config =
// employeeCache.getCacheConfiguration();
// String DiskCacheFolderName = "DBManagerDisk";
// File f = new File(path+"/"+DiskCacheFolderName); // cache folder
// if (!f.exists()){ String cachePath = System.getenv("CATALINA_TMPDIR") + "/DBManager";
// f.mkdir(); logger.info("dbmanager-> Creating cache in folder: " + cachePath);
//
// }
// config.setDiskStorePath(path); CacheManager cacheManager = CacheManager.create();
// config.setDiskPersistent(true);
// config.setOverflowToDisk(true);
} catch (Exception e) { if (cacheManager == null) {
logger.error("dbmanager-> Failed to get cache: ", e); logger.error("dbmanager-> Failed to get the cacheManager. cacheManager null");
// e.printStackTrace(); throw new ServletException(
throw e; "Failed to get the cacheManager. cacheManager null");
} }
// create folder that will contain file samplings and submitquery result if (cacheManager != null) {
// logger.info("dbmanager-> cacheManager not null");
if (cacheManager.cacheExists("DBCache")) {
// logger.info("dbmanager-> cache exists");
cacheManager.removeCache("DBCache");
logger.info("dbmanager-> cache removed");
} else {
File f = new File(cachePath + "/" + "DBCache.data");
if (f.exists()) {
logger.info("dbmanager-> File DBCache.data removed: "
+ f.delete());
}
}
createCache(cachePath);
if (employeeCache == null) {
logger.error("dbmanager-> Failed to get the cache. cache null");
throw new ServletException(
"Failed to get the cache. cache null");
} else {
cacheManager.addCache(employeeCache);
logger.info("dbmanager-> cache added to the cacheManager");
}
}
// create folder that will contain file samplings and submitquery
// result
// in the /webapps/folder_portlet // in the /webapps/folder_portlet
String path = this.getServletContext().getRealPath("") + "/" String path = this.getServletContext().getRealPath("") + "/"
+ "computationResult"; + "computationResult";
// System.out.println("***PATH FILE: " + path);
File computationResult = new File(path); File computationResult = new File(path);
if (!computationResult.exists()) { if (!computationResult.exists()) {
computationResult.mkdir(); computationResult.mkdir();
// System.out.println("***DIRECTORY CREATED"); logger.info("dbmanager-> Folder computationResult created in : "
+ this.getServletContext().getRealPath(""));
}
} catch (Exception e) {
// TODO: handle exception
logger.error("dbmanager-> ", e);
throw e;
}
}
public void createCache(String cachePath) throws ServletException {
try {
CacheConfiguration config = new CacheConfiguration();
config.setName("DBCache");
config.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU);
config.eternal(false);
config.timeToLiveSeconds(172800);
config.timeToIdleSeconds(0);
// config.maxEntriesLocalHeap(10000);
config.diskExpiryThreadIntervalSeconds(120);
config.maxBytesLocalDisk(2, MemoryUnit.GIGABYTES);
config.maxBytesLocalHeap(200, MemoryUnit.MEGABYTES);
config.diskSpoolBufferSizeMB(30);
config.overflowToDisk(true);
config.diskPersistent(false);
config.diskStorePath(cachePath);
employeeCache = new Cache(config);
} catch (Exception e) {
// TODO: handle exception
logger.error("dbmanager-> Failed to create the cache", e);
} }
} }
@ -139,20 +193,30 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
super.destroy(); super.destroy();
try { try {
// System.out.println("*** REMOVING CACHE");
CacheManager cacheManager = CacheManager.getInstance(); CacheManager cacheManager = CacheManager.getInstance();
if (cacheManager != null) {
if (cacheManager.cacheExists("DBCache")) { if (cacheManager.cacheExists("DBCache")) {
// System.out.println("*** cache exist"); // System.out.println("*** cache exist");
cacheManager.removeCache("DBCache"); cacheManager.removeCache("DBCache");
// cacheManager.removalAll(); // cacheManager.removalAll();
// System.out.println("*** cache removed"); // System.out.println("*** cache removed");
logger.info("dbmanager-> DBCache removed");
} }
cacheManager.shutdown(); cacheManager.shutdown();
} else {
logger.error("dbmanager-> Failed to get the cacheManager. cacheManager null");
throw new Exception(
"Failed to get the cacheManager. cacheManager null");
}
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception // TODO: handle exception
logger.error("dbmanager-> ", e); logger.error("dbmanager-> Failed to destroy the cache", e);
// e.printStackTrace(); // e.printStackTrace();
} }
@ -513,22 +577,24 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
logger.info("dbmanager-> DatabaseName: " + db); logger.info("dbmanager-> DatabaseName: " + db);
logger.info("dbmanager-> SchemaName: " + scm); logger.info("dbmanager-> SchemaName: " + scm);
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.SCHEMA))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.SCHEMA))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((scm==null)||(scm.equals(""))){ if ((scm == null) || (scm.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.DATABASE))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.DATABASE))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }
@ -761,8 +827,8 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
// list that contains table attributes // list that contains table attributes
List<String> listAttributes = null; List<String> listAttributes = null;
//converted query // converted query
String convertedQuery=""; String convertedQuery = "";
String algorithmId = "SUBMITQUERY"; String algorithmId = "SUBMITQUERY";
@ -778,13 +844,13 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
logger.info("dbmanager-> SmartCorrections check: " logger.info("dbmanager-> SmartCorrections check: "
+ smartCorrectionQuery); + smartCorrectionQuery);
if ((rs==null)||(rs.equals(""))){ if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((query==null)||(query.equals(""))){ if ((query == null) || (query.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
@ -848,10 +914,10 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
// output.size()); // output.size());
// logger.info("build the result - finished"); // logger.info("build the result - finished");
//get the converted query // get the converted query
if (smartCorrectionQuery==true){ if (smartCorrectionQuery == true) {
convertedQuery=output.get(0).getValue(); convertedQuery = output.get(0).getValue();
output.remove(0); output.remove(0);
} }
@ -892,18 +958,16 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
String partialPathFile = applicationPath + "/computationResult/" String partialPathFile = applicationPath + "/computationResult/"
+ fileName; + fileName;
SubmitQueryResultWithFileFromServlet obj = new SubmitQueryResultWithFileFromServlet( SubmitQueryResultWithFileFromServlet obj = new SubmitQueryResultWithFileFromServlet(
listAttributes, convertedQuery, partialPathFile); listAttributes, convertedQuery, partialPathFile);
return obj; return obj;
} }
@Override @Override
public SamplingResultWithFileFromServlet sample( public SamplingResultWithFileFromServlet sample(
LinkedHashMap<String, String> dataInput, String elementType) throws Exception { LinkedHashMap<String, String> dataInput, String elementType)
throws Exception {
// data input // data input
List<Parameter> inputParameters = new ArrayList<Parameter>(); List<Parameter> inputParameters = new ArrayList<Parameter>();
// output sample result // output sample result
@ -923,28 +987,30 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
logger.info("dbmanager-> SchemaName: " + scm); logger.info("dbmanager-> SchemaName: " + scm);
logger.info("dbmanager-> TableName: " + tab); logger.info("dbmanager-> TableName: " + tab);
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.SCHEMA))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.SCHEMA))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((scm==null)||(scm.equals(""))){ if ((scm == null) || (scm.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((tab==null)||(tab.equals(""))){ if ((tab == null) || (tab.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.DATABASE))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.DATABASE))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((tab==null)||(tab.equals(""))){ if ((tab == null) || (tab.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }
@ -1003,7 +1069,8 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
@Override @Override
public SamplingResultWithFileFromServlet smartSample( public SamplingResultWithFileFromServlet smartSample(
LinkedHashMap<String, String> dataInput, String elementType) throws Exception { LinkedHashMap<String, String> dataInput, String elementType)
throws Exception {
// data input // data input
List<Parameter> inputParameters = new ArrayList<Parameter>(); List<Parameter> inputParameters = new ArrayList<Parameter>();
// output sample result // output sample result
@ -1023,28 +1090,30 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
logger.info("dbmanager-> SchemaName: " + scm); logger.info("dbmanager-> SchemaName: " + scm);
logger.info("dbmanager-> TableName: " + tab); logger.info("dbmanager-> TableName: " + tab);
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.SCHEMA))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.SCHEMA))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((scm==null)||(scm.equals(""))){ if ((scm == null) || (scm.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((tab==null)||(tab.equals(""))){ if ((tab == null) || (tab.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.DATABASE))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.DATABASE))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((tab==null)||(tab.equals(""))){ if ((tab == null) || (tab.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }
@ -1103,7 +1172,8 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
@Override @Override
public SamplingResultWithFileFromServlet randomSample( public SamplingResultWithFileFromServlet randomSample(
LinkedHashMap<String, String> dataInput, String elementType) throws Exception { LinkedHashMap<String, String> dataInput, String elementType)
throws Exception {
// data input // data input
List<Parameter> inputParameters = new ArrayList<Parameter>(); List<Parameter> inputParameters = new ArrayList<Parameter>();
@ -1124,28 +1194,30 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements
logger.info("dbmanager-> SchemaName: " + scm); logger.info("dbmanager-> SchemaName: " + scm);
logger.info("dbmanager-> TableName: " + tab); logger.info("dbmanager-> TableName: " + tab);
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.SCHEMA))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.SCHEMA))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((scm==null)||(scm.equals(""))){ if ((scm == null) || (scm.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((tab==null)||(tab.equals(""))){ if ((tab == null) || (tab.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }
if ((elementType!=null)&&(elementType.equals(ConstantsPortlet.DATABASE))) { if ((elementType != null)
if ((rs==null)||(rs.equals(""))){ && (elementType.equals(ConstantsPortlet.DATABASE))) {
if ((rs == null) || (rs.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((db==null)||(db.equals(""))){ if ((db == null) || (db.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
if ((tab==null)||(tab.equals(""))){ if ((tab == null) || (tab.equals(""))) {
throw new Exception("Unable to load data"); throw new Exception("Unable to load data");
} }
} }