KickJava   Java API By Example, From Geeks To Geeks.

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


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.InstanceAdvised;
25 import org.jboss.aop.proxy.ClassProxy;
26 import org.jboss.aop.proxy.ClassProxyFactory;
27 import org.jboss.logging.Logger;
28 import org.jboss.tm.TransactionLocal;
29 import org.jboss.util.id.GUID;
30
31 import javax.naming.InitialContext JavaDoc;
32 import javax.transaction.Transaction JavaDoc;
33 import javax.transaction.TransactionManager JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Set JavaDoc;
40
41
42 /**
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
45  * @version $Revision: 37406 $
46  */

47 public class DistributedSetState extends CollectionStateManager implements Set JavaDoc, DistributedState, java.io.Externalizable JavaDoc
48 {
49    private static final long serialVersionUID = -6272170697169509758L;
50
51    private static HashMap JavaDoc setMethodMap;
52
53    protected static Logger log = Logger.getLogger(DistributedSetState.class);
54    static
55    {
56       try
57       {
58          setMethodMap = new HashMap JavaDoc();
59          Method JavaDoc[] methods = Set JavaDoc.class.getDeclaredMethods();
60          for (int i = 0; i < methods.length; i++)
61          {
62             long hash = org.jboss.aop.util.MethodHashing.methodHash(methods[i]);
63             setMethodMap.put(new Long JavaDoc(hash), methods[i]);
64          }
65          
66       }
67       catch (Exception JavaDoc ignored)
68       {
69          ignored.printStackTrace();
70       }
71    }
72
73    protected volatile long versionId;
74    protected HashSet JavaDoc updates;
75    protected String JavaDoc classname;
76    transient protected Set JavaDoc base;
77    transient protected TransactionLocal txState = new TransactionLocal();
78    transient protected TransactionLocal txVersion = new TransactionLocal();
79    transient protected DistributedVersionManager versionManager;
80    transient protected SynchronizationManager synchManager;
81    transient protected TransactionManager JavaDoc tm;
82    transient protected ClassProxy proxy;
83
84    /**
85     * For serialization
86     */

87    public DistributedSetState() {}
88
89
90    public DistributedSetState(GUID guid, long timeout, ClassProxy proxy, Set JavaDoc obj, DistributedVersionManager versionManager, SynchronizationManager synchManager)
91       throws Exception JavaDoc
92    {
93       super(guid, timeout, setMethodMap);
94       this.base = obj;
95       this.classname = obj.getClass().getName();
96       this.versionManager = versionManager;
97       this.synchManager = synchManager;
98       this.proxy = proxy;
99       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
100       this.tm = (TransactionManager JavaDoc)ctx.lookup("java:/TransactionManager");
101       this.updates = createSetUpdates(base);
102    }
103
104    public HashMap JavaDoc getMethodMap()
105    {
106       return ClassProxyFactory.getMethodMap(base.getClass().getName());
107    }
108
109    public InstanceAdvised getObject() { return proxy; }
110
111    // The Guts
112

113    protected Set JavaDoc getCurrentState(boolean forUpdate) throws Exception JavaDoc
114    {
115       Transaction JavaDoc tx = tm.getTransaction();
116       if (tx == null)
117       {
118          if (forUpdate) versionId++;
119          return base;
120       }
121
122       Set JavaDoc state = (Set JavaDoc)txState.get(tx);
123       if (state == null && forUpdate)
124       {
125          state = (Set JavaDoc)base.getClass().newInstance();
126          state.addAll(base);
127          txState.set(tx, state);
128          long newId = versionId + 1;
129          synchManager.registerUpdate(tx, this);
130          txVersion.set(tx, new Long JavaDoc(newId));
131          return state;
132       }
133       return base;
134    }
135
136    
137    protected HashSet JavaDoc createSetUpdates(Set JavaDoc state)
138    {
139       HashSet JavaDoc setUpdates = new HashSet JavaDoc(state.size());
140       Iterator JavaDoc it = state.iterator();
141       while (it.hasNext())
142       {
143          Object JavaDoc obj = it.next();
144          if (versionManager.isVersioned(obj))
145          {
146             setUpdates.add(new VersionReference(VersionManager.getGUID((InstanceAdvised)obj)));
147          }
148          else
149          {
150             setUpdates.add(obj);
151          }
152       }
153       return setUpdates;
154    }
155
156    public DistributedUpdate createTxUpdate(Transaction JavaDoc tx)
157    {
158       Set JavaDoc state = (Set JavaDoc)txState.get(tx);
159       long newId = ((Long JavaDoc)txVersion.get(tx)).longValue();
160       DistributedSetUpdate update = new DistributedSetUpdate(guid, createSetUpdates(state), newId);
161       return update;
162    }
163
164    public InstanceAdvised buildObject(SynchronizationManager manager, DistributedVersionManager versionManager)
165       throws Exception JavaDoc
166    {
167       log.trace("building a Set");
168       this.versionManager = versionManager;
169       this.synchManager = manager;
170       log.trace("DistributedSetState: classname: " + classname);
171       Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(classname);
172       base = (Set JavaDoc)clazz.newInstance();
173       Iterator JavaDoc it = updates.iterator();
174       while (it.hasNext())
175       {
176          Object JavaDoc val = it.next();
177          if (val instanceof VersionReference)
178          {
179             VersionReference ref = (VersionReference)val;
180             val = manager.getObject(ref.getGUID());
181             if (val == null)
182             {
183                DistributedState fieldVal = manager.getState(ref.getGUID());
184                val = fieldVal.buildObject(manager, versionManager);
185                ref.set((InstanceAdvised)val);
186             }
187          }
188          base.add(val);
189       }
190       proxy = versionManager.addSetVersioning(base, this);
191       return proxy;
192    }
193
194    public void checkOptimisticLock(Transaction JavaDoc tx)
195    {
196       // NOTE THIS CODE ASSUMES THAT A WRITELOCK HAS BEEN ACQUIRED!!!!
197
Long JavaDoc version = (Long JavaDoc)txVersion.get(tx);
198       if (version.longValue() <= versionId)
199          throw new OptimisticLockFailure("optimistic lock failure for set");
200    }
201    
202    public void mergeState(Transaction JavaDoc tx) throws Exception JavaDoc
203    {
204       // NOTE THIS CODE ASSUMES THAT A WRITELOCK HAS BEEN ACQUIRED!!!!
205
Set JavaDoc current = (Set JavaDoc)txState.get(tx);
206       base = current;
207       Long JavaDoc version = (Long JavaDoc)txVersion.get(tx);
208       versionId = version.longValue();
209    }
210
211    public void mergeState(DistributedUpdate update) throws Exception JavaDoc
212    {
213       DistributedSetUpdate setUpdate = (DistributedSetUpdate)update;
214       this.versionId = setUpdate.versionId;
215       base.clear();
216       Iterator JavaDoc it = setUpdate.setUpdates.iterator();
217       while (it.hasNext())
218       {
219          Object JavaDoc val = it.next();
220          if (val instanceof VersionReference)
221          {
222             VersionReference ref = (VersionReference)val;
223             val = synchManager.getObject(ref.getGUID());
224             ref.set((InstanceAdvised)val);
225          }
226          base.add(val);
227       }
228       updates = setUpdate.setUpdates;
229    }
230
231    // java.util.Set wrap
232

233    public boolean add(Object JavaDoc val)
234    {
235       try
236       {
237          lock.readLock().acquire();
238          try
239          {
240             val = versionManager.makeVersioned(val);
241             Set JavaDoc state = getCurrentState(true);
242             return state.add(val);
243          }
244          finally
245          {
246             lock.readLock().release();
247          }
248       }
249       catch (Exception JavaDoc ex)
250       {
251          throw new RuntimeException JavaDoc(ex);
252       }
253    }
254
255    public boolean addAll(Collection JavaDoc c)
256    {
257       try
258       {
259          lock.readLock().acquire();
260          try
261          {
262             Set JavaDoc state = getCurrentState(true);
263             Object JavaDoc[] copy = c.toArray();
264             for (int i = 0; i < copy.length; i++)
265             {
266                Object JavaDoc item = versionManager.makeVersioned(copy[i]);
267                state.add(item);
268             }
269             return true;
270          }
271          finally
272          {
273             lock.readLock().release();
274          }
275       }
276       catch (Exception JavaDoc ex)
277       {
278          throw new RuntimeException JavaDoc(ex);
279       }
280    }
281
282    public void clear()
283    {
284       try
285       {
286          lock.readLock().acquire();
287          try
288          {
289             Set JavaDoc state = getCurrentState(true);
290             state.clear();
291          }
292          finally
293          {
294             lock.readLock().release();
295          }
296       }
297       catch (Exception JavaDoc ex)
298       {
299          throw new RuntimeException JavaDoc(ex);
300       }
301    }
302
303    public boolean contains(Object JavaDoc o)
304    {
305       try
306       {
307          lock.readLock().acquire();
308          try
309          {
310             Set JavaDoc state = getCurrentState(false);
311             return state.contains(o);
312          }
313          finally
314          {
315             lock.readLock().release();
316          }
317       }
318       catch (Exception JavaDoc ex)
319       {
320          throw new RuntimeException JavaDoc(ex);
321       }
322    }
323    public boolean containsAll(Collection JavaDoc c)
324    {
325       try
326       {
327          lock.readLock().acquire();
328          try
329          {
330             Set JavaDoc state = getCurrentState(false);
331             return state.containsAll(c);
332          }
333          finally
334          {
335             lock.readLock().release();
336          }
337       }
338       catch (Exception JavaDoc ex)
339       {
340          throw new RuntimeException JavaDoc(ex);
341       }
342    }
343    public boolean equals(Object JavaDoc o)
344    {
345       try
346       {
347          lock.readLock().acquire();
348          try
349          {
350             Set JavaDoc state = getCurrentState(false);
351             return state.equals(o);
352          }
353          finally
354          {
355             lock.readLock().release();
356          }
357       }
358       catch (Exception JavaDoc ex)
359       {
360          throw new RuntimeException JavaDoc(ex);
361       }
362    }
363    public int hashCode()
364    {
365       try
366       {
367          lock.readLock().acquire();
368          try
369          {
370             Set JavaDoc state = getCurrentState(false);
371             return state.hashCode();
372          }
373          finally
374          {
375             lock.readLock().release();
376          }
377       }
378       catch (Exception JavaDoc ex)
379       {
380          throw new RuntimeException JavaDoc(ex);
381       }
382    }
383    public boolean isEmpty()
384    {
385       try
386       {
387          lock.readLock().acquire();
388          try
389          {
390             Set JavaDoc state = getCurrentState(false);
391             return state.isEmpty();
392          }
393          finally
394          {
395             lock.readLock().release();
396          }
397       }
398       catch (Exception JavaDoc ex)
399       {
400          throw new RuntimeException JavaDoc(ex);
401       }
402    }
403    public Iterator JavaDoc iterator()
404    {
405       try
406       {
407          lock.readLock().acquire();
408          try
409          {
410             Set JavaDoc state = getCurrentState(false);
411             return state.iterator();
412          }
413          finally
414          {
415             lock.readLock().release();
416          }
417       }
418       catch (Exception JavaDoc ex)
419       {
420          throw new RuntimeException JavaDoc(ex);
421       }
422    }
423    public boolean remove(Object JavaDoc o)
424    {
425       try
426       {
427          lock.readLock().acquire();
428          try
429          {
430             Set JavaDoc state = getCurrentState(true);
431             return state.remove(o);
432          }
433          finally
434          {
435             lock.readLock().release();
436          }
437       }
438       catch (Exception JavaDoc ex)
439       {
440          throw new RuntimeException JavaDoc(ex);
441       }
442    }
443    public boolean removeAll(Collection JavaDoc c)
444    {
445       try
446       {
447          lock.readLock().acquire();
448          try
449          {
450             Set JavaDoc state = getCurrentState(true);
451             return state.removeAll(c);
452          }
453          finally
454          {
455             lock.readLock().release();
456          }
457       }
458       catch (Exception JavaDoc ex)
459       {
460          throw new RuntimeException JavaDoc(ex);
461       }
462    }
463    public boolean retainAll(Collection JavaDoc c)
464    {
465       try
466       {
467          lock.readLock().acquire();
468          try
469          {
470             Set JavaDoc state = getCurrentState(true);
471             return state.retainAll(c);
472          }
473          finally
474          {
475             lock.readLock().release();
476          }
477       }
478       catch (Exception JavaDoc ex)
479       {
480          throw new RuntimeException JavaDoc(ex);
481       }
482    }
483    public int size()
484    {
485       try
486       {
487          lock.readLock().acquire();
488          try
489          {
490             Set JavaDoc state = getCurrentState(false);
491             return state.size();
492          }
493          finally
494          {
495             lock.readLock().release();
496          }
497       }
498       catch (Exception JavaDoc ex)
499       {
500          throw new RuntimeException JavaDoc(ex);
501       }
502    }
503    public Object JavaDoc[] toArray()
504    {
505       try
506       {
507          lock.readLock().acquire();
508          try
509          {
510             Set JavaDoc state = getCurrentState(false);
511             return state.toArray();
512          }
513          finally
514          {
515             lock.readLock().release();
516          }
517       }
518       catch (Exception JavaDoc ex)
519       {
520          throw new RuntimeException JavaDoc(ex);
521       }
522    }
523    public Object JavaDoc[] toArray(Object JavaDoc[] a)
524    {
525       try
526       {
527          lock.readLock().acquire();
528          try
529          {
530             Set JavaDoc state = getCurrentState(false);
531             return state.toArray(a);
532          }
533          finally
534          {
535             lock.readLock().release();
536          }
537       }
538       catch (Exception JavaDoc ex)
539       {
540          throw new RuntimeException JavaDoc(ex);
541       }
542    }
543
544    public void writeExternal(java.io.ObjectOutput JavaDoc out)
545       throws java.io.IOException JavaDoc
546    {
547       super.writeExternal(out);
548       out.writeLong(versionId);
549       out.writeObject(updates);
550       out.writeObject(classname);
551    }
552
553    public void readExternal(java.io.ObjectInput JavaDoc in)
554       throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
555    {
556       super.readExternal(in);
557       versionId = in.readLong();
558       this.updates = (HashSet JavaDoc)in.readObject();
559       this.classname = (String JavaDoc)in.readObject();
560       try
561       {
562          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
563          this.tm = (TransactionManager JavaDoc)ctx.lookup("java:/TransactionManager");
564       }
565       catch (Exception JavaDoc ex)
566       {
567          throw new RuntimeException JavaDoc(ex);
568       }
569       this.txState = new TransactionLocal();
570       this.txVersion = new TransactionLocal();
571       this.methodMap = setMethodMap;
572    }
573
574 }
575
Popular Tags