Coverage Report - org.apache.myfaces.el.unified.resolver.ManagedBeanResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
ManagedBeanResolver
0%
0/89
0%
0/48
0
ManagedBeanResolver$1
0%
0/3
N/A
0
ManagedBeanResolver$2
0%
0/3
N/A
0
ManagedBeanResolver$3
0%
0/3
N/A
0
ManagedBeanResolver$4
0%
0/2
N/A
0
ManagedBeanResolver$Scope
N/A
N/A
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.el.unified.resolver;
 20  
 
 21  
 import java.beans.FeatureDescriptor;
 22  
 import java.util.ArrayList;
 23  
 import java.util.HashMap;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import javax.el.ELContext;
 28  
 import javax.el.ELException;
 29  
 import javax.el.ELResolver;
 30  
 import javax.el.PropertyNotFoundException;
 31  
 import javax.el.PropertyNotWritableException;
 32  
 import javax.faces.context.ExternalContext;
 33  
 import javax.faces.context.FacesContext;
 34  
 import org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 import org.apache.myfaces.config.ManagedBeanBuilder;
 37  
 import org.apache.myfaces.config.RuntimeConfig;
 38  
 import org.apache.myfaces.config.element.ManagedBean;
 39  
 
 40  
 /**
 41  
  * See JSF 1.2 spec section 5.6.1.2
 42  
  *
 43  
  * @author Stan Silvert
 44  
  */
 45  
 public class ManagedBeanResolver extends ELResolver {
 46  0
     private static final Log log              = LogFactory.getLog(ManagedBeanResolver.class);
 47  
     private static final String BEANS_UNDER_CONSTRUCTION = "org.apache.myfaces.el.unified.resolver.managedbean.beansUnderConstruction";
 48  
     
 49  
     // adapted from Manfred's JSF 1.1 VariableResolverImpl
 50  0
     protected static final Map s_standardScopes = new HashMap(16);
 51  
     static {
 52  0
         s_standardScopes.put(
 53  
             "request",
 54  
             new Scope()
 55  0
             {
 56  
                 public void put(ExternalContext extContext, String name, Object obj)
 57  
                 {
 58  0
                     extContext.getRequestMap().put(name, obj);
 59  0
                 }
 60  
             });
 61  0
         s_standardScopes.put(
 62  
             "session",
 63  
             new Scope()
 64  0
             {
 65  
                 public void put(ExternalContext extContext, String name, Object obj)
 66  
                 {
 67  0
                     extContext.getSessionMap().put(name, obj);
 68  0
                 }
 69  
             });
 70  0
         s_standardScopes.put(
 71  
             "application",
 72  
             new Scope()
 73  0
             {
 74  
                 public void put(ExternalContext extContext, String name, Object obj)
 75  
                 {
 76  0
                     extContext.getApplicationMap().put(name, obj);
 77  0
                 }
 78  
             });
 79  0
         s_standardScopes.put(
 80  
             "none",
 81  
             new Scope()
 82  0
             {
 83  
                 public void put(ExternalContext extContext, String name, Object obj)
 84  
                 {
 85  
                     // do nothing
 86  0
                 }
 87  
             });
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Stores all scopes defined for this instance of <code>VariableResolver</code>
 92  
      * <p>
 93  
      * Can store instances of <code>Scope</code> which have the ability to
 94  
      * dynamically resolve against ExternalContext for put operations.
 95  
      * </p>
 96  
      * <p>
 97  
      * WARNING: this implementation is not serialized as it is thread safe because
 98  
      *          it does not update/add to _scopes after object initialization.
 99  
      *          If you need to add your own scopes, either extend and add more
 100  
      *          in an initialization block, or add proper sychronization
 101  
      * </p>
 102  
      */
 103  0
     protected final Map _scopes = new HashMap(16);
 104  
     {
 105  0
         _scopes.putAll(s_standardScopes);
 106  
     }
 107  
     
 108  
     /**
 109  
      * RuntimeConfig is instantiated once per servlet and never changes--we can
 110  
      * safely cache it
 111  
      */
 112  
     private RuntimeConfig runtimeConfig;
 113  
     
 114  0
     private ManagedBeanBuilder beanBuilder = new ManagedBeanBuilder();
 115  
     
 116  
     /** Creates a new instance of ManagedBeanResolver */
 117  0
     public ManagedBeanResolver() {
 118  0
     }
 119  
 
 120  
     public void setValue(final ELContext context, final Object base, final Object property, final Object value)
 121  
         throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException {
 122  
         
 123  0
         if ( (base == null) && (property == null)) {
 124  0
             throw new PropertyNotFoundException();
 125  
         }
 126  
         
 127  0
     }
 128  
 
 129  
     public boolean isReadOnly(final ELContext context, final Object base, final Object property)
 130  
         throws NullPointerException, PropertyNotFoundException, ELException {
 131  
         
 132  0
        if ( (base == null) && (property == null)) {
 133  0
             throw new PropertyNotFoundException();
 134  
         }
 135  
         
 136  0
         return false;
 137  
     }
 138  
 
 139  
     public Object getValue(final ELContext context, final Object base, final Object property)
 140  
         throws NullPointerException, PropertyNotFoundException, ELException {
 141  
         
 142  0
         if (base != null) return null;
 143  
         
 144  0
         if (property == null) {
 145  0
             throw new PropertyNotFoundException();
 146  
         }
 147  
         
 148  0
         final ExternalContext extContext = externalContext(context);
 149  
         
 150  0
         if (extContext == null) return null;
 151  0
         if (extContext.getRequestMap().containsKey(property)) return null;
 152  0
         if (extContext.getSessionMap().containsKey(property)) return null;
 153  0
         if (extContext.getApplicationMap().containsKey(property)) return null;
 154  
         
 155  0
         if ( !(property instanceof String) ) return null;
 156  
         
 157  0
         final String strProperty = (String)property;
 158  
         
 159  0
         final ManagedBean managedBean = runtimeConfig(context).getManagedBean(strProperty);
 160  0
         Object beanInstance = null;
 161  0
         if (managedBean != null) {
 162  0
             FacesContext facesContext = facesContext(context);
 163  0
             context.setPropertyResolved(true);
 164  0
             beanInstance = createManagedBean(managedBean, facesContext);
 165  
         }
 166  
         
 167  0
         return beanInstance;
 168  
     }
 169  
     
 170  
     // Create a managed bean.  If the scope of the bean is "none" then
 171  
     // return it right away.  Otherwise store the bean in the appropriate
 172  
     // scope and return null.
 173  
     //
 174  
     // adapted from Manfred's JSF 1.1 VariableResolverImpl
 175  
     private Object createManagedBean(final ManagedBean managedBean,
 176  
                                   final FacesContext facesContext)
 177  
         throws ELException {
 178  
         
 179  0
         final ExternalContext extContext = facesContext.getExternalContext();
 180  0
         final Map requestMap = extContext.getRequestMap();
 181  
         
 182  
         // check for cyclic references
 183  0
         List beansUnderConstruction = (List)requestMap.get(BEANS_UNDER_CONSTRUCTION);
 184  0
         if (beansUnderConstruction == null) {
 185  0
             beansUnderConstruction = new ArrayList();
 186  0
             requestMap.put(BEANS_UNDER_CONSTRUCTION, beansUnderConstruction);
 187  
         }
 188  
 
 189  0
         final String managedBeanName = managedBean.getManagedBeanName();
 190  0
         if (beansUnderConstruction.contains(managedBeanName)) {
 191  0
             throw new ELException( "Detected cyclic reference to managedBean " + managedBeanName);
 192  
         }
 193  
 
 194  0
         beansUnderConstruction.add(managedBeanName);
 195  
         
 196  0
         Object obj = null;
 197  
         try {
 198  0
             obj = beanBuilder.buildManagedBean(facesContext, managedBean);
 199  
         } finally {
 200  0
             beansUnderConstruction.remove(managedBeanName);
 201  0
         }
 202  
 
 203  0
         putInScope(managedBean, extContext, obj);
 204  
         
 205  0
         return obj;
 206  
     }
 207  
     
 208  
     private void putInScope(final ManagedBean managedBean, final ExternalContext extContext, final Object obj) {
 209  
 
 210  0
         final String managedBeanName = managedBean.getManagedBeanName();
 211  
         
 212  0
         if (obj == null) {
 213  0
             if (log.isDebugEnabled())
 214  0
                 log.debug("Variable '" + managedBeanName + "' could not be resolved.");
 215  
         } else {
 216  
 
 217  0
             final String scopeKey = managedBean.getManagedBeanScope();
 218  
 
 219  
             // find the scope handler object
 220  0
             final Scope scope = (Scope) _scopes.get(scopeKey);
 221  0
             if (scope == null) {
 222  0
                 log.error("Managed bean '" + managedBeanName + "' has illegal scope: " + scopeKey);
 223  
             } else {
 224  0
                 scope.put(extContext, managedBeanName, obj);
 225  
             }
 226  
         }
 227  
 
 228  0
     }
 229  
     
 230  
     // get the FacesContext from the ELContext
 231  
     private static FacesContext facesContext(final ELContext context) {
 232  0
         return (FacesContext)context.getContext(FacesContext.class);
 233  
     }
 234  
     
 235  
     private static ExternalContext externalContext(final ELContext context) {
 236  0
         final FacesContext facesContext = facesContext(context);
 237  
 
 238  0
         return facesContext != null ? facesContext.getExternalContext() : null;
 239  
     }
 240  
 
 241  
     public Class<?> getType(final ELContext context, final Object base, final Object property)
 242  
         throws NullPointerException, PropertyNotFoundException, ELException {
 243  
         
 244  0
         if ( (base == null) && (property == null)) {
 245  0
             throw new PropertyNotFoundException();
 246  
         }
 247  
         
 248  0
         return null;
 249  
     }
 250  
 
 251  
     public Iterator getFeatureDescriptors(final ELContext context, final Object base) {
 252  
         
 253  0
         if (base != null) return null;
 254  
         
 255  0
         final ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>();
 256  
         
 257  0
         final Map<String, ManagedBean> managedBeans = runtimeConfig(context).getManagedBeans();
 258  0
         for (Map.Entry<String, ManagedBean> managedBean : managedBeans.entrySet()) {
 259  0
             descriptors.add(makeDescriptor(managedBean.getKey(), managedBean.getValue()));
 260  
         }
 261  
         
 262  0
         return descriptors.iterator();
 263  
     }
 264  
     
 265  
     private static FeatureDescriptor makeDescriptor(final String beanName, final ManagedBean managedBean) {
 266  0
         final FeatureDescriptor fd = new FeatureDescriptor();
 267  0
         fd.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
 268  0
         fd.setValue(ELResolver.TYPE, managedBean.getManagedBeanClass());
 269  0
         fd.setName(beanName);
 270  0
         fd.setDisplayName(beanName);
 271  0
         fd.setShortDescription(managedBean.getDescription());  
 272  0
         fd.setExpert(false);
 273  0
         fd.setHidden(false);
 274  0
         fd.setPreferred(true);
 275  0
         return fd;
 276  
     }
 277  
 
 278  
     protected RuntimeConfig runtimeConfig(final ELContext context) {
 279  0
         final FacesContext facesContext = facesContext(context);
 280  
         
 281  
         // application-level singleton - we can safely cache this
 282  0
         if (this.runtimeConfig == null) {
 283  0
             this.runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext.getExternalContext());
 284  
         }
 285  
         
 286  0
         return runtimeConfig;
 287  
     }
 288  
     
 289  
     public Class<?> getCommonPropertyType(final ELContext context, final Object base) {
 290  
         
 291  0
         if (base != null) return null;
 292  
         
 293  0
         return Object.class;
 294  
     }
 295  
     
 296  
     interface Scope {
 297  
         public void put(ExternalContext extContext, String name, Object obj);
 298  
     }
 299  
     
 300  
 }