Coverage Report - org.apache.tapestry.bean.BeanProviderPropertyAccessor
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanProviderPropertyAccessor
0%
0/26
0%
0/10
2.8
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.bean;
 16  
 
 17  
 import java.util.Map;
 18  
 
 19  
 import ognl.ObjectPropertyAccessor;
 20  
 import ognl.OgnlContext;
 21  
 import ognl.OgnlException;
 22  
 import ognl.OgnlRuntime;
 23  
 import ognl.PropertyAccessor;
 24  
 import ognl.enhance.ExpressionCompiler;
 25  
 import ognl.enhance.UnsupportedCompilationException;
 26  
 
 27  
 import org.apache.tapestry.IBeanProvider;
 28  
 
 29  
 /**
 30  
  *  Adapts a {@link org.apache.tapestry.IBeanProvider} to
 31  
  *  <a href="http://www.ognl.org">OGNL</a> by exposing the named
 32  
  *  beans provided by the provider as read-only properties of
 33  
  *  the provider.
 34  
  * 
 35  
  *  <p>This is registered by {@link org.apache.tapestry.AbstractComponent}.
 36  
  *
 37  
  *  @author Howard Lewis Ship
 38  
  *  @since 2.2
 39  
  *
 40  
  **/
 41  
 
 42  0
 public class BeanProviderPropertyAccessor extends ObjectPropertyAccessor implements PropertyAccessor
 43  
 {
 44  
     /**
 45  
      *  Checks to see if the name matches the name of a bean inside
 46  
      *  the provider and returns that bean if so.
 47  
      *  Otherwise, invokes the super implementation.
 48  
      * 
 49  
      **/
 50  
     
 51  
     public Object getProperty(Map context, Object target, Object name) throws OgnlException
 52  
     {
 53  0
         IBeanProvider provider = (IBeanProvider)target;
 54  0
         String beanName = (String)name;
 55  
         
 56  0
         if (provider.canProvideBean(beanName))
 57  0
             return provider.getBean(beanName);
 58  
         
 59  0
         return super.getProperty(context, target, name);
 60  
     }
 61  
 
 62  
     /**
 63  
      *  Returns true if the name matches a bean provided by the provider.
 64  
      *  Otherwise invokes the super implementation.
 65  
      * 
 66  
      **/
 67  
     
 68  
     public boolean hasGetProperty(Map context, Object target, Object oname) throws OgnlException
 69  
     {
 70  0
         IBeanProvider provider = (IBeanProvider)target;
 71  0
         String beanName = ((String)oname).replaceAll("\"", "");
 72  
 
 73  0
         if (provider.canProvideBean(beanName))
 74  0
             return true;
 75  
             
 76  0
         return super.hasGetProperty(context, target, oname);
 77  
     }
 78  
     
 79  
     public Class getPropertyClass(OgnlContext context, Object target, Object name)
 80  
     {
 81  0
         IBeanProvider provider = (IBeanProvider)target;
 82  0
         String beanName = ((String)name).replaceAll("\"", "");
 83  
         
 84  0
         if (provider.canProvideBean(beanName))
 85  0
             return provider.getBean(beanName).getClass();
 86  
         
 87  0
         return super.getPropertyClass(context, target, name);
 88  
     }
 89  
     
 90  
     public String getSourceAccessor(OgnlContext context, Object target, Object name)
 91  
     {
 92  0
         IBeanProvider provider = (IBeanProvider)target;
 93  0
         String beanName = ((String)name).replaceAll("\"", "");
 94  
         
 95  0
         if (provider.canProvideBean(beanName)) {
 96  
             
 97  0
             Class type = OgnlRuntime.getCompiler().getInterfaceClass(provider.getBean(beanName).getClass());
 98  
             
 99  0
             ExpressionCompiler.addCastString(context, "((" + type.getName() + ")");
 100  
             
 101  0
             context.setCurrentAccessor(IBeanProvider.class);
 102  0
             context.setCurrentType(type);
 103  
             
 104  0
             return ".getBean(" + name + "))";
 105  
         }
 106  
         
 107  0
         return super.getSourceAccessor(context, target, name);
 108  
     }
 109  
     
 110  
     public String getSourceSetter(OgnlContext context, Object target, Object name)
 111  
     {
 112  0
         throw new UnsupportedCompilationException("Can't set beans on IBeanProvider.");
 113  
     }
 114  
 }