Coverage Report - org.apache.myfaces.el.convert.VariableResolverToELResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
VariableResolverToELResolver
0%
0/65
0%
0/40
4.8
 
 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.convert;
 20  
 
 21  
 import javax.el.ELContext;
 22  
 import javax.el.ELException;
 23  
 import javax.el.ELResolver;
 24  
 import javax.el.PropertyNotFoundException;
 25  
 import javax.el.PropertyNotWritableException;
 26  
 import javax.faces.context.FacesContext;
 27  
 import javax.faces.el.EvaluationException;
 28  
 import javax.faces.el.VariableResolver;
 29  
 import java.beans.FeatureDescriptor;
 30  
 import java.util.Collection;
 31  
 import java.util.HashSet;
 32  
 import java.util.Iterator;
 33  
 import org.apache.myfaces.el.VariableResolverImpl;
 34  
 
 35  
 /**
 36  
  * Wrapper that converts a VariableResolver into an ELResolver. See JSF 1.2 spec section 5.6.1.5
 37  
  * 
 38  
  * @author Stan Silvert (latest modification by $Author$)
 39  
  * @author Mathias Broekelmann
 40  
  * @version $Revision$ $Date$
 41  
  */
 42  
 @SuppressWarnings("deprecation")
 43  
 public final class VariableResolverToELResolver extends ELResolver
 44  
 {
 45  
 
 46  
     // holds a flag to check if this instance is already called in current thread 
 47  0
     private static final ThreadLocal<Collection<String>> propertyGuardThreadLocal
 48  
             = new ThreadLocal<Collection<String>>();
 49  
 
 50  
     /**
 51  
      * Gets the Collection<String> value of the propertyGuardThreadLocal.
 52  
      * If the value from the ThreadLocal ist null, a new Collection<String>
 53  
      * will be created.
 54  
      *
 55  
      * NOTE that we should not accomplish this by setting an initialValue on the
 56  
      * ThreadLocal, because this will automatically be set on the ThreadLocalMap
 57  
      * and thus can propably cause a memory leak.
 58  
      *
 59  
      * @return
 60  
      */
 61  
     private static Collection<String> getPropertyGuard()
 62  
     {
 63  0
         Collection<String> propertyGuard = propertyGuardThreadLocal.get();
 64  
 
 65  0
         if (propertyGuard == null)
 66  
         {
 67  0
             propertyGuard = new HashSet<String>();
 68  0
             propertyGuardThreadLocal.set(propertyGuard);
 69  
         }
 70  
 
 71  0
         return propertyGuard;
 72  
     }
 73  
     
 74  
     private VariableResolver variableResolver;
 75  
     private boolean isDefaultLegacyVariableResolver;
 76  
 
 77  
     /**
 78  
      * Creates a new instance of VariableResolverToELResolver
 79  
      */
 80  
     public VariableResolverToELResolver(final VariableResolver variableResolver)
 81  0
     {
 82  0
         this.variableResolver = variableResolver;
 83  0
         this.isDefaultLegacyVariableResolver = (this.variableResolver instanceof VariableResolverImpl);
 84  0
     }
 85  
     
 86  
     /**
 87  
      * @return the variableResolver
 88  
      */
 89  
     public VariableResolver getVariableResolver()
 90  
     {
 91  0
         return variableResolver;
 92  
     }
 93  
 
 94  
     @Override
 95  
     public Object getValue(ELContext context, Object base, Object property) throws NullPointerException,
 96  
             PropertyNotFoundException, ELException
 97  
     {
 98  0
         if (isDefaultLegacyVariableResolver)
 99  
         {
 100  
             // Skip this one because it causes previous EL Resolvers already processed
 101  
             // before this one to be called again. This behavior is only valid if it 
 102  
             // is installed a custom VariableResolver.
 103  0
             return null;
 104  
         }
 105  0
         if (base != null)
 106  
         {
 107  0
             return null;
 108  
         }
 109  0
         if (property == null)
 110  
         {
 111  0
             throw new PropertyNotFoundException();
 112  
         }
 113  
 
 114  0
         context.setPropertyResolved(true);
 115  
 
 116  0
         if (!(property instanceof String))
 117  
         {
 118  0
             return null;
 119  
         }
 120  
 
 121  0
         final String strProperty = (String) property;
 122  
 
 123  0
         Collection<String> propertyGuard = getPropertyGuard();
 124  
 
 125  0
         Object result = null;
 126  
         try
 127  
         {
 128  
             // only call the resolver if we haven't done it in current stack
 129  0
             if(!propertyGuard.contains(strProperty))
 130  
             {
 131  0
                 propertyGuard.add(strProperty);
 132  0
                 result = variableResolver.resolveVariable(facesContext(context), strProperty);
 133  
             }
 134  
         }
 135  0
         catch (javax.faces.el.PropertyNotFoundException e)
 136  
         {
 137  0
             context.setPropertyResolved(false);
 138  0
             throw new PropertyNotFoundException(e.getMessage(), e);
 139  
         }
 140  0
         catch (EvaluationException e)
 141  
         {
 142  0
             context.setPropertyResolved(false);
 143  0
             throw new ELException(e.getMessage(), e);
 144  
         }
 145  0
         catch (RuntimeException e)
 146  
         {
 147  0
             context.setPropertyResolved(false);
 148  0
             throw e;
 149  
         }
 150  
         finally
 151  
         {
 152  0
             propertyGuard.remove(strProperty);
 153  
 
 154  
             // if the propertyGuard is empty, remove the ThreadLocal
 155  
             // in order to prevent a memory leak
 156  0
             if (propertyGuard.isEmpty())
 157  
             {
 158  0
                 propertyGuardThreadLocal.remove();
 159  
             }
 160  
 
 161  
             // set property resolved to false in any case if result is null
 162  0
             context.setPropertyResolved(result != null);
 163  0
         }
 164  
 
 165  0
         return result;
 166  
     }
 167  
 
 168  
     // get the FacesContext from the ELContext
 169  
     private static FacesContext facesContext(ELContext context)
 170  
     {
 171  0
         return (FacesContext) context.getContext(FacesContext.class);
 172  
     }
 173  
 
 174  
     @Override
 175  
     public Class<?> getCommonPropertyType(ELContext context, Object base)
 176  
     {
 177  0
         if (isDefaultLegacyVariableResolver)
 178  
         {
 179  0
             return null;
 180  
         }
 181  0
         if (base != null)
 182  
         {
 183  0
             return null;
 184  
         }
 185  
 
 186  0
         return String.class;
 187  
     }
 188  
 
 189  
     @Override
 190  
     public void setValue(ELContext context, Object base, Object property, Object value) throws NullPointerException,
 191  
             PropertyNotFoundException, PropertyNotWritableException, ELException
 192  
     {
 193  0
         if (isDefaultLegacyVariableResolver)
 194  
         {
 195  0
             return;
 196  
         }
 197  0
         if ((base == null) && (property == null))
 198  
         {
 199  0
             throw new PropertyNotFoundException();
 200  
         }
 201  0
     }
 202  
 
 203  
     @Override
 204  
     public boolean isReadOnly(ELContext context, Object base, Object property) throws NullPointerException,
 205  
             PropertyNotFoundException, ELException
 206  
     {
 207  0
         if (isDefaultLegacyVariableResolver)
 208  
         {
 209  0
             return false;
 210  
         }
 211  0
         if ((base == null) && (property == null))
 212  
         {
 213  0
             throw new PropertyNotFoundException();
 214  
         }
 215  
 
 216  0
         return false;
 217  
     }
 218  
 
 219  
     @Override
 220  
     public Class<?> getType(ELContext context, Object base, Object property) throws NullPointerException,
 221  
             PropertyNotFoundException, ELException
 222  
     {
 223  0
         if (isDefaultLegacyVariableResolver)
 224  
         {
 225  0
             return null;
 226  
         }
 227  0
         if ((base == null) && (property == null))
 228  
         {
 229  0
             throw new PropertyNotFoundException();
 230  
         }
 231  
 
 232  0
         return null;
 233  
     }
 234  
 
 235  
     @Override
 236  
     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base)
 237  
     {
 238  0
         if (isDefaultLegacyVariableResolver)
 239  
         {
 240  0
             return null;
 241  
         }
 242  0
         return null;
 243  
     }
 244  
 
 245  
 }