KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > api > context > ControlThreadContext


1 package org.apache.beehive.controls.api.context;
2 /*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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
20 import java.util.Stack JavaDoc;
21
22 /**
23  * The ControlThreadContext class manages the association between ControlContainerContexts
24  * and threads of execution. For a given thread of execution, the beginning and ending of
25  * contexts will always be nested (never interleaved), so each thread will maintain its own
26  * stack of currently executing contexts. This can be used to reassociate with the current
27  * active context.
28  */

29 public class ControlThreadContext
30 {
31     /**
32      * This thread local maintains a per-thread stack of ControlContainerContext instances.
33      */

34     static private ThreadLocal JavaDoc<Stack JavaDoc<ControlContainerContext>> _threadContexts =
35                                             new ThreadLocal JavaDoc<Stack JavaDoc<ControlContainerContext>>();
36
37     /**
38      * Returns the active ControlContainerContext for the current thread, or null if no
39      * context is currently active.
40      * @return the current active ControlContainerContext
41      */

42     public static ControlContainerContext getContext()
43     {
44         Stack JavaDoc<ControlContainerContext> contextStack = _threadContexts.get();
45         if (contextStack == null || contextStack.size() == 0)
46             return null;
47
48         return contextStack.peek();
49     }
50
51     /**
52      * Defines the beginning of a new control container execution context.
53      */

54     public static void beginContext(ControlContainerContext context)
55     {
56         Stack JavaDoc<ControlContainerContext> contextStack = _threadContexts.get();
57         if (contextStack == null)
58         {
59             contextStack = new Stack JavaDoc<ControlContainerContext>();
60             _threadContexts.set(contextStack);
61         }
62         contextStack.push(context);
63     }
64
65     /**
66      * Ends the current control container execution context
67      * @throws IllegalStateExeption if there is not current active context or it is not
68      * the requested context.
69      */

70     public static void endContext(ControlContainerContext context)
71     {
72         Stack JavaDoc<ControlContainerContext> contextStack = _threadContexts.get();
73         if (contextStack == null || contextStack.size() == 0)
74             throw new IllegalStateException JavaDoc("No context started for current thread");
75
76         if (contextStack.peek() != context)
77             throw new IllegalStateException JavaDoc("Context is not the current active context");
78
79         contextStack.pop();
80     }
81 }
82
Popular Tags