1 18 package org.objectweb.perseus.persistence.concurrency; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 23 import org.objectweb.fractal.api.NoSuchInterfaceException; 24 import org.objectweb.fractal.api.control.IllegalBindingException; 25 import org.objectweb.perseus.cache.api.CacheEntry; 26 import org.objectweb.perseus.concurrency.api.ConcurrencyException; 27 import org.objectweb.perseus.concurrency.pessimistic.Lock; 28 import org.objectweb.perseus.concurrency.pessimistic.PessimisticConcurrencyManager; 29 import org.objectweb.perseus.persistence.api.NoDSIPersistenceException; 30 import org.objectweb.perseus.persistence.api.PersistenceException; 31 import org.objectweb.perseus.persistence.api.State; 32 import org.objectweb.perseus.persistence.api.StateManager; 33 import org.objectweb.perseus.persistence.api.StorageManager; 34 import org.objectweb.perseus.persistence.api.VirtualState; 35 import org.objectweb.perseus.persistence.api.WorkingSet; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 38 42 public class PPessimisticConcurrencyManager 43 extends PessimisticConcurrencyManager { 44 45 public PPessimisticConcurrencyManager() throws ConcurrencyException { 46 } 47 48 public PPessimisticConcurrencyManager(short policy) throws ConcurrencyException { 49 super(policy); 50 } 51 52 public final static String STORAGE_MANAGER_BINDING = "storage-manager"; 53 public final static String STATE_MANAGER_BINDING = "state-manager"; 54 55 protected StateManager stateManager; 56 57 protected StorageManager storageManager; 58 59 62 public String [] listFc() { 63 String [] super_res = super.listFc(); 64 String res[] = new String [super_res.length + 2]; 65 for (int cpt = 0; cpt < super_res.length; cpt++) { 66 res[cpt] = super_res[cpt]; 67 } 68 res[super_res.length + 0] = STORAGE_MANAGER_BINDING; 69 res[super_res.length + 1] = STATE_MANAGER_BINDING; 70 return res; 71 } 72 73 public Object lookupFc(String s) throws NoSuchInterfaceException { 74 if (STORAGE_MANAGER_BINDING.equals(s)) { 75 return storageManager; 76 } else if (STATE_MANAGER_BINDING.equals(s)) { 77 return stateManager; 78 } else { 79 return super.lookupFc(s); 80 } 81 } 82 83 public void bindFc(String s, Object o) throws NoSuchInterfaceException, IllegalBindingException { 84 if (STORAGE_MANAGER_BINDING.equals(s)) { 85 storageManager = (StorageManager) o; 86 } else if (STATE_MANAGER_BINDING.equals(s)) { 87 stateManager = (StateManager) o; 88 } else super.bindFc(s, o); 89 } 90 91 public void unbindFc(String s) throws NoSuchInterfaceException { 92 if (STORAGE_MANAGER_BINDING.equals(s)) { 93 storageManager = null; 94 } else if (STATE_MANAGER_BINDING.equals(s)) { 95 stateManager = null; 96 } else { 97 super.unbindFc(s); 98 } 99 } 100 101 105 public void finalize(Object arg0) { 106 WorkingSet ws = (WorkingSet) arg0; 107 boolean retainValues = ws.getWSRetainValues(); 108 for (Iterator it = ws.entries().iterator(); it.hasNext();) { 109 State state = (State) it.next(); 110 if (state == VirtualState.instance) { 111 continue; 112 } 113 CacheEntry ce = state.getCacheEntry(); 114 if (!stateManager.isUnexported(state)) { 115 if (retainValues) { 117 if (stateManager.isDirty(state)) { 118 if (stateManager.isToMerge(state)) { 119 synchronized(ce) { 123 State s = stateManager.getReferenceState(ce); 124 if (s != null) { 125 s = stateManager.merge(s, state); 126 stateManager.setReferenceState(ce, s); 127 } 128 } 129 } else { 130 stateManager.makeClean(state); 133 stateManager.setReferenceState(ce, state); 134 } 135 } } else if (stateManager.getReferenceState(ce) == state) { 137 stateManager.setReferenceState(ce, null); 139 } 140 } 141 } 142 super.finalize(arg0); 144 } 145 146 public void abort(Object ctx) { 147 WorkingSet ws = (WorkingSet) ctx; 148 for (Iterator it = ws.entries().iterator(); it.hasNext();) { 149 State state = (State) it.next(); 150 if (state == VirtualState.instance) { 151 continue; 152 } 153 CacheEntry ce = state.getCacheEntry(); 154 if (stateManager.isExported(state)) { 155 stateManager.makeClean(state); 156 } else if (stateManager.isDirty(state)) { 157 if (stateManager.isUnexported(state)) { 158 stateManager.makeClean(state); 159 stateManager.makeBound(ce, ce.getCeIdentifier()); 160 } 161 if (state == stateManager.getReferenceState(ce)) { 162 if (ws.getWSRestoreValues()) { 164 try { 166 load(ws, ce, null); 167 } catch (Exception e) { 168 logger.log(BasicLevel.WARN, "Error during reloading of " + ce.getCeIdentifier() 169 + ":" , e); 170 } 171 } else { 172 stateManager.setReferenceState(ce, null); 174 stateManager.destroyState(state); 175 } 176 } else { 177 stateManager.destroyState(state); 178 } 179 } else if (state != stateManager.getReferenceState(ce)) { 180 stateManager.destroyState(state); 181 } } 183 super.abort(ctx); 184 185 } 186 187 public void closeLock(Lock lock, Object ctx) { 188 WorkingSet ws = (WorkingSet) ctx; 189 State s = ws.lookup(lock.oid); 190 if (s != null && s != VirtualState.instance) { 191 stateManager.stateNoMoreUsed(s); 192 } 193 } 194 195 protected Object getState(Object ctx, 196 Object resource, 197 Lock lock) throws ConcurrencyException { 198 CacheEntry ce = (CacheEntry) resource; 199 WorkingSet ws = (WorkingSet) ctx; 200 State state = stateManager.getReferenceState(ce); 201 State wsstate = ws.lookup(ce.getCeIdentifier()); 202 if (thinLockAllowed && lock.hints != null) { 203 if (wsstate == null 205 || wsstate == VirtualState.instance 206 || wsstate == state) { 207 synchronized (ce) { state = stateManager.getReferenceState(ce); 210 if (state == null) { 211 state = load(ws, ce, lock); 214 stateManager.setReferenceState(state.getCacheEntry(), state); 215 } 216 } 217 state = stateManager.createState(state); 219 stateManager.makeClean(state); 220 } else { 221 state = wsstate; 222 } 223 stateManager.makeToMerge(state, lock.hints); 224 } else if (state == null) { 225 if (wsstate == null || wsstate == VirtualState.instance) { 227 synchronized (ce) { state = stateManager.getReferenceState(ce); 230 if (state == null) { 231 state = load(ws, ce, lock); 234 stateManager.setReferenceState(state.getCacheEntry(), state); 235 } 236 } 237 } else { 238 state = wsstate; 239 } 240 } 241 return state; 242 } 243 244 private State load(WorkingSet ws, CacheEntry ce, Lock lock) 245 throws ConcurrencyException { 246 State state = stateManager.createState(ce); 247 try { 248 storageManager.read(ws, ce.getCeIdentifier(), state); 249 stateManager.makeClean(state); 250 return state; 251 } catch (PersistenceException e) { 252 stateManager.destroyState(state); 253 synchronized (locks) { 255 if (lock != null && lock.close(ws)) { 256 locks.remove(lock); 257 } 258 } 259 if (e instanceof NoDSIPersistenceException) { 260 throw new NoDSIConcurrencyException(e); 261 } else { 262 throw new ConcurrencyException(e); 263 } 264 } 265 } 266 protected Object getResourceId(Object object) { 267 return ((CacheEntry) object).getCeIdentifier(); 268 } 269 270 271 } 272 | Popular Tags |