View Javadoc

1   /*
2    * This file is part of AceLogger.
3    * 
4    * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU
5    * Lesser General Public License as published by the Free Software Foundation, either version 3 of
6    * the License, or (at your option) any later version.
7    * 
8    * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10   * Lesser General Public License for more details.
11   * 
12   * You should have received a copy of the GNU Lesser General Public License along with AceLogger.
13   * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
14   */
15  package net.sourceforge.acelogger.appender;
16  
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import net.sourceforge.acelogger.LogEvent;
23  
24  /**
25   * An implementation of Appender used as a Multiplexer. This class should be used to append a log
26   * call to various appenders.
27   * 
28   * @author Zardi (https://sourceforge.net/users/daniel_zardi)
29   * @version 1.0.0
30   * @since 1.0.0
31   */
32  public class MultipleAppender extends BaseAppender {
33  
34  	/**
35  	 * The list of appenders where log calls will be multiplexed to.
36  	 */
37  	private List<Appender> appenders;
38  
39  	/**
40  	 * Constructs a new MultipleAppender with the supplied identifier that will multiplex its calls
41  	 * to nowhere.
42  	 * 
43  	 * @param identifier
44  	 *            A string that identifies this appender.
45  	 * @since 1.0.0
46  	 */
47  	public MultipleAppender(String identifier) {
48  		this(identifier, Collections.<Appender>emptyList());
49  	}
50  
51  	/**
52  	 * Constructs a new MultipleAppender with the supplied identifier that will multiplex its calls
53  	 * to each appender in the supplied collection.
54  	 * 
55  	 * @param identifier
56  	 *            A string that identifies this appender.
57  	 * @param appenders
58  	 *            The appenders where log calls will be multiplexed to.
59  	 * @since 1.0.0
60  	 */
61  	public MultipleAppender(String identifier, Collection<Appender> appenders) {
62  		super(identifier);
63  		setAppenders(appenders);
64  	}
65  
66  	/**
67  	 * Gets the appenders registered in this multiple appender.
68  	 * 
69  	 * @return An unmodifiable list containing the appenders currently registered.
70  	 * @since 1.0.0
71  	 */
72  	public final List<Appender> getAppenders() {
73  		return Collections.unmodifiableList(appenders);
74  	}
75  
76  	/**
77  	 * Sets the appenders registered in this multiple appender.
78  	 * 
79  	 * @param appenders
80  	 *            A collection of appenders that will be registered in this multiple appender.
81  	 * @since 1.0.0
82  	 */
83  	public final void setAppenders(Collection<Appender> appenders) {
84  		this.appenders = new ArrayList<Appender>(appenders);
85  	}
86  
87  	/**
88  	 * Adds an appender to this multiplexer.
89  	 * 
90  	 * @param appender
91  	 *            The appender that should be added to the multiplexer.
92  	 * @return True if the appender was added successfully; false otherwise.
93  	 * @since 1.0.0
94  	 */
95  	public final boolean addAppender(Appender appender) {
96  		return appenders.add(appender);
97  	}
98  
99  	/**
100 	 * Adds each appender in a collection to this multiplexer.
101 	 * 
102 	 * @param appenders
103 	 *            The appender collection that should be added to the multiplexer.
104 	 * @return True if the appender list was modified; false otherwise.
105 	 * @since 1.0.0
106 	 */
107 	public final boolean addAllAppenders(Collection<Appender> appenders) {
108 		return this.appenders.addAll(appenders);
109 	}
110 
111 	/**
112 	 * Removes an appender from this multiplexer.
113 	 * 
114 	 * @param appender
115 	 *            The appender that should be removed from this multiplexer.
116 	 * @return True if the element was removed; false otherwise.
117 	 * @since 1.0.0
118 	 */
119 	public final boolean removeAppender(Appender appender) {
120 		return appenders.remove(appender);
121 	}
122 
123 	/** {@inheritDoc} */
124 	protected Runnable appendLogProcess(final LogEvent call) {
125 		Runnable process = new Runnable() {
126 			/**
127 			 * Appends the log event in every appender registered.
128 			 */
129 			public void run() {
130 				for (Appender currentAppender : appenders) {
131 					currentAppender.appendLog(call);
132 				}
133 			}
134 		};
135 		return process;
136 	}
137 
138 }