1 31 package org.objectweb.proactive.core.component; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.fractal.api.Component; 36 import org.objectweb.fractal.api.NoSuchInterfaceException; 37 import org.objectweb.fractal.api.Type; 38 import org.objectweb.fractal.api.factory.Factory; 39 import org.objectweb.fractal.api.factory.GenericFactory; 40 import org.objectweb.fractal.api.factory.InstantiationException; 41 import org.objectweb.fractal.api.type.ComponentType; 42 import org.objectweb.fractal.api.type.InterfaceType; 43 import org.objectweb.fractal.api.type.TypeFactory; 44 45 import org.objectweb.proactive.ActiveObjectCreationException; 46 import org.objectweb.proactive.ProActive; 47 import org.objectweb.proactive.core.ProActiveRuntimeException; 48 import org.objectweb.proactive.core.body.ProActiveMetaObjectFactory; 49 import org.objectweb.proactive.core.component.controller.ComponentParametersController; 50 import org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentativeFactory; 51 import org.objectweb.proactive.core.component.type.Composite; 52 import org.objectweb.proactive.core.component.type.ParallelComposite; 53 import org.objectweb.proactive.core.component.type.ProActiveTypeFactory; 54 import org.objectweb.proactive.core.group.Group; 55 import org.objectweb.proactive.core.group.ProActiveComponentGroup; 56 import org.objectweb.proactive.core.group.ProActiveGroup; 57 import org.objectweb.proactive.core.mop.ClassNotReifiableException; 58 import org.objectweb.proactive.core.mop.Proxy; 59 import org.objectweb.proactive.core.mop.StubObject; 60 import org.objectweb.proactive.core.node.Node; 61 import org.objectweb.proactive.core.node.NodeException; 62 63 import java.util.Hashtable ; 64 65 66 73 public class Fractive implements GenericFactory, Component, Factory { 74 private static Logger logger = Logger.getLogger(Fractive.class.getName()); 75 private static Fractive instance = null; 76 private TypeFactory typeFactory = (TypeFactory) ProActiveTypeFactory.instance(); 77 private Type type = null; 78 79 83 public Fractive() { 84 } 85 86 private static Fractive instance() { 87 if (instance == null) { 88 instance = new Fractive(); 89 } 90 return instance; 91 } 92 93 100 public static ComponentParametersController getComponentParametersController( 101 final Component component) throws NoSuchInterfaceException { 102 return (ComponentParametersController) component.getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER); 103 } 104 105 109 public static ProActiveInterface createCollectiveClientInterface( 110 String itfName, String itfSignature) throws ProActiveRuntimeException { 111 try { 112 InterfaceType itf_type = ProActiveTypeFactory.instance() 113 .createFcItfType(itfName, 114 itfSignature, TypeFactory.CLIENT, TypeFactory.MANDATORY, 115 TypeFactory.COLLECTION); 116 ProActiveInterface itf_ref_group = ProActiveComponentGroup.newActiveComponentInterfaceGroup(itf_type); 117 return itf_ref_group; 118 } catch (Exception e) { 119 throw new ProActiveRuntimeException("Impossible to create a collective client interface ", 120 e); 121 } 122 } 123 124 private static Component newFcInstance(ContentDescription contentDesc, 125 ComponentParameters componentParameters) throws InstantiationException { 126 try { 128 if (contentDesc.getFactory() == null) { 130 Hashtable factory_params = new Hashtable (1); 132 factory_params.put(ProActiveMetaObjectFactory.COMPONENT_PARAMETERS_KEY, 133 componentParameters); 134 contentDesc.setFactory(new ProActiveMetaObjectFactory( 135 factory_params)); 136 } 138 139 Object ao = null; 140 141 if (!contentDesc.isLocalizedOnAVirtualNode()) { 143 ao = ProActive.newActive(contentDesc.getClassName(), 145 contentDesc.getConstructorParameters(), 146 contentDesc.getNode(), contentDesc.getActivity(), 147 contentDesc.getFactory()); 148 } else { 149 contentDesc.getVirtualNode().activate(); 151 if (contentDesc.getVirtualNode().getNodes().length == 0) { 152 throw new InstantiationException ( 153 "Cannot create component on virtual node as no node is associated with this virtual node"); 154 } 155 Node[] nodes = contentDesc.getVirtualNode().getNodes(); 156 if (nodes.length > 1) { Component components = (Component) ProActiveGroup.newGroup(Component.class.getName()); 158 Group group_of_components = ProActiveGroup.getGroup(components); 159 Proxy proxy = null; 160 161 if (componentParameters.getHierarchicalType().equals(Constants.PRIMITIVE)) { 162 String original_component_name = componentParameters.getName(); 165 contentDesc.getVirtualNode().activate(); 166 167 for (int i = 0; i < nodes.length; i++) { 168 String new_name = original_component_name + 170 Constants.CYCLIC_NODE_SUFFIX + i; 171 componentParameters.setName(new_name); 172 contentDesc.setNode(nodes[i]); 174 group_of_components.add(Fractive.newFcInstance( 175 contentDesc, componentParameters)); 176 } 177 178 return components; 179 } else { 180 ao = ProActive.newActive(contentDesc.getClassName(), 182 contentDesc.getConstructorParameters(), 183 contentDesc.getVirtualNode().getNode(), 184 contentDesc.getActivity(), 185 contentDesc.getFactory()); 186 } 187 } else { 188 ao = ProActive.newActive(contentDesc.getClassName(), 191 contentDesc.getConstructorParameters(), 192 contentDesc.getVirtualNode().getNode(), 193 contentDesc.getActivity(), contentDesc.getFactory()); 194 } 195 } 196 197 componentParameters.setStubOnReifiedObject(ao); 199 org.objectweb.proactive.core.mop.Proxy myProxy = ((StubObject) ao).getProxy(); 201 if (myProxy == null) { 202 throw new ProActiveRuntimeException( 203 "Cannot find a Proxy on the stub object: " + ao); 204 } 205 return ProActiveComponentRepresentativeFactory.instance() 206 .createComponentRepresentative(componentParameters, 207 myProxy); 208 } catch (ActiveObjectCreationException e) { 209 throw new InstantiationException (e.getMessage()); 210 } catch (NodeException e) { 211 throw new InstantiationException (e.getMessage()); 212 } catch (ClassNotReifiableException e) { 213 throw new InstantiationException (e.getMessage()); 214 } catch (ClassNotFoundException e) { 215 throw new InstantiationException (e.getMessage()); 216 } 217 } 218 219 private Component newFcInstance(Type type, 220 ControllerDescription controllerDesc, ContentDescription contentDesc) 221 throws InstantiationException { 222 if (contentDesc == null) { 223 if (Constants.COMPOSITE.equals(controllerDesc.getHierarchicalType())) { 225 contentDesc = new ContentDescription(Composite.class.getName()); 226 } else if (Constants.PARALLEL.equals( 227 controllerDesc.getHierarchicalType())) { 228 contentDesc = new ContentDescription(ParallelComposite.class.getName()); 229 } else { 230 throw new InstantiationException ( 231 "Content can be null only if the hierarchical type of the component is composite or parallel"); 232 } 233 } 234 ComponentParameters component_params = new ComponentParameters((ComponentType) type, 235 controllerDesc); 236 return Fractive.newFcInstance(contentDesc, component_params); 237 } 238 239 242 public Component newFcInstance(Type arg0, Object arg1, Object arg2) 243 throws InstantiationException { 244 try { 245 return newFcInstance(arg0, (ControllerDescription) arg1, 246 (ContentDescription) arg2); 247 } catch (ClassCastException e) { 248 throw new InstantiationException ( 249 "With this implementation, parameters must be of respective types : " + 250 Type.class.getName() + ',' + 251 ControllerDescription.class.getName() + ',' + 252 ContentDescription.class.getName()); 253 } 254 } 255 256 259 public Object getFcInterface(String itfName) 260 throws NoSuchInterfaceException { 261 if ("generic-factory".equals(itfName)) { 262 return this; 263 } else if ("type-factory".equals(itfName)) { 264 return typeFactory; 265 } else { 266 throw new NoSuchInterfaceException(itfName); 267 } 268 } 269 270 273 public Object [] getFcInterfaces() { 274 return null; 275 } 276 277 280 public Type getFcType() { 281 if (type == null) { 282 try { 283 return type = typeFactory.createFcType(new InterfaceType[] { 284 typeFactory.createFcItfType("generic-factory", 285 GenericFactory.class.getName(), false, false, 286 false), 287 typeFactory.createFcItfType("type-factory", 288 TypeFactory.class.getName(), false, false, false) 289 }); 290 } catch (InstantiationException e) { 291 logger.error(e.getMessage()); 292 return null; 293 } 294 } else { 295 return type; 296 } 297 } 298 299 302 public Object getFcContentDesc() { 303 return null; 304 } 305 306 309 public Object getFcControllerDesc() { 310 return null; 311 } 312 313 316 public Type getFcInstanceType() { 317 return null; 318 } 319 320 323 public Component newFcInstance() throws InstantiationException { 324 return this; 325 } 326 } 327 | Popular Tags |