KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > BasicLogger_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.net.*;
54 import java.util.*;
55 import java.text.*;
56 import org.jdom.*;
57 import com.protomatter.xml.*;
58 import com.protomatter.syslog.*;
59
60 /**
61  * XML configuration helper for <tt>BasicLogger</tt>.
62  */

63
64 public abstract class BasicLogger_Helper
65             implements XMLConfigHelper
66 {
67     /**
68      * Configure this logger given the XML element.
69      * The <TT>&lt;Logger&gt;</tt> element should look like this:<P>
70      *
71      * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
72      * <TR><TD>
73      * <PRE><B>
74      *
75      * &lt;Logger
76      * name="<i>LoggerName</i>"
77      * class="<i>LoggerClassName</i>"
78      * &gt;
79      *
80      * <font color="#888888">&lt;!-- Logger subclass directives --&gt;</font>
81      *
82      * &lt;Policy class="<i>LogPolicyClass</i>" &gt;
83      * <font color="#888888">&lt;!-- Policy directives --&gt;</font>
84      * &lt;/Policy&gt;
85      *
86      * &lt;Format class="<i>LogFormatClass</i>" &gt;
87      * <font color="#888888">&lt;!-- Formatter directives --&gt;</font>
88      * &lt;/Format&gt;
89      *
90      * &lt;/Logger&gt;
91      * </B></PRE>
92      * </TD></TR></TABLE><P>
93      *
94      * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
95      * <TR CLASS="TableHeadingColor">
96      * <TD COLSPAN=3><B>Attributes:</B></TD>
97      * </TR>
98      * <TR CLASS="TableHeadingColor">
99      * <TD><B>name</B></TD>
100      * <TD><B>value</B></TD>
101      * <TD><B>required</B></TD>
102      * </TR>
103      *
104      * <TR CLASS="TableRowColor">
105      * <TD VALIGN=TOP><TT>name</TT></TD>
106      * <TD VALIGN=TOP>Symbolic name for the logger.
107      * </TD>
108      * <TD VALIGN=TOP>no</TD>
109      * </TR>
110      *
111      * <TR CLASS="TableRowColor">
112      * <TD VALIGN=TOP><TT>class</TT></TD>
113      * <TD VALIGN=TOP>Full class name of the logger to load.
114      * Must be a class that implements the
115      * <tt>com.protomatter.syslog.Syslogger</tt> interface.
116      * </TD>
117      * <TD VALIGN=TOP>yes</TD>
118      * </TR>
119      *
120      * </TABLE><P>
121      *
122      * If the logger is a subclass of this class (<TT>BasicLogger</TT>),
123      * then the optional <tt>&lt;Policy&gt;</tt> and <tt>&lt;Format&gt;</tt>
124      * elements are processed. If each is present, it is passed
125      * to the <tt>configure()</tt> method of the
126      * <TT>LogPolicy</TT> or <TT>SyslogTextFormatter</TT> object specified
127      * so they can configure themselves. It is assumed that each of
128      * those elements contains other configuration information that
129      * the individual policy or format class would understand.
130      *
131      * @see SimpleSyslogTextFormatter_Helper#configure(Object,Element)
132      * @see HTMLSyslogTextFormatter_Helper#configure(Object,Element)
133      * @see SimpleLogPolicy_Helper#configure(Object,Element)
134      * @see PerClassPolicy_Helper#configure(Object,Element)
135      */

136     public void configure(Object JavaDoc o, Element e)
137     throws SyslogInitException
138     {
139         configure(o, e, true, true);
140     }
141
142     public void configure(Object JavaDoc o, Element e, boolean includeFormat, boolean includePolicy)
143     throws SyslogInitException
144     {
145         BasicLogger log = (BasicLogger)o;
146
147         // name
148
String JavaDoc tmp = e.getAttributeValue("name");
149
150         if (tmp != null && !tmp.equals(""))
151             log.setName(tmp);
152
153         // figure out what text formatter to use
154
if (includeFormat)
155         {
156             Element formatElement = null;
157             String JavaDoc formatClass = null;
158             formatElement = e.getChild("Format", e.getNamespace());
159
160             if (formatElement != null)
161             {
162                 formatClass = formatElement.getAttributeValue("class");
163
164                 if (formatClass == null)
165                 {
166                     throw new IllegalArgumentException JavaDoc(MessageFormat.format(
167                                                            Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_ATTRIBUTE_MESSAGE),
168                                                            new Object JavaDoc[] { "class", "Format" } ));
169                 }
170
171                 try
172                 {
173                     SyslogTextFormatter formatter
174                     = (SyslogTextFormatter)Class.forName(formatClass).newInstance();
175
176                     try
177                     {
178                         // configure the formatter
179
XMLConfigHelper helper = XMLConfigUtil.getConfigHelper(formatter);
180                         helper.configure(formatter, formatElement);
181                         log.setTextFormatter(formatter);
182                     }
183                     catch (Exception JavaDoc x)
184                     {
185                         x.printStackTrace();
186                         throw new IllegalArgumentException JavaDoc(MessageFormat.format(
187                                                                Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_CONFIGURE_FORMATTER_MESSAGE),
188                                                                new Object JavaDoc[] { formatClass } ));
189                     }
190
191                 }
192                 catch (Exception JavaDoc x)
193                 {
194                     x.printStackTrace();
195                     throw new IllegalArgumentException JavaDoc(MessageFormat.format(
196                                                            Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_LOAD_FORMATTER_MESSAGE),
197                                                            new Object JavaDoc[] { formatClass } ));
198                 }
199
200             }
201             else
202             {
203                 log.setTextFormatter(new SimpleSyslogTextFormatter());
204             }
205
206         }
207
208         // figure out what policy to use
209
if (includePolicy)
210         {
211             Element policyElement = null;
212             String JavaDoc policyClass = null;
213             policyElement = e.getChild("Policy", e.getNamespace());
214
215             if (policyElement != null)
216             {
217                 policyClass = policyElement.getAttributeValue("class");
218
219                 if (policyClass == null)
220                 {
221                     throw new IllegalArgumentException JavaDoc(MessageFormat.format(
222                                                            Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_ATTRIBUTE_MESSAGE),
223                                                            new Object JavaDoc[] { "class", "Policy" } ));
224                 }
225
226                 try
227                 {
228                     LogPolicy policy = (LogPolicy)Class.forName(policyClass).newInstance();
229
230                     try
231                     {
232                         // configure the formatter
233
XMLConfigHelper helper = XMLConfigUtil.getConfigHelper(policy);
234                         helper.configure(policy, policyElement);
235                         log.setPolicy(policy);
236                     }
237                     catch (Exception JavaDoc x)
238                     {
239                         x.printStackTrace();
240                         throw new IllegalArgumentException JavaDoc(MessageFormat.format(
241                                                                Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_CONFIGURE_POLICY_MESSAGE),
242                                                                new Object JavaDoc[] { policyClass } ));
243                     }
244
245                 }
246                 catch (Exception JavaDoc x)
247                 {
248                     x.printStackTrace();
249                     throw new IllegalArgumentException JavaDoc(MessageFormat.format(
250                                                            Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_LOAD_POLICY_MESSAGE),
251                                                            new Object JavaDoc[] { policyClass } ));
252                 }
253
254             }
255             else
256             {
257                 log.setPolicy(new SimpleLogPolicy());
258             }
259
260         }
261     }
262
263     public Element getConfiguration(Object JavaDoc o, Element element)
264     {
265         return getConfiguration(o, element, true, true);
266     }
267
268     public Element getConfiguration(Object JavaDoc o, Element element, boolean includeFormat, boolean includePolicy)
269     {
270         BasicLogger log = (BasicLogger)o;
271
272         if (element == null)
273         {
274             element = new Element("Logger");
275         }
276
277         // set the name of this logger.
278
if (log.getName() != null)
279         {
280             element.setAttribute("name", log.getName());
281         }
282
283         element.setAttribute("class", log.getClass().getName());
284
285         // text formatter stuff
286
if (includeFormat)
287         {
288             Object JavaDoc format = log.getTextFormatter();
289             Element formatterElement = new Element("Format");
290             formatterElement.setAttribute("class", format.getClass().getName());
291
292             XMLConfigHelper helper = null;
293
294             try
295             {
296                 helper = XMLConfigUtil.getConfigHelper(format);
297             }
298             catch (Exception JavaDoc x)
299             {
300                 x.printStackTrace();
301             }
302
303             formatterElement = helper.getConfiguration(format, formatterElement);
304
305             element.getChildren().add(formatterElement);
306         }
307
308         // policy stuff
309
if (includePolicy)
310         {
311             Object JavaDoc policy = log.getPolicy();
312             Element policyElement = new Element("Policy");
313             policyElement.setAttribute("class", policy.getClass().getName());
314
315             XMLConfigHelper helper = null;
316
317             try
318             {
319                 helper = XMLConfigUtil.getConfigHelper(policy);
320             }
321             catch (Exception JavaDoc x)
322             {
323                 x.printStackTrace();
324             }
325
326             policyElement = helper.getConfiguration(policy, policyElement);
327
328             element.getChildren().add(policyElement);
329         }
330
331         return element;
332     }
333 }
334
Popular Tags