JSFUtils.java for ADF Application
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
public class JSFUtils
{
private static final String MISSING_RESOURCE = "Missing resource: ";
/**
* Method for taking a reference to a JSF binding expression and returning
*/
public static Object resolveExpression(String expression)
{
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression, Object.class);
return valueExp.getValue(elContext);
}
public static String resolveRemoteUser()
{
FacesContext facesContext = getFacesContext();
ExternalContext ectx = facesContext.getExternalContext();
return ectx.getRemoteUser();
}
public static String resolveUserPrincipal()
{
FacesContext facesContext = getFacesContext();
ExternalContext ectx = facesContext.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
return request.getUserPrincipal().getName();
}
public static Object resloveMethodExpression(String expression,
Class returnType,
Class[] argTypes,
Object[] argValues)
{
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
MethodExpression methodExpression =
elFactory.createMethodExpression(elContext, expression, returnType,
argTypes);
return methodExpression.invoke(elContext, argValues);
}
/**
* Method for taking a reference to a JSF binding expression and returning
* the matching Boolean.
* @param expression EL expression
* @return Managed object
*/
public static Boolean resolveExpressionAsBoolean(String expression)
{
return (Boolean) resolveExpression(expression);
}
/**
* Method for taking a reference to a JSF binding expression and returning
* the matching String (or creating it).
* @param expression EL expression
* @return Managed object
*/
public static String resolveExpressionAsString(String expression)
{
return (String) resolveExpression(expression);
}
/**
* Convenience method for resolving a reference to a managed bean by name
* rather than by expression.
* @param beanName name of managed bean
* @return Managed object
*/
public static Object getManagedBeanValue(String beanName)
{
StringBuffer buff = new StringBuffer("#{");
buff.append(beanName);
buff.append("}");
return resolveExpression(buff.toString());
}
/**
* Method for setting a new object into a JSF managed bean
* Note: will fail silently if the supplied object does
* not match the type of the managed bean.
* @param expression EL expression
* @param newValue new value to set
*/
public static void setExpressionValue(String expression, Object newValue)
{
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression, Object.class);
//Check that the input newValue can be cast to the property type
//expected by the managed bean.
//If the managed Bean expects a primitive we rely on Auto-Unboxing
//I could do a more comprehensive check and conversion from the object
//to the equivilent primitive but life is too short
Class bindClass = valueExp.getType(elContext);
if (bindClass.isPrimitive() || bindClass.isInstance(newValue))
{
valueExp.setValue(elContext, newValue);
}
}
/**
* Convenience method for setting the value of a managed bean by name
* rather than by expression.
* @param beanName name of managed bean
* @param newValue new value to set
*/
public static void setManagedBeanValue(String beanName, Object newValue)
{
StringBuffer buff = new StringBuffer("#{");
buff.append(beanName);
buff.append("}");
setExpressionValue(buff.toString(), newValue);
}
/**
* Convenience method for setting Session variables.
* @param key object key
* @param object value to store
*/
public static void storeOnSession(String key, Object object)
{
FacesContext ctx = getFacesContext();
Map sessionState = ctx.getExternalContext().getSessionMap();
sessionState.put(key, object);
}
/**
* Convenience method for getting Session variables.
* @param key object key
* @return session object for key
*/
public static Object getFromSession(String key)
{
FacesContext ctx = getFacesContext();
Map sessionState = ctx.getExternalContext().getSessionMap();
return sessionState.get(key);
}
public static String getFromHeader(String key)
{
FacesContext ctx = getFacesContext();
ExternalContext ectx = ctx.getExternalContext();
return ectx.getRequestHeaderMap().get(key);
}
/**
* Convenience method for getting Request variables.
* @param key object key
* @return session object for key
*/
public static Object getFromRequest(String key)
{
FacesContext ctx = getFacesContext();
Map sessionState = ctx.getExternalContext().getRequestMap();
return sessionState.get(key);
}
/**
* This method will give you the message from resource bundle for key passed.
*/
public static String getStringFromBundle(String key)
{
ResourceBundle bundle = getBundle();
return getStringSafely(bundle, key, null);
}
/**
* Method to construct a FacesMesssage from a defined error key and severity
*/
public static FacesMessage getMessageFromBundle(String key,
FacesMessage.Severity severity)
{
ResourceBundle bundle = getBundle();
String summary = getStringSafely(bundle, key, null);
String detail = getStringSafely(bundle, key + "_detail", summary);
FacesMessage message = new FacesMessage(summary, detail);
message.setSeverity(severity);
return message;
}
/**
* Method used to add a information message.
*/
public static void addFacesInformationMessage(String msg)
{
FacesContext ctx = getFacesContext();
FacesMessage fm =
new FacesMessage(FacesMessage.SEVERITY_INFO, msg, "");
ctx.addMessage(getRootViewComponentId(), fm);
}
/**
* Method to add JSF error message.
*/
public static void addFacesErrorMessage(String msg)
{
FacesContext ctx = getFacesContext();
FacesMessage fm =
new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "");
ctx.addMessage(getRootViewComponentId(), fm);
}
/**
* Method to add JSF error message for a specific attribute.
*/
public static void addFacesErrorMessage(String attrName, String msg)
{
FacesContext ctx = getFacesContext();
FacesMessage fm =
new FacesMessage(FacesMessage.SEVERITY_ERROR, attrName, msg);
ctx.addMessage(getRootViewComponentId(), fm);
}
/**
* Method to get view id of the view root.
*/
public static String getRootViewId()
{
return getFacesContext().getViewRoot().getViewId();
}
/**
* Method to get component id of the view root.
*/
public static String getRootViewComponentId()
{
return getFacesContext().getViewRoot().getId();
}
/**
* Method to get FacesContext.
*/
public static FacesContext getFacesContext()
{
return FacesContext.getCurrentInstance();
}
/**
* Method to get the correct local
*/
private static ResourceBundle getBundle()
{
FacesContext ctx = getFacesContext();
UIViewRoot uiRoot = ctx.getViewRoot();
Locale locale = uiRoot.getLocale();
ClassLoader ldr = Thread.currentThread().getContextClassLoader();
return ResourceBundle.getBundle(ctx.getApplication().getMessageBundle(),
locale, ldr);
}
/**
* Method to get an HTTP Request attribute.
* @param name attribute name
* @return attribute value
*/
public static Object getRequestAttribute(String name)
{
return getFacesContext().getExternalContext().getRequestMap().get(name);
}
/**
* Method to set an HTTP Request attribute.
* @param name attribute name
* @param value attribute value
*/
public static void setRequestAttribute(String name, Object value)
{
getFacesContext().getExternalContext().getRequestMap().put(name,
value);
}
/**
* Method to proxy for resource keys that don't exist
*/
private static String getStringSafely(ResourceBundle bundle, String key,
String defaultValue)
{
String resource = null;
try
{
resource = bundle.getString(key);
}
catch (MissingResourceException mrex)
{
if (defaultValue != null)
{
resource = defaultValue;
}
else
{
resource = MISSING_RESOURCE + key;
}
}
return resource;
}
/**
* Method to get an UIComponent in page with its component id.
*/
public static UIComponent findComponentInRoot(String id)
{
UIComponent component = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null)
{
UIComponent root = facesContext.getViewRoot();
component = findComponent(root, id);
}
return component;
}
/**
* Method to get an UIComponent from its root component.
*/
public static UIComponent findComponent(UIComponent base, String id)
{
if (id.equals(base.getId()))
return base;
UIComponent children = null;
UIComponent result = null;
Iterator childrens = base.getFacetsAndChildren();
while (childrens.hasNext() && (result == null))
{
children = (UIComponent) childrens.next();
if (id.equals(children.getId()))
{
result = children;
break;
}
result = findComponent(children, id);
if (result != null)
{
break;
}
}
return result;
}
/**
* Method to get the URL.
*/
public static String getPageURL(String view)
{
FacesContext facesContext = getFacesContext();
ExternalContext externalContext = facesContext.getExternalContext();
String url =
((HttpServletRequest) externalContext.getRequest()).getRequestURL().toString();
StringBuffer urlBuff = new StringBuffer();
urlBuff.append(url.substring(0, url.lastIndexOf("faces/")));
urlBuff.append("faces");
String targetUrl = view.startsWith("/")? view: "/" + view;
urlBuff.append(targetUrl);
return urlBuff.toString();
}
}
Comments
Post a Comment