01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal.util;
16:
17: /**
18: * Logic for handling one shot semantics for classes; classes that include a method (or methods)
19: * that "locks down" the instance, to prevent it from being used again in the future.
20: */
21: public class OneShotLock {
22: private boolean _lock;
23:
24: /**
25: * Checks to see if the lock has been set (via {@link #lock()}).
26: *
27: * @throws IllegalStateException
28: * if the lock is set
29: */
30: public synchronized void check() {
31: innerCheck();
32: }
33:
34: private void innerCheck() {
35: if (_lock) {
36: // [0] is getStackTrace()
37: // [1] is innerCheck()
38: // [2] is check() or lock()
39: // [3] is caller of check() or lock()
40:
41: StackTraceElement element = Thread.currentThread()
42: .getStackTrace()[4];
43:
44: throw new IllegalStateException(UtilMessages
45: .oneShotLock(element));
46: }
47: }
48:
49: /** Checks the lock, then sets it. */
50: public synchronized void lock() {
51: innerCheck();
52:
53: _lock = true;
54: }
55: }
|