KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > latch > LatchSupport


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: LatchSupport.java,v 1.9 2006/10/30 21:14:20 bostic Exp $
7  */

8
9 package com.sleepycat.je.latch;
10
11 import com.sleepycat.je.dbi.EnvironmentImpl;
12
13 /**
14  * Various constructs to support Latches. Switch hitting for 1.4 vs Java 5
15  * JVM latch implementation (i.e. our's vs the JVM's), and assert-based
16  * latch counting code.
17  */

18 public class LatchSupport {
19
20     private static String JavaDoc DISABLE_JAVA5_LATCHES = "je.disable.java5.latches";
21
22     private static Class JavaDoc JAVA5_LATCH_CLASS = null;
23
24     private static Class JavaDoc JAVA5_SHARED_LATCH_CLASS = null;
25
26     static {
27     try {
28         if (System.getProperty(DISABLE_JAVA5_LATCHES) == null) {
29         Class.forName("java.util.concurrent.locks.ReentrantLock");
30         JAVA5_LATCH_CLASS = Class.forName
31             ("com.sleepycat.je.latch.Java5LatchImpl");
32         }
33     } catch (ClassNotFoundException JavaDoc CNFE) {
34     }
35     }
36
37     static {
38     try {
39         if (System.getProperty(DISABLE_JAVA5_LATCHES) == null) {
40         Class.forName
41             ("java.util.concurrent.locks.ReentrantReadWriteLock");
42         JAVA5_SHARED_LATCH_CLASS = Class.forName
43             ("com.sleepycat.je.latch.Java5SharedLatchImpl");
44         }
45     } catch (ClassNotFoundException JavaDoc CNFE) {
46     }
47     }
48
49     public static Class JavaDoc getJava5LatchClass() {
50     return JAVA5_LATCH_CLASS;
51     }
52
53     public static Latch makeLatch(String JavaDoc name, EnvironmentImpl env) {
54     if (JAVA5_LATCH_CLASS == null) {
55         return new LatchImpl(name, env);
56     } else {
57         try {
58         Latch ret = (Latch) JAVA5_LATCH_CLASS.newInstance();
59         ret.setName(name);
60         return ret;
61         } catch (InstantiationException JavaDoc IE) {
62         } catch (IllegalAccessException JavaDoc IAE) {
63         }
64
65         /* Something bad happened. Revert back to our 1.4 latches. */
66         JAVA5_LATCH_CLASS = null;
67         return new LatchImpl(name, env);
68     }
69     }
70
71     public static Latch makeLatch(EnvironmentImpl env) {
72     if (JAVA5_LATCH_CLASS == null) {
73         return new LatchImpl(env);
74     } else {
75         try {
76         return (Latch) JAVA5_LATCH_CLASS.newInstance();
77         } catch (InstantiationException JavaDoc IE) {
78         } catch (IllegalAccessException JavaDoc IAE) {
79         }
80
81         /* Something bad happened. Revert back to our 1.4 latches. */
82         JAVA5_LATCH_CLASS = null;
83         return new LatchImpl(env);
84     }
85     }
86
87     public static SharedLatch makeSharedLatch(String JavaDoc name,
88                           EnvironmentImpl env) {
89     if (JAVA5_SHARED_LATCH_CLASS == null) {
90         return new SharedLatchImpl(name, env);
91     } else {
92         try {
93         SharedLatch ret = (SharedLatch)
94             JAVA5_SHARED_LATCH_CLASS.newInstance();
95         ret.setName(name);
96         return ret;
97         } catch (InstantiationException JavaDoc IE) {
98         } catch (IllegalAccessException JavaDoc IAE) {
99         }
100
101         /* Something bad happened. Revert back to our 1.4 latches. */
102         JAVA5_SHARED_LATCH_CLASS = null;
103         return new SharedLatchImpl(name, env);
104     }
105     }
106
107     /* Used for debugging */
108     static LatchTable latchTable = new LatchTable("LatchImpl");
109
110     /**
111      * Only call under the assert system. This records and counts held latches.
112      */

113     static public int countLatchesHeld() {
114
115         return latchTable.countLatchesHeld();
116     }
117
118     static public void dumpLatchesHeld() {
119
120         System.out.println(latchesHeldToString());
121     }
122
123     static public String JavaDoc latchesHeldToString() {
124
125         return latchTable.latchesHeldToString();
126     }
127
128     static public void clearNotes() {
129
130         latchTable.clearNotes();
131     }
132 }
133
Popular Tags