KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > locks > D_LockControl


1 /*
2
3    Derby - Class org.apache.derby.impl.services.locks.D_LockControl
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.locks;
23
24 import org.apache.derby.iapi.services.diag.Diagnosticable;
25 import org.apache.derby.iapi.services.diag.DiagnosticUtil;
26 import org.apache.derby.iapi.error.StandardException;
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28 import org.apache.derby.iapi.services.locks.Lockable;
29
30 import java.util.Properties JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35 **/

36
37 public class D_LockControl implements Diagnosticable
38 {
39     protected LockControl control;
40
41     public D_LockControl()
42     {
43     }
44
45     /* Private/Protected methods of This class: */
46
47     /*
48     ** Methods of Diagnosticable
49     */

50     public void init(Object JavaDoc obj)
51     {
52         control = (LockControl) obj;
53     }
54
55     /**
56      *
57      * @exception StandardException Standard exception policy.
58      **/

59     public String JavaDoc diag()
60         throws StandardException
61     {
62         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(1024);
63
64         sb.append("LockControl:\n granted list: ");
65
66         int i = 0;
67
68         Object JavaDoc firstGrant = control.getFirstGrant();
69         if (firstGrant != null) {
70                 sb.append("\n g[" + i + "]:" + DiagnosticUtil.toDiagString(firstGrant));
71                 i++;
72             }
73
74         List JavaDoc granted = control.getGranted();
75         
76         if (granted != null) {
77             for (Iterator JavaDoc dli = granted.iterator(); dli.hasNext(); )
78             {
79                 sb.append("\n g[" + i + "]:" + DiagnosticUtil.toDiagString(dli.next()));
80                 i++;
81             }
82         }
83
84
85         sb.append("\n waiting list:");
86
87         List JavaDoc waiting = control.getWaiting();
88
89         int num_waiting = 0;
90
91         if (waiting != null)
92         {
93             for (Iterator JavaDoc dli = waiting.iterator(); dli.hasNext(); )
94             {
95                 sb.append(
96                     "\n w[" + num_waiting + "]:" +
97                     DiagnosticUtil.toDiagString(dli.next()));
98
99                 num_waiting++;
100             }
101         }
102
103         if (num_waiting == 0)
104             sb.append(" no waiting locks.");
105  
106         return sb.toString();
107     }
108     public void diag_detail(Properties JavaDoc prop) {}
109
110     /*
111     ** Static routines that were in SinglePool
112     */

113
114     
115     /*
116     ** Debugging routines
117     */

118
119     static void debugLock(String JavaDoc type, Object JavaDoc compatabilitySpace, Object JavaDoc group, Lockable ref, Object JavaDoc qualifier, int timeout) {
120
121         if (SanityManager.DEBUG) {
122
123             SanityManager.DEBUG(Constants.LOCK_TRACE, type +
124                 debugLockString(
125                     compatabilitySpace, group, ref, qualifier, timeout));
126         }
127     }
128     static void debugLock(String JavaDoc type, Object JavaDoc compatabilitySpace, Object JavaDoc group) {
129
130         if (SanityManager.DEBUG) {
131
132             SanityManager.DEBUG(Constants.LOCK_TRACE, type +
133                     debugLockString(compatabilitySpace, group));
134         }
135     }
136     static void debugLock(String JavaDoc type, Object JavaDoc compatabilitySpace, Object JavaDoc group, Lockable ref) {
137
138         if (SanityManager.DEBUG) {
139
140             SanityManager.DEBUG(Constants.LOCK_TRACE, type +
141                     debugLockString(compatabilitySpace, group, ref));
142         }
143     }
144
145
146     static String JavaDoc debugLockString(Object JavaDoc compatabilitySpace, Object JavaDoc group) {
147
148         if (SanityManager.DEBUG) {
149
150             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
151
152             debugAppendObject(sb, " CompatabilitySpace=", compatabilitySpace);
153             debugAppendObject(sb, " Group=", group);
154
155             debugAddThreadInfo(sb);
156
157             return sb.toString();
158
159         } else {
160             return null;
161         }
162     }
163
164     static String JavaDoc debugLockString(Object JavaDoc compatabilitySpace, Object JavaDoc group, Lockable ref) {
165
166         if (SanityManager.DEBUG) {
167
168             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
169
170             debugAppendObject(sb, " Lockable ", ref);
171             debugAppendObject(sb, " CompatabilitySpace=", compatabilitySpace);
172             debugAppendObject(sb, " Group=", group);
173
174             debugAddThreadInfo(sb);
175
176             return sb.toString();
177
178         } else {
179             return null;
180         }
181     }
182
183
184     static String JavaDoc debugLockString(Object JavaDoc compatabilitySpace, Object JavaDoc group, Lockable ref, Object JavaDoc qualifier, int timeout) {
185
186         if (SanityManager.DEBUG) {
187
188             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
189
190             debugAppendObject(sb, " Lockable ", ref);
191             debugAppendObject(sb, " Qualifier=", qualifier);
192             debugAppendObject(sb, " CompatabilitySpace=", compatabilitySpace);
193             debugAppendObject(sb, " Group=", group);
194
195             if (timeout >= 0) {
196                 sb.append(" Timeout(ms)=");
197                 sb.append(timeout);
198             }
199
200             debugAddThreadInfo(sb);
201
202
203             return sb.toString();
204
205         } else {
206             return null;
207         }
208     }
209
210     static void debugAddThreadInfo(StringBuffer JavaDoc sb) {
211
212         if (SanityManager.DEBUG) {
213             if (SanityManager.DEBUG_ON(Constants.LOCK_TRACE_ADD_THREAD_INFO)) {
214                 debugAppendObject(sb, " Thread=", Thread.currentThread());
215             }
216         }
217     }
218
219     static void debugAppendObject(StringBuffer JavaDoc sb, String JavaDoc desc, Object JavaDoc item) {
220         if (SanityManager.DEBUG) {
221
222             sb.append(desc);
223
224             if (item != null)
225                 sb.append(item.toString());
226             else
227                 sb.append("<null>");
228         }
229     }
230 }
231
232
Popular Tags