KickJava   Java API By Example, From Geeks To Geeks.

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


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.proactive.core.descriptor.data.ProActiveDescriptor;
36 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
37 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
38 import org.objectweb.proactive.core.xml.io.Attributes;
39
40 import org.xml.sax.SAXException JavaDoc;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.List JavaDoc;
45
46
47 /**
48  * @author Matthieu Morel
49  */

50 public class ComponentsHandler extends AbstractUnmarshallerDecorator
51     implements ContainerHandlerMarker {
52     public static Logger logger = Logger.getLogger(ComponentsHandler.class.getName());
53     private ProActiveDescriptor deploymentDescriptor;
54     private ComponentsCache componentsCache;
55     private HashMap JavaDoc componentTypes;
56     private List JavaDoc subComponents;
57     private boolean enabled;
58     private ContainerElementHierarchy containersHierarchy;
59
60     public ComponentsHandler(ProActiveDescriptor deploymentDescriptor,
61         ComponentsCache componentsCache, HashMap JavaDoc componentTypes,
62         AbstractContainerComponentHandler fatherHandler) {
63         enable();
64         containersHierarchy = new ContainerElementHierarchy();
65         containersHierarchy.addFatherHandler(fatherHandler);
66         this.deploymentDescriptor = deploymentDescriptor;
67         this.componentsCache = componentsCache;
68         this.componentTypes = componentTypes;
69
70         // handlers for the different types of components are added when components are effectively encountered
71
// this avoids infinite handlers due to the recursive structure of components
72
BindingsHandler bindings_handler = new BindingsHandler(componentsCache);
73         addHandler(ComponentsDescriptorConstants.BINDINGS_TAG, bindings_handler);
74         subComponents = new ArrayList JavaDoc();
75         getContainerElementHierarchy().disableGrandFatherHandler();
76     }
77
78     /* (non-Javadoc)
79      * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
80      */

81     protected void notifyEndActiveHandler(String JavaDoc name,
82         UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
83         if (isEnabled() ||
84                 getContainerElementHierarchy().containsChild(activeHandler)) {
85             enable(); // just to make sure
86
if (name.equals(
87                         ComponentsDescriptorConstants.PRIMITIVE_COMPONENT_TAG) ||
88                     name.equals(
89                         ComponentsDescriptorConstants.COMPOSITE_COMPONENT_TAG) ||
90                     name.equals(
91                         ComponentsDescriptorConstants.PARALLEL_COMPOSITE_COMPONENT_TAG)) {
92                 // add the name of this sub component to the list
93
ComponentResultObject result = (ComponentResultObject) activeHandler.getResultObject();
94                 if (result.componentsAreParallelized()) {
95                     String JavaDoc[] component_names = result.getNames();
96                     for (int i = 0; i < component_names.length; i++) {
97                         subComponents.add(component_names[i]);
98                         if (logger.isDebugEnabled()) {
99                             logger.debug("adding component's name : " +
100                                 component_names[i]);
101                         }
102                     }
103                 } else {
104                     subComponents.add(result.getName());
105                     if (logger.isDebugEnabled()) {
106                         logger.debug("adding component's name : " +
107                             ((ComponentResultObject) activeHandler.getResultObject()).getName());
108                     }
109                 }
110             }
111         }
112     }
113
114     /* (non-Javadoc)
115      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
116      */

117     public Object JavaDoc getResultObject() throws SAXException JavaDoc {
118         return subComponents;
119     }
120
121     /* (non-Javadoc)
122      * @see org.objectweb.proactive.core.xml.io.XMLHandler#readValue(java.lang.String)
123      */

124     public void readValue(String JavaDoc value) throws SAXException JavaDoc {
125         super.readValue(value);
126     }
127
128     /* (non-Javadoc)
129      * @see org.objectweb.proactive.core.xml.io.XMLHandler#startElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
130      */

131     public void startElement(String JavaDoc name, Attributes attributes)
132         throws SAXException JavaDoc {
133         if (isEnabled()) {
134             if (name.equals(
135                         ComponentsDescriptorConstants.COMPOSITE_COMPONENT_TAG)) {
136                 CompositeComponentHandler handler = new CompositeComponentHandler(deploymentDescriptor,
137                         componentsCache, componentTypes, this);
138                 getContainerElementHierarchy().addChildContainerHandler(handler);
139                 addHandler(ComponentsDescriptorConstants.COMPOSITE_COMPONENT_TAG,
140                     handler);
141             }
142             if (name.equals(
143                         ComponentsDescriptorConstants.PRIMITIVE_COMPONENT_TAG)) {
144                 addHandler(ComponentsDescriptorConstants.PRIMITIVE_COMPONENT_TAG,
145                     new PrimitiveComponentHandler(deploymentDescriptor,
146                         componentsCache, componentTypes));
147             }
148             if (name.equals(
149                         ComponentsDescriptorConstants.PARALLEL_COMPOSITE_COMPONENT_TAG)) {
150                 ParallelCompositeComponentHandler handler = new ParallelCompositeComponentHandler(deploymentDescriptor,
151                         componentsCache, componentTypes, this);
152                 addHandler(ComponentsDescriptorConstants.PARALLEL_COMPOSITE_COMPONENT_TAG,
153                     handler);
154                 getContainerElementHierarchy().addChildContainerHandler(handler);
155             }
156         }
157         super.startElement(name, attributes);
158     }
159
160     public void enable() {
161         enabled = true;
162     }
163
164     public void disable() {
165         enabled = false;
166     }
167
168     public boolean isEnabled() {
169         return enabled;
170     }
171
172     /* (non-Javadoc)
173      * @see org.objectweb.proactive.core.component.xml.ContainerHandlerMarker#getContainerElementHierarchy()
174      */

175     public ContainerElementHierarchy getContainerElementHierarchy() {
176         return containersHierarchy;
177     }
178
179     /* (non-Javadoc)
180      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
181      */

182     public void startContextElement(String JavaDoc name, Attributes attributes)
183         throws SAXException JavaDoc {
184         // Auto-generated method stub
185
}
186 }
187
Popular Tags