1 18 package org.objectweb.speedo.genclass; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Collection ; 25 26 27 import org.objectweb.jorm.api.PExceptionIO; 28 import org.objectweb.jorm.api.PGenClassAccessor; 29 import org.objectweb.jorm.api.PIndexedElem; 30 import org.objectweb.jorm.util.api.Loggable; 31 import org.objectweb.perseus.persistence.api.State; 32 import org.objectweb.speedo.api.Debug; 33 import org.objectweb.speedo.genclass.api.SpeedoGenClassCoherence; 34 import org.objectweb.speedo.genclass.api.SpeedoGenClassProxy; 35 import org.objectweb.speedo.mim.api.SpeedoAccessor; 36 import org.objectweb.speedo.mim.lib.BasicSpeedoAccessor; 37 import org.objectweb.speedo.pm.api.ProxyManager; 38 import org.objectweb.util.monolog.api.BasicLevel; 39 import org.objectweb.util.monolog.api.Logger; 40 41 47 public abstract class GenClassAccessor 48 extends BasicSpeedoAccessor 49 implements PGenClassAccessor, SpeedoGenClassCoherence { 50 51 54 public SpeedoGenClassProxy jdoProxy; 55 56 59 public boolean supportDelta = true; 60 61 65 protected GenClassElement tmpelem = null; 66 67 protected Logger logger; 68 69 73 public List elements = null; 74 75 public List deltaForMerge = null; 76 77 public GenClassAccessor(SpeedoGenClassProxy _jdoProxy) { 78 elements = new ArrayList (); 79 this.jdoProxy = _jdoProxy; 80 this.proxy = jdoProxy; 81 this.tmpelem = (GenClassElement) createPIndexedElem(); 82 } 83 84 public abstract void loadFieldsFromAccessor(SpeedoAccessor sa); 85 86 public abstract PIndexedElem createPIndexedElem(GenClassAccessor gca); 87 88 public abstract void setElements(Object o); 89 90 public Logger getLogger() { 91 if (logger == null) { 92 logger = ((Loggable) jdoProxy).getLogger(); 93 } 94 return logger; 95 } 96 97 102 public void setFlushed(boolean val) { 103 super.setFlushed(val); 104 if (val == true) { 105 supportDelta = true; 106 Iterator it = elements.iterator(); 107 while (it.hasNext()) { 108 GenClassElement gce = (GenClassElement) it.next(); 109 if (gce.getElemStatus() == PIndexedElem.ELEM_DELETED) { 110 it.remove(); 112 } 113 } 114 } 115 } 116 117 121 public void workingSetClosed() { 122 if (Debug.ON) { 123 ((Loggable) jdoProxy).getLogger().log( 124 BasicLevel.DEBUG, "workingSetClosed()"); 125 } 126 supportDelta = true; 127 for(Iterator it = elements.iterator(); it.hasNext();) { 128 GenClassElement gce = (GenClassElement) it.next(); 129 if (gce.getElemStatus() == PIndexedElem.ELEM_DELETED) { 130 it.remove(); 132 } 133 } 134 } 135 136 public void unSwizzle() { 137 for(Iterator it = elements.iterator(); it.hasNext();) { 138 GenClassElement gce = (GenClassElement) it.next(); 139 gce.unSwizzle(); 140 } 141 } 142 143 public State merge(State oldState) { 144 if (deltaForMerge == null || !isToMerge) { 145 return super.merge(oldState); 146 } 147 GenClassAccessor gca = (GenClassAccessor) oldState; 148 for(int i=0; i<deltaForMerge.size(); i++) { 149 GenClassElement gce = (GenClassElement) deltaForMerge.get(i); 150 switch(gce.getStatusForMerge()) { 151 case PIndexedElem.ELEM_CREATED: 152 gca.elements.add(gce); 153 break; 154 case PIndexedElem.ELEM_DELETED: 155 int idx = gca.elements.indexOf(gce); 156 if (idx == -1) { 157 logger.log(BasicLevel.WARN, 158 "Deleted GenClass element '" + gce + "' not found during merging among " 159 + gca.elements); 160 } else { 161 gca.elements.remove(idx); 162 } 163 break; 164 case PIndexedElem.ELEM_MODIFIED: 165 idx = gca.elements.indexOf(gce); 166 if (idx == -1) { 167 logger.log(BasicLevel.WARN, 168 "Modified GenClass element not found during merging: " 169 + gce); 170 } else { 171 gca.elements.set(idx, gce); 172 } 173 break; 174 } 175 gce.cleanStatusForMerge(); 176 } 177 deltaForMerge = null; 178 return super.merge(oldState); 179 } 180 181 public void makeToMerge(Object thinLock) { 182 if (!isToMerge) { 183 for(int i=0; i<elements.size(); i++) { 184 elements.set(i, ((GenClassElement) elements.get(i)).cloneGCE()); 185 } 186 } 187 super.makeToMerge(thinLock); 188 } 189 190 public void loadFields(ProxyManager pm, long[] fields) { 191 } 192 193 public abstract void deletePersistent(ProxyManager pm); 194 195 public abstract void makePersistent(ProxyManager pm); 196 197 public abstract void detachCopy(ProxyManager pm, Map map, SpeedoAccessor fieldsClone, Collection fgHints); 198 199 public abstract void attachCopy(ProxyManager pm, Map map, SpeedoAccessor fieldsClone, boolean makeTransactional); 200 201 public abstract void refresh(ProxyManager pm, Map map, Collection fgHints); 202 203 public abstract void retrieve(ProxyManager pm, Map map, Collection fgHints); 204 205 208 public abstract boolean speedoAdd(Object elemToAdd, Object hints); 209 210 public abstract boolean speedoRemove(Object elemToRemove, Object hints); 211 212 215 public PIndexedElem createPIndexedElem() { 216 return createPIndexedElem(this); 217 } 218 219 public Object getMemoryInstance() { 220 return jdoProxy; 221 } 222 223 public void paAdd(PIndexedElem elem, Object conn) throws PExceptionIO { 224 ((GenClassElement) elem).setStatus(PIndexedElem.ELEM_UNMODIFIED); 226 elements.add(elem); 227 } 228 229 public int paGetNbElem() { 230 return elements.size(); 231 } 232 233 public Iterator paIterator() { 234 return elements.iterator(); 235 } 236 237 public boolean paDeltaSupported() { 238 return supportDelta; 239 } 240 241 public void paSetNbElem(int nbelem) { 242 elements = (nbelem < 0 ? new ArrayList () : new ArrayList (nbelem)); 243 } 244 245 } 246 | Popular Tags |