1 18 package org.objectweb.perseus.persistence.concurrency; 19 20 import java.util.Iterator ; 21 22 import org.objectweb.perseus.concurrency.optimistic.OptimisticConcurrencyManager; 23 import org.objectweb.perseus.concurrency.lib.TimeStamp; 24 import org.objectweb.perseus.concurrency.api.ConcurrencyException; 25 import org.objectweb.perseus.persistence.api.*; 26 import org.objectweb.perseus.cache.api.CacheEntry; 27 import org.objectweb.util.monolog.api.BasicLevel; 28 29 33 public class POptimisticConcurrencyManager extends OptimisticConcurrencyManager { 34 35 36 public final static String STORAGE_MANAGER_BINDING = "storage-manager"; 37 public final static String STATE_MANAGER_BINDING = "state-manager"; 38 39 protected StateManager stateManager; 40 41 protected StorageManager storageManager; 42 43 46 public String [] listFc() { 47 String [] super_res = super.listFc(); 48 String res[] = new String [super_res.length + 2]; 49 for (int cpt = 0; cpt < super_res.length; cpt++) { 50 res[cpt] = super_res[cpt]; 51 } 52 res[super_res.length + 0] = STORAGE_MANAGER_BINDING; 53 res[super_res.length + 1] = STATE_MANAGER_BINDING; 54 return res; 55 } 56 57 public Object lookupFc(String s) { 58 if (STORAGE_MANAGER_BINDING.equals(s)) { 59 return storageManager; 60 } else if (STATE_MANAGER_BINDING.equals(s)) { 61 return stateManager; 62 } else { 63 return super.lookupFc(s); 64 } 65 } 66 67 public void bindFc(String s, Object o) { 68 if (STORAGE_MANAGER_BINDING.equals(s)) { 69 storageManager = (StorageManager) o; 70 } else if (STATE_MANAGER_BINDING.equals(s)) { 71 stateManager = (StateManager) o; 72 } else super.bindFc(s, o); 73 } 74 75 public void unbindFc(String s) { 76 if (STORAGE_MANAGER_BINDING.equals(s)) { 77 storageManager = null; 78 } else if (STATE_MANAGER_BINDING.equals(s)) { 79 stateManager = null; 80 } else { 81 super.unbindFc(s); 82 } 83 } 84 public void finalize(Object arg0) { 85 WorkingSet ws = (WorkingSet) arg0; 86 boolean retainValues = ws.getWSRetainValues(); 87 for (Iterator it = ws.entries().iterator(); it.hasNext();) { 88 State state = (State) it.next(); 89 if (state == VirtualState.instance) { 90 continue; 91 } 92 CacheEntry ce = state.getCacheEntry(); 93 if (!stateManager.isUnexported(state)) { 94 if (retainValues) { 96 if (stateManager.isDirty(state)) { 97 if (stateManager.isToMerge(state)) { 98 synchronized(ce) { 99 stateManager.setReferenceState(ce, 100 stateManager.merge( 101 stateManager.getReferenceState(ce), 102 state)); 103 } 104 } else { 105 stateManager.makeClean(state); 106 stateManager.setReferenceState(ce, state); 107 } 108 } } else if (stateManager.getReferenceState(ce) == state) { 110 stateManager.setReferenceState(ce, null); 112 } 113 } else { 114 stateManager.setReferenceState(ce, state); 117 } 118 } 119 super.finalize(arg0); 120 } 121 122 public void abort(Object ctx) { 123 boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 124 if (debug) { 125 logger.log(BasicLevel.DEBUG, "Abort the context: " + ctx); 126 } 127 WorkingSet ws = (WorkingSet) ctx; 128 for(Iterator it = ws.entries().iterator(); it.hasNext();) { 129 State state = (State) it.next(); 130 if (state == VirtualState.instance) { 131 continue; 132 } 133 CacheEntry ce = state.getCacheEntry(); 134 if (stateManager.isUnexported(state)) { 135 stateManager.makeClean(state); 136 stateManager.makeBound(ce, ce.getCeIdentifier()); 137 } else if (stateManager.isExported(state)) { 138 stateManager.makeClean(state); 139 } 140 } 141 super.abort(ctx); 142 } 143 144 public void closeTimeStamp(TimeStamp ts, Object ctx) { 145 WorkingSet ws = (WorkingSet) ctx; 146 State s = ws.lookup(ts.oid); 147 if (s != null && s != VirtualState.instance) { 148 stateManager.stateNoMoreUsed(s); 149 } 150 } 151 protected Object getState(Object ctx, 152 Object resource, 153 TimeStamp timeStamp, 154 boolean isDirtyBefore, 155 boolean isWrite) throws ConcurrencyException { 156 CacheEntry ce = (CacheEntry) resource; 157 WorkingSet ws = (WorkingSet) ctx; 158 State state = ws.lookup(ce.getCeIdentifier()); 159 if (state == VirtualState.instance) { 160 state = null; 161 } 162 if (state == null) { 163 state = stateManager.getReferenceState(ce); 164 if (state == null) { state = load(ws, ce); 166 } 167 if (isWrite) { 168 state = stateManager.createState(state); 170 } } else { 172 if (isWrite) { 174 if (!isDirtyBefore) { 175 state = stateManager.createState(state); 177 } } } 180 return state; 181 } 182 183 private State load(WorkingSet ws, CacheEntry ce) 184 throws ConcurrencyException { 185 State state = stateManager.createState(ce); 186 stateManager.setReferenceState(state.getCacheEntry(), state); 187 try { 188 storageManager.read(ws, ce.getCeIdentifier(), state); 189 } catch (PersistenceException e) { 190 stateManager.setReferenceState(state.getCacheEntry(), null); 191 stateManager.destroyState(state); 192 if (e instanceof NoDSIPersistenceException) { 193 throw new NoDSIConcurrencyException(e); 194 } else { 195 throw new ConcurrencyException(e); 196 } 197 } 198 stateManager.makeClean(state); 199 return state; 200 } 201 202 protected Object getResourceId(Object object) { 203 return ((CacheEntry) object).getCeIdentifier(); 204 } 205 206 } 207 | Popular Tags |