duminică, 19 iulie 2015

OmniFaces approach for writing custom ELContexts

If you are in the situation of writing a custom ELContext, then you need to extend the ELContext and override the abstract methods. But, if you have OmniFaces available in your project, then you can extend the org.omnifaces.el.ELContextWrapper, which provides a simple implementation of ELContext that can be sub-classed by developers wishing to provide specialized behavior to an existing ELContext instance. The default implementation of all methods is to call through to the wrapped ELContext. The source code of ELContextWrapper is:

package org.omnifaces.el;

import java.util.Locale;

import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.VariableMapper;
import javax.faces.FacesWrapper;
import javax.faces.application.ViewHandler;

public class ELContextWrapper extends ELContext implements FacesWrapper<ELContext> {

 private ELContext elContext;

 public ELContextWrapper() {}

 public ELContextWrapper(ELContext elContext) {
  this.elContext = elContext;
 }

 @Override
 public ELContext getWrapped() {
  return elContext;
 }

 @Override
 public void setPropertyResolved(boolean resolved) {
  getWrapped().setPropertyResolved(resolved);
 }

 @Override
 public boolean isPropertyResolved() {
  return getWrapped().isPropertyResolved();
 }

 @Override
 public void putContext(@SuppressWarnings("rawtypes") Class key, Object contextObject) {
  getWrapped().putContext(key, contextObject);
}

 @Override
 public Object getContext(@SuppressWarnings("rawtypes") Class key) {
  return getWrapped().getContext(key);
 }

 @Override
 public ELResolver getELResolver() {
  return getWrapped().getELResolver();
 }

 @Override
 public FunctionMapper getFunctionMapper() {
  return getWrapped().getFunctionMapper();
 }

 @Override
 public Locale getLocale() {
  return getWrapped().getLocale();
 }

 @Override
 public void setLocale(Locale locale) {
  getWrapped().setLocale(locale);
 }

 @Override
 public VariableMapper getVariableMapper() {
  return getWrapped().getVariableMapper();
 }
}

Now, you can write a custom ELContext by following the next two skeletons:

·         extend ELContextWrapper and override #getWrapped() to return the instance wrapped by ELContextWrapper

import javax.el.ELContext;
import org.omnifaces.el.ELContextWrapper;

public class MyELContext extends ELContextWrapper {

 // override the desired ELContext methods
   
 //extend ELContextWrapper and override #getWrapped() to return the instance wrapped by ELContextWrapper
 @Override
 public ELContext getWrapped() {
  return super.getWrapped();
 }
}

·         provide ELContext instance to the overloaded constructor

import javax.el.ELContext;
import org.omnifaces.el.ELContextWrapper;

public class MyELContext extends ELContextWrapper {

 public MyELContext(ELContext elContext) {
  super(elContext);
 }   
   
 // override the desired ELContext methods 
}

Further, you can check the OmniFaces custom ELContexts:


These provides valuable lessons about how to correctly write a custom ELContext.

Niciun comentariu:

Trimiteți un comentariu