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 javax.xml.parsers.SAXParser ; 30 import javax.xml.parsers.SAXParserFactory ; 31 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.helpers.DefaultHandler ; 35 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXParseException ; 38 39 42 public class A3CMLSaxWrapper extends DefaultHandler implements A3CMLWrapper { 43 protected A3CMLConfig a3cmlConfig = null; 44 45 public A3CMLSaxWrapper() {} 46 47 50 String configName = "default"; 51 52 String conf = null; 53 57 A3CMLDomain domain = null; 58 62 A3CMLServer server = null; 63 67 A3CMLNetwork network = null; 68 72 A3CMLService service = null; 73 77 A3CMLProperty property = null; 78 82 String jvmArgs = null; 83 87 A3CMLNat nat = null; 88 92 A3CMLCluster cluster = null; 93 94 106 public A3CMLConfig parse(Reader cfgReader, 107 String cfgName) throws Exception { 108 this.configName = cfgName; 109 110 a3cmlConfig = new A3CMLConfig(); 111 112 SAXParserFactory factory = SAXParserFactory.newInstance(); 113 SAXParser parser = factory.newSAXParser(); 114 parser.parse(new InputSource (cfgReader), this); 115 116 return a3cmlConfig; 117 } 118 119 127 public void fatalError(SAXParseException e) throws SAXException { 128 Log.logger.log(BasicLevel.ERROR, 129 "fatal error parsing " + e.getPublicId() + 130 " at " + e.getLineNumber() + "." + e.getColumnNumber()); 131 throw e; 132 } 133 134 142 public void error(SAXParseException e) throws SAXException { 143 Log.logger.log(BasicLevel.ERROR, 144 "error parsing " + e.getPublicId() + 145 " at " + e.getLineNumber() + "." + e.getColumnNumber()); 146 throw e; 147 } 148 149 150 158 public void warning(SAXParseException e) throws SAXException { 159 Log.logger.log(BasicLevel.ERROR, 160 "warning parsing " + e.getPublicId() + 161 " at " + e.getLineNumber() + "." + e.getColumnNumber()); 162 throw e; 163 } 164 165 171 public void startDocument() throws SAXException { 172 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 173 Log.logger.log(BasicLevel.DEBUG, "startDocument"); 174 } 175 176 187 public void startElement(String uri, 188 String localName, 189 String rawName, 190 Attributes atts) throws SAXException { 191 String name = rawName; 192 193 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 194 Log.logger.log(BasicLevel.DEBUG, "startElement: " + name); 195 196 if (name.equals(A3CML.ELT_CONFIG)) { 197 conf = atts.getValue(A3CML.ATT_NAME); 198 if (conf == null) conf = configName; 199 } else if (configName.equals(conf)) { 200 if (name.equals(A3CML.ELT_DOMAIN)) { 201 try { 202 domain = new A3CMLDomain(atts.getValue(A3CML.ATT_NAME), 203 atts.getValue(A3CML.ATT_NETWORK)); 204 } catch (Exception exc) { 205 throw new SAXException (exc.getMessage()); 206 } 207 } else if (name.equals(A3CML.ELT_SERVER)) { 208 try { 209 short sid; 210 try { 211 sid = Short.parseShort(atts.getValue(A3CML.ATT_ID)); 212 } catch (NumberFormatException exc) { 213 throw new Exception ("bad value for server id: " + 214 atts.getValue(A3CML.ATT_ID)); 215 } 216 server = new A3CMLServer(sid, 217 atts.getValue(A3CML.ATT_NAME), 218 atts.getValue(A3CML.ATT_HOSTNAME)); 219 } catch (Exception exc) { 220 throw new SAXException (exc.getMessage()); 221 } 222 } else if (name.equals(A3CML.ELT_CLUSTER)) { 223 try { 224 short sid; 225 try { 226 sid = Short.parseShort(atts.getValue(A3CML.ATT_ID)); 227 } catch (NumberFormatException exc) { 228 throw new Exception ("bad value for cluster id: " + 229 atts.getValue(A3CML.ATT_ID)); 230 } 231 cluster = new A3CMLCluster(sid, 232 atts.getValue(A3CML.ATT_NAME)); 233 } catch (Exception exc) { 234 throw new SAXException (exc.getMessage()); 235 } 236 } else if (name.equals(A3CML.ELT_NETWORK)) { 237 try { 238 int port; 239 try { 240 port = Integer.parseInt(atts.getValue(A3CML.ATT_PORT)); 241 } catch (NumberFormatException exc) { 242 throw new Exception ("bad value for network port: " + 243 atts.getValue(A3CML.ATT_PORT)); 244 } 245 network = new A3CMLNetwork(atts.getValue(A3CML.ATT_DOMAIN), 246 port); 247 } catch (Exception exc) { 248 throw new SAXException (exc.getMessage()); 249 } 250 } else if (name.equals(A3CML.ELT_SERVICE)) { 251 service = new A3CMLService(atts.getValue(A3CML.ATT_CLASS), 252 atts.getValue(A3CML.ATT_ARGS)); 253 } else if (name.equals(A3CML.ELT_PROPERTY)) { 254 property = new A3CMLProperty(atts.getValue(A3CML.ATT_NAME), 255 atts.getValue(A3CML.ATT_VALUE)); 256 } else if (name.equals(A3CML.ELT_NAT)) { 257 nat = new A3CMLNat(Short.parseShort(atts.getValue(A3CML.ATT_SID)), 258 atts.getValue(A3CML.ATT_HOSTNAME), 259 Integer.parseInt(atts.getValue(A3CML.ATT_PORT))); 260 } else if (name.equals(A3CML.ELT_JVM_ARGS)) { 261 jvmArgs = atts.getValue(A3CML.ATT_VALUE); 262 } else { 263 throw new SAXException ("unknown element \"" + name + "\""); 264 } 265 } 266 } 267 268 279 public void endElement(String uri, 280 String localName, 281 String rawName) throws SAXException { 282 String name = rawName; 283 284 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 285 Log.logger.log(BasicLevel.DEBUG, "endElement: " + name); 286 287 if (name.equals(A3CML.ELT_CONFIG)) { 288 conf = null; 289 } else if (configName.equals(conf)) { 290 try { 291 if (name.equals(A3CML.ELT_DOMAIN)) { 292 a3cmlConfig.addDomain(domain); 293 domain = null; 294 } else if (name.equals(A3CML.ELT_SERVER)) { 295 if (cluster == null) 296 a3cmlConfig.addServer(server); 297 else 298 cluster.addServer(server); 299 server = null; 300 } else if (name.equals(A3CML.ELT_CLUSTER)) { 301 a3cmlConfig.addCluster(cluster); 302 cluster = null; 303 } else if (name.equals(A3CML.ELT_NETWORK)) { 304 if (server != null) { 305 server.addNetwork(network); 306 a3cmlConfig.getDomain(network.domain).addServer(server); 308 } else { 309 } 311 network = null; 312 } else if (name.equals(A3CML.ELT_SERVICE)) { 313 if (server != null) { 314 server.addService(service); 315 } else { 316 } 318 service = null; 319 } else if (name.equals(A3CML.ELT_PROPERTY)) { 320 if (server == null && cluster == null) 321 a3cmlConfig.addProperty(property); else if (server != null) 323 server.addProperty(property); else if (server == null && cluster != null) 325 cluster.addProperty(property); property = null; 327 } else if (name.equals(A3CML.ELT_NAT)) { 328 if (server != null) 329 server.addNat(nat); 330 nat = null; 331 } else if (name.equals(A3CML.ELT_JVM_ARGS)) { 332 if (server != null && jvmArgs != null) 333 server.jvmArgs = jvmArgs; 334 else if (server == null && cluster != null) 335 cluster.jvmArgs = jvmArgs; 336 jvmArgs = null; 337 } else { 338 throw new SAXException ("unknown element \"" + name + "\""); 339 } 340 } catch (SAXException exc) { 341 throw exc; 342 } catch (Exception exc) { 343 throw new SAXException (exc.getMessage()); 344 } 345 } 346 } 347 348 354 public void endDocument() throws SAXException { 355 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 356 Log.logger.log(BasicLevel.DEBUG, "endDocument"); 357 } 358 } 359 | Popular Tags |