001: /*
002:
003: Derby - Class org.apache.derby.impl.services.locks.D_LockControl
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.locks;
023:
024: import org.apache.derby.iapi.services.diag.Diagnosticable;
025: import org.apache.derby.iapi.services.diag.DiagnosticUtil;
026: import org.apache.derby.iapi.error.StandardException;
027: import org.apache.derby.iapi.services.sanity.SanityManager;
028: import org.apache.derby.iapi.services.locks.Lockable;
029:
030: import java.util.Properties;
031: import java.util.List;
032: import java.util.Iterator;
033:
034: /**
035: **/
036:
037: public class D_LockControl implements Diagnosticable {
038: protected LockControl control;
039:
040: public D_LockControl() {
041: }
042:
043: /* Private/Protected methods of This class: */
044:
045: /*
046: ** Methods of Diagnosticable
047: */
048: public void init(Object obj) {
049: control = (LockControl) obj;
050: }
051:
052: /**
053: *
054: * @exception StandardException Standard exception policy.
055: **/
056: public String diag() throws StandardException {
057: StringBuffer sb = new StringBuffer(1024);
058:
059: sb.append("LockControl:\n granted list: ");
060:
061: int i = 0;
062:
063: Object firstGrant = control.getFirstGrant();
064: if (firstGrant != null) {
065: sb.append("\n g[" + i + "]:"
066: + DiagnosticUtil.toDiagString(firstGrant));
067: i++;
068: }
069:
070: List granted = control.getGranted();
071:
072: if (granted != null) {
073: for (Iterator dli = granted.iterator(); dli.hasNext();) {
074: sb.append("\n g[" + i + "]:"
075: + DiagnosticUtil.toDiagString(dli.next()));
076: i++;
077: }
078: }
079:
080: sb.append("\n waiting list:");
081:
082: List waiting = control.getWaiting();
083:
084: int num_waiting = 0;
085:
086: if (waiting != null) {
087: for (Iterator dli = waiting.iterator(); dli.hasNext();) {
088: sb.append("\n w[" + num_waiting + "]:"
089: + DiagnosticUtil.toDiagString(dli.next()));
090:
091: num_waiting++;
092: }
093: }
094:
095: if (num_waiting == 0)
096: sb.append(" no waiting locks.");
097:
098: return sb.toString();
099: }
100:
101: public void diag_detail(Properties prop) {
102: }
103:
104: /*
105: ** Static routines that were in SinglePool
106: */
107:
108: /*
109: ** Debugging routines
110: */
111:
112: static void debugLock(String type, Object compatabilitySpace,
113: Object group, Lockable ref, Object qualifier, int timeout) {
114:
115: if (SanityManager.DEBUG) {
116:
117: SanityManager.DEBUG(Constants.LOCK_TRACE, type
118: + debugLockString(compatabilitySpace, group, ref,
119: qualifier, timeout));
120: }
121: }
122:
123: static void debugLock(String type, Object compatabilitySpace,
124: Object group) {
125:
126: if (SanityManager.DEBUG) {
127:
128: SanityManager.DEBUG(Constants.LOCK_TRACE, type
129: + debugLockString(compatabilitySpace, group));
130: }
131: }
132:
133: static void debugLock(String type, Object compatabilitySpace,
134: Object group, Lockable ref) {
135:
136: if (SanityManager.DEBUG) {
137:
138: SanityManager.DEBUG(Constants.LOCK_TRACE, type
139: + debugLockString(compatabilitySpace, group, ref));
140: }
141: }
142:
143: static String debugLockString(Object compatabilitySpace,
144: Object group) {
145:
146: if (SanityManager.DEBUG) {
147:
148: StringBuffer sb = new StringBuffer("");
149:
150: debugAppendObject(sb, " CompatabilitySpace=",
151: compatabilitySpace);
152: debugAppendObject(sb, " Group=", group);
153:
154: debugAddThreadInfo(sb);
155:
156: return sb.toString();
157:
158: } else {
159: return null;
160: }
161: }
162:
163: static String debugLockString(Object compatabilitySpace,
164: Object group, Lockable ref) {
165:
166: if (SanityManager.DEBUG) {
167:
168: StringBuffer sb = new StringBuffer("");
169:
170: debugAppendObject(sb, " Lockable ", ref);
171: debugAppendObject(sb, " CompatabilitySpace=",
172: compatabilitySpace);
173: debugAppendObject(sb, " Group=", group);
174:
175: debugAddThreadInfo(sb);
176:
177: return sb.toString();
178:
179: } else {
180: return null;
181: }
182: }
183:
184: static String debugLockString(Object compatabilitySpace,
185: Object group, Lockable ref, Object qualifier, int timeout) {
186:
187: if (SanityManager.DEBUG) {
188:
189: StringBuffer sb = new StringBuffer("");
190:
191: debugAppendObject(sb, " Lockable ", ref);
192: debugAppendObject(sb, " Qualifier=", qualifier);
193: debugAppendObject(sb, " CompatabilitySpace=",
194: compatabilitySpace);
195: debugAppendObject(sb, " Group=", group);
196:
197: if (timeout >= 0) {
198: sb.append(" Timeout(ms)=");
199: sb.append(timeout);
200: }
201:
202: debugAddThreadInfo(sb);
203:
204: return sb.toString();
205:
206: } else {
207: return null;
208: }
209: }
210:
211: static void debugAddThreadInfo(StringBuffer sb) {
212:
213: if (SanityManager.DEBUG) {
214: if (SanityManager
215: .DEBUG_ON(Constants.LOCK_TRACE_ADD_THREAD_INFO)) {
216: debugAppendObject(sb, " Thread=", Thread
217: .currentThread());
218: }
219: }
220: }
221:
222: static void debugAppendObject(StringBuffer sb, String desc,
223: Object item) {
224: if (SanityManager.DEBUG) {
225:
226: sb.append(desc);
227:
228: if (item != null)
229: sb.append(item.toString());
230: else
231: sb.append("<null>");
232: }
233: }
234: }
|