1 17 18 package org.apache.avalon.logging.logkit.factory.datagram; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.UnknownHostException ; 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 61 public class DatagramTargetFactory implements LogTargetFactory 62 { 63 67 private static final Resources REZ = 68 ResourceManager.getPackageResources( DatagramTargetFactory.class ); 69 70 74 private final FormatterFactory m_formatter; 75 76 80 public DatagramTargetFactory( FormatterFactory formatter ) 81 { 82 m_formatter = formatter; 83 } 84 85 89 95 public LogTarget createTarget( final Configuration conf ) 96 throws LogTargetException 97 { 98 InetAddress address; 99 100 final Configuration configChild = conf.getChild( "address", false ); 101 if( null == configChild ) 102 { 103 final String 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 uhex ) 115 { 116 final String error = 117 REZ.getString( "datagram.error.unknown-host" ); 118 throw new LogTargetException( error, uhex ); 119 } 120 catch( ConfigurationException e ) 121 { 122 final String 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 ioex ) 138 { 139 final String error = 140 REZ.getString( "datagram.error.internal" ); 141 throw new LogTargetException( error, ioex ); 142 } 143 catch( ConfigurationException e ) 144 { 145 final String error = 146 REZ.getString( "datagram.error.missing-port" ); 147 throw new LogTargetException( error, e ); 148 } 149 } 150 } 151 152 | Popular Tags |