1 package org.apache.beehive.controls.runtime.bean; 2 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.Stack ; 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 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 49 public void beginContext() 50 { 51 ControlThreadContext.beginContext(this); 52 } 53 54 57 public void endContext() 58 { 59 try 60 { 61 releaseResources(); 65 } 66 finally 67 { 68 ControlThreadContext.endContext(this); 69 } 70 } 71 72 76 public void initialize() 77 { 78 super.initialize(); 79 80 addService(org.apache.beehive.controls.api.context.ResourceContext.class, 84 ResourceContextImpl.getProvider()); 85 } 86 87 94 protected synchronized void addResourceContext(ResourceContext resourceContext, 95 ControlBean bean) 96 { 97 if (!resourceContext.hasResources()) 98 _resourceContexts.push(resourceContext); 99 } 100 101 108 protected synchronized void removeResourceContext(ResourceContext resourceContext, 109 ControlBean bean) 110 { 111 if (!_releasingAll && resourceContext.hasResources()) 117 _resourceContexts.remove(resourceContext); 118 } 119 120 125 protected synchronized void releaseResources() 126 { 127 _releasingAll = true; 129 130 while (!_resourceContexts.empty()) 134 { 135 ResourceContext resourceContext = _resourceContexts.pop(); 136 resourceContext.release(); 137 } 138 139 _releasingAll = false; 141 } 142 143 149 public Object dispatchEvent(ControlHandle handle, EventRef event, Object [] args) 150 throws IllegalArgumentException , IllegalAccessException , InvocationTargetException 151 { 152 ControlBean bean = getBean(handle.getControlID()); 153 if (bean == null) 154 throw new IllegalArgumentException ("Invalid bean ID: " + handle.getControlID()); 155 156 return bean.dispatchEvent(event, args); 157 } 158 159 166 public ControlHandle getControlHandle(org.apache.beehive.controls.api.bean.ControlBean bean) 167 { 168 return null; 173 } 174 175 180 public boolean isSingleThreadedContainer() 181 { 182 return false; 183 } 184 185 boolean _releasingAll; 186 Stack <ResourceContext> _resourceContexts = new Stack <ResourceContext>(); 187 } 188 | Popular Tags |