KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > conf > A3CML


1 /*
2  * Copyright (C) 2002 - 2003 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  */

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 /**
31  * Defines XML syntactic element for A3CML configuration file.
32  */

33 public class A3CML {
34   /** Syntaxic name for config element */
35   static final String JavaDoc ELT_CONFIG = "config";
36   /** Syntaxic name for domain element */
37   static final String JavaDoc ELT_DOMAIN = "domain";
38   /** Syntaxic name for server element */
39   static final String JavaDoc ELT_SERVER = "server";
40   /** Syntaxic name for network element */
41   static final String JavaDoc ELT_NETWORK = "network";
42   /** Syntaxic name for service element */
43   static final String JavaDoc ELT_SERVICE = "service";
44   /** Syntaxic name for property element */
45   static final String JavaDoc ELT_PROPERTY = "property";
46   /** Syntaxic name for nat element */
47   static final String JavaDoc ELT_NAT = "nat";
48   /** Syntaxic name for cluster element */
49   static final String JavaDoc ELT_CLUSTER = "cluster";
50   /** Syntaxic name for id attribute */
51   static final String JavaDoc ATT_ID = "id";
52   /** Syntaxic name for name attribute */
53   static final String JavaDoc ATT_NAME = "name";
54   /** Syntaxic name for domain attribute */
55   static final String JavaDoc ATT_DOMAIN = "domain";
56   /** Syntaxic name for network attribute */
57   static final String JavaDoc ATT_NETWORK = "network";
58   /** Syntaxic name for hostname attribute */
59   static final String JavaDoc ATT_HOSTNAME = "hostname";
60   /** Syntaxic name for port attribute */
61   static final String JavaDoc ATT_PORT = "port";
62   /** Syntaxic name for server attribute */
63   static final String JavaDoc ATT_SERVER = "server";
64   /** Syntaxic name for class attribute */
65   static final String JavaDoc ATT_CLASS = "class";
66   /** Syntaxic name for args attribute */
67   static final String JavaDoc ATT_ARGS = "args";
68   /** Syntaxic name for value attribute */
69   static final String JavaDoc ATT_VALUE = "value";
70   /** Syntaxic for server jvm arguments */
71   static final String JavaDoc ELT_JVM_ARGS = "jvmArgs";
72   /** Syntaxic name for sid attribute */
73   static final String JavaDoc ATT_SID = "sid";
74
75   static final String JavaDoc TAB = " ";
76   static final String JavaDoc TAB2 = TAB + TAB;
77   /**
78    * write a configuration in an A3CML file.
79    */

80   public static final void toXML(A3CMLConfig config,
81                                  String JavaDoc cfgDir,
82                                  String JavaDoc xmlFileName) throws Exception JavaDoc {
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 JavaDoc {
90     // out.write("<?xml version=\"1.0\"?>\n");
91
// out.write("<!DOCTYPE config SYSTEM \"a3config.dtd\">\n");
92
out.write("\n<" + ELT_CONFIG + ">\n\n");
93
94     // write all property
95
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     // write all know domain
110
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     // write all know servers.
122
for (Enumeration e = config.servers.elements();
123          e.hasMoreElements();) {
124       Object JavaDoc obj = e.nextElement();
125
126       if (obj instanceof A3CMLServer)
127         writeToXMLServer(obj,out);
128       out.write("\n");
129     }
130
131     // write all know cluster.
132
for (Enumeration e = config.clusters.elements();
133          e.hasMoreElements();) {
134       Object JavaDoc 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         // write all cluster property
145
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 JavaDoc 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 JavaDoc 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       // jvm args
187
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       // write all property
194
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       // write all Nat
207
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       // network
222
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       //service
235
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   /**
253    * Gets an agent server configuration from a XML file. This method
254    * fills the object graph configuration in the <code>A3CMLConfig</code>
255    * object.
256    *
257    * @return the <code>A3CMLConfig</code> object if file exists and is
258    * correct, null otherwise.
259    *
260    * @exception Exception
261    * unspecialized exception when reading and parsing the configuration file
262    */

263   public static A3CMLConfig getXMLConfig() throws Exception JavaDoc {
264     String JavaDoc cfgDir = System.getProperty(AgentServer.CFG_DIR_PROPERTY,
265                                        AgentServer.DEFAULT_CFG_DIR);
266     String JavaDoc cfgFile = System.getProperty(AgentServer.CFG_FILE_PROPERTY,
267                                         AgentServer.DEFAULT_CFG_FILE);
268     return getXMLConfig(cfgDir,cfgFile);
269   }
270
271   /**
272    * Gets an agent server configuration from a XML file. This method
273    * fills the object graph configuration in the <code>A3CMLConfig</code>
274    * object.
275    *
276    * @param cfgDir directory of XML file
277    * @param cfgFile XML configuration file
278    * @return the <code>A3CMLConfig</code> object if file exists and is
279    * correct, null otherwise.
280    *
281    * @exception Exception
282    * unspecialized exception when reading and parsing the configuration file
283    */

284   public static A3CMLConfig getXMLConfig(String JavaDoc cfgDir,
285                                          String JavaDoc cfgFileName) throws Exception JavaDoc {
286     return getXMLConfig(new File(cfgDir, cfgFileName).getPath());
287   }
288
289   public static A3CMLConfig getXMLConfig(String JavaDoc path) throws Exception JavaDoc {
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     // 1st, search XML configuration file in directory.
297
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       // configuration file seems not exist, search it from the
305
// search path used to load classes.
306
Log.logger.log(BasicLevel.WARN,
307                      "Unable to find configuration file \"" +
308                      cfgFile.getPath() + "\".");
309       reader = null;
310     }
311       
312     // 2nd, search XML configuration file in path used to load classes.
313
if (reader == null) {
314       ClassLoader JavaDoc 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 JavaDoc 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         // Last ditch attempt: get the resource from the class path.
334
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   /**
356    * Gets configuration of agent servers from a XML file. This method
357    * fills the object graph configuration in the <code>Config</code>
358    * object.
359    *
360    * @param reader Reader
361    * @param cfgFileName configuration file name (XML file)
362    * @return the <code>Config</code> object if file exists and is
363    * correct, null otherwise.
364    *
365    * @exception Exception
366    * unspecialized exception when reading and parsing the configuration file
367    */

368   public static A3CMLConfig getConfig(Reader reader)
369     throws Exception JavaDoc {
370     if (Log.logger.isLoggable(BasicLevel.DEBUG))
371       Log.logger.log(BasicLevel.DEBUG, "Config.getConfig(" + reader + ")");
372     
373     String JavaDoc cfgName = System.getProperty(AgentServer.CFG_NAME_PROPERTY,
374                                         AgentServer.DEFAULT_CFG_NAME);
375     String JavaDoc wrpCName = System.getProperty(AgentServer.A3CMLWRP_PROPERTY,
376                                          AgentServer.DEFAULT_A3CMLWRP);
377     Class JavaDoc 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 JavaDoc 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 JavaDoc("Empty configuration \"" + cfgName +
391                           "\" in configuration file.");
392
393     return a3config;
394   }
395 }
396
Popular Tags