KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > bean > ControlContainerContext


1 package org.apache.beehive.controls.runtime.bean;
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.lang.reflect.InvocationTargetException JavaDoc;
21 import java.util.Stack JavaDoc;
22
23 import org.apache.beehive.controls.api.context.ControlHandle;
24 import org.apache.beehive.controls.api.context.ControlThreadContext;
25 import org.apache.beehive.controls.api.context.ResourceContext;
26 import org.apache.beehive.controls.api.events.EventDispatcher;
27 import org.apache.beehive.controls.api.events.EventRef;
28
29 /**
30  * The ControlContainerContext class provides a base class implementation for external containers
31  * of ControlBeans. It provides additional services, such as:
32  *
33  * - defines a contextual service provider for the ResourceManager interface
34  * - defines a simplified contract for the external container to interact with resource
35  * management (beginContext/endContext)
36  */

37 public class ControlContainerContext
38        extends ControlBeanContext
39        implements EventDispatcher, org.apache.beehive.controls.api.context.ControlContainerContext
40 {
41     public ControlContainerContext()
42     {
43         super(null);
44     }
45
46     /**
47      * Defines the beginning of a new control container execution context.
48      */

49     public void beginContext()
50     {
51         ControlThreadContext.beginContext(this);
52     }
53
54     /**
55      * Ends the current control container execution context
56      */

57     public void endContext()
58     {
59         try
60         {
61             //
62
// Release all resources associated with the current execution context.
63
//
64
releaseResources();
65         }
66         finally
67         {
68             ControlThreadContext.endContext(this);
69         }
70     }
71
72     /**
73      * Called by BeanContextSupport superclass during construction and deserialization to
74      * initialize subclass transient state
75      */

76     public void initialize()
77     {
78         super.initialize();
79
80         //
81
// Register the ResourceContext provider on all new ControlContainerContext instances.
82
//
83
addService(org.apache.beehive.controls.api.context.ResourceContext.class,
84                    ResourceContextImpl.getProvider());
85     }
86
87     /**
88      * Adds a new managed ResourceContext to the ControlContainerContext. This method
89      * is used to register a resource context that has just acquired resources
90      * @param resourceContext the ResourceContext service that has acquired resources
91      * @param bean the acquiring ControlBean. Unused by the base implementation, but
92      * available so subclassed containers can have access to the bean.
93      */

94     protected synchronized void addResourceContext(ResourceContext resourceContext,
95                                                    ControlBean bean)
96     {
97         if (!resourceContext.hasResources())
98             _resourceContexts.push(resourceContext);
99     }
100
101     /**
102      * Removes a managed ResourceContext from the ControlContainerContext. This method
103      * is used to unregister a resource context that has already acquired resources
104      * @param resourceContext the ResourceContext service to be removed
105      * @param bean the acquiring ControlBean. Unused by the base implementation, but
106      * available so subclassed containers can have access to the bean.
107      */

108     protected synchronized void removeResourceContext(ResourceContext resourceContext,
109                                                       ControlBean bean)
110     {
111         //
112
// Ignore removal requests received within the context of global cleanup. The
113
// stack is already being popped, so these are just requests for resources that
114
// already have in-flight removal taking place.
115
//
116
if (!_releasingAll && resourceContext.hasResources())
117             _resourceContexts.remove(resourceContext);
118     }
119
120     /**
121      * Releases all ResourceContexts associated with the current ControlContainerContext.
122      * This method is called by the associated container whenever all managed ResourceContexts
123      * that have acquired resources should release them.
124      */

125     protected synchronized void releaseResources()
126     {
127         // Set the local flag indicating global resource release is occuring
128
_releasingAll = true;
129
130         //
131
// Iterate through the list of acquired ResourceContexts and release them
132
//
133
while (!_resourceContexts.empty())
134         {
135             ResourceContext resourceContext = _resourceContexts.pop();
136             resourceContext.release();
137         }
138
139         // Clear the local flag indicating global resource release is occuring
140
_releasingAll = false;
141     }
142
143     /**
144      * Dispatch an operation or an event to a bean within this container bean context.
145      * @param handle the control handle identifying the target bean
146      * @param event the event to be invoked on the target bean
147      * @param args the arguments to be passed to the target method invocation
148      */

149     public Object JavaDoc dispatchEvent(ControlHandle handle, EventRef event, Object JavaDoc [] args)
150             throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
151     {
152         ControlBean bean = getBean(handle.getControlID());
153         if (bean == null)
154             throw new IllegalArgumentException JavaDoc("Invalid bean ID: " + handle.getControlID());
155
156         return bean.dispatchEvent(event, args);
157     }
158
159     /**
160      * Returns a ControlHandle to the component containing the control. This handle can be
161      * used to dispatch events and operations to a control instance. This method will return
162      * null if the containing component does not support direct dispatch.
163      *
164      * @param bean the target control bean
165      */

166     public ControlHandle getControlHandle(org.apache.beehive.controls.api.bean.ControlBean bean)
167     {
168         //
169
// The base implementation doesn't support dispatch. Containers should override
170
// and return a valid service handle that does component-specific dispatch.
171
//
172
return null;
173     }
174
175     /**
176      * Returns true if this container guarantees single-threaded behaviour. By default, top-level
177      * containers are assumed to NOT guarantee this; specific container implementations (for example,
178      * for EJB containers) should override this appropriately.
179      */

180     public boolean isSingleThreadedContainer()
181     {
182         return false;
183     }
184
185     boolean _releasingAll;
186     Stack JavaDoc<ResourceContext> _resourceContexts = new Stack JavaDoc<ResourceContext>();
187 }
188
Popular Tags