Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AppenderFactory |
|
| 1.8333333333333333;1,833 |
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.factory; | |
16 | ||
17 | import java.util.ArrayList; | |
18 | import java.util.HashMap; | |
19 | import java.util.List; | |
20 | import java.util.Map; | |
21 | ||
22 | import net.sourceforge.acelogger.appender.Appender; | |
23 | import net.sourceforge.acelogger.constants.AppenderConstants; | |
24 | import net.sourceforge.acelogger.execution.LogController; | |
25 | ||
26 | /** | |
27 | * Factory for managing instances of configured appenders. | |
28 | * | |
29 | * @author Zardi (https://sourceforge.net/users/daniel_zardi) | |
30 | * @version 1.0.0 | |
31 | * @since 1.0.0 | |
32 | */ | |
33 | public final class AppenderFactory { | |
34 | ||
35 | /** | |
36 | * A instance of this class (the only one). | |
37 | */ | |
38 | 1 | private static final AppenderFactory INSTANCE = new AppenderFactory(); |
39 | ||
40 | /** | |
41 | * Used when no configured appender is found for a given identifier. | |
42 | */ | |
43 | private Appender defaultAppender; | |
44 | ||
45 | /** | |
46 | * Maps each configured appender to a unique identifier. | |
47 | */ | |
48 | private final Map<String, Appender> registeredAppenders; | |
49 | ||
50 | /** | |
51 | * Creates a new AppenderFactory (This class is a singleton). | |
52 | * | |
53 | * @since 1.0.0 | |
54 | */ | |
55 | 1 | private AppenderFactory() { |
56 | 1 | registeredAppenders = new HashMap<String, Appender>(); |
57 | 1 | defaultAppender = AppenderConstants.EMPTY; |
58 | 1 | } |
59 | ||
60 | /** | |
61 | * Obtains a properly configured appender for the given identifier. | |
62 | * | |
63 | * @param identifier | |
64 | * The identifier of the intended appender. | |
65 | * @return A properly configured appender for the given identifier. If the identifier is not | |
66 | * found, then the default appender is returned. | |
67 | * @see AppenderFactory#getDefaultAppender() | |
68 | * @since 1.0.0 | |
69 | */ | |
70 | public static Appender getAppender(String identifier) { | |
71 | 0 | LogController.ensureInitialization(); |
72 | 0 | Appender intendedLogger = INSTANCE.registeredAppenders.get(identifier); |
73 | 0 | if (intendedLogger == null) { |
74 | 0 | int lastDotIndex = identifier.lastIndexOf('.'); |
75 | 0 | if (lastDotIndex > 0) { |
76 | 0 | intendedLogger = getAppender(identifier.substring(0, lastDotIndex)); |
77 | } else { | |
78 | 0 | intendedLogger = INSTANCE.defaultAppender; |
79 | } | |
80 | } | |
81 | 0 | return intendedLogger; |
82 | } | |
83 | ||
84 | /** | |
85 | * Sets the default appender of this factory, the one used when no appender is found for a given | |
86 | * identifier. | |
87 | * | |
88 | * @param defaultAppender | |
89 | * The appender that will be used as the default one from now. | |
90 | * @return The previous default appender. | |
91 | * @since 1.0.0 | |
92 | */ | |
93 | public static Appender setDefaultAppender(Appender defaultAppender) { | |
94 | 0 | LogController.ensureInitialization(); |
95 | 0 | Appender previous = INSTANCE.defaultAppender; |
96 | 0 | if (defaultAppender != null) { |
97 | 0 | INSTANCE.defaultAppender = defaultAppender; |
98 | } | |
99 | 0 | return previous; |
100 | } | |
101 | ||
102 | /** | |
103 | * Returns the default appender of this factory, the one used when no appender is found for a | |
104 | * given identifier. | |
105 | * | |
106 | * @return The default appender. | |
107 | * @since 1.0.0 | |
108 | */ | |
109 | public static Appender getDefaultAppender() { | |
110 | 1 | LogController.ensureInitialization(); |
111 | 1 | return INSTANCE.defaultAppender; |
112 | } | |
113 | ||
114 | /** | |
115 | * Returns a list of all appenders registered in this factory. | |
116 | * | |
117 | * @return A list of appenders. | |
118 | * @since 1.0.0 | |
119 | */ | |
120 | public static List<Appender> getRegisteredAppenders() { | |
121 | 2 | LogController.ensureInitialization(); |
122 | 2 | return new ArrayList<Appender>(INSTANCE.registeredAppenders.values()); |
123 | } | |
124 | ||
125 | /** | |
126 | * Registers an appender within this factory. The mapped identifier will be the appenders | |
127 | * identifier. | |
128 | * | |
129 | * @param appender | |
130 | * The appender to be registered. | |
131 | * @return The default appender if there was no previous mapping for the identifier or the | |
132 | * previous configured appender otherwise. | |
133 | * @see AppenderFactory#getDefaultAppender() | |
134 | * @since 1.0.0 | |
135 | */ | |
136 | public static Appender registerAppender(Appender appender) { | |
137 | 1 | LogController.ensureInitialization(); |
138 | 1 | Appender intendedAppender = null; |
139 | 1 | if (appender != null) { |
140 | 0 | intendedAppender = INSTANCE.registeredAppenders.put(appender.getIdentifier(), appender); |
141 | } | |
142 | 1 | if (intendedAppender == null) { |
143 | 1 | intendedAppender = INSTANCE.defaultAppender; |
144 | } | |
145 | 1 | return intendedAppender; |
146 | } | |
147 | } |