View Javadoc

1   /*
2    * $Id: BeanProviderAccessor.java 1187867 2011-10-23 11:35:10Z simonetripodi $
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  /**
21   * 
22   */
23  package org.apache.commons.ognl.test.objects;
24  
25  import org.apache.commons.ognl.ObjectPropertyAccessor;
26  import org.apache.commons.ognl.OgnlContext;
27  import org.apache.commons.ognl.OgnlException;
28  import org.apache.commons.ognl.OgnlRuntime;
29  import org.apache.commons.ognl.PropertyAccessor;
30  import org.apache.commons.ognl.enhance.ExpressionCompiler;
31  import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
32  
33  import java.util.Map;
34  
35  /**
36   * Implementation of provider that works with {@link BeanProvider} instances.
37   */
38  public class BeanProviderAccessor
39      extends ObjectPropertyAccessor
40      implements PropertyAccessor
41  {
42      @Override
43      public Object getProperty( Map<String, Object> context, Object target, Object name )
44          throws OgnlException
45      {
46          BeanProvider provider = (BeanProvider) target;
47          String beanName = (String) name;
48  
49          return provider.getBean( beanName );
50      }
51  
52      /**
53       * Returns true if the name matches a bean provided by the provider. Otherwise invokes the super implementation.
54       **/
55  
56      @Override
57      public boolean hasGetProperty( Map<String, Object> context, Object target, Object oname )
58          throws OgnlException
59      {
60          BeanProvider provider = (BeanProvider) target;
61          String beanName = ( (String) oname ).replaceAll( "\"", "" );
62  
63          return provider.getBean( beanName ) != null;
64      }
65  
66      @Override
67      public String getSourceAccessor( OgnlContext context, Object target, Object name )
68      {
69          BeanProvider provider = (BeanProvider) target;
70          String beanName = ( (String) name ).replaceAll( "\"", "" );
71  
72          if ( provider.getBean( beanName ) != null )
73          {
74              context.setCurrentAccessor( BeanProvider.class );
75              context.setCurrentType( provider.getBean( beanName ).getClass() );
76  
77              ExpressionCompiler.addCastString( context,
78                                                "(("
79                                                    + OgnlRuntime.getCompiler( context ).getInterfaceClass( provider.getBean( beanName ).getClass() ).getName()
80                                                    + ")" );
81  
82              return ".getBean(\"" + beanName + "\"))";
83          }
84  
85          return super.getSourceAccessor( context, target, name );
86      }
87  
88      @Override
89      public String getSourceSetter( OgnlContext context, Object target, Object name )
90      {
91          throw new UnsupportedCompilationException( "Can't set beans on BeanProvider." );
92      }
93  }