1 31 package org.objectweb.proactive.core.component.identity; 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.Type; 39 import org.objectweb.fractal.api.type.InterfaceType; 40 41 import org.objectweb.proactive.Body; 42 import org.objectweb.proactive.core.ProActiveRuntimeException; 43 import org.objectweb.proactive.core.UniqueID; 44 import org.objectweb.proactive.core.component.ComponentParameters; 45 import org.objectweb.proactive.core.component.Constants; 46 import org.objectweb.proactive.core.component.ProActiveInterface; 47 import org.objectweb.proactive.core.component.asmgen.MetaObjectInterfaceClassGenerator; 48 import org.objectweb.proactive.core.component.controller.ComponentParametersController; 49 import org.objectweb.proactive.core.component.controller.ProActiveBindingController; 50 import org.objectweb.proactive.core.component.controller.ProActiveComponentParametersController; 51 import org.objectweb.proactive.core.component.controller.ProActiveContentController; 52 import org.objectweb.proactive.core.component.controller.ProActiveLifeCycleController; 53 import org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentative; 54 import org.objectweb.proactive.core.component.request.ComponentRequestQueue; 55 import org.objectweb.proactive.core.group.Group; 56 import org.objectweb.proactive.core.group.ProActiveComponentGroup; 57 import org.objectweb.proactive.core.group.ProActiveGroup; 58 59 import java.io.Serializable ; 60 61 import java.util.Iterator ; 62 import java.util.Vector ; 63 64 65 73 public class ProActiveComponentImpl implements ProActiveComponent, Interface, 74 Serializable { 75 protected static Logger logger = Logger.getLogger(ProActiveComponentImpl.class.getName()); 76 77 private Interface[] interfaceReferences; 79 private Body body; 80 81 public ProActiveComponentImpl() { 82 } 83 84 89 public ProActiveComponentImpl(ComponentParameters componentParameters, 90 Body myBody) { 91 this.body = myBody; 92 93 boolean component_is_primitive = componentParameters.getHierarchicalType() 97 .equals(Constants.PRIMITIVE); 98 99 Vector interface_references_vector = new Vector (4); 101 102 interface_references_vector.addElement(this); 104 105 interface_references_vector.add(new ProActiveLifeCycleController(this)); 107 108 ComponentParametersController component_parameters_controller = new ProActiveComponentParametersController(this); 110 component_parameters_controller.setComponentParameters(componentParameters); 111 interface_references_vector.add(component_parameters_controller); 112 113 if (!(componentParameters.getHierarchicalType().equals(Constants.PRIMITIVE) && 116 (componentParameters.getClientInterfaceTypes().length == 0))) { 117 interface_references_vector.add(new ProActiveBindingController(this)); 118 } else { 119 if (logger.isDebugEnabled()) { 121 logger.debug("user component class of '" + 122 componentParameters.getName() + 123 "' does not have any client interface. It will have no BindingController"); 124 } 125 } 126 127 if (componentParameters.getHierarchicalType().equals(Constants.COMPOSITE) || 129 (componentParameters.getHierarchicalType().equals(Constants.PARALLEL))) { 130 interface_references_vector.addElement(new ProActiveContentController( 132 this)); 133 } 134 135 InterfaceType[] interface_types = componentParameters.getComponentType() 137 .getFcInterfaceTypes(); 138 try { 139 for (int i = 0; i < interface_types.length; i++) { 140 ProActiveInterface itf_ref = null; 141 142 if (interface_types[i].isFcClientItf() && 144 interface_types[i].isFcCollectionItf()) { 145 itf_ref = createInterfaceOnGroupOfDelegatees(interface_types[i], 147 component_is_primitive); 148 } 149 else if (componentParameters.getHierarchicalType().equals(Constants.PARALLEL) && 151 (!interface_types[i].isFcClientItf())) { 152 itf_ref = createInterfaceOnGroupOfDelegatees(interface_types[i], 154 component_is_primitive); 155 } else { 156 itf_ref = MetaObjectInterfaceClassGenerator.instance() 157 .generateInterface(interface_types[i].getFcItfName(), 158 this, interface_types[i], true, 159 component_is_primitive); 160 } 163 164 if (componentParameters.getHierarchicalType().equals(Constants.PRIMITIVE)) { 166 if (!interface_types[i].isFcCollectionItf()) { 168 if (!interface_types[i].isFcClientItf()) { 169 (itf_ref).setFcItfImpl(getReferenceOnBaseObject()); 170 } else if (interface_types[i].isFcClientItf()) { 171 (itf_ref).setFcItfImpl(null); 172 } 173 } 174 } else { 175 } 179 interface_references_vector.addElement(itf_ref); 180 } 181 } catch (Exception e) { 182 if (logger.isDebugEnabled()) { 183 logger.debug("cannot create interface references : " + 184 e.getMessage()); 185 } 186 e.printStackTrace(); 187 throw new RuntimeException ("cannot create interface references : " + 188 e.getMessage()); 189 } 190 191 interfaceReferences = (Interface[]) interface_references_vector.toArray(new Interface[interface_references_vector.size()]); 193 } 194 195 private ProActiveInterface createInterfaceOnGroupOfDelegatees( 198 InterfaceType itfType, boolean isPrimitive) throws Exception { 199 ProActiveInterface itf_ref = MetaObjectInterfaceClassGenerator.instance() 200 .generateInterface(itfType.getFcItfName(), 201 this, itfType, true, isPrimitive); 202 203 ProActiveInterface itf_ref_group = ProActiveComponentGroup.newActiveComponentInterfaceGroup(itfType); 208 itf_ref.setFcItfImpl(itf_ref_group); 209 return itf_ref; 210 } 211 212 215 public Object getFcInterface(String interfaceName) 216 throws NoSuchInterfaceException { 217 if (interfaceReferences != null) { 218 for (int i = 0; i < interfaceReferences.length; i++) { 219 if (ProActiveGroup.isGroup(interfaceReferences[i])) { 220 int count = 0; 224 Group itf_ref_group = ProActiveGroup.getGroup(interfaceReferences[i]); 225 Iterator iterator = itf_ref_group.iterator(); 226 227 while (iterator.hasNext()) { 230 Interface group_element = (Interface) iterator.next(); 231 if (group_element.getFcItfName().equals(interfaceName)) { 232 count++; 233 } 234 } 235 if (count > 0) { 236 if (count == itf_ref_group.size()) { 237 return interfaceReferences[i]; 238 } else { 239 throw new NoSuchInterfaceException( 240 "some elements of the collection are not named " + 241 interfaceName); 242 } 243 } 244 } else { 245 if (interfaceReferences[i].getFcItfName().equals(interfaceName)) { 247 return interfaceReferences[i]; 248 } 249 } 250 } 251 } 252 throw new NoSuchInterfaceException(interfaceName); 253 } 254 255 258 public Object [] getFcInterfaces() { 259 Vector external_interfaces = new Vector (interfaceReferences.length); 260 for (int i = 0; i < interfaceReferences.length; i++) { 261 if (!interfaceReferences[i].isFcInternalItf()) { 262 external_interfaces.add(interfaceReferences[i]); 263 } 264 } 265 external_interfaces.trimToSize(); 266 return external_interfaces.toArray(); 267 } 268 269 272 public Type getFcType() { 273 try { 274 return ((ComponentParametersController) getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters() 275 .getComponentType(); 276 } catch (NoSuchInterfaceException nsie) { 277 nsie.printStackTrace(); 278 throw new ProActiveRuntimeException("cannot retreive the type of the component", 279 nsie); 280 } 281 } 282 283 286 public String getFcItfName() { 287 return Constants.COMPONENT; 288 } 289 290 293 public Component getFcItfOwner() { 294 return (Component) this; 295 } 296 297 300 public Type getFcItfType() { 301 return getFcType(); 302 } 303 304 307 public boolean isFcInternalItf() { 308 return false; 309 } 310 311 317 public Object getReferenceOnBaseObject() { 318 return getBody().getReifiedObject(); 319 } 320 321 324 public ComponentRequestQueue getRequestQueue() { 325 return (ComponentRequestQueue) getBody().getRequestQueue(); 326 } 327 328 331 public ComponentParameters getComponentParameters() 332 throws NoSuchInterfaceException { 333 return ((ComponentParametersController) getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters(); 335 } 336 337 340 public Body getBody() { 341 return body; 342 } 343 344 351 public boolean equals(Object component) { 352 if (component instanceof ProActiveComponentRepresentative) { 353 return getBody().getID().equals(((ProActiveComponentRepresentative) component).getID()); 354 } else { 355 logger.error( 356 "can only compare the current component with component representatives"); 357 return false; 358 } 359 } 360 361 365 public int hashCode() { 366 return getBody().hashCode(); 367 } 368 369 372 public UniqueID getID() { 373 return getBody().getID(); 374 } 375 } 376 | Popular Tags |