Link to home
Start Free TrialLog in
Avatar of Manikandan Thiagarajan
Manikandan ThiagarajanFlag for India

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.HttpSession;

import net.sf.ehcache.CacheManager;
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(GlobalDataStore.class);
            
       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(String sessID)
      {
            widgetCache = cacheManager.getEhcache( "widgets" );
            widgetCache.remove(sessID);
      }

      public static String getParentSessionObj(String sessID)  
       {  
            try{                  
                 widgetCache = cacheManager.getEhcache( "widgets" );            
            System.out.println(widgetCache.getSize());
            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]==>parentSessionObjectMap := "+parentSessionObjectMap);
             }
             catch(Exception e){e.printStackTrace();}
       }
      
       public static void deleteParentSessionObj(String sessID)
      
       {
             widgetCache =  cacheManager.getEhcache( "widgets" );
             widgetCache.remove(sessID);
            // parentSessionObjectMap.remove(sessID);
             parentLog.info("********* A Parent Session is Deleted here !!! *********");
             parentLog.info("The Deleted Parent Session ID: " + sessID);
             parentLog.info("[After A Session Deleted]==>parentSessionObjectMap := "+parentSessionObjectMap);
       }

}
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial