KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.avalon.excalibur.logger.factory;
2
3 import java.io.IOException JavaDoc;
4 import java.net.InetAddress JavaDoc;
5 import java.net.UnknownHostException JavaDoc;
6
7 import org.apache.log.LogTarget;
8 import org.apache.log.output.net.SocketOutputTarget;
9 import org.apache.avalon.framework.configuration.Configuration;
10 import org.apache.avalon.framework.configuration.ConfigurationException;
11
12 /**
13  * SocketTargetFactory
14  *
15  * This factory creates LogTargets with a wrapped SocketOutputTarget around it:
16  *
17  * <pre>
18  * &lt;socket-target id="target-id"&gt;
19  * &lt;address hostname="hostname" port="4455" /&gt;
20  * &lt;/socket-target&gt;
21  * </pre>
22  *
23  * <p>
24  *
25  * This factory creates a SocketOutputTarget object which will
26  * TCP/IP socket to communicate with the server. The name of the target is specified by the
27  * hostname attribute of the &lt;address&gt; element and the port by the port attribute.
28  * In the config file above the formatting for the log messages is not embedded as it should
29  * be specified on the server side
30  *
31  * </p>
32  *
33  *
34  * @author <a HREF="mailto:rghorpade@onebridge.de"> Rajendra Ghorpade </a>
35  * @version
36  */

37 public class SocketTargetFactory extends AbstractTargetFactory
38 {
39
40      /**
41      * Creates a log target based on Configuration
42      *
43      *@param conf Configuration requied for creating the log target
44      *@exception ConfigurationException if something goes wrong while reading from
45      * configuration
46      */

47     public LogTarget createTarget( final Configuration conf )
48     throws ConfigurationException
49     {
50         final InetAddress JavaDoc address;
51
52         final Configuration configChild = conf.getChild( "address", false );
53         if ( null == configChild )
54         {
55             throw new ConfigurationException( "target address not specified in the config" );
56         }
57
58         try
59         {
60             address = InetAddress.getByName( configChild.getAttribute( "hostname" ) );
61         }
62         catch ( UnknownHostException JavaDoc uhex )
63         {
64             throw new ConfigurationException( "Host specified in socket target adress is unknown!", uhex );
65         }
66
67         final int port = configChild.getAttributeAsInteger( "port" );
68
69         try
70         {
71             return new SocketOutputTarget( address, port );
72         }
73         catch ( final IOException JavaDoc ioex )
74         {
75             throw new ConfigurationException( "Failed to create target!", ioex.fillInStackTrace() );
76         }
77     }
78 }
79
Popular Tags