org/objectweb/proactive/core/descriptor/xml/ProActiveDescriptorHandler.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 java.net.URL;
00034 import java.util.Enumeration;
00035 
00036 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00037 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
00038 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptorImpl;
00039 import org.objectweb.proactive.core.descriptor.data.VirtualNodeImpl;
00040 import org.objectweb.proactive.core.xml.VariableContract;
00041 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
00042 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
00043 import org.objectweb.proactive.core.xml.handler.PassiveCompositeUnmarshaller;
00044 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
00045 import org.objectweb.proactive.core.xml.io.Attributes;
00046 import org.objectweb.proactive.core.xml.io.SAXParserErrorHandlerTerminating;
00047 import org.objectweb.proactive.scheduler.Scheduler;
00048 
00056 public class ProActiveDescriptorHandler extends AbstractUnmarshallerDecorator
00057                 implements ProActiveDescriptorConstants {
00058         protected ProActiveDescriptor proActiveDescriptor;
00059 
00060         private Scheduler scheduler;
00061 
00062         private String jobID;
00063 
00064         //
00065         // -- CONSTRUCTORS -----------------------------------------------
00066         //
00067         public ProActiveDescriptorHandler(String xmlDescriptorUrl,
00068                         VariableContract variableContract) {
00069                 super(false);
00070                 proActiveDescriptor = new ProActiveDescriptorImpl(xmlDescriptorUrl);
00071                 // keep a reference of the variable contract for future use
00072                 proActiveDescriptor.setVariableContract(variableContract);
00073 
00074                 addHandler(MAIN_DEFINITION_TAG, new MainDefinitionHandler(
00075                                 proActiveDescriptor));
00076                 addHandler(DEPLOYMENT_TAG, new DeploymentHandler(proActiveDescriptor));
00077                 addHandler(INFRASTRUCTURE_TAG, new InfrastructureHandler(
00078                                 proActiveDescriptor));
00079                 addHandler(FILE_TRANSFER_DEFINITIONS_TAG,
00080                                 new FileTransferDefinitionsHandler(proActiveDescriptor));
00081                 addHandler(TECHNICAL_SERVICES_TAG, new TechnicalServicesHandler(
00082                                 proActiveDescriptor));
00083                 addHandler(SECURITY_TAG, new SecurityHandler(proActiveDescriptor));
00084 
00085                 {
00086                         PassiveCompositeUnmarshaller compDefHandler = new PassiveCompositeUnmarshaller();
00087                         PassiveCompositeUnmarshaller vNodesDefHandler = new PassiveCompositeUnmarshaller();
00088                         PassiveCompositeUnmarshaller vNodesAcqHandler = new PassiveCompositeUnmarshaller();
00089                         vNodesDefHandler.addHandler(VIRTUAL_NODE_TAG,
00090                                         new VirtualNodeHandler(proActiveDescriptor));
00091                         vNodesAcqHandler.addHandler(VIRTUAL_NODE_TAG,
00092                                         new VirtualNodeLookupHandler());
00093                         compDefHandler.addHandler(VIRTUAL_NODES_DEFINITION_TAG,
00094                                         vNodesDefHandler);
00095                         compDefHandler.addHandler(VIRTUAL_NODES_ACQUISITION_TAG,
00096                                         vNodesAcqHandler);
00097                         this.addHandler(COMPONENT_DEFINITION_TAG, compDefHandler);
00098                 }
00099 
00100                 this.addHandler(VARIABLES_TAG, new VariablesHandler(variableContract));
00101         }
00102 
00103         public ProActiveDescriptorHandler(Scheduler scheduler, String jobId,
00104                         String xmlDescriptorUrl) {
00105                 super(false);
00106                 this.proActiveDescriptor = new ProActiveDescriptorImpl(xmlDescriptorUrl);
00107                 this.scheduler = scheduler;
00108                 this.jobID = jobId;
00109                 addHandler(MAIN_DEFINITION_TAG, new MainDefinitionHandler(scheduler,
00110                                 jobId, this.proActiveDescriptor));
00111                 addHandler(INFRASTRUCTURE_TAG, new InfrastructureHandler(scheduler,
00112                                 jobId, this.proActiveDescriptor));
00113                 addHandler(DEPLOYMENT_TAG, new DeploymentHandler(proActiveDescriptor,
00114                                 false));
00115                 addHandler(FILE_TRANSFER_DEFINITIONS_TAG,
00116                                 new FileTransferDefinitionsHandler(proActiveDescriptor));
00117                 addHandler(TECHNICAL_SERVICES_TAG, new TechnicalServicesHandler(
00118                                 proActiveDescriptor));
00119                 addHandler(SECURITY_TAG, new SecurityHandler(proActiveDescriptor));
00120 
00121                 {
00122                         PassiveCompositeUnmarshaller compDefHandler = new PassiveCompositeUnmarshaller();
00123                         PassiveCompositeUnmarshaller vNodesDefHandler = new PassiveCompositeUnmarshaller();
00124                         PassiveCompositeUnmarshaller vNodesAcqHandler = new PassiveCompositeUnmarshaller();
00125                         vNodesDefHandler.addHandler(VIRTUAL_NODE_TAG,
00126                                         new VirtualNodeHandler(proActiveDescriptor));
00127                         vNodesAcqHandler.addHandler(VIRTUAL_NODE_TAG,
00128                                         new VirtualNodeLookupHandler());
00129                         compDefHandler.addHandler(VIRTUAL_NODES_DEFINITION_TAG,
00130                                         vNodesDefHandler);
00131                         compDefHandler.addHandler(VIRTUAL_NODES_ACQUISITION_TAG,
00132                                         vNodesAcqHandler);
00133                         this.addHandler(COMPONENT_DEFINITION_TAG, compDefHandler);
00134                 }
00135         }
00136 
00137         //
00138         // -- PUBLIC METHODS -----------------------------------------------
00139         //
00140 //      public static void main(String[] args) throws java.io.IOException, org.xml.sax.SAXException {
00141 //              // String uri =
00142 //              // "Z:\\ProActive\\descriptors\\C3D_Dispatcher_Renderer.xml";
00143 //              String uri = "/user/cjarjouh/home/ProActive/descriptors/C3D_Dispatcher_Renderer.xml";
00144 //              InitialHandler h = new InitialHandler(uri, new VariableContract());
00145 //
00146 //              // String uri =
00147 //              // "file:/net/home/rquilici/ProActive/descriptors/C3D_Dispatcher_Renderer.xml";
00148 //              org.objectweb.proactive.core.xml.io.StreamReader sr;
00149 //
00150 //              if ("enable".equals(ProActiveConfiguration.getSchemaValidationState())) {
00151 //                      sr = new org.objectweb.proactive.core.xml.io.StreamReader(
00152 //                                      new org.xml.sax.InputSource(uri), h, null,
00153 //                                      new SAXParserErrorHandlerTerminating());
00154 //              } else {
00155 //                      sr = new org.objectweb.proactive.core.xml.io.StreamReader(
00156 //                                      new org.xml.sax.InputSource(uri), h);
00157 //              }
00158 //
00159 //              sr.read();
00160 //      }
00161 
00168         public static ProActiveDescriptorHandler createProActiveDescriptor(
00169                         String xmlDescriptorUrl, VariableContract variableContract)
00170                         throws java.io.IOException, org.xml.sax.SAXException {
00171                 // static method added to replace main method
00172                 InitialHandler h = new InitialHandler(xmlDescriptorUrl,
00173                                 variableContract);
00174                 String uri = xmlDescriptorUrl;
00175 
00176                 // we get the schema from the class location
00177                 ClassLoader classLoader = ProActiveDescriptorHandler.class
00178                                 .getClassLoader();
00179                 Enumeration<URL> schemaURLs = classLoader
00180                                 .getResources("org/objectweb/proactive/core/descriptor/xml/DescriptorSchema.xsd");
00181 
00182                 // among the various descriptor schema that we may find, we will always
00183                 // favor the one that is in the jar file
00184                 URL schemaURLcandidate = null;
00185 
00186                 while (schemaURLs.hasMoreElements()) {
00187                         URL schemaURL = schemaURLs.nextElement();
00188 
00189                         if (schemaURL.getProtocol().equals("jar")) {
00190                                 schemaURLcandidate = schemaURL;
00191                         } else if (schemaURLcandidate == null) {
00192                                 schemaURLcandidate = schemaURL;
00193                         }
00194                 }
00195 
00196                 if (schemaURLcandidate == null) {
00197                         // In case the application is executed neither via the ant
00198                         // script, nor via the jar file, we need to find the schema
00199                         // manually
00200 
00201                         // we locate the proactive code
00202                         Enumeration<URL> classURLs = ProActiveDescriptorHandler.class
00203                                         .getClassLoader().getResources(
00204                                                         "org/objectweb/proactive/core/descriptor/xml/");
00205 
00206                         // we make sure that we found a file structure (and not a jar)
00207                         URL classURLcandidate = null;
00208 
00209                         while (classURLs.hasMoreElements()) {
00210                                 URL classURL = classURLs.nextElement();
00211 
00212                                 if (classURL.getProtocol().equals("file")) {
00213                                         classURLcandidate = classURL;
00214                                 }
00215                         }
00216 
00217                         try {
00218 
00219                                 if (classURLcandidate != null) {
00220                                         java.net.URI uriSchema;
00221                                         uriSchema = classURLcandidate.toURI();
00222                                         // we navigate to the descriptor schema
00223                                         uriSchema = uriSchema.resolve(".." + java.io.File.separator
00224                                                         + ".." + java.io.File.separator + ".."
00225                                                         + java.io.File.separator + ".."
00226                                                         + java.io.File.separator + ".."
00227                                                         + java.io.File.separator + ".."
00228                                                         + java.io.File.separator + ".."
00229                                                         + java.io.File.separator + java.io.File.separator
00230                                                         + "descriptors" + java.io.File.separator
00231                                                         + "DescriptorSchema.xsd");
00232 
00233                                         java.io.File test = new java.io.File(uriSchema);
00234 
00235                                         // we make sure that we have found the right file
00236                                         if (test.isFile()
00237                                                         && test.getName().equals("DescriptorSchema.xsd")) {
00238                                                 schemaURLcandidate = uriSchema.toURL();
00239                                         } else {
00240                                                 logger
00241                                                                 .error("The Proactive.jar file doesn't contain the Descriptor Schema.");
00242                                                 schemaURLcandidate = null;
00243                                         }
00244                                 } else {
00245                                         logger
00246                                                         .error("The descriptor schema could not be located in your environment.");
00247                                         schemaURLcandidate = null;
00248                                 }
00249                         } catch (Exception e) {
00250                                 logger
00251                                                 .error("The descriptor schema could not be located in your environment.");
00252                                 schemaURLcandidate = null;
00253                         }
00254                 }
00255 
00256                 logger.debug("Using XML shema: " + schemaURLcandidate);
00257 
00258                 org.objectweb.proactive.core.xml.io.StreamReader sr = null;
00259 
00260                 if ("enable".equals(ProActiveConfiguration.getSchemaValidationState())) {
00261                         sr = new org.objectweb.proactive.core.xml.io.StreamReader(
00262                                         new org.xml.sax.InputSource(uri), h, schemaURLcandidate,
00263                                         new SAXParserErrorHandlerTerminating());
00264                 } else {
00265                         sr = new org.objectweb.proactive.core.xml.io.StreamReader(
00266                                         new org.xml.sax.InputSource(uri), h);
00267                 }
00268 
00269                 sr.read();
00270 
00271                 return (ProActiveDescriptorHandler) h.getResultObject();
00272         }
00273 
00274         //
00275         // -- implements XMLUnmarshaller
00276         // ------------------------------------------------------
00277         //
00278         public Object getResultObject() throws org.xml.sax.SAXException {
00279                 // copy xmlproperties into the pad
00280                 // proActiveDescriptor.setVariableContract(XMLProperties.xmlproperties.duplicate());
00281 
00282                 // Release lock on static global variable XMLProperties
00283                 // XMLProperties.xmlproperties.clear();
00284                 // XMLProperties.xmlproperties.releaseLock();
00285                 return proActiveDescriptor;
00286         }
00287 
00288         public void startContextElement(String name, Attributes attributes)
00289                         throws org.xml.sax.SAXException {
00290         }
00291 
00292         //
00293         // -- PROTECTED METHODS
00294         // ------------------------------------------------------
00295         //
00296         protected void notifyEndActiveHandler(String name,
00297                         UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00298                 /*
00299                  * if(name.equals(VARIABLES_TAG)){ //Check XMLProperties Runtime
00300                  * if(!org.objectweb.proactive.core.xml.XMLProperties.xmlproperties.checkContract())
00301                  * throw new SAXException("Variable contract breached"); }
00302                  */
00303         }
00304 
00305         //
00306         // -- PRIVATE METHODS ------------------------------------------------------
00307         //
00308         //
00309         // -- INNER CLASSES ------------------------------------------------------
00310         //
00311 
00315         private static class InitialHandler extends AbstractUnmarshallerDecorator {
00316                 // line added to return a ProactiveDescriptorHandler object
00317                 private ProActiveDescriptorHandler proActiveDescriptorHandler;
00318 
00319                 private InitialHandler(String xmlDescriptorUrl,
00320                                 VariableContract variableContract) {
00321                         super();
00322                         proActiveDescriptorHandler = new ProActiveDescriptorHandler(
00323                                         xmlDescriptorUrl, variableContract);
00324                         this.addHandler(PROACTIVE_DESCRIPTOR_TAG,
00325                                         proActiveDescriptorHandler);
00326                 }
00327 
00328                 public Object getResultObject() throws org.xml.sax.SAXException {
00329                         return proActiveDescriptorHandler;
00330                 }
00331 
00332                 public void startContextElement(String name, Attributes attributes)
00333                                 throws org.xml.sax.SAXException {
00334                 }
00335 
00336                 protected void notifyEndActiveHandler(String name,
00337                                 UnmarshallerHandler activeHandler)
00338                                 throws org.xml.sax.SAXException {
00339                 }
00340         }
00341 
00345         private class VirtualNodeHandler extends BasicUnmarshaller {
00346                 private ProActiveDescriptor pad;
00347 
00348                 private VirtualNodeHandler(ProActiveDescriptor pad) {
00349                         this.pad = pad;
00350                 }
00351 
00352                 public void startContextElement(String name, Attributes attributes)
00353                                 throws org.xml.sax.SAXException {
00354                         // create and register a VirtualNode
00355                         String vnName = attributes.getValue("name");
00356 
00357                         if (!checkNonEmpty(vnName)) {
00358                                 throw new org.xml.sax.SAXException(
00359                                                 "VirtualNode defined without name");
00360                         }
00361 
00362                         // underneath, we know that it is a VirtualNodeImpl, since the
00363                         // bollean in the method is false
00364                         VirtualNodeImpl vn = (VirtualNodeImpl) proActiveDescriptor
00365                                         .createVirtualNode(vnName, false);
00366 
00367                         // property
00368                         String property = attributes.getValue("property");
00369 
00370                         if (checkNonEmpty(property)) {
00371                                 vn.setProperty(property);
00372                         }
00373 
00374                         String timeout = attributes.getValue("timeout");
00375                         String waitForTimeoutAsString = attributes
00376                                         .getValue("waitForTimeout");
00377                         boolean waitForTimeout = false;
00378 
00379                         if (checkNonEmpty(waitForTimeoutAsString)) {
00380                                 waitForTimeout = new Boolean(waitForTimeoutAsString)
00381                                                 .booleanValue();
00382                         }
00383 
00384                         if (checkNonEmpty(timeout)) {
00385                                 vn.setTimeout(new Integer(timeout).longValue(), waitForTimeout);
00386                         }
00387 
00388                         String minNodeNumber = attributes.getValue("minNodeNumber");
00389 
00390                         if (checkNonEmpty(minNodeNumber)) {
00391                                 vn.setMinNumberOfNodes((new Integer(minNodeNumber).intValue()));
00392                         }
00393 
00394                         String serviceId = attributes.getValue("ftServiceId");
00395 
00396                         if (checkNonEmpty(serviceId)) {
00397                                 pad.registerService(vn, serviceId);
00398                         }
00399 
00400                         String fileTransferDeployName = attributes
00401                                         .getValue(FILE_TRANSFER_DEPLOY_TAG);
00402 
00403                         if (checkNonEmpty(fileTransferDeployName)) {
00404                                 vn.addFileTransferDeploy(pad
00405                                                 .getFileTransfer(fileTransferDeployName));
00406                         }
00407 
00408                         String fileTransferRetrieveName = attributes
00409                                         .getValue(FILE_TRANSFER_RETRIEVE_TAG);
00410 
00411                         if (checkNonEmpty(fileTransferRetrieveName)) {
00412                                 vn.addFileTransferRetrieve(pad
00413                                                 .getFileTransfer(fileTransferRetrieveName));
00414                         }
00415 
00416                         String technicalServiceId = attributes
00417                                         .getValue(TECHNICAL_SERVICE_ID);
00418 
00419                         if (checkNonEmpty(technicalServiceId)) {
00420                                 vn.addTechnicalService(pad
00421                                                 .getTechnicalService(technicalServiceId));
00422                         }
00423                 }
00424         } // end inner class VirtualNodeHandler
00425 
00429         private class VirtualNodeLookupHandler extends BasicUnmarshaller {
00430                 private VirtualNodeLookupHandler() {
00431                 }
00432 
00433                 public void startContextElement(String name, Attributes attributes)
00434                                 throws org.xml.sax.SAXException {
00435                         // create and register a VirtualNode
00436                         String vnName = attributes.getValue("name");
00437 
00438                         if (!checkNonEmpty(vnName)) {
00439                                 throw new org.xml.sax.SAXException(
00440                                                 "VirtualNode defined without name");
00441                         }
00442 
00443                         proActiveDescriptor.createVirtualNode(vnName, true);
00444                 }
00445         } // end inner class VirtualNodeLookupHandler
00446 
00447         // SECURITY
00448 
00452         private class SecurityHandler extends BasicUnmarshaller {
00453                 private ProActiveDescriptor proActiveDescriptor;
00454 
00455                 public SecurityHandler(ProActiveDescriptor proActiveDescriptor) {
00456                         super();
00457                         this.proActiveDescriptor = proActiveDescriptor;
00458                 }
00459 
00460                 public void startContextElement(String name, Attributes attributes)
00461                                 throws org.xml.sax.SAXException {
00462                         // create and register a VirtualNode
00463                         String file = attributes.getValue("file");
00464 
00465                         if (!checkNonEmpty(file)) {
00466                                 throw new org.xml.sax.SAXException("Empty security file");
00467                         }
00468 
00469                         logger.debug("creating ProActiveSecurityManager : " + file);
00470                         proActiveDescriptor.createProActiveSecurityManager(file);
00471                 }
00472         }
00473 
00474         // end inner class SecurityHandler
00475 }

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