1 17 18 package org.apache.avalon.logging.logkit.factory.socket; 19 20 import java.net.InetAddress ; 21 import java.net.UnknownHostException ; 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 57 public class SocketTargetFactory implements LogTargetFactory 58 { 59 63 private static final Resources REZ = 64 ResourceManager.getPackageResources( SocketTargetFactory.class ); 65 66 70 77 public LogTarget createTarget( final Configuration conf ) 78 throws LogTargetException 79 { 80 final InetAddress address; 81 82 final Configuration configChild = 83 conf.getChild( "address", false ); 84 if( null == configChild ) 85 { 86 final String 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 uhex ) 98 { 99 final String error = 100 REZ.getString( "socket.error.unknown-host" ); 101 throw new LogTargetException( error, uhex ); 102 } 103 catch( ConfigurationException e ) 104 { 105 final String 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 error = 118 REZ.getString( "socket.error.missing-port" ); 119 throw new LogTargetException( error, e ); 120 } 121 catch( Throwable e ) 122 { 123 final String error = 124 REZ.getString( "socket.error.internal" ); 125 throw new LogTargetException( error, e ); 126 } 127 } 128 } 129 | Popular Tags |