KickJava   Java API By Example, From Geeks To Geeks.

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


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.factory.InstantiationException;
37
38 import org.objectweb.proactive.core.component.Constants;
39 import org.objectweb.proactive.core.component.ContentDescription;
40 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
41 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
42 import org.objectweb.proactive.core.group.ProActiveGroup;
43 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
44 import org.objectweb.proactive.core.xml.io.Attributes;
45
46 import org.xml.sax.SAXException JavaDoc;
47
48 import java.util.HashMap JavaDoc;
49
50
51 /**
52  * @author Matthieu Morel
53  */

54 public class PrimitiveComponentHandler extends ComponentHandler {
55     public static Logger logger = Logger.getLogger(PrimitiveComponentHandler.class.getName());
56     private String JavaDoc[] names; // when deployed on a VN
57

58     /**
59      * @param deploymentDescriptor
60      */

61     public PrimitiveComponentHandler(ProActiveDescriptor deploymentDescriptor,
62         ComponentsCache componentsCache, HashMap JavaDoc componentTypes) {
63         super(deploymentDescriptor, componentsCache, componentTypes);
64         controllerDescription.setHierarchicalType(Constants.PRIMITIVE);
65     }
66
67     /**
68      * handles the creation of primitive component on a virtual node
69      * If the virtual node attribute is set to "null", then the component is created in the current vm
70      * If the virtual node is cyclic, several instances of the primitive are created, with suffixed names
71      */

72     public void startContextElement(String JavaDoc name, Attributes attributes)
73         throws SAXException JavaDoc {
74         names = null;
75         if (logger.isDebugEnabled()) {
76             logger.debug("startContextElement : " + name + "of : " +
77                 controllerDescription.getName());
78         }
79
80         // for (int i = 0; i < attributes.getLength(); i++) {
81
// System.out.println("ATTRIBUTES [" + i + "] : " + attributes.getValue(i));
82
// }
83
super.startContextElement(name, attributes);
84
85         String JavaDoc implementation = attributes.getValue(ComponentsDescriptorConstants.PRIMITIVE_COMPONENT_IMPLEMENTATION_TAG);
86         if (!checkNonEmpty(implementation)) {
87             throw new SAXException JavaDoc("Component's implementation unspecified");
88         }
89
90         // check whether the given parameter is actually a class. A possible error is to put an interface
91
try {
92             if (Class.forName(implementation).isInterface()) {
93                 throw new SAXException JavaDoc(implementation +
94                     " is an interface. You cannot specify an interface as the implementation of a component.");
95             }
96         } catch (ClassNotFoundException JavaDoc e) {
97             throw new SAXException JavaDoc("Specified class for implementation of the component is not in the classpath ",
98                 e);
99         }
100
101         // instantiate the component and add a stub on it to the cache
102
// if several nodes are mapped onto this virtual node :
103
// instantiate 1 component on each of the nodes of the cycle - if cyclic
104
try {
105             // get corresponding virtual node
106
VirtualNode vn = null;
107             try {
108                 if (virtualNode.equals(ComponentsDescriptorConstants.NULL)) {
109                     componentsCache.addComponent(controllerDescription.getName(),
110                         cf.newFcInstance(componentType, controllerDescription,
111                             new ContentDescription(implementation,
112                                 new Object JavaDoc[] { })));
113                     return;
114                 }
115                 vn = deploymentDescriptor.getVirtualNode(virtualNode);
116             } catch (NullPointerException JavaDoc npe) {
117                 logger.fatal(
118                     "Could not find virtual node. Maybe virtual node names do not match between components descriptor and deployment descriptor");
119                 return;
120             }
121             Component components = cf.newFcInstance(componentType,
122                     controllerDescription,
123                     new ContentDescription(implementation, new Object JavaDoc[] { }, vn));
124
125             if (!ProActiveGroup.isGroup(components)) {
126                 // 1. virtual node was corresponding to 1 node ==> only 1 component has been created
127
componentsCache.addComponent(controllerDescription.getName(),
128                     components);
129             } else {
130                 // 2. virtual node is cyclic; components are created on multiple nodes
131
Component[] components_table = (Component[]) (ProActiveGroup.getGroup(components)
132                                                                             .toArray(new Component[ProActiveGroup.getGroup(
133                             components).size()]));
134
135                 // keep the names of all components for binding them later
136
names = new String JavaDoc[components_table.length];
137                 // ordering in iteration is guaranteed as underlying class containing the elements of the group is a vector
138
for (int i = 0; i < components_table.length; i++) {
139                     names[i] = controllerDescription.getName() +
140                         Constants.CYCLIC_NODE_SUFFIX + i;
141                     componentsCache.addComponent(names[i], components_table[i]);
142                 }
143             }
144         } catch (InstantiationException JavaDoc e) {
145             logger.error("cannot create active component " + e.getMessage());
146             throw new SAXException JavaDoc(e);
147         }
148         if (logger.isDebugEnabled()) {
149             logger.debug("created primitive component : " +
150                 controllerDescription.getName());
151         }
152     }
153
154     /* (non-Javadoc)
155      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
156      */

157     public Object JavaDoc getResultObject() throws SAXException JavaDoc {
158         if (names != null) {
159             // there are several primitives components based on the same name
160
return new ComponentResultObject(names);
161         } else {
162             return new ComponentResultObject(controllerDescription.getName());
163         }
164     }
165
166     /* (non-Javadoc)
167      * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
168      */

169     protected void notifyEndActiveHandler(String JavaDoc name,
170         UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
171     }
172 }
173
Popular Tags