1 /*
2 * Invokation.java
3 *
4 * Created on Sekmadienis, 2003, Kovo 30, 21.16
5 */
6
7 package net.sf.voruta;
8 import java.lang.reflect.*;
9 import java.sql.*;
10 import java.util.*;
11 import net.sf.cglib.*;
12 /***
13 *
14 * @author baliuka
15 */
16 final class InvocationImpl implements MethodInterceptor{
17
18 private String connection;
19 private Map descriptors;
20 private Object delegate = new Object();
21 private CacheRegions regions;
22 private List interceptors;
23
24 final static CacheKey KEYS = (CacheKey)KeyFactory.create(CacheKey.class,CacheKey.class.getClassLoader());
25
26
27 class InvocationObject implements Invocation {
28
29 boolean inCache = false;
30 boolean flushed = false;
31 Iterator interceptors ;
32 ProcedureDescriptor descriptor;
33
34 InvocationObject( ProcedureDescriptor descriptor){
35 this.descriptor = descriptor;
36 List l = new ArrayList(InvocationImpl.this.interceptors.size());
37 l.addAll(InvocationImpl.this.interceptors);
38 this.interceptors = l.iterator();
39 }
40
41 public String currentConnection() {
42 return InvocationImpl.this.currentConnection();
43 }
44
45 public Object invoke(Method method, Object[] args) throws Throwable {
46 if(!interceptors.hasNext()){
47
48 return InvocationImpl.this.invoke( this, method, args );
49
50 }else{
51
52 ProcedureInterceptor intc = (ProcedureInterceptor)interceptors.next();
53
54 return intc.invoke( method, args, this, descriptor.getTags());
55
56 }
57 }
58
59 public boolean wasResultInCache() {
60 return inCache;
61 }
62
63 public boolean flushedCache() {
64 return flushed;
65 }
66
67 public ProcedureDescriptor getProcedureDescriptor() {
68 return descriptor;
69 }
70
71 }
72
73
74
75
76 InvocationImpl(String connection, Map descriptors,
77 List interceptors){
78 this.connection = connection;
79 this.descriptors = descriptors;
80 this.interceptors = interceptors;
81 this.regions = DbUtils.getRegions(connection);
82 }
83
84
85 public Object invoke(InvocationObject invo, Method method, Object[] args) throws Throwable {
86
87
88
89 if(method.getDeclaringClass() == Object.class ){
90
91 return method.invoke(delegate,args);
92 }
93
94 ProcedureDescriptor descriptor = (ProcedureDescriptor)descriptors.get(method);
95
96
97 if(!descriptor.isUpdate()){
98 Object key = null;
99 Object value = null;
100
101 if(descriptor.isCached()){
102 descriptor.hit();
103 key = KEYS.newInstance(method,args);
104 value = regions.get(descriptor.getRegion(), key);
105 if( value != null ){
106 if( invo != null ){
107 invo.inCache = true;
108 }
109 return value;
110 }else{
111 descriptor.miss();
112 }
113 }
114
115 value = descriptor.getHelper().
116 executeQuery( connection, args,
117 descriptor.getHandler(), args);
118
119
120 if(descriptor.isCached() && value != null){
121 regions.put( descriptor.getRegion(), key, value );
122 }
123
124 return value;
125
126 }else{
127 int updateCount = descriptor.getHelper().
128 executeUpdate(connection, args);
129
130 if( updateCount > 0 && descriptor.isFlushOnExecute() ){
131 if( invo != null ){
132 invo.flushed = true;
133 }
134 regions.clearRegions(descriptor.getRegions());
135 }
136
137 return new Integer(updateCount);
138
139 }
140
141 }
142
143
144 public Object intercept(Object obj, Method method, Object[] obj2,
145 MethodProxy methodProxy) throws java.lang.Throwable {
146 try{
147
148 ProcedureDescriptor descriptor =
149 (ProcedureDescriptor)descriptors.get(method);
150
151 if(descriptor == null){
152
153 throw new DbException("descriptor not found: " + method);
154
155 }
156
157 if(interceptors.size() > 0){
158 return new InvocationObject(descriptor).invoke(method, obj2);
159 }else{
160 //save momory
161 return invoke(null, method, obj2);
162 }
163
164 }catch(RuntimeException re){
165
166 throw re;
167
168 }catch(Exception e){
169
170 Class exeptions[] = method.getExceptionTypes();
171 for( int i = 0; i< exeptions.length; i++){
172 if( exeptions[i].isAssignableFrom(e.getClass()) ){
173 throw e;
174 }
175 }
176
177 throw new DbException( e );
178
179 }
180
181
182 }
183
184 public String currentConnection() {
185 return connection;
186 }
187
188 interface CacheKey{
189
190 Object newInstance(Method method,Object args[]);
191
192 }
193
194 }
This page was automatically generated by Maven