KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > Fractive


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2004 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

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 JavaDoc;
64
65
66 /**
67  * This class is used for creating components.
68  * It acts as :<br>
69  * 1. a bootstrap component<br>
70  * 2. a GenericFactory for instantiating new components
71  * 3. a utility class providing static methods to create collective interfaces and retreive references to ComponentParametersController<br>
72  */

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     /**
80      * no-arg constructor (used by Fractal to get a bootstrap component)
81      *
82      */

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     /**
94      * Returns the {@link org.objectweb.fractal.api.control.ContentController} interface of the given component.
95      *
96      * @param component a component.
97      * @return the {@link org.objectweb.fractal.api.control.ContentController} interface of the given component.
98      * @throws NoSuchInterfaceException if there is no such interface.
99      */

100     public static ComponentParametersController getComponentParametersController(
101         final Component component) throws NoSuchInterfaceException {
102         return (ComponentParametersController) component.getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER);
103     }
104
105     /**
106      * Returns a generated interface reference, whose impl field is a group
107      * It is able to handle multiple bindings
108      */

109     public static ProActiveInterface createCollectiveClientInterface(
110         String JavaDoc itfName, String JavaDoc 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 JavaDoc 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 JavaDoc {
126         //MetaObjectFactory factory = null;
127
try {
128             // instantiate the component metaobject factory with parameters of the component
129
if (contentDesc.getFactory() == null) {
130                 // first create a hashtable with the parameters
131
Hashtable JavaDoc factory_params = new Hashtable JavaDoc(1);
132                 factory_params.put(ProActiveMetaObjectFactory.COMPONENT_PARAMETERS_KEY,
133                     componentParameters);
134                 contentDesc.setFactory(new ProActiveMetaObjectFactory(
135                         factory_params));
136                 // factory = ProActiveComponentMetaObjectFactory.newInstance(componentParameters);
137
}
138
139             Object JavaDoc ao = null;
140
141             // 2 possibilities : either the component is created on a node (or null), or it is created on a virtual node
142
if (!contentDesc.isLocalizedOnAVirtualNode()) {
143                 // case 1. Node
144
ao = ProActive.newActive(contentDesc.getClassName(),
145                         contentDesc.getConstructorParameters(),
146                         contentDesc.getNode(), contentDesc.getActivity(),
147                         contentDesc.getFactory());
148             } else {
149                 // case 2. Virtual Node
150
contentDesc.getVirtualNode().activate();
151                 if (contentDesc.getVirtualNode().getNodes().length == 0) {
152                     throw new InstantiationException JavaDoc(
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) { // cyclic node
157
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                         // task = instantiate a component with a different name
163
// on each of the node mapped to the given virtual node
164
String JavaDoc original_component_name = componentParameters.getName();
165                         contentDesc.getVirtualNode().activate();
166
167                         for (int i = 0; i < nodes.length; i++) {
168                             // change the name of each component (add a suffix)
169
String JavaDoc new_name = original_component_name +
170                                 Constants.CYCLIC_NODE_SUFFIX + i;
171                             componentParameters.setName(new_name);
172                             // change location of each component
173
contentDesc.setNode(nodes[i]);
174                             group_of_components.add(Fractive.newFcInstance(
175                                     contentDesc, componentParameters));
176                         }
177
178                         return components;
179                     } else {
180                         // component is a parallel or a composite : it will be created on the first node from this virtual node
181
ao = ProActive.newActive(contentDesc.getClassName(),
182                                 contentDesc.getConstructorParameters(),
183                                 contentDesc.getVirtualNode().getNode(),
184                                 contentDesc.getActivity(),
185                                 contentDesc.getFactory());
186                     }
187                 } else {
188                     // this is the case where contentDesc.getVirtualNode().getNodeCount() == 1) {
189
// create the component on the first node retreived from the virtual node
190
ao = ProActive.newActive(contentDesc.getClassName(),
191                             contentDesc.getConstructorParameters(),
192                             contentDesc.getVirtualNode().getNode(),
193                             contentDesc.getActivity(), contentDesc.getFactory());
194                 }
195             }
196
197             // the following corresponds to the case when the component is created on the local vm or on a single node.
198
componentParameters.setStubOnReifiedObject(ao);
199             // Find the proxy
200
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 JavaDoc(e.getMessage());
210         } catch (NodeException e) {
211             throw new InstantiationException JavaDoc(e.getMessage());
212         } catch (ClassNotReifiableException e) {
213             throw new InstantiationException JavaDoc(e.getMessage());
214         } catch (ClassNotFoundException JavaDoc e) {
215             throw new InstantiationException JavaDoc(e.getMessage());
216         }
217     }
218
219     private Component newFcInstance(Type type,
220         ControllerDescription controllerDesc, ContentDescription contentDesc)
221         throws InstantiationException JavaDoc {
222         if (contentDesc == null) {
223             // either a parallel or a composite component, no activitiy/factory/node specified
224
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 JavaDoc(
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     /**
240      * see {@link org.objectweb.fractal.api.factory.GenericFactory#newFcInstance(org.objectweb.fractal.api.Type, java.lang.Object, java.lang.Object)}
241      */

242     public Component newFcInstance(Type arg0, Object JavaDoc arg1, Object JavaDoc arg2)
243         throws InstantiationException JavaDoc {
244         try {
245             return newFcInstance(arg0, (ControllerDescription) arg1,
246                 (ContentDescription) arg2);
247         } catch (ClassCastException JavaDoc e) {
248             throw new InstantiationException JavaDoc(
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     /**
257      * see {@link org.objectweb.fractal.api.Component#getFcInterface(java.lang.String)}
258      */

259     public Object JavaDoc getFcInterface(String JavaDoc 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     /**
271      * see {@link org.objectweb.fractal.api.Component#getFcInterfaces()}
272      */

273     public Object JavaDoc[] getFcInterfaces() {
274         return null;
275     }
276
277     /**
278      * see {@link org.objectweb.fractal.api.Component#getFcType()}
279      */

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 JavaDoc e) {
291                 logger.error(e.getMessage());
292                 return null;
293             }
294         } else {
295             return type;
296         }
297     }
298
299     /**
300      * see {@link org.objectweb.fractal.api.factory.Factory#getFcContentDesc()}
301      */

302     public Object JavaDoc getFcContentDesc() {
303         return null;
304     }
305
306     /**
307      * see {@link org.objectweb.fractal.api.factory.Factory#getFcControllerDesc()}
308      */

309     public Object JavaDoc getFcControllerDesc() {
310         return null;
311     }
312
313     /**
314      * see {@link org.objectweb.fractal.api.factory.Factory#getFcInstanceType()}
315      */

316     public Type getFcInstanceType() {
317         return null;
318     }
319
320     /**
321      * see {@link org.objectweb.fractal.api.factory.Factory#newFcInstance()}
322      */

323     public Component newFcInstance() throws InstantiationException JavaDoc {
324         return this;
325     }
326 }
327
Popular Tags