KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > xml > ParallelCompositeComponentHandler


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.xml;
32
33 import org.objectweb.fractal.api.Component;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35 import org.objectweb.fractal.api.control.BindingController;
36 import org.objectweb.fractal.api.control.IllegalBindingException;
37 import org.objectweb.fractal.api.control.IllegalContentException;
38 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
39 import org.objectweb.fractal.api.factory.InstantiationException;
40 import org.objectweb.fractal.api.type.InterfaceType;
41 import org.objectweb.fractal.util.Fractal;
42
43 import org.objectweb.proactive.core.component.Constants;
44 import org.objectweb.proactive.core.component.ContentDescription;
45 import org.objectweb.proactive.core.component.ControllerDescription;
46 import org.objectweb.proactive.core.component.Fractive;
47 import org.objectweb.proactive.core.component.type.ParallelComposite;
48 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
49 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
50 import org.objectweb.proactive.core.node.Node;
51 import org.objectweb.proactive.core.node.NodeException;
52 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
53 import org.objectweb.proactive.core.xml.io.Attributes;
54
55 import org.xml.sax.SAXException JavaDoc;
56
57 import java.util.HashMap JavaDoc;
58 import java.util.Iterator JavaDoc;
59 import java.util.List JavaDoc;
60
61
62 /**
63  * @author Matthieu Morel
64  */

65 public class ParallelCompositeComponentHandler
66     extends AbstractContainerComponentHandler {
67
68     /**
69      * @param deploymentDescriptor
70      * @param componentsCache
71      */

72     public ParallelCompositeComponentHandler(
73         ProActiveDescriptor deploymentDescriptor,
74         ComponentsCache componentsCache, HashMap JavaDoc componentTypes,
75         ComponentsHandler fatherHandler) {
76         super(deploymentDescriptor, componentsCache, componentTypes,
77             fatherHandler);
78         controllerDescription.setHierarchicalType(Constants.PARALLEL);
79         //System.out.println("PARALLEL HANDLER");
80
addHandler(ComponentsDescriptorConstants.BINDINGS_TAG,
81             new BindingsHandler(componentsCache));
82     }
83
84     /* (non-Javadoc)
85      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
86      */

87     public Object JavaDoc getResultObject() throws SAXException JavaDoc {
88         return new ComponentResultObject(controllerDescription.getName());
89     }
90
91     /* (non-Javadoc)
92      * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
93      */

94     protected void notifyEndActiveHandler(String JavaDoc name,
95         UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
96         if (getContainerElementHierarchy().containsChild(activeHandler)) {
97             enable();
98         }
99         if (isEnabled()) {
100             Component parallel = null;
101             if (name.equals(ComponentsDescriptorConstants.COMPONENTS_TAG)) {
102                 try {
103                     if (virtualNode.equals(ComponentsDescriptorConstants.NULL)) {
104                         //System.out.println("INSTANTIATING COMPONENT ON CURRENT VM");
105
// componentsCache.addComponent(componentParameters.getName(),
106
// //PrimitiveComponentB.class.getName(),
107
parallel = cf.newFcInstance(componentType,
108                                 new ControllerDescription(controllerDescription),
109                                 new ContentDescription(ParallelComposite.class.getName(),
110                                     new Object JavaDoc[] { }));
111                         componentsCache.addComponent(controllerDescription.getName(),
112                             parallel);
113                         //PrimitiveComponentB.class.getName(),
114
} else {
115                         // we have to instantiate the component, and perform internal bindings (not explicitely specified)
116
// then instantiate the component and add a stub on it to the cache
117
VirtualNode vn = deploymentDescriptor.getVirtualNode(virtualNode);
118
119                         if (vn.getNodeCount() == 0) {
120                            throw new NodeException(
121                                "no node defined for the virtual node " + vn.getName());
122                        }
123                        if (logger.isDebugEnabled()) {
124                            if (vn.getNodeCount() > 1) {
125                                logger.debug("creating a parallel composite component on a virtual node mapped onto several nodes will actually create the component on the first retreived node");
126                            }
127                        }
128                         // get corresponding node (1st one if there are several nodes)
129
Node targeted_node = vn.getNode();
130                         parallel = cf.newFcInstance(componentType,
131                                 new ControllerDescription(controllerDescription.getName(),
132                                     controllerDescription.getHierarchicalType()),
133                                 new ContentDescription(ParallelComposite.class.getName(),
134                                     new Object JavaDoc[] { }, targeted_node));
135                         componentsCache.addComponent(controllerDescription.getName(),
136                             parallel);
137                         if (logger.isDebugEnabled()) {
138                             logger.debug("created parallel component : " +
139                                 controllerDescription.getName());
140                         }
141                     }
142                     // add sub components
143
List JavaDoc sub_components_names = (List JavaDoc) getHandler(name)
144                                                            .getResultObject();
145                     Component[] sub_components = new Component[sub_components_names.size()];
146                     Iterator JavaDoc iterator = sub_components_names.iterator();
147                     int n = 0;
148                     while (iterator.hasNext()) {
149                         String JavaDoc sub_component_name = (String JavaDoc) iterator.next();
150                         if (logger.isDebugEnabled()) {
151                             logger.debug("adding sub component : " +
152                                 sub_component_name);
153                         }
154                         Fractal.getContentController(parallel)
155                                .addFcSubComponent(componentsCache.getComponent(
156                                 sub_component_name));
157                         sub_components[n] = componentsCache.getComponent(sub_component_name);
158                         n++;
159                     }
160
161                     // perform automatic bindings
162
// for a parallel component :
163
// - when a server interface of the parallel component
164
// and a server interface of an internal component have the same name,
165
// OR
166
// - when a client interface of an internal component
167
// and a client interface of the parallel component have the same name,
168
// they are automatically bound together
169

170                     //ProActiveInterface[] parallel_component_interfaces = (ProActiveInterface[])component.getFcInterfaces();
171
//Vector functional_interfaces_names_vector = new Vector();
172
InterfaceType[] current_component_interfaces = componentType.getFcInterfaceTypes();
173                     for (int i = 0; i < sub_components.length; i++) {
174                         // get the interfaces
175
//ProActiveInterface[] interfaces = (ProActiveInterface[])sub_components[i].getFcInterfaces();
176
InterfaceType[] sub_component_interfaces = Fractive.getComponentParametersController(sub_components[i])
177                                                                            .getComponentParameters()
178                                                                            .getInterfaceTypes();
179
180                         // loop on the interfaces
181
for (int j = 0; j < sub_component_interfaces.length;
182                                 j++) {
183                             // perform a binding when names match
184
// if (!(interfaces[i].isControlInterface()) {
185
// we have a functional interface
186
// check name
187
//if (interfaces[j].getFcItfName().equals(anObject))
188
for (int k = 0;
189                                     k < current_component_interfaces.length;
190                                     k++) {
191                                 if (sub_component_interfaces[j].getFcItfName()
192                                                                    .equals(current_component_interfaces[k].getFcItfName())) {
193                                     // names match
194
String JavaDoc itf_name = current_component_interfaces[k].getFcItfName();
195                                     if ((sub_component_interfaces[j].isFcClientItf() &&
196                                             current_component_interfaces[k].isFcClientItf())) {
197                                         if (logger.isDebugEnabled()) {
198                                             logger.debug(
199                                                 "BINDING INTERFACES OF SAME NAME " +
200                                                 itf_name);
201                                         }
202
203                                         // roles match ==> we have a candidate
204
// perform binding sub_component.client --> parallel.client
205
((BindingController) sub_components[i].getFcInterface(Constants.BINDING_CONTROLLER)).bindFc(itf_name,
206                                             parallel.getFcInterface(itf_name));
207                                     } else if ((!sub_component_interfaces[j].isFcClientItf() &&
208                                             !current_component_interfaces[k].isFcClientItf())) {
209                                         if (logger.isDebugEnabled()) {
210                                             logger.debug(
211                                                 "BINDING INTERFACES OF SAME NAME " +
212                                                 itf_name);
213                                         }
214
215                                         // roles match ==> we have a candidate
216
// perform binding parallel.server --> subcomponent.server
217
((BindingController) parallel.getFcInterface(Constants.BINDING_CONTROLLER)).bindFc(itf_name,
218                                             sub_components[i].getFcInterface(
219                                                 itf_name));
220                                     }
221                                 }
222                             }
223                         }
224                     }
225                 } catch (InstantiationException JavaDoc e) {
226                     logger.error(
227                         "cannot create active component: instantiation exception");
228                     throw new SAXException JavaDoc(e);
229                 } catch (NodeException e) {
230                     logger.error(
231                         "cannot create active component: node exception");
232                     throw new SAXException JavaDoc(e);
233                 } catch (IllegalContentException e) {
234                     logger.error(
235                         "cannot assemble active component : illegal content");
236                     throw new SAXException JavaDoc(e);
237                 } catch (NoSuchInterfaceException e) {
238                     logger.error("interface not found");
239                     throw new SAXException JavaDoc(e);
240                 } catch (IllegalLifeCycleException e) {
241                     logger.error(
242                         "cannot bind active component : illegal life cycle operation");
243                     throw new SAXException JavaDoc(e);
244                 } catch (IllegalBindingException e) {
245                     logger.error(
246                         "cannot bind active component : illegal binding operation");
247                     throw new SAXException JavaDoc(e);
248                 }
249             }
250         }
251     }
252
253     /* (non-Javadoc)
254      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
255      */

256     public void startContextElement(String JavaDoc name, Attributes attributes)
257         throws SAXException JavaDoc {
258         if (isEnabled()) {
259             super.startContextElement(name, attributes);
260         }
261     }
262 }
263
Popular Tags