1 17 18 package org.objectweb.jac.aspects.persistence; 19 20 import org.apache.log4j.Logger; 21 import org.objectweb.jac.core.AspectComponent; 22 import org.objectweb.jac.core.Interaction; 23 import org.objectweb.jac.core.Wrappee; 24 import org.objectweb.jac.core.Wrapping; 25 import org.objectweb.jac.core.rtti.CollectionItem; 26 import org.objectweb.jac.core.rtti.RttiAC; 27 import org.objectweb.jac.util.ExtArrays; 28 import org.objectweb.jac.util.ExtBoolean; 29 30 33 34 public abstract class CollectionWrapper extends AbstractPersistenceWrapper { 35 static Logger logger = Logger.getLogger("persistence"); 36 37 boolean isLoaded = false; 38 CollectionItem collection; 39 Object substance; 40 41 public CollectionWrapper(AspectComponent ac, 42 Object substance, 43 CollectionItem collection, 44 boolean isLoaded) 45 { 46 super(ac); 47 this.collection = collection; 48 this.substance = substance; 49 this.isLoaded = isLoaded; 50 } 51 52 boolean cache=false; 53 54 57 public synchronized void load(Wrappee wrappee) throws Exception { 58 if (!isLoaded) { 59 logger.debug("loading collection "+getOID(wrappee)+" - "+wrappee); 60 doLoad(wrappee); 61 isLoaded = true; 62 } 63 } 64 65 public boolean isLoaded() { 66 return isLoaded; 67 } 68 69 72 public synchronized void unload(Wrappee wrappee) { 73 logger.debug(getOID(wrappee)+".unload..."); 74 isLoaded = false; 75 Wrapping.invokeOrg(wrappee,"clear",ExtArrays.emptyObjectArray); 76 } 77 78 82 protected abstract void doLoad(Wrappee wrappee) throws Exception ; 83 84 87 public abstract Object clear(Interaction interaction) throws Exception ; 88 89 public Object preload(Interaction interaction) throws Exception { 90 logger.debug(getOID(interaction.wrappee)+ 91 ".preload for "+interaction.method); 92 try { 93 load(interaction.wrappee); 94 } catch (Exception e) { 95 logger.warn("Failed to preload collection for "+interaction,e); 96 } 97 return proceed(interaction); 98 } 99 100 public synchronized Object size(Interaction interaction) throws Exception { 101 if (!isLoaded) { 102 long size = getCollectionSize(getOID(interaction.wrappee)); 103 if (size==0) 106 isLoaded = true; 107 return new Integer (new Long (size).intValue()); 108 } else { 109 return proceed(interaction); 110 } 111 } 112 113 protected abstract long getCollectionSize(OID oid) throws Exception ; 114 115 public synchronized Object isEmpty(Interaction interaction) throws Exception { 116 if (!isLoaded) { 117 boolean result = getCollectionSize(getOID(interaction.wrappee))==0; 118 if (result) 121 isLoaded = true; 122 return ExtBoolean.valueOf(result); 123 } else { 124 return proceed(interaction); 125 } 126 } 127 128 long useDate = System.currentTimeMillis(); 130 131 136 public long getUseDate(Wrappee wrappee) { 137 return useDate; 138 } 139 140 143 protected void touch() { 144 useDate = System.currentTimeMillis(); 145 } 146 147 public abstract Object iterator(Interaction interaction); 148 149 public boolean isCache() { 150 return cache; 151 } 152 153 public void setCache(boolean b) { 154 cache = b; 155 } 156 157 protected Object convert(Object value, Object wrappee) throws Exception { 158 if (value==null) { 159 return null; 160 } else { 161 Class collType = (Class )collection.getComponentType().getDelegate(); 162 if (!collType.isAssignableFrom(value.getClass())) { 163 Object converted = RttiAC.convert(value,collType); 164 if (converted == value) 165 logger.warn( 166 "Failed to convert "+value+" into "+collType.getName()+ 167 " for collection "+substance+"["+getOID((Wrappee)substance)+"]."+ 168 collection.getName()); 169 return converted; 170 } else { 171 return value; 172 } 173 } 174 } 175 } 176 | Popular Tags |