KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > factory > datagram > DatagramTargetFactory


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.logging.logkit.factory.datagram;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26
27 import org.apache.avalon.logging.logkit.LogTargetException;
28 import org.apache.avalon.logging.logkit.LogTargetFactory;
29 import org.apache.avalon.logging.logkit.FormatterFactory;
30
31 import org.apache.avalon.util.i18n.ResourceManager;
32 import org.apache.avalon.util.i18n.Resources;
33
34 import org.apache.log.LogTarget;
35 import org.apache.log.format.Formatter;
36 import org.apache.log.output.net.DatagramOutputTarget;
37
38 /**
39  * This factory creates LogTargets with a wrapped DatagramOutputTarget around it.
40  * <p>
41  * Configuration syntax:
42  * <pre>
43  * &lt;datagram-target id="target-id"&gt;
44  * &lt;address hostname="hostname" port="4455" /&gt;
45  * &lt;format type="extended"&gt;
46  * %7.7{priority} %23.23{time:yyyy-MM-dd HH:mm:ss:SSS} [%25.25{category}] : %{message}\n%{throwable}
47  * &lt;/format&gt;
48  * &lt;/datagram-target&gt;
49  * </pre>
50  * </p>
51  * <p>
52  * This factory creates a DatagramOutputTarget object which will
53  * sends datagrams to the specified address. The name of the target is specified by the hostname attribute
54  * of the &lt;address&gt; element and the port by the port attribute.The &lt;format&gt; element
55  * wraps the format to output the log.
56  * </p>
57  *
58  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
59  * @version $Revision: 1.5 $ $Date: 2004/03/17 10:30:09 $
60  */

61 public class DatagramTargetFactory implements LogTargetFactory
62 {
63     //--------------------------------------------------------------
64
// static
65
//--------------------------------------------------------------
66

67     private static final Resources REZ =
68       ResourceManager.getPackageResources( DatagramTargetFactory.class );
69
70     //--------------------------------------------------------------
71
// immutable state
72
//--------------------------------------------------------------
73

74     private final FormatterFactory m_formatter;
75
76     //--------------------------------------------------------------
77
// constructor
78
//--------------------------------------------------------------
79

80     public DatagramTargetFactory( FormatterFactory formatter )
81     {
82         m_formatter = formatter;
83     }
84
85     //--------------------------------------------------------------
86
// LogTargetFactory
87
//--------------------------------------------------------------
88

89     /**
90      * Create a LogTarget based on a supplied configuration
91      * @param conf the target coonfiguration
92      * @return the datagram target
93      * @exception LogTargetException if a target creation error occurs
94      */

95     public LogTarget createTarget( final Configuration conf )
96         throws LogTargetException
97     {
98         InetAddress JavaDoc address;
99
100         final Configuration configChild = conf.getChild( "address", false );
101         if( null == configChild )
102         {
103             final String JavaDoc error =
104               REZ.getString( "datagram.error.missing-address" );
105             throw new LogTargetException( error );
106         }
107
108         try
109         {
110             address =
111               InetAddress.getByName(
112                 configChild.getAttribute( "hostname" ) );
113         }
114         catch( UnknownHostException JavaDoc uhex )
115         {
116             final String JavaDoc error =
117               REZ.getString( "datagram.error.unknown-host" );
118             throw new LogTargetException( error, uhex );
119         }
120         catch( ConfigurationException e )
121         {
122             final String JavaDoc error =
123               REZ.getString( "datagram.error.missing-host" );
124             throw new LogTargetException( error, e );
125         }
126
127         Configuration formatConfig = conf.getChild( "format" );
128
129         final Formatter formatter =
130           m_formatter.createFormatter( formatConfig );
131
132         try
133         {
134             int port = configChild.getAttributeAsInteger( "port" );
135             return new DatagramOutputTarget( address, port, formatter );
136         }
137         catch( IOException JavaDoc ioex )
138         {
139             final String JavaDoc error =
140               REZ.getString( "datagram.error.internal" );
141             throw new LogTargetException( error, ioex );
142         }
143         catch( ConfigurationException e )
144         {
145             final String JavaDoc error =
146               REZ.getString( "datagram.error.missing-port" );
147             throw new LogTargetException( error, e );
148         }
149     }
150 }
151
152
Popular Tags