1 18 package org.objectweb.perseus.persistence.concurrency; 19 20 import org.objectweb.perseus.concurrency.dbdelegate.DbDelegateConcurrencyManager; 21 import org.objectweb.perseus.concurrency.api.ConcurrencyException; 22 import org.objectweb.perseus.cache.api.CacheEntry; 23 import org.objectweb.perseus.persistence.api.StateManager; 24 import org.objectweb.perseus.persistence.api.StorageManager; 25 import org.objectweb.perseus.persistence.api.VirtualState; 26 import org.objectweb.perseus.persistence.api.WorkingSet; 27 import org.objectweb.perseus.persistence.api.State; 28 import org.objectweb.perseus.persistence.api.PersistenceException; 29 import org.objectweb.util.monolog.api.BasicLevel; 30 import org.objectweb.fractal.api.NoSuchInterfaceException; 31 import org.objectweb.fractal.api.control.IllegalBindingException; 32 33 import java.util.Iterator ; 34 35 39 public class PDbDelegateConcurrencyManager 40 extends DbDelegateConcurrencyManager { 41 42 public PDbDelegateConcurrencyManager() throws ConcurrencyException { 43 } 44 45 public final static String STORAGE_MANAGER_BINDING = "storage-manager"; 46 public final static String STATE_MANAGER_BINDING = "state-manager"; 47 48 protected StateManager stateManager; 49 50 protected StorageManager storageManager; 51 52 55 public String [] listFc() { 56 String [] super_res = super.listFc(); 57 String res[] = new String [super_res.length + 2]; 58 for (int cpt = 0; cpt < super_res.length; cpt++) { 59 res[cpt] = super_res[cpt]; 60 } 61 res[super_res.length + 0] = STORAGE_MANAGER_BINDING; 62 res[super_res.length + 1] = STATE_MANAGER_BINDING; 63 return res; 64 } 65 66 public Object lookupFc(String s) throws NoSuchInterfaceException { 67 if (STORAGE_MANAGER_BINDING.equals(s)) { 68 return storageManager; 69 } else if (STATE_MANAGER_BINDING.equals(s)) { 70 return stateManager; 71 } else { 72 return super.lookupFc(s); 73 } 74 } 75 76 public void bindFc(String s, Object o) throws NoSuchInterfaceException, IllegalBindingException { 77 if (STORAGE_MANAGER_BINDING.equals(s)) { 78 storageManager = (StorageManager) o; 79 } else if (STATE_MANAGER_BINDING.equals(s)) { 80 stateManager = (StateManager) o; 81 } else 82 super.bindFc(s, o); 83 } 84 85 public void unbindFc(String s) throws NoSuchInterfaceException { 86 if (STORAGE_MANAGER_BINDING.equals(s)) { 87 storageManager = null; 88 } else if (STATE_MANAGER_BINDING.equals(s)) { 89 stateManager = null; 90 } else { 91 super.unbindFc(s); 92 } 93 } 94 public void finalize(Object ctx) { 95 boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 96 if (debug) { 97 logger.log(BasicLevel.DEBUG, "Finalize the context: " + ctx); 98 } 99 WorkingSet ws = (WorkingSet) ctx; 100 for(Iterator it = ws.entries().iterator(); it.hasNext();) { 101 State state = (State) it.next(); 102 if (state == VirtualState.instance) { 103 continue; 104 } 105 CacheEntry ce = state.getCacheEntry(); 106 if (stateManager.isUnexported(state)) { 107 stateManager.setReferenceState(ce, state); 108 } else { 109 stateManager.setReferenceState(ce, null); 110 } 111 } 112 } 113 114 public void abort(Object ctx) { 115 boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 116 if (debug) { 117 logger.log(BasicLevel.DEBUG, "Abort the context: " + ctx); 118 } 119 WorkingSet ws = (WorkingSet) ctx; 120 for(Iterator it = ws.entries().iterator(); it.hasNext();) { 121 State state = (State) it.next(); 122 if (state == VirtualState.instance) { 123 continue; 124 } 125 CacheEntry ce = state.getCacheEntry(); 126 if (stateManager.isUnexported(state)) { 127 stateManager.makeClean(state); 128 stateManager.makeBound(ce, ce.getCeIdentifier()); 129 } 130 } 131 } 132 133 protected Object getState(Object ctx, 134 Object resource, 135 Object thinLock) throws ConcurrencyException { 136 CacheEntry ce = (CacheEntry) resource; 137 WorkingSet ws = (WorkingSet) ctx; 138 synchronized (ce) { 139 State state = stateManager.getReferenceState(ce); 140 if (state != null) { 141 return state; 142 } 143 state = ws.lookup(ce.getCeIdentifier()); 144 if (state != null && state != VirtualState.instance) { 145 return state; 146 } 147 state = load(ws, ce.getCeIdentifier(), ce); 148 return state; 149 } 150 } 151 152 private State load(WorkingSet ws, Object resourceId, CacheEntry ce) 153 throws ConcurrencyException { 154 State state = stateManager.createState(ce); 155 try { 156 storageManager.read(ws, resourceId, state); 157 stateManager.makeClean(state); 158 } catch (PersistenceException e) { 159 stateManager.destroyState(state); 160 throw new NoDSIConcurrencyException(e); 161 } 162 return state; 163 } 164 } 165 | Popular Tags |