1 17 18 package org.objectweb.jac.aspects.persistence; 19 20 import java.util.List ; 21 import org.aopalliance.intercept.ConstructorInvocation; 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.apache.log4j.Logger; 24 import org.objectweb.jac.core.AspectComponent; 25 import org.objectweb.jac.core.Interaction; 26 import org.objectweb.jac.core.Wrappee; 27 import org.objectweb.jac.core.Wrapping; 28 import org.objectweb.jac.core.rtti.ClassRepository; 29 import org.objectweb.jac.core.rtti.CollectionItem; 30 import org.objectweb.jac.core.rtti.MethodItem; 31 import org.objectweb.jac.util.ExtBoolean; 32 33 36 public class SetWrapper extends CollectionWrapper { 37 static Logger logger = Logger.getLogger("persistence"); 38 39 public SetWrapper(AspectComponent ac, 40 Object substance, 41 CollectionItem collection, 42 boolean isLoaded) 43 { 44 super(ac, substance, collection, isLoaded); 45 } 46 47 protected void doLoad(Wrappee wrappee) throws Exception { 48 logger.debug( "do load " + wrappee.getClass()); 49 OID oid = getOID(wrappee); 51 List list = oid.getStorage().getSet(oid); 52 MethodItem add = cr.getClass(wrappee).getMethod("add(java.lang.Object)"); 53 Object [] params = new Object [1]; 54 for (int i = 0; i < list.size(); i++) { 55 try { 56 params[0] = convert(normalizeOutput(list.get(i)),wrappee); 58 Wrapping.invokeOrg(wrappee, add, params); 59 } catch (NoSuchOIDError e) { 60 logger.error( 61 "SetWrapper.doLoad(" 62 + oid + "): "+collection.getName() 63 + ": skipping object at pos "+i 64 +" with unknown OID "+list.get(i)); 65 list.set(i, null); 66 } catch (Exception e) { 67 logger.error( 68 "SetWrapper.doLoad(" 69 + oid + "): "+collection.getName() 70 +"skipping object at pos "+i 71 +" because of exception",e); 72 list.set(i, null); 73 } 74 } 75 80 } 81 82 public Object contains(Interaction interaction) throws Exception { 83 touch(); 84 if (isLoaded) { 85 return interaction.proceed(); 86 } else { 87 OID oid = getOID(interaction.wrappee); 88 return ExtBoolean.valueOf( 89 oid.getStorage().setContains( 90 oid, 91 normalizeInput(interaction.args[0]))); 92 } 93 } 94 95 public boolean add(Interaction interaction) throws Exception { 96 touch(); 97 if (interaction.args[0] != null) 98 logger.debug( 99 "adding " 100 + interaction.args[0].getClass().getName() 101 + " to set"); 102 if (isLoaded) 103 interaction.proceed(); 104 OID oid = getOID(interaction.wrappee); 105 return oid.getStorage().addToSet( 106 oid, 107 normalizeInput(interaction.args[0])); 108 } 109 110 public Object remove(Interaction interaction) throws Exception { 111 touch(); 112 boolean result1 = false; 113 if (isLoaded) { 114 result1 = ((Boolean ) interaction.proceed()).booleanValue(); 115 } 116 OID oid = getOID(interaction.wrappee); 117 boolean result2 = 118 oid.getStorage().removeFromSet( 119 oid, 120 normalizeInput(interaction.args[0])); 121 if (isLoaded) { 122 if (result1 != result2) 123 throw new Error ("SetWrapper.remove result1 != result2"); 124 return ExtBoolean.valueOf(result1); 125 } else { 126 return ExtBoolean.valueOf(result2); 127 } 128 } 129 130 133 public Object clear(Interaction interaction) throws Exception { 134 touch(); 135 Object result = interaction.proceed(); 136 OID oid = getOID(interaction.wrappee); 137 oid.getStorage().clearSet(oid); 138 return result; 139 } 140 141 protected long getCollectionSize(OID oid) throws Exception { 142 return oid.getStorage().getSetSize(oid); 143 } 144 145 public Object iterator(Interaction interaction) { 146 touch(); 147 return new SetIterator(interaction.wrappee); 148 } 149 150 public Object invoke(MethodInvocation invocation) throws Throwable { 151 String name = invocation.getMethod().getName(); 152 Interaction interaction = (Interaction) invocation; 153 if (name.equals("iterator")) { 154 load(interaction.wrappee); 155 return interaction.proceed(); 156 } else if (name.equals("isEmpty")) { 157 return isEmpty(interaction); 158 } else if (name.equals("size")) { 159 return size(interaction); 160 } else if (name.equals("clear")) { 161 return clear(interaction); 162 } else if (name.equals("add")) { 163 return new Boolean (add(interaction)); 164 } else if (name.equals("remove")) { 165 return remove(interaction); 166 } else if (name.equals("contains")) { 167 return contains(interaction); 168 } else if (name.equals("toArray")) { 169 load(interaction.wrappee); 170 return interaction.proceed(); 171 } else if (name.equals("clone")) { 172 load(interaction.wrappee); 173 return interaction.proceed(); 174 } else { 175 logger.error("SetWrapper: don't know what to do with method "+name); 176 } 177 return interaction.proceed(); 178 } 179 180 } 181 | Popular Tags |