KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > factory > socket > SocketTargetFactory


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.socket;
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
29 import org.apache.avalon.util.i18n.ResourceManager;
30 import org.apache.avalon.util.i18n.Resources;
31
32 import org.apache.log.LogTarget;
33 import org.apache.log.output.net.SocketOutputTarget;
34
35 /**
36  * This plugin factory creates LogTargets with a wrapped SocketOutputTarget around it.
37  * <p>
38  * Configuration syntax:
39  * <pre>
40  * &lt;socket-target id="target-id"&gt;
41  * &lt;address hostname="hostname" port="4455" /&gt;
42  * &lt;/socket-target&gt;
43  * </pre>
44  * </p>
45  * <p>
46  * This factory creates a SocketOutputTarget object which will
47  * TCP/IP socket to communicate with the server. The name of the target is specified by the
48  * hostname attribute of the &lt;address&gt; element and the port by the port attribute.
49  * In the config file above the formatting for the log messages is not embedded as it should
50  * be specified on the server side.
51  * </p>
52  *
53  *
54  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
55  * @version $Revision: 1.5 $ $Date: 2004/03/17 10:30:09 $
56  */

57 public class SocketTargetFactory implements LogTargetFactory
58 {
59     //--------------------------------------------------------------
60
// static
61
//--------------------------------------------------------------
62

63     private static final Resources REZ =
64       ResourceManager.getPackageResources( SocketTargetFactory.class );
65
66     //--------------------------------------------------------------
67
// LogTargetFactory
68
//--------------------------------------------------------------
69

70     /**
71      * Creates a log target based on Configuration
72      *
73      * @param conf Configuration requied for creating the log target
74      * @throws ConfigurationException if something goes wrong while reading from
75      * configuration
76      */

77     public LogTarget createTarget( final Configuration conf )
78         throws LogTargetException
79     {
80         final InetAddress JavaDoc address;
81
82         final Configuration configChild =
83           conf.getChild( "address", false );
84         if( null == configChild )
85         {
86             final String JavaDoc error =
87               REZ.getString( "socket.error.missing-address" );
88             throw new LogTargetException( error );
89         }
90
91         try
92         {
93             address =
94               InetAddress.getByName(
95                 configChild.getAttribute( "hostname" ) );
96         }
97         catch( UnknownHostException JavaDoc uhex )
98         {
99             final String JavaDoc error =
100               REZ.getString( "socket.error.unknown-host" );
101             throw new LogTargetException( error, uhex );
102         }
103         catch( ConfigurationException e )
104         {
105             final String JavaDoc error =
106               REZ.getString( "socket.error.missing-host" );
107             throw new LogTargetException( error, e );
108         }
109
110         try
111         {
112             final int port = configChild.getAttributeAsInteger( "port" );
113             return new SocketOutputTarget( address, port );
114         }
115         catch( ConfigurationException e )
116         {
117             final String JavaDoc error =
118               REZ.getString( "socket.error.missing-port" );
119             throw new LogTargetException( error, e );
120         }
121         catch( Throwable JavaDoc e )
122         {
123             final String JavaDoc error =
124               REZ.getString( "socket.error.internal" );
125             throw new LogTargetException( error, e );
126         }
127     }
128 }
129
Popular Tags