KickJava   Java API By Example, From Geeks To Geeks.

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


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.factory.InstantiationException;
34 import org.objectweb.fractal.api.type.InterfaceType;
35 import org.objectweb.fractal.api.type.TypeFactory;
36
37 import org.objectweb.proactive.core.component.type.ProActiveTypeFactory;
38 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
39 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
40 import org.objectweb.proactive.core.xml.handler.CollectionUnmarshaller;
41 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
42 import org.objectweb.proactive.core.xml.io.Attributes;
43
44 import org.xml.sax.SAXException JavaDoc;
45
46 import java.util.Vector JavaDoc;
47
48
49 /**
50  * @author Matthieu Morel
51  */

52 public class ComponentTypeHandler extends AbstractUnmarshallerDecorator {
53     Vector JavaDoc interfaceTypes;
54     String JavaDoc typeName;
55
56     public ComponentTypeHandler() {
57         interfaceTypes = new Vector JavaDoc();
58         addHandler(ComponentsDescriptorConstants.PROVIDES_TAG,
59             new ProvidesHandler());
60         addHandler(ComponentsDescriptorConstants.REQUIRES_TAG,
61             new RequiresHandler());
62     }
63
64     /**
65      * resets the variables so that this handler can be reused properly for the next type
66      * occurence
67      */

68     public void reset() {
69         interfaceTypes.removeAllElements();
70         typeName = null;
71     }
72
73     /* (non-Javadoc)
74      * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
75      */

76     protected void notifyEndActiveHandler(String JavaDoc name,
77         UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
78     }
79
80     /* (non-Javadoc)
81      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
82      */

83     public Object JavaDoc getResultObject() throws SAXException JavaDoc {
84         try {
85             // return a (typeName / ComponentType instance) couple.
86
interfaceTypes.trimToSize();
87             InterfaceType[] itf_types = (InterfaceType[]) interfaceTypes.toArray(new InterfaceType[interfaceTypes.size()]);
88             return new Object JavaDoc[] {
89                 typeName,
90                 ProActiveTypeFactory.instance().createFcType(itf_types)
91             };
92         } catch (InstantiationException JavaDoc e) {
93             throw new SAXException JavaDoc("cannot create component type");
94         }
95     }
96
97     /* (non-Javadoc)
98      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
99      */

100     public void startContextElement(String JavaDoc name, Attributes attributes)
101         throws SAXException JavaDoc {
102         String JavaDoc type_name = attributes.getValue(ComponentsDescriptorConstants.COMPONENT_TYPE_NAME_TAG);
103         if (!checkNonEmpty(type_name)) {
104             throw new SAXException JavaDoc(
105                 "the name of the component type needs to be specified");
106         }
107         typeName = type_name;
108     }
109
110     // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
111
// provides has no attributes, but contains several interface elements
112
private class ProvidesHandler extends CollectionUnmarshaller {
113         private ProvidesHandler() {
114             addHandler(ComponentsDescriptorConstants.INTERFACE_TAG,
115                 new InterfaceHandler(ComponentsDescriptorConstants.PROVIDES_TAG));
116         }
117
118         /* (non-Javadoc)
119          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
120          */

121         protected void notifyEndActiveHandler(String JavaDoc name,
122             UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
123         }
124
125         /* (non-Javadoc)
126          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
127          */

128         public Object JavaDoc getResultObject() throws SAXException JavaDoc {
129             return null;
130         }
131     }
132
133     // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
134
// requires has no attributes, but contains several interface elements
135
private class RequiresHandler extends CollectionUnmarshaller {
136         private RequiresHandler() {
137             addHandler(ComponentsDescriptorConstants.INTERFACE_TAG,
138                 new InterfaceHandler(ComponentsDescriptorConstants.REQUIRES_TAG));
139         }
140
141         /* (non-Javadoc)
142          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
143          */

144         protected void notifyEndActiveHandler(String JavaDoc name,
145             UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
146         }
147     }
148
149     // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
150
protected class InterfaceHandler extends BasicUnmarshaller {
151         boolean isClient;
152         boolean contingency;
153         boolean cardinality;
154
155         private InterfaceHandler(String JavaDoc role) {
156             if (role.equals(ComponentsDescriptorConstants.REQUIRES_TAG)) {
157                 isClient = true;
158             } else if (role.equals(ComponentsDescriptorConstants.PROVIDES_TAG)) {
159                 isClient = false;
160             } else {
161                 throw new RuntimeException JavaDoc(
162                     "none of the following tags was found : " +
163                     ComponentsDescriptorConstants.REQUIRES_TAG + ',' +
164                     ComponentsDescriptorConstants.PROVIDES_TAG);
165             }
166         }
167
168         public void startContextElement(String JavaDoc name, Attributes attributes)
169             throws org.xml.sax.SAXException JavaDoc {
170             ComponentHandler.logger.debug("#####startContextElement : " + name);
171             // store all data in a FunctionalInterfaceData object
172
String JavaDoc itf_name = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_NAME_TAG);
173             if (!checkNonEmpty(itf_name)) {
174                 throw new SAXException JavaDoc("interface name unspecified");
175             }
176             String JavaDoc itf_signature = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_SIGNATURE_TAG);
177             if (!checkNonEmpty(itf_signature)) {
178                 throw new SAXException JavaDoc("interface signature unspecified");
179             }
180             String JavaDoc value = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_TAG);
181             if (!checkNonEmpty(value) ||
182                     value.equals(
183                         ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_OPTIONAL_TAG)) {
184                 contingency = TypeFactory.OPTIONAL;
185             } else if (value.equals(
186                         ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_MANDATORY_TAG)) {
187                 contingency = TypeFactory.MANDATORY;
188             } else {
189                 throw new SAXException JavaDoc("contingency values are : " +
190                     ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_MANDATORY_TAG +
191                     ", " +
192                     ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_OPTIONAL_TAG +
193                     ", or nothing (if schema validation is enabled) (default is " +
194                     ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_OPTIONAL_TAG +
195                     ")");
196             }
197
198             value = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_CARDINALITY_TAG);
199             if (checkNonEmpty(value)) {
200                 if (value.equals(
201                             ComponentsDescriptorConstants.INTERFACE_CARDINALITY_SINGLE_TAG)) {
202                     cardinality = TypeFactory.SINGLE;
203                 } else if (value.equals(
204                             ComponentsDescriptorConstants.INTERFACE_CARDINALITY_COLLECTIVE_TAG)) {
205                     cardinality = TypeFactory.COLLECTION;
206                 } else {
207                     throw new SAXException JavaDoc("cardinality values are : " +
208                         ComponentsDescriptorConstants.INTERFACE_CARDINALITY_COLLECTIVE_TAG +
209                         ", " +
210                         ComponentsDescriptorConstants.INTERFACE_CARDINALITY_SINGLE_TAG +
211                         ", or nothing (if schema validation is enabled) (default is " +
212                         ComponentsDescriptorConstants.INTERFACE_CARDINALITY_SINGLE_TAG +
213                         ") ");
214                 }
215             }
216
217             InterfaceType itf_type = null;
218             try {
219                 itf_type = ProActiveTypeFactory.instance().createFcItfType(itf_name,
220                         itf_signature, isClient, contingency, cardinality);
221             } catch (InstantiationException JavaDoc e) {
222                 throw new SAXException JavaDoc(e);
223             }
224
225             //componentParameters.addInterfaceTypes((InterfaceType[]) activeHandler.getResultObject());
226
if (logger.isDebugEnabled()) {
227                 ComponentHandler.logger.debug("new interface type added: " +
228                     itf_type.getFcItfName());
229             }
230
231             interfaceTypes.add(itf_type);
232             //componentParameters.addInterfaceType(itf_type);
233
//setResultObject(itf_type);
234
}
235     }
236 }
237
Popular Tags