KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > bean > Singleton


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.spring.bean;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 public class Singleton implements ISingleton, Serializable JavaDoc {
11   private transient String JavaDoc transientValue = "aaa";
12   private transient boolean transientBoolean = true;
13
14   private int counter = 0;
15   private boolean sharedBoolean;
16   
17   private List JavaDoc recorder;
18   private List JavaDoc localRecorder = new ArrayList JavaDoc();
19   private transient List JavaDoc transientRecorder = new ArrayList JavaDoc();
20  
21   // InitializingBean
22
public void afterPropertiesSet() throws Exception JavaDoc {
23     final String JavaDoc 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 JavaDoc {
33     final String JavaDoc 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 JavaDoc r, String JavaDoc 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 JavaDoc recorder) {
51     this.recorder = recorder;
52   }
53
54   public List JavaDoc getRecorder() {
55     return recorder;
56   }
57
58   public List JavaDoc getLocalRecorder() {
59     return localRecorder;
60   }
61
62   public List JavaDoc 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 JavaDoc getTransientValue() {
76     return transientValue;
77   }
78
79   public void setTransientValue(String JavaDoc transientValue) {
80     this.transientValue = transientValue;
81   }
82   
83   
84   public String JavaDoc toString() {
85     return "Singleton:" + counter + " " + transientValue;
86   }
87
88   public synchronized boolean toggleBoolean() {
89     return (sharedBoolean = !sharedBoolean);
90   }
91 }
92
Popular Tags