01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.controls.api.context;
20:
21: import java.util.Stack;
22:
23: /**
24: * The ControlThreadContext class manages the association between ControlContainerContexts
25: * and threads of execution. For a given thread of execution, the beginning and ending of
26: * contexts will always be nested (never interleaved), so each thread will maintain its own
27: * stack of currently executing contexts. This can be used to reassociate with the current
28: * active context.
29: */
30: public class ControlThreadContext {
31: /**
32: * This thread local maintains a per-thread stack of ControlContainerContext instances.
33: */
34: private static ThreadLocal<Stack<ControlContainerContext>> _threadContexts = new ThreadLocal<Stack<ControlContainerContext>>();
35:
36: /**
37: * Returns the active ControlContainerContext for the current thread, or null if no
38: * context is currently active.
39: * @return the current active ControlContainerContext
40: */
41: public static ControlContainerContext getContext() {
42: Stack<ControlContainerContext> contextStack = _threadContexts
43: .get();
44: if (contextStack == null || contextStack.size() == 0)
45: return null;
46:
47: return contextStack.peek();
48: }
49:
50: /**
51: * Defines the beginning of a new control container execution context.
52: */
53: public static void beginContext(ControlContainerContext context) {
54: Stack<ControlContainerContext> contextStack = _threadContexts
55: .get();
56: if (contextStack == null) {
57: contextStack = new Stack<ControlContainerContext>();
58: _threadContexts.set(contextStack);
59: }
60: contextStack.push(context);
61: }
62:
63: /**
64: * Ends the current control container execution context
65: * @throws IllegalStateException if there is not current active context or it is not
66: * the requested context.
67: */
68: public static void endContext(ControlContainerContext context) {
69: Stack<ControlContainerContext> contextStack = _threadContexts
70: .get();
71: if (contextStack == null || contextStack.size() == 0)
72: throw new IllegalStateException(
73: "No context started for current thread");
74:
75: if (contextStack.peek() != context)
76: throw new IllegalStateException(
77: "Context is not the current active context");
78:
79: contextStack.pop();
80: }
81: }
|