KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.beehive.controls.api.context.ResourceContext;
21
22 import java.beans.beancontext.BeanContextServiceProvider JavaDoc;
23 import java.beans.beancontext.BeanContextServices JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * The ResourceContextImpl class provides an implementation of the ResourceContext service,
30  * as well as a simple singleton provider that can be used to obtain new instances.
31  */

32 public class ResourceContextImpl implements ResourceContext, InvokeListener
33 {
34     /**
35      * The ResourceContextProvider inner class acts as a single BeanContext service
36      * provider for the ResourceContext service class.
37      */

38     private static class ResourceContextProvider implements BeanContextServiceProvider JavaDoc
39     {
40         //
41
// BeanContextServiceProvider.getService()
42
//
43
public Object JavaDoc getService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Class JavaDoc serviceClass,
44                                  Object JavaDoc serviceSelector)
45         {
46             //
47
// There is an implied contract between ControlContainerContext and ControlBean
48
// classes required to implement the resource management contract. This cannot
49
// be supported for any generic BeanContextChild class.
50
//
51
if (requestor instanceof ControlBean)
52             {
53                 return new ResourceContextImpl((ControlContainerContext)bcs,
54                                                (ControlBean)requestor);
55             }
56
57             return null;
58         }
59
60         //
61
// BeanContextServiceProvider.releaseService()
62
//
63
public void releaseService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Object JavaDoc service)
64         {
65             return; // Should not happen, service is never unregistered
66
}
67
68         //
69
// BeanContextServiceProvider.getContextServiceSelectors()
70
//
71
public Iterator JavaDoc getCurrentServiceSelectors(BeanContextServices JavaDoc bcs, Class JavaDoc serviceClass)
72         {
73             return null; // no selectors
74
}
75     }
76
77     /**
78      * A singleton instance of the ResourceContextProvider class is what will be registered
79      * on all ControlContainerContext instances. The provider can be a singleton because it is
80      * completely stateless and thread-safe.
81      */

82     static private ResourceContextProvider _theProvider = new ResourceContextProvider();
83
84     /**
85      * Returns the ResourceContextProvider used to create new ResourceContext instances
86      */

87     static /* package */ ResourceContextProvider getProvider() { return _theProvider; }
88
89     /**
90      * Constructs a new ResourceContext service implementation to manage resources for
91      * a target ControlBean within a specific ControlContainerContext
92      */

93     public ResourceContextImpl(ControlContainerContext containerContext, ControlBean bean)
94     {
95         _containerContext = containerContext;
96         _bean = bean;
97
98         //
99
// Register to receive invocation notifications from the target bean
100
//
101
_bean.addInvokeListener(this);
102     }
103
104     /**
105      * Implements the InvokeListener.preInvoke method. This hook will be called before the
106      * managed beans' operations are invoked
107      */

108     public void preInvoke(Method JavaDoc m, Object JavaDoc [] args)
109     {
110         if (!_hasAcquired)
111             acquire();
112     }
113
114     /**
115      * Implements the InvokeListener.postInvoke method.
116      */

117     public void postInvoke(Object JavaDoc retval, Throwable JavaDoc t) {};
118
119     // ResourceContext.acquire()
120
public void acquire()
121     {
122         if (_hasAcquired)
123             return;
124
125         // Deliver the onAcquire event to registered listeners
126
for (ResourceEvents resourceListener : _listeners)
127             resourceListener.onAcquire();
128
129         // Register this ResourceContext with associated container context
130
_containerContext.addResourceContext(this, _bean);
131
132         // Set the flag to indicate resources have been acquired.
133
_hasAcquired = true;
134     };
135
136     // ResourceContext.release()
137
public void release()
138     {
139         if (!_hasAcquired)
140             return;
141
142         // Deliver the onRelease event to the registered listeners
143
for (ResourceEvents resourceListener : _listeners)
144             resourceListener.onRelease();
145
146         // Unregister this ResourceContext with associated container context
147
_containerContext.removeResourceContext(this, _bean);
148
149         // Reset the flag to indicate resources have been released.
150
_hasAcquired = false;
151     };
152
153     // ResourceContext.hasResources()
154
public boolean hasResources() { return _hasAcquired; }
155
156     // ResourceContext.addResourceEventsListener
157
public void addResourceEventsListener(ResourceEvents resourceListener)
158     {
159         _listeners.add(resourceListener);
160     }
161
162     // ResourceContext.removeResourceEventsListener
163
public void removeResourceEventsListener(ResourceEvents resourceListener)
164     {
165         _listeners.remove(resourceListener);
166     }
167
168     private Vector JavaDoc<ResourceEvents> _listeners = new Vector JavaDoc<ResourceEvents>();
169     private boolean _hasAcquired = false;
170     private ControlContainerContext _containerContext;
171     private ControlBean _bean;
172 }
173
Popular Tags