KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > bean > NodeInvocationHandler


1 package org.sapia.regis.bean;
2
3 import java.lang.reflect.InvocationHandler JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.sapia.regis.Node;
9 import org.sapia.regis.RegisSession;
10 import org.sapia.regis.Registry;
11 import org.sapia.regis.SessionUtil;
12 import org.sapia.regis.type.BuiltinType;
13 import org.sapia.regis.type.BuiltinTypes;
14
15 public class NodeInvocationHandler implements InvocationHandler JavaDoc{
16   
17   static final String JavaDoc REG_PROVIDER_METHOD = "getRegistry";
18   
19   private Registry _reg;
20   private Node _delegate;
21   private Map JavaDoc _methodsToProps = new HashMap JavaDoc();
22   
23   public NodeInvocationHandler(Registry reg, Node delegate, Class JavaDoc interf){
24     _delegate = delegate;
25     _reg = reg;
26     Method JavaDoc[] methods = interf.getMethods();
27     for(int i = 0; i < methods.length; i++){
28       Method JavaDoc m = methods[i];
29       if(m.getDeclaringClass().equals(Object JavaDoc.class)){
30         continue;
31       }
32       else{
33         if(m.getName().startsWith("get") &&
34             m.getName().length()>3 &&
35             !m.getReturnType().equals(void.class)){
36           String JavaDoc propName = propName(m.getName().substring(3));
37           BuiltinType type = BuiltinTypes.getTypeFor(m.getReturnType());
38           if(type == null){
39             throw new InvalidReturnTypeException("Property " + propName +
40                 " does not correspond to a valid configuration type: " + m.getReturnType().getName());
41           }
42           PropertyConf conf = new PropertyConf();
43           conf.name = propName;
44           conf.type = type;
45           _methodsToProps.put(m, conf);
46         }
47       }
48     }
49   }
50   
51   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
52     
53     PropertyConf conf = (PropertyConf)_methodsToProps.get(method);
54     if(conf != null){
55       if(SessionUtil.isJoined()){
56         return conf.getProperty(_delegate);
57       }
58       else{
59         RegisSession session = _reg.open();
60         try{
61           return conf.getProperty(_delegate);
62         }finally{
63           session.close();
64         }
65       }
66     }
67     else if(method.getName().equals(REG_PROVIDER_METHOD)){
68       return _reg;
69     }
70     else{
71       throw new InvalidPropertyException("No method corresponds to property " + conf.name + " on Node: " + _delegate.getAbsolutePath());
72     }
73   }
74   
75   public String JavaDoc propName(String JavaDoc name){
76     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(name);
77     buf.setCharAt(0, Character.toLowerCase(name.charAt(0)));
78     return buf.toString();
79   }
80   
81   static class PropertyConf{
82     private String JavaDoc name;
83     private BuiltinType type;
84     
85     Object JavaDoc getProperty(Node delegate){
86       return type.getProperty(name, delegate);
87     }
88   }
89   
90 }
91
Popular Tags