KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > cache > Wrapper


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  */

20 package org.enhydra.dods.cache;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Vector JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import org.enhydra.dods.DODS;
27 import com.lutris.logging.Logger;
28
29
30 /**
31  *
32  *
33  * @author Tanja Jovanovic
34  * @author Sinisa Milosevic
35  * @author Vladimir Puskas
36  * @version $Revision: 1.3 $
37  */

38 public final class Wrapper {
39
40     /**
41      *
42      */

43     private static Wrapper instance = null;
44     private long locked;
45     private long timeout;
46
47     /**
48      *
49      */

50     public static Wrapper getInstance() {
51         if (null == instance) {
52             instance = new Wrapper();
53         }
54         return instance;
55     }
56
57     /**
58      *
59      */

60     protected Vector JavaDoc vec;
61     protected HashMap JavaDoc nonVisibleSimpleComplexQCList;
62     protected HashMap JavaDoc nonVisibleMultiJoinQCList;
63
64     /**
65      * Constructor is private to ensure nobody instantiate this one.
66      */

67     private Wrapper() {
68         vec = new Vector JavaDoc();
69         nonVisibleSimpleComplexQCList = new HashMap JavaDoc();
70         nonVisibleMultiJoinQCList = new HashMap JavaDoc();
71         locked = 0;
72     }
73
74     /**
75      *
76      */

77     public void registerCache(DataStructCache dc) {
78         if (dc instanceof QueryCache) {
79             vec.add(dc);
80         }
81     }
82
83     /**
84      *
85      */

86     public void setTimeout(long to) {
87         timeout = to;
88     }
89
90     /**
91      *
92      */

93     public synchronized long lock() {
94         if (locked > 0 && locked < System.currentTimeMillis()) {
95             return 0;
96         }
97         for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
98             ((DataStructCache) e.nextElement()).lock();
99         }
100         locked = System.currentTimeMillis() + timeout;
101         return locked;
102     }
103
104     /**
105      *
106      */

107     public synchronized void unlock(long handle) {
108         if (handle != locked) {
109             DODS.getLogChannel().write(Logger.DEBUG,
110                     "Wrapper: wrong handle to unlock " + handle);
111             return;
112         }
113         for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
114             ((DataStructCache) e.nextElement()).unlock();
115         }
116         locked = 0;
117     }
118
119     /**
120      *
121      */

122     public void removeAllComplexQueries() {
123         for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
124             Object JavaDoc obj = e.nextElement();
125
126             if (obj instanceof QueryCache) {
127                 ((QueryCache) obj).getCacheAdministration(CacheConstants.COMPLEX_QUERY_CACHE).refresh();
128             }
129         }
130     }
131
132     /**
133      *
134      */

135     public void removeAllMultiJoinQueries() {
136         for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
137             Object JavaDoc obj = e.nextElement();
138
139             if (obj instanceof QueryCache) {
140                 ((QueryCache) obj).getCacheAdministration(CacheConstants.MULTI_JOIN_QUERY_CACHE).refresh();
141             }
142         }
143     }
144
145     /**
146      *
147      */

148     public void makeSimpleComplexQCachesInvisible(Vector JavaDoc vecQC) {
149         for (Enumeration JavaDoc e = vecQC.elements(); e.hasMoreElements();) {
150             Class JavaDoc doClass = (Class JavaDoc) e.nextElement();
151             Method JavaDoc mtd = null;
152             QueryCache qc = null;
153
154             try {
155                 mtd = doClass.getMethod("getConfigurationAdministration",
156                         new Class JavaDoc[] {});
157                 qc = (QueryCache) mtd.invoke(null, new Object JavaDoc[] {});
158             } catch (Throwable JavaDoc t) {
159                 DODS.getLogChannel().write(Logger.DEBUG,
160                         "Wrapper: couldn't get QueryCache for " + doClass);
161                 continue;
162             }
163             Integer JavaDoc intObj = (Integer JavaDoc) nonVisibleSimpleComplexQCList.get(qc);
164             int num;
165
166             if (intObj != null) {
167                 num = intObj.intValue();
168                 num++;
169                 nonVisibleSimpleComplexQCList.put(qc, new Integer JavaDoc(num));
170             } else {
171                 nonVisibleSimpleComplexQCList.put(qc, new Integer JavaDoc(1));
172                 qc.lockSimpleComplexQCache();
173             }
174         }
175     }
176
177     /**
178      *
179      */

180     public void makeSimpleComplexQCachesVisible(Vector JavaDoc vecQC) {
181         for (Enumeration JavaDoc e = vecQC.elements(); e.hasMoreElements();) {
182             Class JavaDoc doClass = (Class JavaDoc) e.nextElement();
183             Method JavaDoc mtd = null;
184             QueryCache qc = null;
185
186             try {
187                 mtd = doClass.getMethod("getConfigurationAdministration",
188                         new Class JavaDoc[] {});
189                 qc = (QueryCache) mtd.invoke(null, new Object JavaDoc[] {});
190             } catch (Throwable JavaDoc t) {
191                 DODS.getLogChannel().write(Logger.DEBUG,
192                         "Wrapper: couldn't get QueryCache for " + doClass);
193                 continue;
194             }
195             Integer JavaDoc intObj = (Integer JavaDoc) nonVisibleSimpleComplexQCList.get(qc);
196             int num;
197
198             if (intObj != null) {
199                 num = intObj.intValue();
200                 num--;
201                 if (num == 0) {
202                     nonVisibleSimpleComplexQCList.remove(qc);
203                     qc.unlockSimpleComplexQCache();
204                 } else {
205                     nonVisibleSimpleComplexQCList.put(qc, new Integer JavaDoc(num));
206                 }
207             }
208         }
209     }
210     public void makeMultiJoinQCachesInvisible() {
211         for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
212             Object JavaDoc obj = e.nextElement();
213
214             if (obj instanceof QueryCache) {
215                QueryCache qc = (QueryCache)obj;
216                Integer JavaDoc intObj = (Integer JavaDoc) nonVisibleMultiJoinQCList.get(qc);
217                int num;
218
219                    if (intObj != null) {
220                        num = intObj.intValue();
221                        num++;
222                        nonVisibleMultiJoinQCList.put(qc, new Integer JavaDoc(num));
223                    } else {
224                        nonVisibleMultiJoinQCList.put(qc, new Integer JavaDoc(1));
225                        qc.lockMultiJoinQCache();
226                    }
227                 }
228         }
229     }
230     public void makeMultiJoinQCachesVisible() {
231         for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
232             Object JavaDoc obj = e.nextElement();
233             
234             if (obj instanceof QueryCache) {
235                QueryCache qc = (QueryCache)obj;
236    
237                Integer JavaDoc intObj = (Integer JavaDoc) nonVisibleSimpleComplexQCList.get(qc);
238                int num;
239
240                if (intObj != null) {
241                   num = intObj.intValue();
242                   num--;
243                   if (num == 0) {
244                      nonVisibleMultiJoinQCList.remove(qc);
245                      qc.unlockMultiJoinQCache();
246                   }
247                   else {
248                      nonVisibleMultiJoinQCList.put(qc, new Integer JavaDoc(num));
249                   }
250                }
251             }
252         }
253     }
254 }
255
Popular Tags