KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > descriptor > xml > DeploymentHandler


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 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.descriptor.xml;
32
33 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
34 import org.objectweb.proactive.core.descriptor.data.VirtualMachine;
35 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
36 import org.objectweb.proactive.core.descriptor.data.VirtualNodeImpl;
37 import org.objectweb.proactive.core.descriptor.data.VirtualNodeLookup;
38 import org.objectweb.proactive.core.util.UrlBuilder;
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.PassiveCompositeUnmarshaller;
42 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
43 import org.objectweb.proactive.core.xml.io.Attributes;
44
45
46 /**
47  * This class receives deployment events
48  *
49  * @author ProActive Team
50  * @version 1.0, 2002/09/20
51  * @since ProActive 0.9.3
52  */

53 class DeploymentHandler extends PassiveCompositeUnmarshaller
54     implements ProActiveDescriptorConstants {
55     private ProActiveDescriptor proActiveDescriptor;
56
57     //
58
// ----- PRIVATE MEMBERS -----------------------------------------------------------------------------------
59
//
60
//
61
// ----- CONSTRUCTORS -----------------------------------------------------------------------------------
62
//
63
public DeploymentHandler(ProActiveDescriptor proActiveDescriptor) {
64         super(false);
65         this.proActiveDescriptor = proActiveDescriptor;
66         this.addHandler(REGISTER_TAG, new RegisterHandler());
67         this.addHandler(LOOKUP_TAG, new LookupHandler());
68         {
69             PassiveCompositeUnmarshaller ch = new PassiveCompositeUnmarshaller();
70             ch.addHandler(MAP_TAG, new MapHandler());
71             this.addHandler(MAPPING_TAG, ch);
72         }
73         {
74             PassiveCompositeUnmarshaller ch = new PassiveCompositeUnmarshaller();
75             ch.addHandler(JVM_TAG, new JVMHandler());
76             this.addHandler(JVMS_TAG, ch);
77         }
78     }
79
80     //
81
// ----- PUBLIC METHODS -----------------------------------------------------------------------------------
82
//
83
//
84
// -- implements UnmarshallerHandler ------------------------------------------------------
85
//
86
//
87
// ----- PRIVATE METHODS -----------------------------------------------------------------------------------
88
//
89
//
90
// ----- INNER CLASSES -----------------------------------------------------------------------------------
91
//
92
private class RegisterHandler extends BasicUnmarshaller {
93         private RegisterHandler() {
94         }
95
96         public void startContextElement(String JavaDoc name, Attributes attributes)
97             throws org.xml.sax.SAXException JavaDoc {
98             String JavaDoc vn = attributes.getValue("virtualNode");
99             if (!checkNonEmpty(vn)) {
100                 throw new org.xml.sax.SAXException JavaDoc(
101                     "register Tag without any virtualnode defined");
102             }
103             String JavaDoc protocol = attributes.getValue("protocol");
104             if (!checkNonEmpty(protocol)) {
105                 throw new org.xml.sax.SAXException JavaDoc(
106                     "lookup Tag without any protocol defined");
107             }
108             protocol = UrlBuilder.checkProtocol(protocol);
109             VirtualNodeImpl vnImpl = (VirtualNodeImpl) proActiveDescriptor.createVirtualNode(vn,
110                     false);
111
112             //VirtualNodeImpl vnImpl= (VirtualNodeImpl)vnStrat.getVirtualNode();
113
//vnImpl.setRegistrationValue(true);
114
vnImpl.setRegistrationProtocol(protocol);
115         }
116     }
117
118     private class LookupHandler extends BasicUnmarshaller {
119         private LookupHandler() {
120         }
121
122         public void startContextElement(String JavaDoc name, Attributes attributes)
123             throws org.xml.sax.SAXException JavaDoc {
124             String JavaDoc vnLookup = attributes.getValue("virtualNode");
125             if (!checkNonEmpty(vnLookup)) {
126                 throw new org.xml.sax.SAXException JavaDoc(
127                     "lookup Tag without any virtualnode defined");
128             }
129             String JavaDoc protocol = attributes.getValue("protocol");
130             if (!checkNonEmpty(protocol)) {
131                 throw new org.xml.sax.SAXException JavaDoc(
132                     "lookup Tag without any protocol defined");
133             }
134             String JavaDoc host = attributes.getValue("host");
135             if (!checkNonEmpty(host) && protocol.equals("rmi")) {
136                 throw new org.xml.sax.SAXException JavaDoc(
137                     "within a lookup tag attribute host must be defined for rmi protocol");
138             }
139             protocol = UrlBuilder.checkProtocol(protocol);
140             String JavaDoc url = UrlBuilder.buildUrl(host, vnLookup, protocol);
141             VirtualNodeLookup vn = (VirtualNodeLookup) proActiveDescriptor.createVirtualNode(vnLookup,
142                     true);
143
144             // vn.setLookupInformations(url,protocol);
145
String JavaDoc port = attributes.getValue("port");
146
147             //System.out.println(port);
148
if (checkNonEmpty(port)) {
149                 if (protocol.equals("jini:")) {
150                     throw new org.xml.sax.SAXException JavaDoc(
151                         "For a jini lookup, no port number should be specified");
152                 }
153                 url = UrlBuilder.buildUrl(host, vnLookup, protocol,
154                         new Integer JavaDoc(port).intValue());
155                 vn.setLookupInformations(url, protocol,
156                     new Integer JavaDoc(port).intValue());
157                 //if no port is specified we use 1099 since it is the default port. Even if it is jini
158
// the UrlBuilder will not use the port when building the url
159
} else {
160                 vn.setLookupInformations(url, protocol, 1099);
161             }
162         }
163     }
164
165     /**
166      * This class receives map events
167      */

168     private class MapHandler extends PassiveCompositeUnmarshaller {
169         VirtualNode vn;
170
171         private MapHandler() {
172             // CollectionUnmarshaller cu = new CollectionUnmarshaller(String.class);
173
// cu.addHandler(VMNAME_TAG, new VmNameHandler());
174
// this.addHandler(JVMSET_TAG, cu);
175
this.addHandler(JVMSET_TAG, new JvmSetHandler());
176         }
177
178         public void startContextElement(String JavaDoc name, Attributes attributes)
179             throws org.xml.sax.SAXException JavaDoc {
180             // create and register a VirtualNode
181
String JavaDoc vnName = attributes.getValue("virtualNode");
182             if (!checkNonEmpty(vnName)) {
183                 throw new org.xml.sax.SAXException JavaDoc(
184                     "mapping defined without specifying virtual node");
185             }
186             vn = proActiveDescriptor.createVirtualNode(vnName, false);
187         }
188
189         protected void notifyEndActiveHandler(String JavaDoc name,
190             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException JavaDoc {
191             if (name.equals(JVMSET_TAG)) {
192                 String JavaDoc[] vmNames = (String JavaDoc[]) activeHandler.getResultObject();
193
194                 //throws an exception if vn has property unique or unique_singleAO and more than one vm are defined
195
if ((vmNames.length > 1) && (vn.getProperty() != null) &&
196                         (vn.getProperty().equals("unique") ||
197                         vn.getProperty().equals("unique_singleAO"))) {
198                     throw new org.xml.sax.SAXException JavaDoc(
199                         "a set of virtual machine is defined for a virtualNode that is unique");
200                 }
201                 if (vmNames.length > 0) {
202                     for (int i = 0; i < vmNames.length; i++) {
203                         VirtualMachine vm = proActiveDescriptor.createVirtualMachine(vmNames[i]);
204
205                         if (vm.getCreatorId() == null) {
206                             vm.setCreatorId(vn.getName());
207                         }
208                         vn.addVirtualMachine(vm);
209                     }
210                 }
211             }
212         }
213
214         //
215
// -- INNER CLASSES ------------------------------------------------------
216
//
217
private class SingleValueUnmarshaller extends BasicUnmarshaller {
218             public void readValue(String JavaDoc value) throws org.xml.sax.SAXException JavaDoc {
219                 setResultObject(value);
220             }
221         }
222          //end of inner class SingleValueUnmarshaller
223

224         private class JvmSetHandler extends CollectionUnmarshaller {
225             protected JvmSetHandler() {
226                 super(String JavaDoc.class);
227                 this.addHandler(VMNAME_TAG, new VmNameHandler());
228                 this.addHandler(CURRENTJVM_TAG, new CurrentJvmHandler());
229             }
230
231             protected void notifyEndActiveHandler(String JavaDoc name,
232                 UnmarshallerHandler activeHandler)
233                 throws org.xml.sax.SAXException JavaDoc {
234                 if (name.equals(CURRENTJVM_TAG)) {
235                     String JavaDoc protocol = (String JavaDoc) activeHandler.getResultObject();
236                     vn.createNodeOnCurrentJvm(protocol);
237                 } else {
238                     super.notifyEndActiveHandler(name, activeHandler);
239                 }
240             }
241         }
242          //end of inner class JvmSetHandler
243

244         private class VmNameHandler extends BasicUnmarshaller {
245             private VmNameHandler() {
246             }
247
248             public void startContextElement(String JavaDoc name, Attributes attributes)
249                 throws org.xml.sax.SAXException JavaDoc {
250                 String JavaDoc vmName = attributes.getValue("value");
251                 if (checkNonEmpty(vmName)) {
252                     setResultObject(vmName);
253                 } else {
254                     throw new org.xml.sax.SAXException JavaDoc(
255                         "The name of the Jvm cannot be set to an empty string");
256                 }
257             }
258         }
259          //end of inner class VmNameHandler
260

261         private class CurrentJvmHandler extends BasicUnmarshaller {
262             private CurrentJvmHandler() {
263             }
264
265             public void startContextElement(String JavaDoc name, Attributes attributes)
266                 throws org.xml.sax.SAXException JavaDoc {
267                 String JavaDoc protocol = attributes.getValue("protocol");
268                 setResultObject(protocol);
269             }
270         }
271          // end of inner class CurrentJvmHandler
272
}
273      // end inner class MapHandler
274

275     /**
276      * This class receives jvm events
277      */

278     private class JVMHandler extends PassiveCompositeUnmarshaller {
279         private VirtualMachine currentVM;
280
281         private JVMHandler() {
282             //this.addHandler(ACQUISITION_TAG, new AcquisitionHandler());
283
this.addHandler(CREATION_PROCESS_TAG, new CreationHandler());
284         }
285
286         public void startContextElement(String JavaDoc name, Attributes attributes)
287             throws org.xml.sax.SAXException JavaDoc {
288             // create and register a VirtualNode
289
String JavaDoc vmName = attributes.getValue("name");
290             if (!checkNonEmpty(vmName)) {
291                 throw new org.xml.sax.SAXException JavaDoc(
292                     "VirtualMachine defined without name");
293             }
294             currentVM = proActiveDescriptor.createVirtualMachine(vmName);
295             String JavaDoc nodeNumber = attributes.getValue("nodeNumber");
296             try {
297                 if (checkNonEmpty(nodeNumber)) {
298                     currentVM.setHostsNumber(nodeNumber);
299                 }
300             } catch (java.io.IOException JavaDoc e) {
301                 throw new org.xml.sax.SAXException JavaDoc(e);
302             }
303         }
304
305         /**
306          * This class receives acquisition events
307          */

308 // private class AcquisitionHandler extends BasicUnmarshaller {
309
// private AcquisitionHandler() {
310
// }
311
//
312
// public void startContextElement(String name, Attributes attributes)
313
// throws org.xml.sax.SAXException {
314
// String acquisitionMethod = attributes.getValue("method");
315
// //String portNumber = attributes.getValue("port");
316
// if (acquisitionMethod != null) {
317
// currentVM.setAcquisitionMethod(acquisitionMethod);
318
// }
319
//// if (portNumber != null) {
320
//// currentVM.setPortNumber(portNumber);
321
//// }
322
// }
323
// }
324

325         // end inner class AcquisitionHandler
326

327         /**
328          * This class receives acquisition events
329          */

330         private class CreationHandler extends PassiveCompositeUnmarshaller {
331             private CreationHandler() {
332                 this.addHandler(PROCESS_REFERENCE_TAG,
333                     new ProcessReferenceHandler());
334             }
335
336             protected void notifyEndActiveHandler(String JavaDoc name,
337                 UnmarshallerHandler activeHandler)
338                 throws org.xml.sax.SAXException JavaDoc {
339                 Object JavaDoc o = activeHandler.getResultObject();
340                 if (o == null) {
341                     return;
342                 }
343                 if (o instanceof String JavaDoc) {
344                     // its an id
345
proActiveDescriptor.registerProcess(currentVM, (String JavaDoc) o);
346                 }
347             }
348         }
349
350         // end inner class CreationHandler
351
}
352
353     // end inner class JVMHandler
354
}
355
Popular Tags