Manikandan Thiagarajan
asked on
Could you please apply singleton pattern for cachemanager for this code
Could you please apply singleton pattern for cachemanager for this code
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSes sion;
import net.sf.ehcache.CacheManage r;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;
public class GlobalCacheDataStore {
private static final CacheManager cacheManager = new CacheManager();
/**
* A cache that we're designating to hold Widget instances
*/
private static Ehcache widgetCache;
public GlobalCacheDataStore()
{
// Load our widgets cache:
// widgetCache = cacheManager.getEhcache( "widgets" );
}
public static Logger parentLog = Logger.getLogger(GlobalDat aStore.cla ss);
public static Map<String, HttpSession> parentSessionObjectMap = new HashMap<String, HttpSession>();
public static Map<String, String> parentBrowserMap = new HashMap<String, String>();
public static String getParentBrowserMap(String sessID) {
widgetCache = cacheManager.getEhcache( "widgets" );
Element element = widgetCache.get( sessID );
if( element != null )
{
// Get the value out of the element and cast it to a Widget
return (String) element.getValue();
// return "Firefox";
}
return null;
}
public static void setParentBrowserMap(String sessID,String browser) {
widgetCache = cacheManager.getEhcache( "widgets" );
Element element = new Element( sessID, browser);
widgetCache.put(element);
}
public static void deleteParentBrowserMap(Str ing sessID)
{
widgetCache = cacheManager.getEhcache( "widgets" );
widgetCache.remove(sessID) ;
}
public static String getParentSessionObj(String sessID)
{
try{
widgetCache = cacheManager.getEhcache( "widgets" );
System.out.println(widgetC ache.getSi ze());
System.out.println(sessID) ;
// Retrieve the element that contains the requested widget
Element element = widgetCache.get( sessID );
System.out.println(element );
if( element != null )
{
// Get the value out of the element and cast it to a Widget
return (String) element.getValue();
}
}
catch(Exception e){e.printStackTrace();}
// We don't have the object in the cache so return null
return null;
}
public static void setParentSessionObj(String sessID,String sessStatus)
{
try
{
widgetCache = cacheManager.getEhcache( "widgets" );
Element element = new Element( sessID, sessStatus);
// Add the element to the cache
widgetCache.put( element );
// parentSessionObjectMap.put (sessID, sessObj);
parentLog.info("********* A New Parent Session is Starting here !!! *********");
parentLog.info("The New Parent Session ID: " + sessID);
parentLog.info("[After New Session Added]==>parentSessionObje ctMap := "+parentSessionObjectMap);
}
catch(Exception e){e.printStackTrace();}
}
public static void deleteParentSessionObj(Str ing sessID)
{
widgetCache = cacheManager.getEhcache( "widgets" );
widgetCache.remove(sessID) ;
// parentSessionObjectMap.rem ove(sessID );
parentLog.info("********* A Parent Session is Deleted here !!! *********");
parentLog.info("The Deleted Parent Session ID: " + sessID);
parentLog.info("[After A Session Deleted]==>parentSessionOb jectMap := "+parentSessionObjectMap);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSes
import net.sf.ehcache.CacheManage
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;
public class GlobalCacheDataStore {
private static final CacheManager cacheManager = new CacheManager();
/**
* A cache that we're designating to hold Widget instances
*/
private static Ehcache widgetCache;
public GlobalCacheDataStore()
{
// Load our widgets cache:
// widgetCache = cacheManager.getEhcache( "widgets" );
}
public static Logger parentLog = Logger.getLogger(GlobalDat
public static Map<String, HttpSession> parentSessionObjectMap = new HashMap<String, HttpSession>();
public static Map<String, String> parentBrowserMap = new HashMap<String, String>();
public static String getParentBrowserMap(String
widgetCache = cacheManager.getEhcache( "widgets" );
Element element = widgetCache.get( sessID );
if( element != null )
{
// Get the value out of the element and cast it to a Widget
return (String) element.getValue();
// return "Firefox";
}
return null;
}
public static void setParentBrowserMap(String
widgetCache = cacheManager.getEhcache( "widgets" );
Element element = new Element( sessID, browser);
widgetCache.put(element);
}
public static void deleteParentBrowserMap(Str
{
widgetCache = cacheManager.getEhcache( "widgets" );
widgetCache.remove(sessID)
}
public static String getParentSessionObj(String
{
try{
widgetCache = cacheManager.getEhcache( "widgets" );
System.out.println(widgetC
System.out.println(sessID)
// Retrieve the element that contains the requested widget
Element element = widgetCache.get( sessID );
System.out.println(element
if( element != null )
{
// Get the value out of the element and cast it to a Widget
return (String) element.getValue();
}
}
catch(Exception e){e.printStackTrace();}
// We don't have the object in the cache so return null
return null;
}
public static void setParentSessionObj(String
{
try
{
widgetCache = cacheManager.getEhcache( "widgets" );
Element element = new Element( sessID, sessStatus);
// Add the element to the cache
widgetCache.put( element );
// parentSessionObjectMap.put
parentLog.info("********* A New Parent Session is Starting here !!! *********");
parentLog.info("The New Parent Session ID: " + sessID);
parentLog.info("[After New Session Added]==>parentSessionObje
}
catch(Exception e){e.printStackTrace();}
}
public static void deleteParentSessionObj(Str
{
widgetCache = cacheManager.getEhcache( "widgets" );
widgetCache.remove(sessID)
// parentSessionObjectMap.rem
parentLog.info("********* A Parent Session is Deleted here !!! *********");
parentLog.info("The Deleted Parent Session ID: " + sessID);
parentLog.info("[After A Session Deleted]==>parentSessionOb
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.