001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.runtime.bean;
020:
021: import java.beans.beancontext.BeanContextServiceProvider;
022: import java.beans.beancontext.BeanContextServices;
023: import java.lang.reflect.Method;
024: import java.util.Iterator;
025: import java.util.Vector;
026:
027: import org.apache.beehive.controls.api.context.ResourceContext;
028:
029: /**
030: * The ResourceContextImpl class provides an implementation of the ResourceContext service,
031: * as well as a simple singleton provider that can be used to obtain new instances.
032: */
033: public class ResourceContextImpl implements ResourceContext,
034: InvokeListener {
035: /**
036: * The ResourceContextProvider inner class acts as a single BeanContext service
037: * provider for the ResourceContext service class.
038: */
039: private static class ResourceContextProvider implements
040: BeanContextServiceProvider {
041:
042: //
043: // BeanContextServiceProvider.getService()
044: //
045: public Object getService(BeanContextServices bcs,
046: Object requestor, Class serviceClass,
047: Object serviceSelector) {
048: //
049: // There is an implied contract between ControlContainerContext and ControlBean
050: // classes required to implement the resource management contract. This cannot
051: // be supported for any generic BeanContextChild class.
052: //
053: if (requestor instanceof ControlBean) {
054: return new ResourceContextImpl(
055: (ControlContainerContext) bcs,
056: (ControlBean) requestor);
057: }
058:
059: return null;
060: }
061:
062: //
063: // BeanContextServiceProvider.releaseService()
064: //
065: public void releaseService(BeanContextServices bcs,
066: Object requestor, Object service) {
067: return; // Should not happen, service is never unregistered
068: }
069:
070: //
071: // BeanContextServiceProvider.getContextServiceSelectors()
072: //
073: public Iterator getCurrentServiceSelectors(
074: BeanContextServices bcs, Class serviceClass) {
075: return null; // no selectors
076: }
077: }
078:
079: /**
080: * A singleton instance of the ResourceContextProvider class is what will be registered
081: * on all ControlContainerContext instances. The provider can be a singleton because it is
082: * completely stateless and thread-safe.
083: */
084: static private ResourceContextProvider _theProvider = new ResourceContextProvider();
085:
086: /**
087: * Returns the ResourceContextProvider used to create new ResourceContext instances
088: */
089: static/* package */ResourceContextProvider getProvider() {
090: return _theProvider;
091: }
092:
093: /**
094: * Constructs a new ResourceContext service implementation to manage resources for
095: * a target ControlBean within a specific ControlContainerContext
096: */
097: public ResourceContextImpl(
098: ControlContainerContext containerContext, ControlBean bean) {
099: _containerContext = containerContext;
100: _bean = bean;
101:
102: //
103: // Register to receive invocation notifications from the target bean
104: //
105: _bean.addInvokeListener(this );
106: }
107:
108: /**
109: * Implements the InvokeListener.preInvoke method. This hook will be called before the
110: * managed beans' operations are invoked
111: */
112: public void preInvoke(Method m, Object[] args) {
113: if (!_hasAcquired)
114: acquire();
115: }
116:
117: /**
118: * Implements the InvokeListener.postInvoke method.
119: */
120: public void postInvoke(Object retval, Throwable t) {
121: }
122:
123: // ResourceContext.acquire()
124: public void acquire() {
125: if (_hasAcquired)
126: return;
127:
128: // Deliver the onAcquire event to registered listeners
129: for (ResourceEvents resourceListener : _listeners)
130: resourceListener.onAcquire();
131:
132: // Register this ResourceContext with associated container context
133: _containerContext.addResourceContext(this , _bean);
134:
135: // Set the flag to indicate resources have been acquired.
136: _hasAcquired = true;
137: }
138:
139: // ResourceContext.release()
140: public void release() {
141: if (!_hasAcquired)
142: return;
143:
144: // Deliver the onRelease event to the registered listeners
145: for (ResourceEvents resourceListener : _listeners)
146: resourceListener.onRelease();
147:
148: // Unregister this ResourceContext with associated container context
149: _containerContext.removeResourceContext(this , _bean);
150:
151: // Reset the flag to indicate resources have been released.
152: _hasAcquired = false;
153: }
154:
155: // ResourceContext.hasResources()
156: public boolean hasResources() {
157: return _hasAcquired;
158: }
159:
160: // ResourceContext.addResourceEventsListener
161: public void addResourceEventsListener(
162: ResourceEvents resourceListener) {
163: _listeners.add(resourceListener);
164: }
165:
166: // ResourceContext.removeResourceEventsListener
167: public void removeResourceEventsListener(
168: ResourceEvents resourceListener) {
169: _listeners.remove(resourceListener);
170: }
171:
172: private Vector<ResourceEvents> _listeners = new Vector<ResourceEvents>();
173: private boolean _hasAcquired = false;
174: private ControlContainerContext _containerContext;
175: private ControlBean _bean;
176: }
|