1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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 }