1 17 18 package org.apache.avalon.logging.logkit.factory; 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.LogTargetManager; 30 import org.apache.avalon.logging.logkit.MissingIdException; 31 import org.apache.avalon.logging.logkit.UnknownLogTargetException; 32 33 import org.apache.avalon.util.i18n.ResourceManager; 34 import org.apache.avalon.util.i18n.Resources; 35 36 import org.apache.log.LogTarget; 37 import org.apache.log.LogEvent; 38 import org.apache.log.format.ExtendedPatternFormatter; 39 import org.apache.log.format.Formatter; 40 import org.apache.log.format.PatternFormatter; 41 import org.apache.log.format.RawFormatter; 42 import org.apache.log.output.net.DatagramOutputTarget; 43 44 51 public class MulticastTargetFactory implements LogTargetFactory 52 { 53 57 private static final Resources REZ = 58 ResourceManager.getPackageResources( MulticastTargetFactory.class ); 59 60 64 private final LogTargetManager m_manager; 65 66 70 public MulticastTargetFactory( LogTargetManager manager ) 71 { 72 m_manager = manager; 73 } 74 75 79 88 public LogTarget createTarget( final Configuration config ) 89 throws LogTargetException 90 { 91 Configuration[] references = config.getChildren( "targetref" ); 92 LogTarget[] targets = new LogTarget[ references.length ]; 93 for( int i=0; i<references.length; i++ ) 94 { 95 Configuration ref = references[i]; 96 final String id = ref.getAttribute( "id", null ); 97 if( null == id ) 98 { 99 final String error = 100 REZ.getString( "multicast.error.missing-id" ); 101 throw new MissingIdException( error ); 102 } 103 LogTarget target = m_manager.getLogTarget( id ); 104 if( null == target ) 105 { 106 final String error = 107 REZ.getString( "multicast.error.unknown-id", id ); 108 throw new UnknownLogTargetException( error ); 109 } 110 targets[i] = target; 111 } 112 return new MulticastLogTarget( targets ); 113 } 114 115 private final class MulticastLogTarget implements LogTarget 116 { 117 private final LogTarget[] m_targets; 118 119 public MulticastLogTarget( LogTarget[] targets ) 120 { 121 m_targets = targets; 122 } 123 124 public void processEvent( LogEvent event ) 125 { 126 for( int i=0; i<m_targets.length; i++ ) 127 { 128 m_targets[i].processEvent( event ); 129 } 130 } 131 } 132 } 133 134 | Popular Tags |