001    package edu.nrao.sss.webapp;
002    
003    import javax.faces.context.FacesContext;
004    import javax.faces.application.Application;
005    import javax.faces.el.ValueBinding;
006    import javax.faces.el.EvaluationException;
007    import javax.faces.el.ReferenceSyntaxException;
008    
009    import org.apache.log4j.Logger;
010    
011    /**
012     * This class provides an easy way to get access to JSF Managed Beans via
013     * their EL value bindings (For example "#{managedBean}").
014     * @author btruitt
015     *
016     */
017    public class ManagedBeanRepository {
018            private static final Logger log = Logger.getLogger(ManagedBeanRepository.class);
019            
020            /**
021             * Fetches the Object that is currently being used in the FacesContext 
022             * by using its JSF Expression Language ValueBinding. The reference returned
023             * is a refrence of type <code>c</code>
024             * 
025             * @param c The class of the object being referenced by elStatement
026             * @param elStatement a JSF EL statement (i.e. "#{managedBean}").
027             * @return null if the ValueBinding doesn't exist or is of the wrong type.
028             * @throws IllegalArgumentException if <code>elStatement</code> is invalid.
029             */
030            @SuppressWarnings("unchecked")
031            public static <T> T getManagedBean(Class<T> c, String elStatement)
032            {
033                    FacesContext context = FacesContext.getCurrentInstance();
034                    
035                    T ptab = null;
036                    if (context != null)
037                    {
038                            Application app = context.getApplication();
039                            if (app != null)
040                            {
041                                    try
042                                    {
043                                            ValueBinding vb = app.createValueBinding(elStatement);
044                                            if (vb != null)
045                                            {
046                                                    Object value = vb.getValue(context);
047                                                    if (value != null && value.getClass().equals(c))
048                                                    {
049                                                            ptab = (T)value;
050                                                    }
051                                            }
052                                    }
053                                    catch (ReferenceSyntaxException rse)
054                                    {
055                                            log.warn("Could not resolve the expression: " + elStatement, rse);
056                                            throw new IllegalArgumentException("EL statement: " + elStatement +
057                                                    "is invalid", rse);
058                                    }
059                                    catch(EvaluationException ee)
060                                    {
061                                            log.warn("Could not resolve the expression: " + elStatement, ee);
062                                            throw new IllegalArgumentException("EL statement: " + elStatement +
063                                                    "is invalid", ee);
064                                    }
065                            }
066                    }
067                    
068                    return ptab;
069            }
070    }