KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > factory > syslog > SyslogTargetFactory


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

62 public class SyslogTargetFactory implements LogTargetFactory
63 {
64     //--------------------------------------------------------------
65
// static
66
//--------------------------------------------------------------
67

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

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

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

90     /**
91      * Creates a log target based on Configuration
92      *
93      * @param conf Configuration requied for creating the log target
94      * @throws ConfigurationException if something goes wrong while reading from
95      * configuration
96      */

97     public LogTarget createTarget( final Configuration conf )
98         throws LogTargetException
99     {
100         Configuration formatConfig = conf.getChild( "format" );
101         final Formatter formatter =
102           m_formatter.createFormatter( formatConfig );
103
104         final Configuration configChild =
105           conf.getChild( "address", false );
106         if( null == configChild )
107         {
108             final String JavaDoc error =
109               REZ.getString( "syslog.error.missing-address" );
110             throw new LogTargetException( error );
111         }
112
113         final InetAddress JavaDoc address = getAddress( configChild );
114         String JavaDoc name = getFacilityName( configChild );
115         int facility = SyslogTarget.getFacilityValue( name );
116         int port = getPort( configChild );
117
118         try
119         {
120             return new SyslogTarget( address, port, formatter, facility );
121         }
122         catch( Throwable JavaDoc e )
123         {
124             final String JavaDoc error =
125               REZ.getString( "syslog.error.internal" );
126             throw new LogTargetException( error, e );
127         }
128     }
129
130     private InetAddress JavaDoc getAddress( Configuration config ) throws LogTargetException
131     {
132         try
133         {
134             return InetAddress.getByName(
135                 config.getAttribute( "hostname" ) );
136         }
137         catch( UnknownHostException JavaDoc uhex )
138         {
139             final String JavaDoc error =
140               REZ.getString( "syslog.error.unknown-host" );
141             throw new LogTargetException( error, uhex );
142         }
143         catch( ConfigurationException e )
144         {
145             final String JavaDoc error =
146               REZ.getString( "syslog.error.missing-host" );
147             throw new LogTargetException( error, e );
148         }
149     }
150
151     private String JavaDoc getFacilityName( Configuration config ) throws LogTargetException
152     {
153         try
154         {
155             return config.getAttribute( "facility" );
156         }
157         catch( ConfigurationException e )
158         {
159             final String JavaDoc error =
160               REZ.getString( "syslog.error.missing-facility" );
161             throw new LogTargetException( error, e );
162         }
163     }
164
165     private int getPort( Configuration config ) throws LogTargetException
166     {
167         try
168         {
169             return config.getAttributeAsInteger( "port" );
170         }
171         catch( ConfigurationException e )
172         {
173             final String JavaDoc error =
174               REZ.getString( "syslog.error.missing-port" );
175             throw new LogTargetException( error, e );
176         }
177     }
178
179 }
180
Popular Tags