A Legacy Notes Developer's journey into madness.

Getting a handle on Scope using Java

Devin Olson  June 4 2013 03:54:15 AM

XPages gives us some wonderfully cool pre-built containers into which we can throw various variables, and retrieve them later at our convenience.


The containers to which I'm referring are the SCOPE containers, specifically applicationScope, sessionScope, viewScope, and requestScope.   Using SSJS, we can reference these containers at will by using their respective names:



// put "bass" into viewScope using the key "fish"
viewScope.put("fish", "bass");

. . .

// retrieve the variable later in the code (or in an entirely different code block
var myFish = viewScope.get("fish");


This is really very powerful, and allows us to do all kinds of way cool things.


But what about Java?   In other posts I have harped on about how important it is to learn Java.  So if you create some Java code to handle a bunch of your business logic (either POJO or Beans), would it not be useful to be able to access these Scope containers?

Each of these Scope containers implements the Map interface ( "is a" Map applies), and using Maps in Java is easy.  Using them is so easy that I'm not even going to bother explaining how to use them.  Read the docs and figure it out.  

What I am going to tell you is how I access them.  Because the hardest part about using a Map is finding the Map.   (Hmmm -could there be a not-so hidden meaning there?)   This is a helper function I have in my core XSP Java library.   I hope it helps you out.  

Anyway, here is the code:



package com.learningxpages.demo;

import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

public class Core {
 public static enum SCOPE {
         Application, Session, View, Request;

         @Override
         public String toString() {
                 return this.name();
         }

         public String getInfo() {
                 return this.getDeclaringClass() + "." + this.getClass() + ":" + this.name();
         }
         
 }; // SCOPE


 @SuppressWarnings("unchecked")
 public static Map getScopeMap(FacesContext context, Core.SCOPE scope) {
         try {
                 if (null == context) {
                         throw new IllegalArgumentException("FacesContext is null");
                 }
                 if (null == scope) {
                         throw new IllegalArgumentException("Scope is null");
                 }

                 ExternalContext ec = context.getExternalContext();
                 switch (scope) {
                 case Session:
                         return ec.getSessionMap();

                 case Application:
                         return ec.getApplicationMap();

                 case View:
                         return context.getViewRoot().getViewMap();

                 case Request:
                         return ec.getRequestMap();

                 default:
                         return null;
                 } // switch

         } catch (Exception e) {
                 System.out.println("*");
                 System.out.println("EXCEPTION in Core.getScopeMap()");
                 if (null != scope) {
                         System.out.println("Scope:" + scope.name());
                 }
                 e.printStackTrace();
         } // try

         return null;
 } // getScopeMap(FacesContext, Scope)
 
} // Core




Hope this helps!
-Devin.
Comments
No Comments Found

Discussion for this entry is now closed.