KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > factory > DatagramTargetFactory


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.apache.avalon.excalibur.logger.factory;
9
10 import java.io.OutputStream JavaDoc;
11 import org.apache.avalon.excalibur.logger.factory.AbstractTargetFactory;
12 import org.apache.avalon.framework.configuration.Configuration;
13 import org.apache.avalon.framework.configuration.ConfigurationException;
14 import org.apache.log.LogTarget;
15 import org.apache.log.format.ExtendedPatternFormatter;
16 import org.apache.log.format.Formatter;
17 import org.apache.log.format.PatternFormatter;
18 import org.apache.log.format.RawFormatter;
19 import org.apache.log.output.net.DatagramOutputTarget;
20
21 import java.io.IOException JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24
25 /**
26  * DatagramTargetFactory
27  *
28  * This factory creates LogTargets with a wrapped DatagramOutputTarget around it:
29  *
30  * <pre>
31  *
32  * &lt;datagram-target id="target-id"&gt;
33  * &lt;address hostname="hostname" port="4455" /&gt;
34  * &lt;format type="extended"&gt;
35  * %7.7{priority} %23.23{time:yyyy-MM-dd HH:mm:ss:SSS} [%25.25{category}] : %{message}\n%{throwable}
36  * &lt;/format&gt;
37  * &lt;/datagram-target&gt;
38  * </pre>
39  *
40  * <p>
41  * This factory creates a DatagramOutputTarget object which will
42  * sends datagrams to the specified address. The name of the target is specified by the hostname attribute
43  * of the &lt;address&gt; element and the port by the port attribute.The &lt;address&gt; element
44  * wraps the format to output the log.
45  * </p>
46  *
47  *
48  * @author <a HREF="mailto:rghorpade@onebridge.de"> Rajendra Ghorpade </a>
49  * @version
50  */

51 public class DatagramTargetFactory
52     extends AbstractTargetFactory
53 {
54     /** Default format */
55     private static final String JavaDoc FORMAT =
56         "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}";
57
58     /**
59      * Create a LogTarget based on a Configuration
60      */

61     public LogTarget createTarget( final Configuration conf )
62         throws ConfigurationException
63     {
64         InetAddress JavaDoc address;
65
66         final Configuration configChild = conf.getChild( "address", false );
67         if ( null == configChild )
68         {
69             throw new ConfigurationException( "target address not specified in the config" );
70         }
71
72         try
73         {
74             address = InetAddress.getByName( configChild.getAttribute( "hostname" ) );
75         }
76         catch ( UnknownHostException JavaDoc uhex )
77         {
78             throw new ConfigurationException( "Host specified in datagram target adress is unknown!", uhex );
79         }
80
81         int port = configChild.getAttributeAsInteger( "port" );
82
83         final Formatter formatter = getFormatter( conf.getChild( "format", false ) );
84
85         try
86         {
87             return new DatagramOutputTarget( address, port, formatter );
88         }
89         catch ( IOException JavaDoc ioex )
90         {
91             throw new ConfigurationException( "Failed to create target!", ioex );
92         }
93     }
94
95     /**
96      * Returns the Formatter
97      *
98      * @param conf Configuration for the formatter
99      */

100     protected Formatter getFormatter( final Configuration conf )
101     {
102         final String JavaDoc type = conf.getAttribute( "type", "pattern" );
103         final String JavaDoc format = conf.getValue( FORMAT );
104
105         if( "extended".equals( type ) )
106         {
107             return new ExtendedPatternFormatter( format );
108         }
109         else if( "raw".equals( type ) )
110         {
111             return new RawFormatter();
112         }
113
114         /** default formatter */
115         return new PatternFormatter( format );
116     }
117 }
118
119
Popular Tags