1 22 package org.jboss.aspects.versioned; 23 24 import org.jboss.aop.Advised; 25 import org.jboss.aop.ClassAdvisor; 26 import org.jboss.aop.InstanceAdvised; 27 import org.jboss.aop.proxy.ClassProxy; 28 import org.jboss.aop.proxy.ClassProxyFactory; 29 import org.jboss.logging.Logger; 30 import org.jboss.util.id.GUID; 31 32 import java.lang.reflect.Field ; 33 import java.lang.reflect.Modifier ; 34 import java.util.ArrayList ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 40 41 46 public class DistributedVersionManager extends VersionManager 47 { 48 protected static Logger log = Logger.getLogger(DistributedVersionManager.class); 49 protected SynchronizationManager synchManager; 50 protected long timeout; 51 52 public DistributedVersionManager(long timeout, SynchronizationManager synchManager) 53 { 54 this.synchManager = synchManager; 55 this.timeout = timeout; 56 } 57 58 public boolean isVersioned(Object obj) 59 { 60 if (!(obj instanceof InstanceAdvised)) return false; 61 InstanceAdvised advised = (InstanceAdvised)obj; 62 return getGUID(advised) != null; 63 } 64 65 public GUID tag(InstanceAdvised advised) 66 { 67 GUID guid = new GUID(); 68 org.jboss.aop.metadata.SimpleMetaData metaData = advised._getInstanceAdvisor().getMetaData(); 69 metaData.addMetaData(VERSION_MANAGER, VERSION_ID, guid); 70 return guid; 71 } 72 73 public void untag(InstanceAdvised advised) 74 { 75 org.jboss.aop.metadata.SimpleMetaData metaData = advised._getInstanceAdvisor().getMetaData(); 76 metaData.removeMetaData(VERSION_MANAGER, VERSION_ID); 77 } 78 79 public List makeVersionedList(List list, ArrayList newObjects) throws Exception 80 { 81 ClassProxy proxy = ClassProxyFactory.newInstance(list.getClass()); 82 GUID guid = tag(proxy); 83 DistributedListState manager = new DistributedListState(guid, timeout, proxy, list, this, synchManager); 84 StateManager.setStateManager(proxy, manager); 85 CollectionStateChangeInterceptor interceptor = new CollectionStateChangeInterceptor(manager); 86 proxy._getInstanceAdvisor().appendInterceptor(interceptor); 87 newObjects.add(manager); 88 return (List )proxy; 89 } 90 91 92 public Map makeVersionedMap(Map map, ArrayList newObjects) throws Exception 93 { 94 ClassProxy proxy = ClassProxyFactory.newInstance(map.getClass()); 95 GUID guid = tag(proxy); 96 DistributedMapState manager = new DistributedMapState(guid, timeout, proxy, map, this, synchManager); 97 StateManager.setStateManager(proxy, manager); 98 CollectionStateChangeInterceptor interceptor = new CollectionStateChangeInterceptor(manager); 99 proxy._getInstanceAdvisor().appendInterceptor(interceptor); 100 newObjects.add(manager); 101 return (Map )proxy; 102 } 103 104 105 public Set makeVersionedSet(Set set, ArrayList newObjects) throws Exception 106 { 107 ClassProxy proxy = ClassProxyFactory.newInstance(set.getClass()); 108 GUID guid = tag(proxy); 109 DistributedSetState manager = new DistributedSetState(guid, timeout, proxy, set, this, synchManager); 110 StateManager.setStateManager(proxy, manager); 111 CollectionStateChangeInterceptor interceptor = new CollectionStateChangeInterceptor(manager); 112 proxy._getInstanceAdvisor().appendInterceptor(interceptor); 113 newObjects.add(manager); 114 return (Set )proxy; 115 } 116 117 118 public Object makeVersioned(Object obj) 119 throws Exception 120 { 121 ArrayList newObjects = new ArrayList (); 122 obj = makeVersioned(obj, newObjects); 123 synchManager.createObjects(newObjects); 124 return obj; 125 } 126 127 Object makeVersioned(Object obj, ArrayList newObjects) 128 throws Exception 129 { 130 if (obj instanceof ClassProxy) return obj; 132 133 if (!(obj instanceof Advised)) 134 { 135 if (obj instanceof List ) 136 { 137 List list = (List )obj; 138 return makeVersionedList(list, newObjects); 139 } 140 else if (obj instanceof Map ) 141 { 142 Map map = (Map )obj; 143 return makeVersionedMap(map, newObjects); 144 } 145 else if (obj instanceof Set ) 146 { 147 Set set = (Set )obj; 148 return makeVersionedSet(set, newObjects); 149 } 150 else 151 { 152 return obj; 153 } 154 } 155 Advised advised = (Advised)obj; 156 org.jboss.aop.metadata.SimpleMetaData metaData = advised._getInstanceAdvisor().getMetaData(); 157 GUID guid; 158 synchronized (metaData) 159 { 160 if (isVersioned(advised)) return obj; 161 guid = tag(advised); 162 } 163 System.out.println("VersionManager: " + guid); 164 DistributedPOJOState manager = new DistributedPOJOState(guid, timeout, advised, this, synchManager); 165 StateManager.setStateManager(advised, manager); 166 StateChangeInterceptor interceptor = new StateChangeInterceptor(manager); 167 manager.acquireWriteLock(); 168 advised._getInstanceAdvisor().appendInterceptor(interceptor); 169 try 170 { 171 Field [] advisedFields = ((ClassAdvisor)advised._getAdvisor()).getAdvisedFields(); 172 for (int i = 0; i < advisedFields.length; i++) 173 { 174 Field field = advisedFields[i]; 175 if (Modifier.isStatic(field.getModifiers())) continue; 176 Object fieldVal = field.get(advised); 177 if (fieldVal != null) 178 { 179 if (fieldVal instanceof Advised) 180 { 181 Advised fieldAdvised = (Advised)fieldVal; 182 makeVersioned(fieldAdvised, newObjects); 183 fieldVal = new VersionReference(getGUID(fieldAdvised), fieldAdvised); 184 } 185 else if (fieldVal instanceof List ) 186 { 187 List list = (List )fieldVal; 188 InstanceAdvised instanceAdvised = (InstanceAdvised)makeVersionedList(list, newObjects); 189 fieldVal = new VersionReference(getGUID(instanceAdvised), instanceAdvised); 190 } 191 else if (fieldVal instanceof Map ) 192 { 193 Map map = (Map )fieldVal; 194 InstanceAdvised instanceAdvised = (InstanceAdvised)makeVersionedMap(map, newObjects); 195 fieldVal = new VersionReference(getGUID(instanceAdvised), instanceAdvised); 196 } 197 else if (fieldVal instanceof Set ) 198 { 199 Set set = (Set )fieldVal; 200 InstanceAdvised instanceAdvised = (InstanceAdvised)makeVersionedSet(set, newObjects); 201 fieldVal = new VersionReference(getGUID(instanceAdvised), instanceAdvised); 202 } 203 } 204 manager.fieldMap.put(new Integer (i), new DistributedFieldUpdate(fieldVal, 0, i)); 205 } 206 newObjects.add(manager); 207 return advised; 208 } 209 finally 210 { 211 manager.releaseWriteLock(); 212 } 213 } 214 215 219 public void addVersioning(DistributedPOJOState manager, Advised advised) 220 { 221 StateManager.setStateManager(advised, manager); 222 StateChangeInterceptor interceptor = new StateChangeInterceptor(manager); 223 org.jboss.aop.metadata.SimpleMetaData metaData = advised._getInstanceAdvisor().getMetaData(); 224 metaData.addMetaData(VERSION_MANAGER, VERSION_ID, manager.getGUID()); 225 advised._getInstanceAdvisor().appendInterceptor(interceptor); 226 } 227 228 232 public ClassProxy addListVersioning(List list, DistributedListState manager) throws Exception 233 { 234 ClassProxy proxy = ClassProxyFactory.newInstance(list.getClass()); 235 GUID guid = manager.getGUID(); 236 org.jboss.aop.metadata.SimpleMetaData metaData = proxy._getInstanceAdvisor().getMetaData(); 237 metaData.addMetaData(VERSION_MANAGER, VERSION_ID, guid); 238 StateManager.setStateManager(proxy, manager); 239 CollectionStateChangeInterceptor interceptor = new CollectionStateChangeInterceptor(manager); 240 proxy._getInstanceAdvisor().appendInterceptor(interceptor); 241 return proxy; 242 } 243 244 248 public ClassProxy addMapVersioning(Map map, DistributedMapState manager) throws Exception 249 { 250 ClassProxy proxy = ClassProxyFactory.newInstance(map.getClass()); 251 GUID guid = manager.getGUID(); 252 org.jboss.aop.metadata.SimpleMetaData metaData = proxy._getInstanceAdvisor().getMetaData(); 253 metaData.addMetaData(VERSION_MANAGER, VERSION_ID, guid); 254 StateManager.setStateManager(proxy, manager); 255 CollectionStateChangeInterceptor interceptor = new CollectionStateChangeInterceptor(manager); 256 proxy._getInstanceAdvisor().appendInterceptor(interceptor); 257 return proxy; 258 } 259 260 264 public ClassProxy addSetVersioning(Set set, DistributedSetState manager) throws Exception 265 { 266 ClassProxy proxy = ClassProxyFactory.newInstance(set.getClass()); 267 GUID guid = manager.getGUID(); 268 org.jboss.aop.metadata.SimpleMetaData metaData = proxy._getInstanceAdvisor().getMetaData(); 269 metaData.addMetaData(VERSION_MANAGER, VERSION_ID, guid); 270 StateManager.setStateManager(proxy, manager); 271 CollectionStateChangeInterceptor interceptor = new CollectionStateChangeInterceptor(manager); 272 proxy._getInstanceAdvisor().appendInterceptor(interceptor); 273 return proxy; 274 } 275 } 276 | Popular Tags |