1 31 package org.objectweb.proactive.core.component.controller; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.fractal.api.Component; 36 import org.objectweb.fractal.api.Interface; 37 import org.objectweb.fractal.api.NoSuchInterfaceException; 38 import org.objectweb.fractal.api.control.BindingController; 39 import org.objectweb.fractal.api.control.IllegalBindingException; 40 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 41 import org.objectweb.fractal.api.control.LifeCycleController; 42 import org.objectweb.fractal.api.type.InterfaceType; 43 import org.objectweb.fractal.util.Fractal; 44 45 import org.objectweb.proactive.ProActive; 46 import org.objectweb.proactive.core.ProActiveRuntimeException; 47 import org.objectweb.proactive.core.component.Binding; 48 import org.objectweb.proactive.core.component.Bindings; 49 import org.objectweb.proactive.core.component.Constants; 50 import org.objectweb.proactive.core.component.Fractive; 51 import org.objectweb.proactive.core.component.ProActiveInterface; 52 import org.objectweb.proactive.core.component.identity.ProActiveComponent; 53 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType; 54 import org.objectweb.proactive.core.group.Group; 55 import org.objectweb.proactive.core.group.ProActiveGroup; 56 57 import java.io.Serializable ; 58 59 import java.util.Collection ; 60 import java.util.Hashtable ; 61 62 63 71 public class ProActiveBindingController extends ProActiveController 72 implements BindingController, Serializable { 73 protected static Logger logger = Logger.getLogger(ProActiveBindingController.class.getName()); 76 private Bindings bindings; protected Hashtable groupBindings; 78 79 public ProActiveBindingController(Component owner) { 80 super(owner, Constants.BINDING_CONTROLLER); 81 bindings = new Bindings(); 82 } 83 84 public void addBinding(Binding binding) { 85 bindings.add(binding); 87 } 88 89 protected boolean existsBinding(String clientItfName) { 90 return bindings.containsBindingOn(clientItfName); 91 } 92 93 protected boolean existsClientInterface(String clientItfName) { 94 try { 95 return (getFcItfOwner().getFcInterface(clientItfName) != null); 96 } catch (NoSuchInterfaceException nsie) { 97 logger.error("interface not found : " + nsie.getMessage()); 98 throw new ProActiveRuntimeException(nsie); 99 } 100 } 101 102 103 protected void checkBindability(String clientItfName, Interface serverItf) 104 throws NoSuchInterfaceException, IllegalBindingException, 105 IllegalLifeCycleException { 106 if (!existsClientInterface(clientItfName)) { 107 throw new NoSuchInterfaceException(clientItfName + 108 " is not a client interface"); 109 } 110 if (existsBinding(clientItfName)) { 111 if (!((ProActiveInterfaceType) ((Interface) getFcItfOwner() 112 .getFcInterface(clientItfName)).getFcItfType()).isFcCollectionItf()) { 113 logger.warn(((ComponentParametersController) getFcItfOwner() 115 .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters() 116 .getName() + "." + clientItfName + 117 " is already bound"); 118 119 throw new IllegalBindingException(clientItfName + 120 " is already bound"); 121 } else { 122 if (((InterfaceType) serverItf.getFcItfType()).isFcClientItf()) { 124 throw new IllegalBindingException(serverItf.getFcItfName() + " is not a server interface"); 126 } 127 } 128 } 129 130 132 133 134 if (((LifeCycleController) getFcItfOwner().getFcInterface(Constants.LIFECYCLE_CONTROLLER)).getFcState() != LifeCycleController.STOPPED) { 140 throw new IllegalLifeCycleException( 141 "component has to be stopped to perform binding operations"); 142 } 143 } 144 145 protected void checkUnbindability(String clientItfName) 146 throws NoSuchInterfaceException, IllegalBindingException, 147 IllegalLifeCycleException { 148 checkLifeCycleIsStopped(); 149 if (!existsClientInterface(clientItfName)) { 150 throw new NoSuchInterfaceException(clientItfName + 151 " is not a client interface"); 152 } 153 if (!existsBinding(clientItfName)) { 154 throw new IllegalBindingException(clientItfName + 155 " is not yet bound"); 156 } 157 } 158 159 164 public Object removeBinding(String clientItfName) { 165 return bindings.remove(clientItfName); 166 } 167 168 173 public Object getBinding(String clientItfName) { 174 return bindings.get(clientItfName); 175 } 176 177 180 public Object lookupFc(String clientItfName) 181 throws NoSuchInterfaceException { 182 if (!existsBinding(clientItfName)) { 183 throw new NoSuchInterfaceException(clientItfName); 184 } else { 185 if (getBinding(clientItfName) instanceof Collection ) { 186 logger.error( 187 "you are looking up a collection of bindings. This method cannot return one single Interface object"); 188 return null; 189 } else { 190 return ((Binding) getBinding(clientItfName)).getServerInterface(); 191 } 192 } 193 } 194 195 199 public void bindFc(String clientItfName, Object serverItf) 200 throws NoSuchInterfaceException, IllegalBindingException, 201 IllegalLifeCycleException { 202 checkBindability(clientItfName, (Interface) serverItf); 203 String hierarchical_type = (Fractive.getComponentParametersController(getFcItfOwner())).getComponentParameters() 204 .getHierarchicalType(); 205 if (hierarchical_type.equals(Constants.PRIMITIVE)) { 206 primitiveBindFc(clientItfName, (Interface) serverItf); 207 } 208 if (hierarchical_type.equals(Constants.COMPOSITE)) { 209 compositeBindFc(clientItfName, (Interface) serverItf); 210 } 211 if (hierarchical_type.equals(Constants.PARALLEL)) { 212 parallelBindFc(clientItfName, (Interface) serverItf); 213 } 214 } 215 216 private void primitiveBindFc(String clientItfName, Interface serverItf) 217 throws NoSuchInterfaceException, IllegalBindingException, 218 IllegalLifeCycleException { 219 BindingController user_binding_controller = (BindingController) ((ProActiveComponent) getFcItfOwner()).getReferenceOnBaseObject(); 221 222 serverItf = (Interface) ProActive.getFutureValue(serverItf); 224 user_binding_controller.bindFc(clientItfName, serverItf); 225 addBinding(new Binding( 226 (Interface) getFcItfOwner().getFcInterface(clientItfName), 227 serverItf)); 228 } 229 230 private void parallelBindFc(String clientItfName, Interface serverItf) 231 throws NoSuchInterfaceException, IllegalBindingException, 232 IllegalLifeCycleException { 233 ProActiveInterface clientItf = (ProActiveInterface) getFcItfOwner() 234 .getFcInterface(clientItfName); 235 236 boolean condition1 = !((InterfaceType) clientItf.getFcItfType()).isFcClientItf(); 240 241 boolean condition2 = ((ProActiveContentController) (Fractal.getContentController(getFcItfOwner()))).isSubComponent(serverItf.getFcItfOwner()); 243 if (condition1 && condition2) { 244 if (ProActiveGroup.isGroup(clientItf.getFcItfImpl())) { 245 Group group = ProActiveGroup.getGroup(clientItf.getFcItfImpl()); 248 group.add(serverItf); 249 } else { 250 throw new IllegalBindingException( 251 "illegal binding : server interface " + clientItfName + 252 " of parallel component " + 253 Fractive.getComponentParametersController(getFcItfOwner()) 254 .getComponentParameters().getName() + 255 " should be a collective interface"); 256 } 257 } else if (!condition1 && !condition2) { 258 compositeBindFc(clientItfName, serverItf); 261 } else { 262 throw new IllegalBindingException("illegal binding of " + 263 Fractive.getComponentParametersController(getFcItfOwner()) 264 .getComponentParameters().getName() + '.' + 265 clientItfName); 266 } 267 } 268 269 272 private void compositeBindFc(String clientItfName, Interface serverItf) 273 throws NoSuchInterfaceException, IllegalBindingException, 274 IllegalLifeCycleException { 275 ProActiveInterface clientItf = (ProActiveInterface) getFcItfOwner() 276 .getFcInterface(clientItfName); 277 278 InterfaceType client_itf_type = ((ComponentParametersController) getFcItfOwner() 279 .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters() 280 .getComponentType().getFcInterfaceType(clientItfName); 281 282 if (client_itf_type.isFcCollectionItf() || 286 (((ComponentParametersController) getFcItfOwner() 287 .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters() 288 .getHierarchicalType().equals(Constants.PARALLEL) && 289 !client_itf_type.isFcClientItf())) { 290 if (ProActiveGroup.isGroup(clientItf.getFcItfImpl())) { 291 Group itf_ref_group = ProActiveGroup.getGroup(clientItf.getFcItfImpl()); 292 293 itf_ref_group.add((Interface) serverItf); 295 if (logger.isDebugEnabled()) { 296 logger.debug("performed collective binding : " + 297 ((ComponentParametersController) getFcItfOwner() 298 .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters() 299 .getName() + '.' + clientItfName + " --> " + 300 ((ComponentParametersController) ((Interface) serverItf) 301 .getFcItfOwner().getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters() 302 .getName() + '.' + 303 ((Interface) serverItf).getFcItfName()); 304 } 305 } 306 } else { 307 clientItf.setFcItfImpl(serverItf); 308 } 309 addBinding(new Binding(clientItf, (Interface) serverItf)); 310 } 311 312 319 public void unbindFc(String clientItfName) 320 throws NoSuchInterfaceException, IllegalBindingException, 321 IllegalLifeCycleException { 323 checkUnbindability(clientItfName); 324 ProActiveInterface clientItf = (ProActiveInterface) getFcItfOwner() 325 .getFcInterface(clientItfName); 326 if (clientItf == null) { 327 throw new NoSuchInterfaceException(clientItfName); 328 } 329 330 if (ProActiveGroup.isGroup(clientItf.getFcItfImpl())) { 332 Group group = ProActiveGroup.getGroup(clientItf.getFcItfImpl()); 335 group.clear(); 336 removeBinding(clientItfName); 337 } else { 338 Binding binding = (Binding) (removeBinding(clientItfName)); 339 ((ProActiveInterface) (binding.getClientInterface())).setFcItfImpl(null); 340 } 341 342 } 343 344 347 public String [] listFc() { 348 return bindings.getExternalClientBindings(); 349 } 350 } 351 | Popular Tags |