1 19 package fr.dyade.aaa.agent.conf; 20 21 import java.io.*; 22 import java.util.*; 23 24 import fr.dyade.aaa.agent.*; 25 26 import org.objectweb.util.monolog.api.BasicLevel; 27 import org.objectweb.util.monolog.api.Logger; 28 29 import org.kxml.parser.*; 30 import org.kxml.*; 31 32 35 public class A3CMLKXmlWrapper implements A3CMLWrapper { 36 protected A3CMLConfig a3cmlconfig = null; 37 38 public A3CMLKXmlWrapper() { } 39 40 public static final String getValue(Vector atts, String qName) { 41 if(atts != null){ 42 for (int i=0; i<atts.size(); i++) { 43 if (((Attribute) atts.elementAt(i)).getName().equals(qName)) 44 return ((Attribute) atts.elementAt(i)).getValue(); 45 } 46 } 47 return null; 48 } 49 50 53 String configName = "default"; 54 55 String conf = null; 56 60 A3CMLDomain domain = null; 61 65 A3CMLServer server = null; 66 70 A3CMLNetwork network = null; 71 75 A3CMLService service = null; 76 80 A3CMLProperty property = null; 81 85 String jvmArgs = null; 86 90 A3CMLNat nat = null; 91 92 100 public A3CMLConfig parse(Reader reader, 101 String configName) 102 throws Exception { 103 this.configName = configName; 104 105 a3cmlconfig = new A3CMLConfig(); 106 107 XmlParser parser = new XmlParser(reader); 109 110 ParseEvent event = parser.read(); 111 while (event.getType() != Xml.END_DOCUMENT) { 112 switch (event.getType()) { 113 case Xml.START_DOCUMENT: 114 break; 115 case Xml.START_TAG: { 116 String name = event.getName(); 117 118 if (name.equals(A3CML.ELT_CONFIG)) { 119 conf = getValue(event.getAttributes(), 120 A3CML.ATT_NAME); 121 if (conf == null) 122 conf = configName; 123 } else if (configName.equals(conf)) { 124 Vector atts = event.getAttributes(); 125 126 if (name.equals(A3CML.ELT_DOMAIN)) { 127 domain = new A3CMLDomain( 128 getValue(atts, A3CML.ATT_NAME), 129 getValue(atts, A3CML.ATT_NETWORK)); 130 } else if (name.equals(A3CML.ELT_SERVER)) { 131 short sid; 132 try { 133 sid = Short.parseShort(getValue(atts, A3CML.ATT_ID)); 134 } catch (NumberFormatException exc) { 135 throw new Exception ("bad value for server id: " + 136 getValue(atts, A3CML.ATT_ID)); 137 } 138 server = new A3CMLServer( 139 sid, 140 getValue(atts, A3CML.ATT_NAME), 141 getValue(atts, A3CML.ATT_HOSTNAME)); 142 } else if (name.equals(A3CML.ELT_NETWORK)) { 143 int port; 144 try { 145 port = Integer.parseInt(getValue(atts, A3CML.ATT_PORT)); 146 } catch (NumberFormatException exc) { 147 throw new Exception ("bad value for network port: " + 148 getValue(atts, A3CML.ATT_PORT)); 149 } 150 network = new A3CMLNetwork( 151 getValue(atts, A3CML.ATT_DOMAIN), 152 port); 153 } else if (name.equals(A3CML.ELT_SERVICE)) { 154 service = new A3CMLService( 155 getValue(atts, A3CML.ATT_CLASS), 156 getValue(atts, A3CML.ATT_ARGS)); 157 } else if (name.equals(A3CML.ELT_PROPERTY)) { 158 property = new A3CMLProperty( 159 getValue(atts, A3CML.ATT_NAME), 160 getValue(atts, A3CML.ATT_VALUE)); 161 } else if (name.equals(A3CML.ELT_NAT)) { 162 nat = new A3CMLNat(Short.parseShort(getValue(atts, A3CML.ATT_SID)), 163 getValue(atts, A3CML.ATT_HOSTNAME), 164 Integer.parseInt(getValue(atts, A3CML.ATT_PORT))); 165 } else if (name.equals(A3CML.ELT_JVM_ARGS)) { 166 jvmArgs = getValue(atts, A3CML.ATT_VALUE); 167 } else { 168 throw new Exception ("unknown element \"" + name + "\""); 169 } 170 } 171 break; 172 } 173 case Xml.END_TAG: { 174 String name = event.getName(); 175 176 if (name.equals(A3CML.ELT_CONFIG)) { 177 conf = null; 178 } else if (configName.equals(conf)) { 179 if (name.equals(A3CML.ELT_DOMAIN)) { 180 a3cmlconfig.addDomain(domain); 181 domain = null; 182 } else if (name.equals(A3CML.ELT_SERVER)) { 183 a3cmlconfig.addServer(server); 184 server = null; 185 } else if (name.equals(A3CML.ELT_NETWORK)) { 186 if (server != null) { 187 server.addNetwork(network); 188 a3cmlconfig.getDomain(network.domain).addServer(server); 192 } else { 193 } 195 network = null; 196 } else if (name.equals(A3CML.ELT_SERVICE)) { 197 if (server != null) { 198 server.addService(service); 199 } else { 200 } 202 service = null; 203 } else if (name.equals(A3CML.ELT_PROPERTY)) { 204 if (server == null) 205 a3cmlconfig.addProperty(property); else 207 server.addProperty(property); property = null; 209 } else if (name.equals(A3CML.ELT_NAT)) { 210 if (server != null) 211 server.addNat(nat); 212 nat = null; 213 } else if (name.equals(A3CML.ELT_JVM_ARGS)) { 214 if (server != null && jvmArgs != null) 215 server.jvmArgs = jvmArgs; 216 jvmArgs = null; 217 } else { 218 throw new Exception ("unknown element \"" + name + "\""); 219 } 220 } 221 break; 222 } 223 } 224 event = parser.read(); 225 } 226 227 return a3cmlconfig; 228 } 229 } 230 | Popular Tags |