1 18 package org.objectweb.perseus.persistence.concurrency; 19 20 import java.util.Iterator ; 21 22 import org.objectweb.fractal.api.NoSuchInterfaceException; 23 import org.objectweb.fractal.api.control.IllegalBindingException; 24 import org.objectweb.perseus.cache.api.CacheEntry; 25 import org.objectweb.perseus.concurrency.api.ConcurrencyException; 26 import org.objectweb.perseus.concurrency.distributed.DistributedConcurrencyManager; 27 import org.objectweb.perseus.concurrency.pessimistic.Lock; 28 import org.objectweb.perseus.persistence.api.PersistenceException; 29 import org.objectweb.perseus.persistence.api.State; 30 import org.objectweb.perseus.persistence.api.StateManager; 31 import org.objectweb.perseus.persistence.api.StorageManager; 32 import org.objectweb.perseus.persistence.api.VirtualState; 33 import org.objectweb.perseus.persistence.api.WorkingSet; 34 35 39 public class PDistributedConcurrencyManager 40 extends DistributedConcurrencyManager { 41 42 43 44 public final static String STORAGE_MANAGER_BINDING = "storage-manager"; 45 public final static String STATE_MANAGER_BINDING = "state-manager"; 46 47 protected StateManager stateManager; 48 49 protected StorageManager storageManager; 50 51 54 public String [] listFc() { 55 String [] super_res = super.listFc(); 56 String res[] = new String [super_res.length + 2]; 57 for (int cpt = 0; cpt < super_res.length; cpt++) { 58 res[cpt] = super_res[cpt]; 59 } 60 res[super_res.length + 0] = STORAGE_MANAGER_BINDING; 61 res[super_res.length + 1] = STATE_MANAGER_BINDING; 62 return res; 63 } 64 65 public Object lookupFc(String s) throws NoSuchInterfaceException { 66 if (STORAGE_MANAGER_BINDING.equals(s)) { 67 return storageManager; 68 } else if (STATE_MANAGER_BINDING.equals(s)) { 69 return stateManager; 70 } else { 71 return super.lookupFc(s); 72 } 73 } 74 75 public void bindFc(String s, Object o) throws IllegalBindingException, 76 NoSuchInterfaceException { 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 super.bindFc(s, o); 82 } 83 84 public void unbindFc(String s) throws NoSuchInterfaceException { 85 if (STORAGE_MANAGER_BINDING.equals(s)) { 86 storageManager = null; 87 } else if (STATE_MANAGER_BINDING.equals(s)) { 88 stateManager = null; 89 } else { 90 super.unbindFc(s); 91 } 92 } 93 94 95 public PDistributedConcurrencyManager() throws ConcurrencyException { 96 super(); 97 } 98 99 public void finalize(Object arg0) { 100 WorkingSet ws = (WorkingSet) arg0; 101 boolean retainValues = ws.getWSRetainValues(); 102 for (Iterator it = ws.entries().iterator(); it.hasNext();) { 103 State state = (State) it.next(); 104 if (state == VirtualState.instance) { 105 continue; 106 } 107 CacheEntry ce = state.getCacheEntry(); 108 if (!stateManager.isUnexported(state)) { 109 if (retainValues) { 111 if (stateManager.isDirty(state)) { 112 if (stateManager.isToMerge(state)) { 113 synchronized(ce) { 114 stateManager.setReferenceState(ce, 115 stateManager.merge( 116 stateManager.getReferenceState(ce), 117 state)); 118 } 119 } else { 120 stateManager.makeClean(state); 121 stateManager.setReferenceState(ce, state); 122 } 123 } } else if (stateManager.getReferenceState(ce) == state) { 125 stateManager.setReferenceState(ce, null); 127 } 128 } 129 } 130 super.finalize(arg0); 131 } 132 133 134 protected Object getState(Object ctx, 135 Object resource, 136 Lock lock) throws ConcurrencyException { 137 CacheEntry ce = (CacheEntry) resource; 138 WorkingSet ws = (WorkingSet) ctx; 139 State state = stateManager.getReferenceState(ce); 140 if (state == null) { 141 synchronized (ce) { 142 state = stateManager.getReferenceState(ce); 143 if (state == null) { 144 state = stateManager.createState(ce); 145 stateManager.setReferenceState(state.getCacheEntry(), state); 146 try { 147 storageManager.read(ws, ce.getCeIdentifier(), state); 148 stateManager.makeClean(state); 149 } catch (PersistenceException e) { 150 stateManager.setReferenceState(state.getCacheEntry(), null); 151 stateManager.destroyState(state); 152 synchronized (locks) { 154 if (lock.close(ctx)) { 155 locks.remove(lock); 156 } 157 } 158 throw new NoDSIConcurrencyException(e); 159 } 160 } 161 } 162 } 163 return state; 164 } 165 protected void invalidateState(Object resId, Object hints) { 166 CacheEntry ce = (CacheEntry) hints; 167 State state = stateManager.getReferenceState(ce); 168 if (state != null) { 169 synchronized(ce) { 170 state = stateManager.getReferenceState(ce); 171 if (state != null) 172 stateManager.destroyState(state); 173 } 174 } 175 } 176 177 } 178 | Popular Tags |