KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2002-2004 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 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 JavaDoc;
30 import javax.xml.parsers.SAXParserFactory JavaDoc;
31
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.helpers.DefaultHandler JavaDoc;
35
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.SAXParseException JavaDoc;
38
39 /**
40  * XML SAX Wrapper for A3 configuration file.
41  */

42 public class A3CMLSaxWrapper extends DefaultHandler JavaDoc implements A3CMLWrapper {
43   protected A3CMLConfig a3cmlConfig = null;
44
45   public A3CMLSaxWrapper() {}
46
47   /**
48    * Name of configuration to get from the file.
49    */

50   String JavaDoc configName = "default";
51   /** Working attribute used during configuration's */
52   String JavaDoc conf = null;
53   /**
54    * Working attribute used during domain's definition between start and
55    * end element.
56    */

57   A3CMLDomain domain = null;
58   /**
59    * Working attribute used during server's definition between start and
60    * end element.
61    */

62   A3CMLServer server = null;
63   /**
64    * Working attribute used during network's definition between start and
65    * end element.
66    */

67   A3CMLNetwork network = null;
68   /**
69    * Working attribute used during service's definition between start and
70    * end element.
71    */

72   A3CMLService service = null;
73   /**
74    * Working attribute used during service's definition between start and
75    * end element.
76    */

77   A3CMLProperty property = null;
78   /**
79    * Working attribute used during jvmArgs' definition between start and
80    * end element.
81    */

82   String JavaDoc jvmArgs = null;
83   /**
84    * Working attribute used during nat' definition between start and
85    * end element.
86    */

87   A3CMLNat nat = null;
88   /**
89    * Working attribute used during cluster' definition between start and
90    * end element.
91    */

92   A3CMLCluster cluster = null;
93
94   /**
95    * Parses the xml file named <code>cfgFileName</code> and calls handler
96    * methods. Calls only methode <code>startDocument()</code>,
97    * <code>startElement</code>, <code>endElement</code> and
98    * <code>endDocument</code>.
99    *
100    * @param cfgFileName the name of the xml file
101    * @param configName the name of the configuration
102    * @param serverId the id of the local server
103    *
104    * @exception Exception unspecialized error
105    */

106   public A3CMLConfig parse(Reader cfgReader,
107                            String JavaDoc cfgName) throws Exception JavaDoc {
108     this.configName = cfgName;
109
110     a3cmlConfig = new A3CMLConfig();
111
112     SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
113     SAXParser JavaDoc parser = factory.newSAXParser();
114     parser.parse(new InputSource JavaDoc(cfgReader), this);
115
116     return a3cmlConfig;
117   }
118
119   /**
120    * Handles notification of a non-recoverable parser error.
121    *
122    * @param e The warning information encoded as an exception.
123    *
124    * @exception SAXException
125    * Any SAX exception, possibly wrapping another exception.
126    */

127   public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
128     Log.logger.log(BasicLevel.ERROR,
129                    "fatal error parsing " + e.getPublicId() +
130                    " at " + e.getLineNumber() + "." + e.getColumnNumber());
131     throw e;
132   }
133
134   /**
135    * Handles notification of a recoverable parser error.
136    *
137    * @param e The warning information encoded as an exception.
138    *
139    * @exception SAXException
140    * Any SAX exception, possibly wrapping another exception.
141    */

142   public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
143     Log.logger.log(BasicLevel.ERROR,
144                    "error parsing " + e.getPublicId() +
145                    " at " + e.getLineNumber() + "." + e.getColumnNumber());
146     throw e;
147   }
148
149
150   /**
151    * Handles notification of a parser warning.
152    *
153    * @param e The warning information encoded as an exception.
154    *
155    * @exception SAXException
156    * Any SAX exception, possibly wrapping another exception.
157    */

158   public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
159     Log.logger.log(BasicLevel.ERROR,
160                    "warning parsing " + e.getPublicId() +
161                    " at " + e.getLineNumber() + "." + e.getColumnNumber());
162     throw e;
163   }
164
165   /**
166    * Initializes parsing of a document.
167    *
168    * @exception SAXException
169    * unspecialized error
170    */

171   public void startDocument() throws SAXException JavaDoc {
172     if (Log.logger.isLoggable(BasicLevel.DEBUG))
173       Log.logger.log(BasicLevel.DEBUG, "startDocument");
174   }
175
176   /**
177    * Receive notification of the start of an element.
178    *
179    * @param uri The Namespace URI
180    * @param localName The local name
181    * @param rawName The qualified name
182    * @param atts The attributes attached to the element.
183    *
184    * @exception SAXException
185    * unspecialized error
186    */

187   public void startElement(String JavaDoc uri,
188                String JavaDoc localName,
189                String JavaDoc rawName,
190                Attributes JavaDoc atts) throws SAXException JavaDoc {
191     String JavaDoc 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 JavaDoc exc) {
205           throw new SAXException JavaDoc(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 JavaDoc exc) {
213             throw new Exception JavaDoc("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 JavaDoc exc) {
220           throw new SAXException JavaDoc(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 JavaDoc exc) {
228             throw new Exception JavaDoc("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 JavaDoc exc) {
234           throw new SAXException JavaDoc(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 JavaDoc exc) {
242             throw new Exception JavaDoc("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 JavaDoc exc) {
248           throw new SAXException JavaDoc(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 JavaDoc("unknown element \"" + name + "\"");
264       }
265     }
266   }
267
268   /**
269    * Receive notification of the end of an element.
270    *
271    * @param uri The Namespace URI
272    * @param localName The local name
273    * @param rawName The qualified name
274    * @param atts The attributes attached to the element.
275    *
276    * @exception SAXException
277    * unspecialized error
278    */

279   public void endElement(String JavaDoc uri,
280              String JavaDoc localName,
281              String JavaDoc rawName) throws SAXException JavaDoc {
282     String JavaDoc 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             // Add the server to the corresponding domains
307
a3cmlConfig.getDomain(network.domain).addServer(server);
308           } else {
309             // Can never happen (see DTD).
310
}
311           network = null;
312         } else if (name.equals(A3CML.ELT_SERVICE)) {
313           if (server != null) {
314             server.addService(service);
315           } else {
316             // Can never happen (see DTD).
317
}
318           service = null;
319         } else if (name.equals(A3CML.ELT_PROPERTY)) {
320           if (server == null && cluster == null)
321             a3cmlConfig.addProperty(property); // Global property
322
else if (server != null)
323             server.addProperty(property); // Server property
324
else if (server == null && cluster != null)
325             cluster.addProperty(property); // Cluster property
326
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 JavaDoc("unknown element \"" + name + "\"");
339         }
340       } catch (SAXException JavaDoc exc) {
341         throw exc;
342       } catch (Exception JavaDoc exc) {
343         throw new SAXException JavaDoc(exc.getMessage());
344       }
345     }
346   }
347
348   /**
349    * Finalizes parsing of a document.
350    *
351    * @exception SAXException
352    * unspecialized error
353    */

354   public void endDocument() throws SAXException JavaDoc {
355     if (Log.logger.isLoggable(BasicLevel.DEBUG))
356       Log.logger.log(BasicLevel.DEBUG, "endDocument");
357   }
358 }
359
Popular Tags