View Javadoc

1   /*
2    * PropertyGetOperation.java
3    * Created on 21 Май 2006 г., 22:24
4    */
5   
6   package org.weda.property.impl;
7   
8   import java.beans.IntrospectionException;
9   import org.weda.property.PropertyGetOperationListener;
10  import org.weda.property.PropertyListenerResult;
11  import org.weda.property.PropertyOperationException;
12  
13  /**
14   *
15   * @author Mikhail Titov
16   */
17  public class PropertyGetOperation 
18          extends AbstractPropertyOperation<PropertyGetOperationListener> 
19  {
20      private final static Object[] EMPTY_ARRAY = new Object[]{};
21      
22      public PropertyGetOperation(Class propOwnerClass, String propName) 
23          throws IntrospectionException
24      {
25          super(propOwnerClass, propName, true);
26      } 
27  
28      public Object invoke(Object obj, Object propValue) 
29          throws PropertyOperationException
30      {
31          try{
32              PropertyListenerResult res = null;
33              Object resValue = null;
34              if (listeners!=null)
35                  for (PropertyGetOperationListener listener: listeners)
36                      res = listener.beforeGet(obj, propertyName);
37              if (res==null)
38                  resValue = method.invoke(obj, EMPTY_ARRAY);
39              else
40                  resValue = res.getValue();
41              res = null;
42              if (listeners!=null)
43                  for (PropertyGetOperationListener listener: listeners)
44                      res = listener.afterGet(obj, propertyName, resValue);
45              if (res!=null)
46                  resValue = res.getValue();
47              return resValue;
48          }catch(Exception e){
49              throw new PropertyOperationException(
50                  String.format(
51                      "Can't execute GET operation on property (%s) " +
52                      "for instance of class (%s)"
53                      , propertyName, obj.getClass().getName())
54                  , e);
55          }
56      }    
57  }