KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > factory > MulticastTargetFactory


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;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
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 /**
45  * A log target factory that handles creation of a new multicast log
46  * target instances.
47  *
48  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
49  * @version $Revision: 1.3 $ $Date: 2004/03/08 11:32:01 $
50  */

51 public class MulticastTargetFactory implements LogTargetFactory
52 {
53     //--------------------------------------------------------------
54
// static
55
//--------------------------------------------------------------
56

57     private static final Resources REZ =
58       ResourceManager.getPackageResources( MulticastTargetFactory.class );
59
60     //--------------------------------------------------------------
61
// immutable state
62
//--------------------------------------------------------------
63

64     private final LogTargetManager m_manager;
65
66     //--------------------------------------------------------------
67
// constructor
68
//--------------------------------------------------------------
69

70     public MulticastTargetFactory( LogTargetManager manager )
71     {
72         m_manager = manager;
73     }
74
75     //--------------------------------------------------------------
76
// LogTargetFactory
77
//--------------------------------------------------------------
78

79     /**
80      * Create a LogTarget based on a supplied configuration
81      * @param config the target configuration
82      * @return the multicast target
83      * @exception MissingIdException if a nested target reference
84      * does not declare an id attribute
85      * @exception UnknownLogTargetException if nasted target reference
86      * references an unknown target id
87      */

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 JavaDoc id = ref.getAttribute( "id", null );
97             if( null == id )
98             {
99                 final String JavaDoc 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 JavaDoc 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