1 17 18 package org.objectweb.jac.aspects.persistence; 19 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import org.aopalliance.intercept.ConstructorInvocation; 23 import org.aopalliance.intercept.MethodInvocation; 24 import org.apache.log4j.Logger; 25 import org.objectweb.jac.core.AspectComponent; 26 import org.objectweb.jac.core.Interaction; 27 import org.objectweb.jac.core.Wrappee; 28 import org.objectweb.jac.core.Wrapping; 29 import org.objectweb.jac.core.rtti.ClassRepository; 30 import org.objectweb.jac.core.rtti.CollectionItem; 31 import org.objectweb.jac.core.rtti.MethodItem; 32 import org.objectweb.jac.util.ExtBoolean; 33 import org.objectweb.jac.util.Log; 34 35 38 public class MapWrapper extends CollectionWrapper { 39 static Logger logger = Logger.getLogger("persistence"); 40 41 public MapWrapper(AspectComponent ac, 42 Object substance, 43 CollectionItem collection, 44 boolean isLoaded) 45 { 46 super(ac, substance, collection, isLoaded); 47 } 48 49 protected void doLoad(Wrappee wrappee) throws Exception { 50 OID oid = getOID(wrappee); 51 Map map = oid.getStorage().getMap(oid); 52 Iterator i = map.entrySet().iterator(); 55 Object [] params = new Object [2]; 56 MethodItem put = cr.getClass(wrappee).getMethod("put"); 57 while (i.hasNext()) { 58 Map.Entry entry = (Map.Entry ) i.next(); 59 try { 60 65 params[0] = normalizeOutput(entry.getKey()); 66 params[1] = normalizeOutput(entry.getValue()); 67 Wrapping.invokeOrg(wrappee, put, params); 68 } catch (NoSuchOIDError e) { 69 logger.error( 70 "MapWrapper.doLoad(" 71 + oid + "): skipping entry with unknown OID " + entry); 72 } 73 } 74 79 } 80 81 public Object containsKey(Interaction interaction) throws Exception { 82 touch(); 83 if (isLoaded) { 84 return interaction.proceed(); 85 } else { 86 OID oid = getOID(interaction.wrappee); 87 return ExtBoolean.valueOf( 88 oid.getStorage().mapContainsKey( 89 oid, 90 normalizeInput(interaction.args[0]))); 91 } 92 } 93 94 public Object containsValue(Interaction interaction) throws Exception { 95 touch(); 96 if (isLoaded) { 97 return interaction.proceed(); 98 } else { 99 OID oid = getOID(interaction.wrappee); 100 return ExtBoolean.valueOf( 101 oid.getStorage().mapContainsValue( 102 oid, 103 normalizeInput(interaction.args[0]))); 104 } 105 } 106 107 public Object put(Interaction interaction) throws Exception { 108 touch(); 109 Object ret = null; 110 if (isLoaded) 111 interaction.proceed(); 112 OID oid = getOID(interaction.wrappee); 113 ret = 114 normalizeOutput( 115 oid.getStorage().putInMap( 116 oid, 117 normalizeInput(interaction.args[0]), 118 normalizeInput(interaction.args[1]))); 119 return ret; 120 } 121 122 public Object get(Interaction interaction) throws Exception { 123 touch(); 124 if (isLoaded) { 125 return interaction.proceed(); 126 } else { 127 OID oid = getOID(interaction.wrappee); 128 return normalizeOutput( 129 oid.getStorage().getFromMap( 130 oid, 131 normalizeInput(interaction.args[0]))); 132 } 133 } 134 135 public Object remove(Interaction interaction) throws Exception { 136 touch(); 137 Object result1 = null; 138 boolean proceeded = false; 139 if (isLoaded) { 140 result1 = interaction.proceed(); 141 proceeded = true; 142 } 143 OID oid = getOID(interaction.wrappee); 144 Object result2 = 145 oid.getStorage().removeFromMap( 146 oid, 147 normalizeInput(interaction.args[0])); 148 if (proceeded) 149 return result1; 150 else 151 return normalizeOutput(result2); 152 } 153 154 157 public Object clear(Interaction interaction) throws Exception { 158 touch(); 159 Object result = interaction.proceed(); 160 OID oid = getOID(interaction.wrappee); 161 oid.getStorage().clearMap(oid); 162 return result; 163 } 164 165 protected long getCollectionSize(OID oid) throws Exception { 166 return oid.getStorage().getMapSize(oid); 167 } 168 169 public Object iterator(Interaction interaction) { 170 touch(); 171 return new MapIterator(interaction.wrappee); 172 } 173 174 public Object invoke(MethodInvocation invocation) throws Throwable { 175 String name = invocation.getMethod().getName(); 176 Interaction interaction = (Interaction) invocation; 177 if (name.equals("keySet")) { 178 load(interaction.wrappee); 179 return interaction.proceed(); 180 } else if (name.equals("entrySet")) { 181 load(interaction.wrappee); 182 return interaction.proceed(); 183 } else if (name.equals("values")) { 184 load(interaction.wrappee); 185 return interaction.proceed(); 186 } else if (name.equals("isEmpty")) { 187 return isEmpty(interaction); 188 } else if (name.equals("size")) { 189 return size(interaction); 190 } else if (name.equals("containsKey")) { 191 return containsKey(interaction); 192 } else if (name.equals("containsValue")) { 193 return containsValue(interaction); 194 } else if (name.equals("clear")) { 195 return clear(interaction); 196 } else if (name.equals("put")) { 197 return put(interaction); 198 } else if (name.equals("remove")) { 199 return remove(interaction); 200 } else if (name.equals("get")) { 201 return get(interaction); 202 } else if (name.equals("clone")) { 203 load(interaction.wrappee); 204 return interaction.proceed(); 205 } else { 206 logger.error("MapWrapper: don't know what to do with method "+name); 207 } 208 return interaction.proceed(); 209 } 210 211 } 212 | Popular Tags |