KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > CurrentDatabase


1 /* CurrentDatabase.java
2 */

3
4 package org.ozoneDB;
5
6 import org.ozoneDB.data.SimpleArrayList;
7
8 /**
9     A method for maintaining an implicit reference to the current database
10     
11     @author <A HREF="http://www.medium.net/">Xuân Baldauf,Medium.net</A>
12 */

13 public class CurrentDatabase {
14     
15     protected static ThreadLocal JavaDoc instances = new ThreadLocal JavaDoc() {
16         protected Object JavaDoc initialValue() {
17             return new CurrentDatabase();
18         }
19     };
20     
21     protected SimpleArrayList stack;
22     
23     protected CurrentDatabase() {
24         stack = new SimpleArrayList(6);
25     }
26     
27     protected void addCurrentDatabase(OzoneInterface newCurrentDatabase) {
28         stack.push(newCurrentDatabase);
29     }
30     
31     protected void removeCurrentDatabase() {
32         Object JavaDoc result = stack.pop();
33         
34         assert result!=null;
35     }
36     
37     protected OzoneInterface getCurrentDatabase() {
38         OzoneInterface currentDatabase = (OzoneInterface) stack.peek();
39         
40         // Maybe we should asserts instead?
41
if (currentDatabase==null) {
42             throw new IllegalStateException JavaDoc("There is no current database defined for this thread "+Thread.currentThread()+".");
43         }
44         
45         return currentDatabase;
46     }
47     
48     /**
49         Registers a new current database.
50         Every call to this method must be matched by a call to {@link #unregister}.
51         Thus, the usage should be as following:
52         
53         <PRE>
54           CurrentDatabase.register(database);
55             
56             try {
57                 ... do something useful ...
58             } finally {
59                 CurrentDatabase.unregister();
60             }
61         </PRE>
62         
63         <DIV>
64             Registering current databases may be nested. If already a current database is registered,
65             registering a new one hides the old one until {@link #unregister} is called.
66         </DIV>
67
68         <DIV>
69             Registered current databases only apply to the current thread. Each thread has to register
70             its current database separately.
71         </DIV>
72     */

73     public static void register(OzoneInterface newCurrentDatabase) {
74         assert newCurrentDatabase!=null;
75         
76         ((CurrentDatabase) instances.get()).addCurrentDatabase(newCurrentDatabase);
77     }
78
79     /**
80         Unregisters the current database. The database which was current before the matching call to
81         {@link #register} will be the new current database.
82     */

83     public static void unregister() {
84         ((CurrentDatabase) instances.get()).removeCurrentDatabase();
85     }
86     
87     /**
88         Returns a reference to the current database.
89         
90         @return
91             a reference to the current database
92         @throws IllegalStateException
93             If no current database is set.
94     */

95     public static OzoneInterface get() throws IllegalStateException JavaDoc {
96         return ((CurrentDatabase) instances.get()).getCurrentDatabase();
97     }
98 }
99
100 // :indentSize=4:tabSize=4:noTabs=true:
101
Popular Tags