1 package org.apache.beehive.controls.runtime.bean; 2 19 20 import org.apache.beehive.controls.api.context.ResourceContext; 21 22 import java.beans.beancontext.BeanContextServiceProvider ; 23 import java.beans.beancontext.BeanContextServices ; 24 import java.lang.reflect.Method ; 25 import java.util.Iterator ; 26 import java.util.Vector ; 27 28 32 public class ResourceContextImpl implements ResourceContext, InvokeListener 33 { 34 38 private static class ResourceContextProvider implements BeanContextServiceProvider 39 { 40 public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, 44 Object serviceSelector) 45 { 46 if (requestor instanceof ControlBean) 52 { 53 return new ResourceContextImpl((ControlContainerContext)bcs, 54 (ControlBean)requestor); 55 } 56 57 return null; 58 } 59 60 public void releaseService(BeanContextServices bcs, Object requestor, Object service) 64 { 65 return; } 67 68 public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) 72 { 73 return null; } 75 } 76 77 82 static private ResourceContextProvider _theProvider = new ResourceContextProvider(); 83 84 87 static ResourceContextProvider getProvider() { return _theProvider; } 88 89 93 public ResourceContextImpl(ControlContainerContext containerContext, ControlBean bean) 94 { 95 _containerContext = containerContext; 96 _bean = bean; 97 98 _bean.addInvokeListener(this); 102 } 103 104 108 public void preInvoke(Method m, Object [] args) 109 { 110 if (!_hasAcquired) 111 acquire(); 112 } 113 114 117 public void postInvoke(Object retval, Throwable t) {}; 118 119 public void acquire() 121 { 122 if (_hasAcquired) 123 return; 124 125 for (ResourceEvents resourceListener : _listeners) 127 resourceListener.onAcquire(); 128 129 _containerContext.addResourceContext(this, _bean); 131 132 _hasAcquired = true; 134 }; 135 136 public void release() 138 { 139 if (!_hasAcquired) 140 return; 141 142 for (ResourceEvents resourceListener : _listeners) 144 resourceListener.onRelease(); 145 146 _containerContext.removeResourceContext(this, _bean); 148 149 _hasAcquired = false; 151 }; 152 153 public boolean hasResources() { return _hasAcquired; } 155 156 public void addResourceEventsListener(ResourceEvents resourceListener) 158 { 159 _listeners.add(resourceListener); 160 } 161 162 public void removeResourceEventsListener(ResourceEvents resourceListener) 164 { 165 _listeners.remove(resourceListener); 166 } 167 168 private Vector <ResourceEvents> _listeners = new Vector <ResourceEvents>(); 169 private boolean _hasAcquired = false; 170 private ControlContainerContext _containerContext; 171 private ControlBean _bean; 172 } 173 | Popular Tags |