1 22 package org.jboss.proxy.ejb; 23 24 import java.io.IOException ; 25 import java.io.ObjectOutput ; 26 import java.io.ObjectInput ; 27 import java.lang.reflect.Method ; 28 import java.util.List ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import org.jboss.ejb.ListCacheKey; 33 import org.jboss.invocation.Invocation; 34 35 44 public class ListEntityInterceptor 45 extends EntityInterceptor 46 { 47 48 private static final long serialVersionUID = -5165912623246270565L; 49 50 protected static final Method GET_READ_AHEAD_VALUES; 51 52 54 57 private List list; 58 59 62 private transient HashMap readAheadValues; 63 64 66 static 67 { 68 try 69 { 70 final Class [] empty = {}; 71 72 GET_READ_AHEAD_VALUES = ReadAheadBuffer.class.getMethod("getReadAheadValues", empty); 73 } 74 catch (Exception e) 75 { 76 e.printStackTrace(); 77 throw new ExceptionInInitializerError (e); 78 } 79 } 80 81 83 86 public ListEntityInterceptor() 87 { 88 } 89 90 105 106 public ListEntityInterceptor(List list) 107 { 108 this.list = list; 109 } 110 111 113 public Map getReadAheadValues() 114 { 115 if (readAheadValues == null) 116 { 117 readAheadValues = new HashMap (); 118 } 119 return readAheadValues; 120 } 121 122 123 132 public Object invoke(Invocation invocation) 133 throws Throwable 134 { 135 Object result; 136 ReadAheadResult raResult; 137 Object [] aheadResult; 138 int from; 139 int to; 140 ReadAheadBuffer buf; 141 142 Method m = invocation.getMethod(); 143 144 if (m.equals(GET_READ_AHEAD_VALUES)) 145 { 146 return getReadAheadValues(); 147 } 148 149 if (readAheadValues != null) 151 { 152 result = readAheadValues.get(m); 153 if (readAheadValues.containsKey(m)) 154 { 155 return readAheadValues.remove(m); 156 } 157 } 158 159 result = super.invoke(invocation); 160 161 163 if (result instanceof ReadAheadResult) 164 { 165 raResult = (ReadAheadResult) result; 166 aheadResult = raResult.getAheadResult(); 167 ListCacheKey key = (ListCacheKey) invocation.getInvocationContext().getCacheId(); 168 from = key.getIndex() + 1; 169 to = Math.min(from + aheadResult.length, list.size()); 170 for (int i = from; i < to; i++) 171 { 172 buf = (ReadAheadBuffer) list.get(i); 173 buf.getReadAheadValues().put(m, aheadResult[i - from]); 174 } 175 return raResult.getMainResult(); 176 } 177 else 178 { 179 return result; 180 } 181 } 182 183 185 187 194 public void writeExternal(final ObjectOutput out) 195 throws IOException 196 { 197 super.writeExternal(out); 198 out.writeObject(list); 199 } 200 201 209 public void readExternal(final ObjectInput in) 210 throws IOException , ClassNotFoundException 211 { 212 super.readExternal(in); 213 list = (List ) in.readObject(); 214 } 215 216 217 219 } 221 222 | Popular Tags |