01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest.spring.bean;
05:
06: import java.io.Serializable;
07: import java.util.ArrayList;
08: import java.util.List;
09:
10: public class Singleton implements ISingleton, Serializable {
11: private transient String transientValue = "aaa";
12: private transient boolean transientBoolean = true;
13:
14: private int counter = 0;
15: private boolean sharedBoolean;
16:
17: private List recorder;
18: private List localRecorder = new ArrayList();
19: private transient List transientRecorder = new ArrayList();
20:
21: // InitializingBean
22: public void afterPropertiesSet() throws Exception {
23: final String msg = "afterPropertiesSet";
24: // System.err.println("### "+Thread.currentThread().getName()+" "+msg+" "+System.identityHashCode(this));
25:
26: record(recorder, msg);
27: record(localRecorder, msg);
28: record(transientRecorder, msg);
29: }
30:
31: // DisposableBean
32: public void destroy() throws Exception {
33: final String msg = "destroy";
34: // System.err.println("### "+Thread.currentThread().getName()+" "+msg+" "+System.identityHashCode(this));
35:
36: record(recorder, msg);
37: record(localRecorder, msg);
38: record(transientRecorder, msg);
39: }
40:
41: private void record(List r, String msg) {
42:
43: if (r != null) {
44: synchronized (r) {
45: r.add(Thread.currentThread().getName() + " " + msg);
46: }
47: }
48: }
49:
50: public void setRecorder(List recorder) {
51: this .recorder = recorder;
52: }
53:
54: public List getRecorder() {
55: return recorder;
56: }
57:
58: public List getLocalRecorder() {
59: return localRecorder;
60: }
61:
62: public List getTransientRecorder() {
63: return transientRecorder;
64: }
65:
66: // ISingleton
67: public synchronized int getCounter() {
68: return counter;
69: }
70:
71: public synchronized void incrementCounter() {
72: this .counter++;
73: }
74:
75: public String getTransientValue() {
76: return transientValue;
77: }
78:
79: public void setTransientValue(String transientValue) {
80: this .transientValue = transientValue;
81: }
82:
83: public synchronized void resetCounter() {
84: this .counter = 0;
85: }
86:
87: public String toString() {
88: return "Singleton:" + counter + " " + transientValue;
89: }
90:
91: public synchronized boolean toggleBoolean() {
92: return (sharedBoolean = !sharedBoolean);
93: }
94: }
|