00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
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         
00066         
00067         public ProActiveDescriptorHandler(String xmlDescriptorUrl,
00068                         VariableContract variableContract) {
00069                 super(false);
00070                 proActiveDescriptor = new ProActiveDescriptorImpl(xmlDescriptorUrl);
00071                 
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         
00139         
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00168         public static ProActiveDescriptorHandler createProActiveDescriptor(
00169                         String xmlDescriptorUrl, VariableContract variableContract)
00170                         throws java.io.IOException, org.xml.sax.SAXException {
00171                 
00172                 InitialHandler h = new InitialHandler(xmlDescriptorUrl,
00173                                 variableContract);
00174                 String uri = xmlDescriptorUrl;
00175 
00176                 
00177                 ClassLoader classLoader = ProActiveDescriptorHandler.class
00178                                 .getClassLoader();
00179                 Enumeration<URL> schemaURLs = classLoader
00180                                 .getResources("org/objectweb/proactive/core/descriptor/xml/DescriptorSchema.xsd");
00181 
00182                 
00183                 
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                         
00198                         
00199                         
00200 
00201                         
00202                         Enumeration<URL> classURLs = ProActiveDescriptorHandler.class
00203                                         .getClassLoader().getResources(
00204                                                         "org/objectweb/proactive/core/descriptor/xml/");
00205 
00206                         
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                                         
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                                         
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         
00276         
00277         
00278         public Object getResultObject() throws org.xml.sax.SAXException {
00279                 
00280                 
00281 
00282                 
00283                 
00284                 
00285                 return proActiveDescriptor;
00286         }
00287 
00288         public void startContextElement(String name, Attributes attributes)
00289                         throws org.xml.sax.SAXException {
00290         }
00291 
00292         
00293         
00294         
00295         
00296         protected void notifyEndActiveHandler(String name,
00297                         UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00298                 
00299 
00300 
00301 
00302 
00303         }
00304 
00305         
00306         
00307         
00308         
00309         
00310         
00311 
00315         private static class InitialHandler extends AbstractUnmarshallerDecorator {
00316                 
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                         
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                         
00363                         
00364                         VirtualNodeImpl vn = (VirtualNodeImpl) proActiveDescriptor
00365                                         .createVirtualNode(vnName, false);
00366 
00367                         
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         } 
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                         
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         } 
00446 
00447         
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                         
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         
00475 }