org/objectweb/proactive/core/descriptor/xml/VariablesHandler.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.xml.VariableContract;
00034 import org.objectweb.proactive.core.xml.VariableContractType;
00035 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
00036 import org.objectweb.proactive.core.xml.handler.PassiveCompositeUnmarshaller;
00037 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
00038 import org.objectweb.proactive.core.xml.io.Attributes;
00039 import org.xml.sax.InputSource;
00040 
00041 
00047 public class VariablesHandler extends PassiveCompositeUnmarshaller
00048     implements ProActiveDescriptorConstants {
00049     protected VariableContract variableContract;
00050 
00051     public VariablesHandler(VariableContract variableContract) {
00052         super(false);
00053         this.variableContract = variableContract;
00054 
00055         this.addHandler(VARIABLES_DESCRIPTOR_TAG, new VariableHandler(VARIABLES_DESCRIPTOR_TAG));
00056         this.addHandler(VARIABLES_PROGRAM_TAG, new VariableHandler(VARIABLES_PROGRAM_TAG));
00057         this.addHandler(VARIABLES_JAVAPROPERTY_TAG, new VariableHandler(VARIABLES_JAVAPROPERTY_TAG));
00058         this.addHandler(VARIABLES_PROGRAM_DEFAULT_TAG, new VariableHandler(VARIABLES_PROGRAM_DEFAULT_TAG));
00059         this.addHandler(VARIABLES_DESCRIPTOR_DEFAULT_TAG, new VariableHandler(VARIABLES_DESCRIPTOR_DEFAULT_TAG));
00060         this.addHandler(VARIABLES_JAVAPROPERTY_DESCRIPTOR_TAG, new VariableHandler(VARIABLES_JAVAPROPERTY_DESCRIPTOR_TAG));
00061         this.addHandler(VARIABLES_JAVAPROPERTY_PROGRAM_TAG, new VariableHandler(VARIABLES_JAVAPROPERTY_PROGRAM_TAG));
00062         
00063         this.addHandler(VARIABLES_INCLUDE_XML_FILE_TAG, new IncludeXMLFileHandler());
00064         this.addHandler(VARIABLES_INCLUDE_PROPERTY_FILE_TAG, new IncludePropertiesFileHandler());
00065     }
00066     
00067     protected void notifyEndActiveHandler(String name,
00068             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException {
00069         
00070         //Once the variables have been defined, we load pending values from the javaproperties
00071         variableContract.setJavaPropertiesValues();
00072     }
00073 
00079     public static void createVariablesHandler(String filename,
00080         VariableContract variableContract) {
00081         VariablesFileHandler vfh = new VariablesFileHandler(variableContract);
00082 
00083         org.objectweb.proactive.core.xml.io.StreamReader sr;
00084         //String file = VariablesHandler.class.getResource(filename).getPath();
00085         InputSource source = new org.xml.sax.InputSource(filename);
00086         try {
00087             sr = new org.objectweb.proactive.core.xml.io.StreamReader(source, vfh);
00088             sr.read();
00089             //return (cast) vh.getResultObject();
00090         } catch (Exception e) {
00091             logger.error("Unable to load Variable Contract from:" + filename);
00092             e.printStackTrace();
00093         }
00094     }
00095     
00096     public static class VariablesFileHandler extends PassiveCompositeUnmarshaller{
00097         
00098         VariablesFileHandler(VariableContract variableContract){
00099                 super(false);
00100             this.addHandler(VARIABLES_TAG, new VariablesHandler(variableContract));
00101         }
00102     }
00103     
00104     private class VariableHandler extends BasicUnmarshaller {
00105         
00106         VariableContractType varType;
00107         String varStringType;
00108         
00109         VariableHandler(String varStringType) {
00110                 this.varType=VariableContractType.getType(varStringType);
00111                 this.varStringType=varStringType;
00112        
00113         }
00114 
00115         public void startContextElement(String tag, Attributes attributes)
00116             throws org.xml.sax.SAXException {
00117                 
00118                 if(this.varType==null)
00119                 throw new org.xml.sax.SAXException("Ilegal Descriptor Variable Type: "+varStringType);
00120                 
00121             // Variable Name
00122             String name = attributes.getValue("name");
00123             if (!checkNonEmpty(name)) {
00124                 throw new org.xml.sax.SAXException(
00125                     "Variable has no name");
00126             }
00127 
00128             String value = attributes.getValue("value");
00129             if(value==null) value="";
00130             // Define and set variables into the contract
00131             variableContract.setDescriptorVariable(name, value,varType);
00132         }
00133     }
00134 
00135     private class IncludeXMLFileHandler extends BasicUnmarshaller {
00136         IncludeXMLFileHandler() {
00137         }
00138 
00139         public void startContextElement(String tag, Attributes attributes)
00140             throws org.xml.sax.SAXException {
00141 
00142             String file = attributes.getValue("location");
00143             if (checkNonEmpty(file)) {
00144                 // Specific processing for loading an xml file
00145                 variableContract.loadXML(file);
00146                 return;
00147             }
00148         }
00149     }
00150     
00151     private class IncludePropertiesFileHandler extends BasicUnmarshaller {
00152         IncludePropertiesFileHandler() {
00153         }
00154 
00155         public void startContextElement(String tag, Attributes attributes)
00156             throws org.xml.sax.SAXException {
00157 
00158             String file = attributes.getValue("location");
00159             if (checkNonEmpty(file)) {
00160                 // Specific processing for loading a file
00161                 variableContract.load(file);
00162                 return;
00163             }
00164         }
00165     }
00166 }

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