KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > DatabaseLog_Helper


1 package com.protomatter.syslog.xml;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.text.*;
57
58 import com.protomatter.xml.*;
59 import com.protomatter.syslog.*;
60 import com.protomatter.util.ChainedRuntimeException;
61 import org.jdom.*;
62
63 /**
64  * XML configuration helper for <tt>DatabaseLog</tt>.
65  */

66 public class DatabaseLog_Helper
67 extends BasicLogger_Helper
68 {
69   /**
70    * Configure this logger given the XML element.
71    * The <tt>&lt;Logger&gt;</tt> element should look like
72    * this:<P>
73    *
74    * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
75    * <TR><TD>
76    * <PRE><B>
77    *
78    * &lt;Logger class="com.protomatter.syslog.DatabaseLog" &gt;
79    *
80    * <font color="#888888">&lt;!--
81    * Config params from {@link BasicLogger_Helper#configure(Object,Element) BasicLogger_Helper} can
82    * get inserted here.
83    * --&gt;</font>
84    *
85    * <font color="#888888">&lt;!--
86    * The &lt;Format&gt; tag is included here, as it is used
87    * to set field truncation sizes.
88    * --&gt;</font>
89    * &lt;Format class="com.protomatter.syslog.SimpleSyslogTextFormatter"&gt;
90    * &lt;classWidth&gt;<i>column-width</i>&lt;/classWidth&gt;
91    * &lt;channelWidth&gt;<i>column-width</i>&lt;/channelWidth&gt;
92    * &lt;threadWidth&gt;<i>column-width</i>&lt;/threadWidth&gt;
93    * &lt;hostWidth&gt;<i>column-width</i>&lt;/hostWidth&gt;
94    * &lt;/Format&gt;
95    *
96    * &lt;messageWidth&gt;<i>column-width</i>&lt;/messageWidth&gt;
97    * &lt;detailWidth&gt;<i>column-width</i>&lt;/detailWidth&gt;
98    *
99    * &lt;driver&gt;<i>JDBCDriver</i>&lt;/driver&gt;
100    * &lt;url&gt;<i>JDBC-URL</i>&lt;/url&gt;
101    * &lt;tablePrefix&gt;<i>TablePrefix</i>&lt;/tablePrefix&gt;
102    * &lt;numRetries&gt;<i>NumRetries</i>&lt;/numRetries&gt;
103    *
104    * &lt;statementAdapter&gt;<i>StatementAdapterClass</i>&lt;/statementAdapter&gt;
105    *
106    * &lt;ConnectionProperties&gt;
107    * &lt;property&gt;
108    * &lt;name&gt;<i>name-1</i>&lt;/name&gt;
109    * &lt;value&gt;<i>value-1</i>&lt;/value&gt;
110    * &lt;/property&gt;
111    * &lt;property&gt;
112    * &lt;name&gt;<i>name-2</i>&lt;/name&gt;
113    * &lt;value&gt;<i>value-2</i>&lt;/value&gt;
114    * &lt;/property&gt;
115    * ...
116    * &lt;property&gt;
117    * &lt;name&gt;<i>name-N</i>&lt;/name&gt;
118    * &lt;value&gt;<i>value-N</i>&lt;/value&gt;
119    * &lt;/property&gt;
120    * &lt;/ConnectionProperties&gt;
121    *
122    * &lt;/Logger&gt;
123    * </B></PRE>
124    * </TD></TR></TABLE><P>
125    *
126    * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
127    * <TR CLASS="TableHeadingColor">
128    * <TD COLSPAN=3><B>Element</B></TD>
129    * </TR>
130    * <TR CLASS="TableHeadingColor">
131    * <TD><B>name</B></TD>
132    * <TD><B>value</B></TD>
133    * <TD><B>required</B></TD>
134    * </TR>
135    *
136    * <TR CLASS="TableRowColor">
137    * <TD VALIGN=TOP><tt>messageWidth</tt></TD>
138    * <TD VALIGN=TOP>Short message column width.</TD>
139    * <TD VALIGN=TOP>no (default is 255)</TD>
140    * </TR>
141    *
142    * <TR CLASS="TableRowColor">
143    * <TD VALIGN=TOP><tt>detailWidth</tt></TD>
144    * <TD VALIGN=TOP>Short message detail width.</TD>
145    * <TD VALIGN=TOP>no (default is 4000)</TD>
146    * </TR>
147    *
148    * <TR CLASS="TableRowColor">
149    * <TD VALIGN=TOP><tt>driver</tt></TD>
150    * <TD VALIGN=TOP>JDBC Driver class name</TD>
151    * <TD VALIGN=TOP>yes</TD>
152    * </TR>
153    *
154    * <TR CLASS="TableRowColor">
155    * <TD VALIGN=TOP><tt>url</tt></TD>
156    * <TD VALIGN=TOP>JDBC connection URL</TD>
157    * <TD VALIGN=TOP>yes</TD>
158    * </TR>
159    *
160    * <TR CLASS="TableRowColor">
161    * <TD VALIGN=TOP><tt>tablePrefix</tt></TD>
162    * <TD VALIGN=TOP>A prefix for table names</TD>
163    * <TD VALIGN=TOP>no</TD>
164    * </TR>
165    *
166    * <TR CLASS="TableRowColor">
167    * <TD VALIGN=TOP><tt>numRetries</tt></TD>
168    * <TD VALIGN=TOP>Number of times to retry writing each message</TD>
169    * <TD VALIGN=TOP>no (default is 3)</TD>
170    * </TR>
171    *
172    * <TR CLASS="TableRowColor">
173    * <TD VALIGN=TOP><tt>statementAdapter</tt></TD>
174    * <TD VALIGN=TOP>Full classname of the database statement adapter.</TD>
175    * <TD VALIGN=TOP>no (default is "<TT>StringDatabaseStatementAdapter</TT>")</TD>
176    * </TR>
177    *
178    * </TR>
179    *
180    * <TR CLASS="TableRowColor">
181    * <TD VALIGN=TOP><tt>ConnectionProperties</tt></TD>
182    * <TD VALIGN=TOP>
183    * Contains a set of <TT>&lt;property&gt;</TT> elements,
184    * each containing a <TT>&lt;name&gt;</TT> and
185    * <TT>&lt;value&gt;</TT> elements defining the connection
186    * properties.
187    * </TD>
188    * <TD VALIGN=TOP>no</TD>
189    * </TR>
190    *
191    * </TR>
192    *
193    * </TABLE><P>
194    *
195    */

196   public void configure(Object JavaDoc o, Element e)
197   throws SyslogInitException
198   {
199     super.configure(o, e);
200
201     if (Syslog.getLocalHostName() == null)
202     {
203       throw new IllegalArgumentException JavaDoc(
204         MessageFormat.format(
205           Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_ATTRIBUTE_MESSAGE),
206           new Object JavaDoc[] { "hostname", "Syslog" } ));
207     }
208
209     DatabaseLog log = (DatabaseLog)o;
210
211     String JavaDoc tmp = null;
212
213     tmp = e.getChildTextTrim("driver", e.getNamespace());
214     if (tmp != null)
215     {
216       log.setDriver(tmp);
217     }
218     else
219     {
220       throw new IllegalArgumentException JavaDoc(
221         MessageFormat.format(
222         Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
223           new Object JavaDoc[] { "driver" } ));
224     }
225
226     tmp = e.getChildTextTrim("url", e.getNamespace());
227     if (tmp != null)
228     {
229       log.setURL(tmp);
230     }
231     else
232     {
233       throw new IllegalArgumentException JavaDoc(
234         MessageFormat.format(
235         Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
236           new Object JavaDoc[] { "url" } ));
237     }
238
239     tmp = e.getChildTextTrim("messageWidth", e.getNamespace());
240     if (tmp != null)
241     {
242       log.setMessageWidth(Integer.parseInt(tmp));
243     }
244
245     tmp = e.getChildTextTrim("detailWidth", e.getNamespace());
246     if (tmp != null)
247     {
248       log.setDetailWidth(Integer.parseInt(tmp));
249     }
250
251     tmp = e.getChildTextTrim("tablePrefix", e.getNamespace());
252     if (tmp != null)
253     {
254       log.setTablePrefix(tmp);
255     }
256
257     tmp = e.getChildTextTrim("numRetries", e.getNamespace());
258     if (tmp != null)
259     {
260       try
261       {
262         log.setNumRetries(Integer.parseInt(tmp.trim()));
263       }
264       catch (NumberFormatException JavaDoc x)
265       {
266         throw new IllegalArgumentException JavaDoc(
267           MessageFormat.format(Syslog.getResourceString(MessageConstants.MUST_BE_INTEGER_PARAM_MESSAGE),
268           new Object JavaDoc[] { "numRetries" } ));
269       }
270     }
271
272     tmp = e.getChildTextTrim("statementAdapter", e.getNamespace());
273     if (tmp != null)
274     {
275       try
276       {
277         DatabaseLogStatementAdapter adapter = (DatabaseLogStatementAdapter)Class.forName(tmp).newInstance();
278         log.setStatementAdapter(adapter);
279       }
280       catch (Exception JavaDoc x)
281       {
282          throw new ChainedRuntimeException(MessageFormat.format(
283            Syslog.getResourceString(MessageConstants.DATABASELOG_CANNOT_LOAD_STATEMENT_ADAPTER_MESSAGE),
284            new Object JavaDoc[] { tmp }), x);
285       }
286     }
287
288     Element element = e.getChild("ConnectionProperties", e.getNamespace());
289     Properties props = new Properties();
290     if (element != null)
291     {
292       Iterator params = element.getChildren("property", e.getNamespace()).iterator();
293       while (params.hasNext())
294       {
295         Element param = (Element)params.next();
296         String JavaDoc name = param.getChildTextTrim("name", e.getNamespace());
297         String JavaDoc value = param.getChildTextTrim("value", e.getNamespace());
298         if (name != null && value != null)
299           props.put(name.trim(), value.trim());
300       }
301     }
302     log.setProperties(props);
303   }
304
305   public Element getConfiguration(Object JavaDoc o, Element element)
306   {
307     Element e = super.getConfiguration(o, element);
308
309     DatabaseLog log = (DatabaseLog)o;
310
311     Element param = new Element("driver");
312     param.setText(log.getDriver());
313     e.getChildren().add(param);
314
315     param = new Element("url");
316     param.setText(log.getURL());
317     e.getChildren().add(param);
318
319     param = new Element("tablePrefix");
320     param.setText(log.getTablePrefix());
321     e.getChildren().add(param);
322
323     param = new Element("numRetries");
324     param.setText(String.valueOf(log.getNumRetries()));
325     e.getChildren().add(param);
326
327     param = new Element("messageWidth");
328     param.setText(String.valueOf(log.getMessageWidth()));
329     e.getChildren().add(param);
330
331     param = new Element("detailWidth");
332     param.setText(String.valueOf(log.getDetailWidth()));
333     e.getChildren().add(param);
334
335     param = new Element("ConnectionProperties");
336
337     Properties props = log.getProperties();
338     Enumeration pe = props.keys();
339     while (pe.hasMoreElements())
340     {
341       String JavaDoc key = (String JavaDoc)pe.nextElement();
342       String JavaDoc val = props.getProperty(key);
343
344       Element prop = new Element("property");
345       prop.getChildren().add((new Element("name")).setText(key));
346       prop.getChildren().add((new Element("value")).setText(val));
347       param.getChildren().add(prop);
348     }
349     e.getChildren().add(param);
350
351     param = new Element("statementAdapter");
352     param.setText(String.valueOf(log.getStatementAdapter().getClass().getName()));
353     e.getChildren().add(param);
354
355
356     return e;
357   }
358 }
359
Popular Tags