KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > versioned > DistributedVersionManager


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
33 import java.lang.reflect.Modifier JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39
40
41 /**
42  *
43  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
44  * @version $Revision: 37406 $
45  */

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 JavaDoc 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 JavaDoc makeVersionedList(List JavaDoc list, ArrayList JavaDoc newObjects) throws Exception JavaDoc
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 JavaDoc)proxy;
89    }
90
91
92    public Map JavaDoc makeVersionedMap(Map JavaDoc map, ArrayList JavaDoc newObjects) throws Exception JavaDoc
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 JavaDoc)proxy;
102    }
103
104
105    public Set JavaDoc makeVersionedSet(Set JavaDoc set, ArrayList JavaDoc newObjects) throws Exception JavaDoc
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 JavaDoc)proxy;
115    }
116
117
118    public Object JavaDoc makeVersioned(Object JavaDoc obj)
119       throws Exception JavaDoc
120    {
121       ArrayList JavaDoc newObjects = new ArrayList JavaDoc();
122       obj = makeVersioned(obj, newObjects);
123       synchManager.createObjects(newObjects);
124       return obj;
125    }
126
127    Object JavaDoc makeVersioned(Object JavaDoc obj, ArrayList JavaDoc newObjects)
128       throws Exception JavaDoc
129    {
130       // Proxies cannot be versioned
131
if (obj instanceof ClassProxy) return obj;
132
133       if (!(obj instanceof Advised))
134       {
135          if (obj instanceof List JavaDoc)
136          {
137             List JavaDoc list = (List JavaDoc)obj;
138             return makeVersionedList(list, newObjects);
139          }
140          else if (obj instanceof Map JavaDoc)
141          {
142             Map JavaDoc map = (Map JavaDoc)obj;
143             return makeVersionedMap(map, newObjects);
144          }
145          else if (obj instanceof Set JavaDoc)
146          {
147             Set JavaDoc set = (Set JavaDoc)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 JavaDoc[] advisedFields = ((ClassAdvisor)advised._getAdvisor()).getAdvisedFields();
172          for (int i = 0; i < advisedFields.length; i++)
173          {
174             Field JavaDoc field = advisedFields[i];
175             if (Modifier.isStatic(field.getModifiers())) continue;
176             Object JavaDoc 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 JavaDoc)
186                {
187                   List JavaDoc list = (List JavaDoc)fieldVal;
188                   InstanceAdvised instanceAdvised = (InstanceAdvised)makeVersionedList(list, newObjects);
189                   fieldVal = new VersionReference(getGUID(instanceAdvised), instanceAdvised);
190                }
191                else if (fieldVal instanceof Map JavaDoc)
192                {
193                   Map JavaDoc map = (Map JavaDoc)fieldVal;
194                   InstanceAdvised instanceAdvised = (InstanceAdvised)makeVersionedMap(map, newObjects);
195                   fieldVal = new VersionReference(getGUID(instanceAdvised), instanceAdvised);
196                }
197                else if (fieldVal instanceof Set JavaDoc)
198                {
199                   Set JavaDoc set = (Set JavaDoc)fieldVal;
200                   InstanceAdvised instanceAdvised = (InstanceAdvised)makeVersionedSet(set, newObjects);
201                   fieldVal = new VersionReference(getGUID(instanceAdvised), instanceAdvised);
202                }
203             }
204             manager.fieldMap.put(new Integer JavaDoc(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    /**
216     * This is used by DistributedState.buildObject when the DistributedState object is
217     * serialized across the wire and must recreate the object it represents
218     */

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    /**
229     * This is used by DistributedState.buildObject when the DistributedState object is
230     * serialized across the wire and must recreate the object it represents
231     */

232    public ClassProxy addListVersioning(List JavaDoc list, DistributedListState manager) throws Exception JavaDoc
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    /**
245     * This is used by DistributedState.buildObject when the DistributedState object is
246     * serialized across the wire and must recreate the object it represents
247     */

248    public ClassProxy addMapVersioning(Map JavaDoc map, DistributedMapState manager) throws Exception JavaDoc
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    /**
261     * This is used by DistributedState.buildObject when the DistributedState object is
262     * serialized across the wire and must recreate the object it represents
263     */

264    public ClassProxy addSetVersioning(Set JavaDoc set, DistributedSetState manager) throws Exception JavaDoc
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