1 17 18 27 28 package org.objectweb.jac.aspects.persistence; 29 30 import java.util.Arrays ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import org.aopalliance.intercept.ConstructorInvocation; 35 import org.aopalliance.intercept.MethodInvocation; 36 import org.apache.log4j.Logger; 37 import org.objectweb.jac.core.AspectComponent; 38 import org.objectweb.jac.core.Interaction; 39 import org.objectweb.jac.core.Wrappee; 40 import org.objectweb.jac.core.Wrapping; 41 import org.objectweb.jac.core.rtti.ClassRepository; 42 import org.objectweb.jac.core.rtti.CollectionItem; 43 import org.objectweb.jac.core.rtti.MethodItem; 44 import org.objectweb.jac.util.ExtBoolean; 45 46 49 public class ListWrapper extends CollectionWrapper { 50 static Logger logger = Logger.getLogger("persistence"); 51 52 public ListWrapper(AspectComponent ac, 53 Object substance, 54 CollectionItem collection, 55 boolean isLoaded) 56 { 57 super(ac, substance, collection, isLoaded); 58 } 59 60 protected void doLoad(Wrappee wrappee) throws Exception { 61 OID oid = getOID(wrappee); 62 List list = oid.getStorage().getList(oid); 63 MethodItem add = cr.getClass(wrappee).getMethod("add(java.lang.Object)"); 64 Object [] params = new Object [1]; 65 for (int i=0; i<list.size(); i++) { 66 try { 67 params[0] = convert(normalizeOutput(list.get(i)),wrappee); 69 Wrapping.invokeOrg(wrappee, add, params); 70 } catch (NoSuchOIDError e) { 71 logger.error( 72 "ListWrapper.doLoad(" 73 + oid + "): "+collection.getName()+ 74 ": skipping item "+i+" with unknown OID " + list.get(i)); 75 list.set(i, null); 76 } catch (Exception e) { 77 logger.error( 78 "ListWrapper.doLoad(" 79 + oid + "): "+collection.getName()+ 80 ": skipping item "+i+" because of exception",e); 81 list.set(i, null); 82 } 83 } 84 90 } 91 92 public Object contains(Interaction interaction) throws Exception { 93 if (isLoaded) { 94 return interaction.proceed(); 95 } else { 96 OID oid = getOID(interaction.wrappee); 97 return ExtBoolean.valueOf( 98 oid.getStorage().listContains( 99 oid, 100 normalizeInput(interaction.args[0]))); 101 } 102 } 103 104 public Object add(Interaction interaction) throws Exception { 105 logger.debug("adding " + interaction.args[0] 106 + " to list " + getOID(interaction.wrappee)); 107 Object result = Boolean.TRUE; 108 if (isLoaded) 109 result = interaction.proceed(); 110 111 OID oid = getOID(interaction.wrappee); 112 if (interaction.args.length == 1) { 113 oid.getStorage().addToList( 115 oid, 116 normalizeInput(interaction.args[0])); 117 } else if (interaction.args.length == 2) { 118 oid.getStorage().addToList( 120 oid, 121 ((Integer ) interaction.args[0]).longValue(), 122 normalizeInput(interaction.args[1])); 123 } 124 return result; 125 } 126 127 public Object addAll(Interaction interaction) throws Exception { 128 logger.debug("adding " + interaction.args[0] 129 + " to list " + getOID(interaction.wrappee)); 130 Object result = Boolean.TRUE; 131 if (isLoaded) 132 result = interaction.proceed(); 133 if (interaction.args.length==1) { 134 Iterator i = ((Collection )interaction.args[0]).iterator(); 136 OID cid = getOID(interaction.wrappee); 137 Storage storage = cid.getStorage(); 138 while (i.hasNext()) { 139 storage.addToList( 140 cid, normalizeInput(i.next())); 141 } 142 } else { 143 Iterator i = ((Collection )interaction.args[0]).iterator(); 145 OID cid = getOID(interaction.wrappee); 146 long index = ((Integer ) interaction.args[0]).longValue(); 147 Storage storage = cid.getStorage(); 148 while (i.hasNext()) { 149 storage.addToList( 150 cid, index++, normalizeInput(i.next())); 151 } 152 } 153 return result; 154 } 155 156 public Object get(Interaction interaction) throws Exception { 157 touch(); 158 if (isLoaded) { 159 return interaction.proceed(); 160 } else { 161 OID oid = getOID(interaction.wrappee); 162 return normalizeOutput( 163 oid.getStorage().getListItem( 164 oid, 165 ((Integer ) interaction.args[0]).longValue())); 166 } 167 } 168 169 public Object remove(Interaction interaction) throws Exception { 170 touch(); 171 logger.debug("remove : "+ interaction.wrappee 172 + "," + Arrays.asList(interaction.args)); 173 Object result = null; 174 if (isLoaded) { 175 result = interaction.proceed(); 176 } else { 177 result = Boolean.TRUE; 179 } 180 logger.debug("proceeded"); 181 OID oid = getOID(interaction.wrappee); 182 if (interaction.args[0] instanceof Integer ) { 183 logger.debug("remove position"); 184 oid.getStorage().removeFromList( 185 oid, 186 ((Integer ) interaction.args[0]).longValue()); 187 } else { 188 logger.debug("remove value"); 189 oid.getStorage().removeFromList( 190 oid, 191 normalizeInput(interaction.args[0])); 192 } 193 logger.debug("value removed"); 194 return result; 195 } 196 197 public Object removeRange(Interaction interaction) throws Exception { 198 touch(); 200 if (isLoaded) { 201 interaction.proceed(); 202 } 203 int from = ((Integer )interaction.args[0]).intValue(); 204 int to = ((Integer )interaction.args[1]).intValue(); 205 int size = ((Integer )size(interaction)).intValue(); 206 if (to>size) 207 to = size; 208 OID cid = getOID(interaction.wrappee); 209 Storage storage = cid.getStorage(); 210 for (; from<to; from++) { 211 storage.removeFromList(cid,from); 212 } 213 return null; 214 } 215 216 219 public Object clear(Interaction interaction) throws Exception { 220 touch(); 221 logger.debug("clear"); 222 Object result = interaction.proceed(); 223 OID oid = getOID(interaction.wrappee); 224 oid.getStorage().clearList(oid); 225 return result; 226 } 227 228 public Object set(Interaction interaction) throws Exception { 229 touch(); 230 if (isLoaded()) 231 interaction.proceed(); 232 OID oid = getOID(interaction.wrappee); 233 oid.getStorage().setListItem( 234 oid, 235 ((Integer ) interaction.args[0]).intValue(), 236 interaction.args[1]); 237 return null; 238 } 239 240 public Object indexOf(Interaction interaction) throws Exception { 241 touch(); 242 if (isLoaded) { 243 return interaction.proceed(); 244 } else { 245 OID oid = getOID(interaction.wrappee); 246 return new Integer ( 247 new Long ( 248 oid.getStorage().getIndexInList( 249 oid, 250 normalizeInput(interaction.args[0]))) 251 .intValue()); 252 } 253 } 254 255 public Object lastIndexOf(Interaction interaction) throws Exception { 256 touch(); 257 if (isLoaded) { 258 return interaction.proceed(); 259 } else { 260 OID oid = getOID(interaction.wrappee); 261 return new Integer ( 262 new Long ( 263 oid.getStorage().getLastIndexInList( 264 oid, 265 normalizeInput(interaction.args[0]))) 266 .intValue()); 267 } 268 } 269 270 protected long getCollectionSize(OID oid) throws Exception { 271 return oid.getStorage().getListSize(oid); 272 } 273 274 public Object iterator(Interaction interaction) { 275 touch(); 276 return new ListIterator(interaction.wrappee); 277 } 278 279 public Object invoke(MethodInvocation invocation) throws Throwable { 280 String name = invocation.getMethod().getName(); 281 Interaction interaction = (Interaction) invocation; 282 if (name.equals("iterator")) { 283 load(interaction.wrappee); 284 return interaction.proceed(); 286 } else if (name.equals("isEmpty")) { 287 return isEmpty(interaction); 288 } else if (name.equals("size")) { 289 return size(interaction); 290 } else if (name.equals("get")) { 291 return get(interaction); 292 } else if (name.equals("indexOf")) { 293 return indexOf(interaction); 294 } else if (name.equals("lastIndexOf")) { 295 return lastIndexOf(interaction); 296 } else if (name.equals("clear")) { 297 return clear(interaction); 298 } else if (name.equals("set")) { 299 return set(interaction); 300 } else if (name.equals("add")) { 301 return add(interaction); 302 } else if (name.equals("addAll")) { 303 return addAll(interaction); 304 } else if (name.equals("remove")) { 305 return remove(interaction); 306 } else if (name.equals("contains")) { 307 return contains(interaction); 308 } else if (name.equals("toArray")) { 309 load(interaction.wrappee); 310 return interaction.proceed(); 311 } else if (name.equals("clone")) { 312 load(interaction.wrappee); 313 return interaction.proceed(); 314 } else if (name.equals("removeRange")) { 315 return removeRange(interaction); 316 } else { 317 logger.error("ListWrapper: don't know what to do with method "+name); 318 } 319 return interaction.proceed(); 320 } 321 322 } 323 | Popular Tags |