Last active
August 29, 2015 14:04
-
-
Save dilnei/6d01f6bf497b3c2b219f to your computer and use it in GitHub Desktop.
FacesUtil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.weblog.usefull; | |
import java.util.Map; | |
import javax.faces.context.ExternalContext; | |
import javax.faces.context.FacesContext; | |
import org.apache.log4j.Logger; | |
/** | |
* Classe com métodos utilitarios para o faces. | |
* | |
* @author Dilnei Cunha. | |
*/ | |
public class FacesUtil { | |
static transient Logger logger = Logger.getLogger(FacesUtil.class); | |
/** | |
* Método responsavel por registrar um atributo em sessão. | |
* | |
* @param attributeName | |
* @param attributeValue | |
*/ | |
@SuppressWarnings("unchecked") | |
public static void setSessionAttribute(String attributeName, Object attributeValue) { | |
try { | |
ExternalContext externalContext = getExternalContext(); | |
if (externalContext != null) { | |
Map attrMap = externalContext.getSessionMap(); | |
if (attrMap != null) { | |
attrMap.put(attributeName, attributeValue); | |
} | |
} | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} | |
} | |
/** | |
* Método responsável por trazer instância atual do contexto ou um contexto | |
* externo. | |
* | |
* @return ExternalContext | |
*/ | |
public static ExternalContext getExternalContext() { | |
try { | |
FacesContext facesContext = FacesContext.getCurrentInstance(); | |
if (facesContext == null) { | |
return null; | |
} else { | |
return facesContext.getExternalContext(); | |
} | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
return null; | |
} | |
} | |
/** | |
* Método responsavel por buscar os atributos da sessão. | |
* | |
* @param attributeName | |
* @return Object | |
*/ | |
public static Object getSessionAttribute(String attributeName) { | |
try { | |
ExternalContext externalContext = getExternalContext(); | |
if (externalContext != null) { | |
Map attrMap = externalContext.getSessionMap(); | |
if (attrMap != null) { | |
return attrMap.get(attributeName); | |
} else { | |
return null; | |
} | |
} else { | |
return null; | |
} | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment