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 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
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
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
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
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
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
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
00161 variableContract.load(file);
00162 return;
00163 }
00164 }
00165 }
00166 }