KickJava   Java API By Example, From Geeks To Geeks.

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


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

63 public class CompositeComponentHandler extends AbstractContainerComponentHandler {
64     public static Logger logger = Logger.getLogger(CompositeComponentHandler.class.getName());
65     private List JavaDoc subComponents;
66
67     /**
68      * @param deploymentDescriptor
69      * @param componentsCache
70      */

71     public CompositeComponentHandler(ProActiveDescriptor deploymentDescriptor,
72         ComponentsCache componentsCache, HashMap JavaDoc componentTypes,
73         ComponentsHandler fatherHandler) {
74         super(deploymentDescriptor, componentsCache, componentTypes,
75             fatherHandler);
76         controllerDescription.setHierarchicalType(Constants.COMPOSITE);
77         addHandler(ComponentsDescriptorConstants.BINDINGS_TAG,
78             new BindingsHandler(componentsCache));
79     }
80
81     /**
82      * see {@link org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)}
83      */

84     protected void notifyEndActiveHandler(String JavaDoc name,
85         UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
86         if (getContainerElementHierarchy().containsChild(activeHandler)) {
87             enable();
88         }
89         if (isEnabled()) {
90             Component composite;
91             if (name.equals(ComponentsDescriptorConstants.COMPONENTS_TAG)) {
92                 // then instantiate the component and add a stub on it to the cache
93
try {
94                     if (virtualNode.equals(ComponentsDescriptorConstants.NULL)) {
95                         composite = cf.newFcInstance(componentType,
96                                 new ControllerDescription(controllerDescription.getName(),
97                                     controllerDescription.getHierarchicalType()),
98                                 new ContentDescription(Composite.class.getName(),
99                                     new Object JavaDoc[] { }));
100                         componentsCache.addComponent(controllerDescription.getName(),
101                             composite);
102                     } else {
103                         VirtualNode vn = deploymentDescriptor.getVirtualNode(virtualNode);
104                         vn.activate();
105                         if (vn.getNodeCount() == 0) {
106                             throw new NodeException(
107                                 "no node defined for the virtual node " +
108                                 vn.getName());
109                         }
110                         if (logger.isDebugEnabled()) {
111                             if (vn.getNodeCount() > 1) {
112                                 if (logger.isDebugEnabled()) {
113                                     logger.debug(
114                                         "creating a composite component on a virtual node mapped onto several nodes will actually create the component on the first retreived node");
115                                 }
116                             }
117                         }
118
119                         // get corresponding node (1st node retreived if the vn is multiple)
120
Node targeted_node = vn.getNode();
121                         composite = cf.newFcInstance(componentType,
122                                 new ControllerDescription(controllerDescription.getName(),
123                                     controllerDescription.getHierarchicalType()),
124                                 new ContentDescription(Composite.class.getName(),
125                                     new Object JavaDoc[] { }, targeted_node));
126                         componentsCache.addComponent(controllerDescription.getName(),
127                             composite);
128                     }
129
130                     // add sub components
131
List JavaDoc sub_components = (List JavaDoc) getHandler(name)
132                                                      .getResultObject();
133                     Iterator JavaDoc iterator = sub_components.iterator();
134                     while (iterator.hasNext()) {
135                         String JavaDoc sub_component_name = (String JavaDoc) iterator.next();
136                         if (logger.isDebugEnabled()) {
137                             logger.debug("adding sub component : " +
138                                 sub_component_name);
139                         }
140                         ((ContentController) composite.getFcInterface(Constants.CONTENT_CONTROLLER)).addFcSubComponent(componentsCache.getComponent(
141                                 sub_component_name));
142                     }
143                 } catch (InstantiationException JavaDoc e) {
144                     logger.error("cannot instantiate component");
145                     throw new SAXException JavaDoc(e);
146                 } catch (NodeException ne) {
147                     logger.error(
148                         "cannot create active component: node exception");
149                     throw new SAXException JavaDoc(ne);
150                 } catch (NoSuchInterfaceException nsie) {
151                     logger.error(
152                         "cannot create active component : interface not found");
153                     throw new SAXException JavaDoc(nsie);
154                 } catch (IllegalLifeCycleException ilce) {
155                     logger.error(
156                         "cannot create active component : illegal life cycle operation");
157                     throw new SAXException JavaDoc(ilce);
158                 } catch (IllegalContentException ice) {
159                     logger.error(
160                         "cannot create active component : illegal content operation");
161                     throw new SAXException JavaDoc(ice);
162                 }
163                 logger.debug("created composite component : " +
164                     controllerDescription.getName());
165             }
166         }
167     }
168
169     /**
170      * see {@link org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()}
171      */

172     public Object JavaDoc getResultObject() throws SAXException JavaDoc {
173         return new ComponentResultObject(controllerDescription.getName());
174     }
175
176     /**
177      * see {@link org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)}
178      */

179     public void startContextElement(String JavaDoc name, Attributes attributes)
180         throws SAXException JavaDoc {
181         if (isEnabled()) {
182             super.startContextElement(name, attributes);
183         }
184     }
185 }
186
Popular Tags