[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Could you please apply singleton pattern for cachemanager for this code

Posted on 2012-08-28
1
Medium Priority
?
614 Views
Last Modified: 2012-09-04
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);
       }

}
0
Comment
1 Comment
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 38343838
I think you may be confused about what a singleton is?  You are currently creating one here:

private static final CacheManager cacheManager = new CacheManager();

There will only ever be one instance of this CacheManager object, so it's a singleton.

Doug
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question