org/objectweb/proactive/core/descriptor/xml/DeploymentHandler.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.descriptor.xml;
00032 
00033 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
00034 import org.objectweb.proactive.core.descriptor.data.VirtualMachine;
00035 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
00036 import org.objectweb.proactive.core.descriptor.data.VirtualNodeImpl;
00037 import org.objectweb.proactive.core.descriptor.data.VirtualNodeLookup;
00038 import org.objectweb.proactive.core.util.UrlBuilder;
00039 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
00040 import org.objectweb.proactive.core.xml.handler.CollectionUnmarshaller;
00041 import org.objectweb.proactive.core.xml.handler.PassiveCompositeUnmarshaller;
00042 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
00043 import org.objectweb.proactive.core.xml.io.Attributes;
00044 
00045 
00053 class DeploymentHandler extends PassiveCompositeUnmarshaller
00054     implements ProActiveDescriptorConstants {
00055     private ProActiveDescriptor proActiveDescriptor;
00056     private boolean activate = true;
00057 
00058     //
00059     //  ----- PRIVATE MEMBERS -----------------------------------------------------------------------------------
00060     //
00061     //
00062     //  ----- CONSTRUCTORS -----------------------------------------------------------------------------------
00063     //
00064     public DeploymentHandler(ProActiveDescriptor proActiveDescriptor) {
00065         super(false);
00066         this.proActiveDescriptor = proActiveDescriptor;
00067         this.addHandler(REGISTER_TAG, new RegisterHandler());
00068         this.addHandler(LOOKUP_TAG, new LookupHandler());
00069 
00070         {
00071             PassiveCompositeUnmarshaller ch = new PassiveCompositeUnmarshaller();
00072             ch.addHandler(MAP_TAG, new MapHandler());
00073             this.addHandler(MAPPING_TAG, ch);
00074         }
00075 
00076         {
00077             PassiveCompositeUnmarshaller ch = new PassiveCompositeUnmarshaller();
00078             ch.addHandler(JVM_TAG, new JVMHandler());
00079             this.addHandler(JVMS_TAG, ch);
00080         }
00081     }
00082 
00090     public DeploymentHandler(ProActiveDescriptor proActiveDescriptor,
00091         boolean activate) {
00092         super(false);
00093         this.activate = activate;
00094         this.proActiveDescriptor = proActiveDescriptor;
00095         this.addHandler(REGISTER_TAG, new RegisterHandler());
00096         this.addHandler(LOOKUP_TAG, new LookupHandler());
00097 
00098         {
00099             PassiveCompositeUnmarshaller ch = new PassiveCompositeUnmarshaller();
00100             ch.addHandler(MAP_TAG, new MapHandler());
00101             this.addHandler(MAPPING_TAG, ch);
00102         }
00103 
00104         {
00105             PassiveCompositeUnmarshaller ch = new PassiveCompositeUnmarshaller();
00106             ch.addHandler(JVM_TAG, new JVMHandler());
00107             this.addHandler(JVMS_TAG, ch);
00108         }
00109     }
00110 
00111     //
00112     //  ----- PUBLIC METHODS -----------------------------------------------------------------------------------
00113     //
00114     //
00115     // -- implements UnmarshallerHandler ------------------------------------------------------
00116     //
00117     //
00118     //  ----- PRIVATE METHODS -----------------------------------------------------------------------------------
00119     //
00120     //
00121     //  ----- INNER CLASSES -----------------------------------------------------------------------------------
00122     //
00123     private class RegisterHandler extends BasicUnmarshaller {
00124         private RegisterHandler() {
00125         }
00126 
00127         public void startContextElement(String name, Attributes attributes)
00128             throws org.xml.sax.SAXException {
00129             String vn = attributes.getValue("virtualNode");
00130 
00131             if (!checkNonEmpty(vn)) {
00132                 throw new org.xml.sax.SAXException(
00133                     "register Tag without any virtualnode defined");
00134             }
00135 
00136             String protocol = attributes.getValue("protocol");
00137 
00138             if (!checkNonEmpty(protocol)) {
00139                 protocol = System.getProperty(
00140                         "proactive.communication.protocol");
00141             }
00142 
00143             protocol = UrlBuilder.checkProtocol(protocol);
00144 
00145             VirtualNodeImpl vnImpl = (VirtualNodeImpl) proActiveDescriptor.createVirtualNode(vn,
00146                     false);
00147 
00148             //VirtualNodeImpl vnImpl= (VirtualNodeImpl)vnStrat.getVirtualNode();
00149             //vnImpl.setRegistrationValue(true);
00150             vnImpl.setRegistrationProtocol(protocol);
00151         }
00152     }
00153 
00154     private class LookupHandler extends BasicUnmarshaller {
00155         private LookupHandler() {
00156         }
00157 
00158         public void startContextElement(String name, Attributes attributes)
00159             throws org.xml.sax.SAXException {
00160             String vnLookup = attributes.getValue("virtualNode");
00161 
00162             if (!checkNonEmpty(vnLookup)) {
00163                 throw new org.xml.sax.SAXException(
00164                     "lookup Tag without any virtualnode defined");
00165             }
00166 
00167             String protocol = attributes.getValue("protocol");
00168 
00169             if (!checkNonEmpty(protocol)) {
00170                 throw new org.xml.sax.SAXException(
00171                     "lookup Tag without any protocol defined");
00172             }
00173 
00174             String host = attributes.getValue("host");
00175 
00176             if (!checkNonEmpty(host) && protocol.equals("rmi")) {
00177                 throw new org.xml.sax.SAXException(
00178                     "within a lookup tag attribute host must be defined for rmi protocol");
00179             }
00180 
00181             protocol = UrlBuilder.checkProtocol(protocol);
00182 
00183             // String url = UrlBuilder.buildUrl(host, vnLookup, protocol);
00184             VirtualNodeLookup vn = (VirtualNodeLookup) proActiveDescriptor.createVirtualNode(vnLookup,
00185                     true);
00186 
00187             //          vn.setLookupInformations(url,protocol);
00188             String port = attributes.getValue("port");
00189 
00190             //System.out.println(port);
00191             if (checkNonEmpty(port)) {
00192                 if (protocol.equals("jini:")) {
00193                     throw new org.xml.sax.SAXException(
00194                         "For a jini lookup, no port number should be specified");
00195                 }
00196 
00197                 vn.setLookupInformations(host, protocol, port);
00198 
00199                 //if no port is specified we use 1099 since it is the default port. Even if it is jini
00200                 // the UrlBuilder will not use the port when building the url
00201             } else {
00202                 vn.setLookupInformations(host, protocol, "1099");
00203             }
00204         }
00205     }
00206 
00210     private class MapHandler extends PassiveCompositeUnmarshaller {
00211         VirtualNode vn;
00212 
00213         private MapHandler() {
00214             //          CollectionUnmarshaller cu = new CollectionUnmarshaller(String.class);
00215             //                  cu.addHandler(VMNAME_TAG, new VmNameHandler());
00216             //          this.addHandler(JVMSET_TAG, cu);
00217             this.addHandler(JVMSET_TAG, new JvmSetHandler());
00218         }
00219 
00220         public void startContextElement(String name, Attributes attributes)
00221             throws org.xml.sax.SAXException {
00222             // create and register a VirtualNode
00223             String vnName = attributes.getValue("virtualNode");
00224 
00225             if (!checkNonEmpty(vnName)) {
00226                 throw new org.xml.sax.SAXException(
00227                     "mapping defined without specifying virtual node");
00228             }
00229 
00230             vn = proActiveDescriptor.createVirtualNode(vnName, false);
00231         }
00232 
00233         protected void notifyEndActiveHandler(String name,
00234             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00235             if (name.equals(JVMSET_TAG)) {
00236                 String[] vmNames = (String[]) activeHandler.getResultObject();
00237 
00238                 //throws an exception if vn has property unique or unique_singleAO and more than one vm are defined
00239                 if ((vmNames.length > 1) && (vn.getProperty() != null) &&
00240                         (vn.getProperty().equals("unique") ||
00241                         vn.getProperty().equals("unique_singleAO"))) {
00242                     throw new org.xml.sax.SAXException(
00243                         "a set of virtual machine is defined for a virtualNode that is unique");
00244                 }
00245 
00246                 if (vmNames.length > 0) {
00247                     for (int i = 0; i < vmNames.length; i++) {
00248                         VirtualMachine vm = proActiveDescriptor.createVirtualMachine(vmNames[i]);
00249 
00250                         vn.addVirtualMachine(vm);
00251                     }
00252                 }
00253             }
00254         }
00255 
00256         //
00257         // -- INNER CLASSES ------------------------------------------------------
00258         //
00259         private class JvmSetHandler extends CollectionUnmarshaller {
00260             protected JvmSetHandler() {
00261                 super(String.class);
00262                 this.addHandler(VMNAME_TAG, new VmNameHandler());
00263                 this.addHandler(CURRENTJVM_TAG, new CurrentJvmHandler());
00264             }
00265 
00266             protected void notifyEndActiveHandler(String name,
00267                 UnmarshallerHandler activeHandler)
00268                 throws org.xml.sax.SAXException {
00269                 if (name.equals(CURRENTJVM_TAG)) {
00270                     // we create the node if and only if we are activating the mapping.
00271                     // this is used by the scheduler to ensure that when parsing for 
00272                     // description we don't create a node in the currentJVM
00273                     if (activate) {
00274                         String protocol = (String) activeHandler.getResultObject();
00275 
00276                         if (!checkNonEmpty(protocol)) {
00277                             protocol = System.getProperty(
00278                                     "proactive.communication.protocol");
00279                         }
00280 
00281                         vn.createNodeOnCurrentJvm(protocol);
00282                     } else {
00283                         throw new org.xml.sax.SAXException(
00284                             "The use of the currentJVM tag is banned");
00285                     }
00286                 } else {
00287                     super.notifyEndActiveHandler(name, activeHandler);
00288                 }
00289             }
00290 
00291             //end of inner class JvmSetHandler
00292             private class VmNameHandler extends BasicUnmarshaller {
00293                 private VmNameHandler() {
00294                 }
00295 
00296                 public void startContextElement(String name,
00297                     Attributes attributes) throws org.xml.sax.SAXException {
00298                     String vmName = attributes.getValue("value");
00299 
00300                     if (checkNonEmpty(vmName)) {
00301                         setResultObject(vmName);
00302                     } else {
00303                         throw new org.xml.sax.SAXException(
00304                             "The name of the Jvm cannot be set to an empty string");
00305                     }
00306                 }
00307             } //end of inner class VmNameHandler
00308 
00309             private class CurrentJvmHandler extends BasicUnmarshaller {
00310                 private CurrentJvmHandler() {
00311                 }
00312 
00313                 public void startContextElement(String name,
00314                     Attributes attributes) throws org.xml.sax.SAXException {
00315                     String protocol = attributes.getValue("protocol");
00316                     setResultObject(protocol);
00317                 }
00318             } // end of inner class CurrentJvmHandler
00319         } // end of inner class JvmSetHandler
00320     }
00321 
00322     // end inner class MapHandler
00323 
00327     private class JVMHandler extends PassiveCompositeUnmarshaller {
00328         private VirtualMachine currentVM;
00329 
00330         private JVMHandler() {
00331             this.addHandler(ACQUISITION_TAG, new AcquisitionHandler());
00332             this.addHandler(CREATION_PROCESS_TAG, new CreationHandler());
00333         }
00334 
00335         public void startContextElement(String name, Attributes attributes)
00336             throws org.xml.sax.SAXException {
00337             // create and register a VirtualNode
00338             String vmName = attributes.getValue("name");
00339 
00340             if (!checkNonEmpty(vmName)) {
00341                 throw new org.xml.sax.SAXException(
00342                     "VirtualMachine defined without name");
00343             }
00344 
00345             currentVM = proActiveDescriptor.createVirtualMachine(vmName);
00346 
00347             String nodeNumber = attributes.getValue("askedNodes");
00348 
00349             try {
00350                 if (checkNonEmpty(nodeNumber)) {
00351                     currentVM.setNbNodes(nodeNumber);
00352                 }
00353             } catch (java.io.IOException e) {
00354                 throw new org.xml.sax.SAXException(e);
00355             }
00356         }
00357 
00361         private class AcquisitionHandler extends PassiveCompositeUnmarshaller {
00362             private AcquisitionHandler() {
00363                 this.addHandler(SERVICE_REFERENCE_TAG,
00364                     new ProcessReferenceHandler());
00365             }
00366 
00367             protected void notifyEndActiveHandler(String name,
00368                 UnmarshallerHandler activeHandler)
00369                 throws org.xml.sax.SAXException {
00370                 Object o = activeHandler.getResultObject();
00371 
00372                 if (o == null) {
00373                     return;
00374                 }
00375 
00376                 proActiveDescriptor.registerService(currentVM, (String) o);
00377             }
00378 
00379             //            public void startContextElement(String name, Attributes attributes)
00380             //                throws org.xml.sax.SAXException {
00381             //                String runtimeURL = attributes.getValue("url");
00382             //
00383             //                //String portNumber = attributes.getValue("port");
00384             //                if (runtimeURL != null) {
00385             //                    String protocol = UrlBuilder.getProtocol(runtimeURL);
00386             //                    String url = UrlBuilder.removeProtocol(runtimeURL, protocol);
00387             //                    proActiveDescriptor.registerProcess(currentVM,
00388             //                        (String) runtimeURL);
00389             //                    ProActiveRuntime proActiveRuntimeRegistered = null;
00390             //                    try {
00391             //                        proActiveRuntimeRegistered = RuntimeFactory.getRuntime(url,
00392             //                                protocol);
00393             //                    } catch (ProActiveException e) {
00394             //                        e.printStackTrace();
00395             //                    }
00396             //                    currentVM.setAcquired(true);
00397             //                    currentVM.setRemoteRuntime(proActiveRuntimeRegistered);
00398             //                    //currentVM.setAcquisitionMethod(acquisitionMethod);
00399             //                }
00403             //   }
00404         }
00405 
00406         // end inner class AcquisitionHandler
00407 
00411         private class CreationHandler extends PassiveCompositeUnmarshaller {
00412             private CreationHandler() {
00413                 this.addHandler(PROCESS_REFERENCE_TAG,
00414                     new ProcessReferenceHandler());
00415             }
00416 
00417             protected void notifyEndActiveHandler(String name,
00418                 UnmarshallerHandler activeHandler)
00419                 throws org.xml.sax.SAXException {
00420                 Object o = activeHandler.getResultObject();
00421 
00422                 if (o == null) {
00423                     return;
00424                 }
00425 
00426                 if (o instanceof String) {
00427                     // its an id
00428                     proActiveDescriptor.registerProcess(currentVM, (String) o);
00429                 }
00430             }
00431         }
00432 
00433         // end inner class CreationHandler
00434     }
00435 
00436     // end inner class JVMHandler
00437 }

Generated on Mon Jan 22 15:16:07 2007 for ProActive by  doxygen 1.5.1