/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.portlet.forums.ui;
//core java classes
import java.util.Locale;
import java.util.Map;
import java.util.Iterator;
import java.util.ResourceBundle;
//logging related
import org.apache.log4j.Logger;
//servlet import
import javax.servlet.http.HttpServletRequest;
//portal import
import javax.portlet.PortletRequest;
//jsf imports
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.component.UIViewRoot;
import javax.faces.component.*;
/**
* @author <a href="mailto:sohil.shah@jboss.com">Sohil Shah</a>
*
*
*/
public class JSFUtil
{
private static Logger log = Logger.getLogger(JSFUtil.class);
/**
*
* @author sshah
*
*/
public static String getRequestParameter(String name)
{
String parameter = null;
Map requestParameterMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
if(requestParameterMap!=null)
{
parameter = (String)requestParameterMap.get(name);
}
return parameter;
}
/**
*
*
*/
public static boolean isRunningInPortal()
{
boolean isRunningInPortal = false;
Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if(request instanceof PortletRequest)
{
isRunningInPortal = true;
}
return isRunningInPortal;
}
/**
*
*
*/
public static String getContextPath()
{
String contextPath = "";
contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
return contextPath;
}
/**
*
* @return
*/
public static boolean isAnonymous()
{
boolean anonymous = true;
String remoteUser = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
if(remoteUser!=null && remoteUser.trim().length()>0)
{
anonymous = false;
}
return anonymous;
}
/**
*
* @author sshah
*
*
*/
public static String getComponentValue(String componentId)
{
String value = null;
UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
UIComponent component = root.findComponent(componentId);
if(component!=null)
{
Object o = component.getValueBinding("value").getValue(FacesContext.getCurrentInstance());
value = (String)o;
}
return value;
}
/**
*
* @author sshah
*
*
*/
public static void removeComponent(String componentId)
{
UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
UIComponent component = root.findComponent(componentId);
if(component!=null)
{
UIComponent parent = component.getParent();
parent.getChildren().remove(component);
}
}
/**
*
* @author sshah
*
*/
public static String handleException(Exception e)
{
String genericNavState = Constants.ERROR;
String msg = e.toString();
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,//severity
msg,//summary
msg//detail
);
FacesContext.getCurrentInstance().addMessage(Constants.ERROR,message);
//spit the stack trace
log.error(JSFUtil.class.getName(),e);
return genericNavState;
}
/**
*
* @return
*/
public static String getErrorMsg()
{
String errorMsg = null;
Iterator msgs = FacesContext.getCurrentInstance().getMessages(Constants.ERROR);
if(msgs!=null)
{
if(msgs.hasNext())
{
FacesMessage message = (FacesMessage)msgs.next();
errorMsg = message.getDetail();
}
}
return errorMsg;
}
/**
*
*
*/
public static boolean isErrorOccurred()
{
boolean errorOccurred = false;
Iterator msgs = FacesContext.getCurrentInstance().getMessages(Constants.ERROR);
if(msgs!=null && msgs.hasNext())
{
errorOccurred = true;
}
return errorOccurred;
}
/**
*
*
*/
public static void setMessage(String id,String msg)
{
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_INFO,//severity
msg,//summary
msg//detail
);
FacesContext.getCurrentInstance().addMessage(id,message);
}
/**
*
*
*/
public static String getMessage(String id)
{
String msg = null;
Iterator msgs = FacesContext.getCurrentInstance().getMessages(id);
if(msgs!=null)
{
if(msgs.hasNext())
{
FacesMessage message = (FacesMessage)msgs.next();
msg = message.getDetail();
}
}
return msg;
}
/**
*
*
*/
public static String getBundleMessage(String bundleName,String messageKey)
{
String bundleMessage = null;
//Getting ResourceBundle with current Locale
FacesContext ctx = FacesContext.getCurrentInstance();
UIViewRoot uiRoot = ctx.getViewRoot();
Locale locale = uiRoot.getLocale();
ClassLoader ldr = Thread.currentThread().getContextClassLoader();
ResourceBundle bundle = ResourceBundle.getBundle(bundleName,locale,ldr);
bundleMessage = bundle.getString(messageKey);
return bundleMessage;
}
/**
*
*/
public static Locale getSelectedLocale()
{
return FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
}
/**
*
*/
public static Locale getDefaultLocale()
{
return FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
}
/**
*
*/
public static Iterator getSupportedLocales()
{
return FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
}
}
|