1 19 package fr.dyade.aaa.agent.conf; 20 21 import java.io.*; 22 import java.util.*; 23 24 import org.objectweb.util.monolog.api.BasicLevel; 25 import org.objectweb.util.monolog.api.Logger; 26 27 import fr.dyade.aaa.agent.Debug; 28 import fr.dyade.aaa.agent.AgentServer; 29 30 33 public class A3CML { 34 35 static final String ELT_CONFIG = "config"; 36 37 static final String ELT_DOMAIN = "domain"; 38 39 static final String ELT_SERVER = "server"; 40 41 static final String ELT_NETWORK = "network"; 42 43 static final String ELT_SERVICE = "service"; 44 45 static final String ELT_PROPERTY = "property"; 46 47 static final String ELT_NAT = "nat"; 48 49 static final String ELT_CLUSTER = "cluster"; 50 51 static final String ATT_ID = "id"; 52 53 static final String ATT_NAME = "name"; 54 55 static final String ATT_DOMAIN = "domain"; 56 57 static final String ATT_NETWORK = "network"; 58 59 static final String ATT_HOSTNAME = "hostname"; 60 61 static final String ATT_PORT = "port"; 62 63 static final String ATT_SERVER = "server"; 64 65 static final String ATT_CLASS = "class"; 66 67 static final String ATT_ARGS = "args"; 68 69 static final String ATT_VALUE = "value"; 70 71 static final String ELT_JVM_ARGS = "jvmArgs"; 72 73 static final String ATT_SID = "sid"; 74 75 static final String TAB = " "; 76 static final String TAB2 = TAB + TAB; 77 80 public static final void toXML(A3CMLConfig config, 81 String cfgDir, 82 String xmlFileName) throws Exception { 83 File xmlFile = new File(cfgDir, xmlFileName); 84 PrintWriter out = new PrintWriter(new FileWriter(xmlFile)); 85 toXML(config, out); 86 } 87 88 public static final void toXML(A3CMLConfig config, 89 PrintWriter out) throws Exception { 90 out.write("\n<" + ELT_CONFIG + ">\n\n"); 93 94 for (Enumeration e = config.properties.elements(); 96 e.hasMoreElements();) { 97 A3CMLProperty p = (A3CMLProperty) e.nextElement(); 98 out.write(TAB + 99 "<" + ELT_PROPERTY + " " + 100 ATT_NAME + "=\""); 101 out.write(p.name); 102 out.write("\" " + 103 ATT_VALUE + "=\""); 104 out.write(p.value); 105 out.write("\"/>\n"); 106 } 107 out.write("\n"); 108 109 for (Enumeration e = config.domains.elements(); 111 e.hasMoreElements();) { 112 A3CMLDomain d = (A3CMLDomain) e.nextElement(); 113 out.write(TAB + "<" + ELT_DOMAIN + " " + ATT_NAME + "=\""); 114 out.write(d.name); 115 out.write("\" " + ATT_NETWORK + "=\""); 116 out.write(d.network); 117 out.write("\"/>\n"); 118 } 119 out.write("\n"); 120 121 for (Enumeration e = config.servers.elements(); 123 e.hasMoreElements();) { 124 Object obj = e.nextElement(); 125 126 if (obj instanceof A3CMLServer) 127 writeToXMLServer(obj,out); 128 out.write("\n"); 129 } 130 131 for (Enumeration e = config.clusters.elements(); 133 e.hasMoreElements();) { 134 Object obj = e.nextElement(); 135 136 if (obj instanceof A3CMLCluster) { 137 A3CMLCluster cluster = (A3CMLCluster) obj; 138 out.write(TAB + "<" + ELT_CLUSTER + " " + ATT_ID + "=\""); 139 out.write(Short.toString(cluster.sid)); 140 out.write("\" " + ATT_NAME + "=\""); 141 out.write(cluster.name); 142 out.write("\">\n"); 143 144 for (Enumeration e2 = cluster.properties.elements(); 146 e2.hasMoreElements();) { 147 A3CMLProperty p = (A3CMLProperty) e2.nextElement(); 148 out.write(TAB + 149 "<" + ELT_PROPERTY + " " + 150 ATT_NAME + "=\""); 151 out.write(p.name); 152 out.write("\" " + 153 ATT_VALUE + "=\""); 154 out.write(p.value); 155 out.write("\"/>\n"); 156 } 157 out.write("\n"); 158 159 for (Enumeration e2 = cluster.servers.elements(); 160 e2.hasMoreElements();) { 161 Object o = e2.nextElement(); 162 if (o instanceof A3CMLServer) 163 writeToXMLServer(o,out); 164 } 165 out.write(TAB + "</" + ELT_CLUSTER + ">\n"); 166 } 167 out.write("\n"); 168 } 169 170 out.write("</" + ELT_CONFIG + ">\n"); 171 out.flush(); 172 } 173 174 private static final void writeToXMLServer(Object obj, 175 PrintWriter out) { 176 if (obj instanceof A3CMLServer) { 177 A3CMLServer server = (A3CMLServer) obj; 178 out.write(TAB + "<" + ELT_SERVER + " " + ATT_HOSTNAME + "=\""); 179 out.write(server.hostname); 180 out.write("\" " + ATT_ID + "=\""); 181 out.write(Short.toString(server.sid)); 182 out.write("\" " + ATT_NAME + "=\""); 183 out.write(server.name); 184 out.write("\">\n"); 185 186 if (server.jvmArgs != null && server.jvmArgs.length() > 0) { 188 out.write(TAB2 + "<" + ELT_JVM_ARGS + " " + ATT_VALUE + "=\""); 189 out.write(server.jvmArgs); 190 out.write("\"/>\n"); 191 } 192 193 if (server.properties != null) { 195 for (Enumeration e = server.properties.elements(); 196 e.hasMoreElements();) { 197 A3CMLProperty p = (A3CMLProperty) e.nextElement(); 198 out.write(TAB2 + "<" + ELT_PROPERTY + " " + ATT_NAME + "=\""); 199 out.write(p.name); 200 out.write("\" " + ATT_VALUE + "=\""); 201 out.write(p.value); 202 out.write("\"/>\n"); 203 } 204 } 205 206 if (server.nat != null) { 208 for (Enumeration e = server.nat.elements(); 209 e.hasMoreElements();) { 210 A3CMLNat n = (A3CMLNat) e.nextElement(); 211 out.write(TAB2 + "<" + ELT_NAT + " " + ATT_SID + "=\""); 212 out.write(Short.toString(n.sid)); 213 out.write("\" " + ATT_HOSTNAME + "=\""); 214 out.write(n.host); 215 out.write("\" " + ATT_PORT + "=\""); 216 out.write(Integer.toString(n.port)); 217 out.write("\"/>\n"); 218 } 219 } 220 221 if (server.networks != null) { 223 for (Enumeration e = server.networks.elements(); 224 e.hasMoreElements();) { 225 A3CMLNetwork n = (A3CMLNetwork) e.nextElement(); 226 out.write(TAB2 + "<" + ELT_NETWORK + " " + ATT_DOMAIN + "=\""); 227 out.write(n.domain); 228 out.write("\" " + ATT_PORT + "=\""); 229 out.write(Integer.toString(n.port)); 230 out.write("\"/>\n"); 231 } 232 } 233 234 if (server.services != null) { 236 for (Enumeration e = server.services.elements(); 237 e.hasMoreElements();) { 238 A3CMLService service = (A3CMLService) e.nextElement(); 239 out.write(TAB2 + "<" + ELT_SERVICE + " " + ATT_CLASS + "=\""); 240 out.write(service.classname); 241 out.write("\" " + ATT_ARGS + "=\""); 242 if ((service.args != null) && (service.args.length() > 0)) { 243 out.write(service.args); 244 } 245 out.write("\"/>\n"); 246 } 247 } 248 out.write(TAB + "</" + ELT_SERVER + ">\n"); 249 } 250 } 251 252 263 public static A3CMLConfig getXMLConfig() throws Exception { 264 String cfgDir = System.getProperty(AgentServer.CFG_DIR_PROPERTY, 265 AgentServer.DEFAULT_CFG_DIR); 266 String cfgFile = System.getProperty(AgentServer.CFG_FILE_PROPERTY, 267 AgentServer.DEFAULT_CFG_FILE); 268 return getXMLConfig(cfgDir,cfgFile); 269 } 270 271 284 public static A3CMLConfig getXMLConfig(String cfgDir, 285 String cfgFileName) throws Exception { 286 return getXMLConfig(new File(cfgDir, cfgFileName).getPath()); 287 } 288 289 public static A3CMLConfig getXMLConfig(String path) throws Exception { 290 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 291 Log.logger.log(BasicLevel.DEBUG,"Config.getXMLConfig(" + path + ")"); 292 293 A3CMLConfig a3cmlconfig = null; 294 Reader reader = null; 295 296 File cfgFile = new File(path); 298 try { 299 if (!cfgFile.exists() || !cfgFile.isFile() || (cfgFile.length() == 0)) { 300 throw new IOException(); 301 } 302 reader = new FileReader(cfgFile); 303 } catch (IOException exc) { 304 Log.logger.log(BasicLevel.WARN, 307 "Unable to find configuration file \"" + 308 cfgFile.getPath() + "\"."); 309 reader = null; 310 } 311 312 if (reader == null) { 314 ClassLoader classLoader = null; 315 InputStream is = null; 316 try { 317 classLoader = A3CMLConfig.class.getClassLoader(); 318 if (classLoader != null) { 319 Log.logger.log(BasicLevel.WARN, 320 "Trying to find [" + path + "] using " + 321 classLoader + " class loader."); 322 is = classLoader.getResourceAsStream(path); 323 } 324 } catch (Throwable t) { 325 Log.logger.log(BasicLevel.WARN, 326 "Can't find [" + path + "] using " + 327 classLoader + " class loader.", 328 t); 329 is = null; 330 } 331 332 if (is == null) { 333 Log.logger.log(BasicLevel.WARN, 335 "Trying to find [" + path + 336 "] using ClassLoader.getSystemResource()."); 337 is = ClassLoader.getSystemResourceAsStream(path); 338 } 339 if (is != null) { 340 a3cmlconfig = getConfig(new InputStreamReader(is)); 341 } 342 } else { 343 a3cmlconfig = getConfig(reader); 344 } 345 346 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 347 Log.logger.log(BasicLevel.DEBUG, "a3cmlconfig = " + a3cmlconfig); 348 349 if (a3cmlconfig == null) 350 throw new FileNotFoundException("xml configuration file not found."); 351 352 return a3cmlconfig; 353 } 354 355 368 public static A3CMLConfig getConfig(Reader reader) 369 throws Exception { 370 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 371 Log.logger.log(BasicLevel.DEBUG, "Config.getConfig(" + reader + ")"); 372 373 String cfgName = System.getProperty(AgentServer.CFG_NAME_PROPERTY, 374 AgentServer.DEFAULT_CFG_NAME); 375 String wrpCName = System.getProperty(AgentServer.A3CMLWRP_PROPERTY, 376 AgentServer.DEFAULT_A3CMLWRP); 377 Class wrpClass = Class.forName(wrpCName); 378 379 A3CMLWrapper wrapper = (A3CMLWrapper) wrpClass.newInstance(); 380 A3CMLConfig a3config = null; 381 try { 382 a3config = wrapper.parse(reader,cfgName); 383 } catch (Exception exc) { 384 Log.logger.log(BasicLevel.ERROR, 385 "Config.getConfig : " + exc.getMessage(), 386 exc); 387 } 388 389 if ((a3config == null) || (a3config.servers == null)) 390 throw new Exception ("Empty configuration \"" + cfgName + 391 "\" in configuration file."); 392 393 return a3config; 394 } 395 } 396 | Popular Tags |