Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / config / logback-config / src / main / java / org / opendaylight / controller / config / yang / logback / config / ContextSetterImpl.java
1 /**
2  * Generated file
3
4  * Generated from: yang module name: config-test  yang module local name: testing
5  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
6  * Generated at: Fri Sep 27 14:06:33 CEST 2013
7  *
8  * Do not modify this file unless it is present under src/main directory
9  */
10 package org.opendaylight.controller.config.yang.logback.config;
11
12 import java.io.Closeable;
13 import java.io.IOException;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.slf4j.LoggerFactory;
21
22 import ch.qos.logback.classic.Level;
23 import ch.qos.logback.classic.LoggerContext;
24 import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
25 import ch.qos.logback.classic.filter.ThresholdFilter;
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.Appender;
28 import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
29 import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
30 import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
31
32 import com.google.common.base.Optional;
33 import com.google.common.base.Preconditions;
34 import com.google.common.collect.Sets;
35
36 /**
37  * Implementation of {@link ContextSetter}. Resets running logback
38  * configuration.
39  */
40 public class ContextSetterImpl implements ContextSetter, Closeable {
41
42     private final LogbackStatusListener statusListener;
43     private static final org.slf4j.Logger classLogger = LoggerFactory.getLogger(ContextSetterImpl.class);
44
45     public ContextSetterImpl(LogbackRuntimeRegistrator rootRuntimeBeanRegistratorWrapper) {
46         statusListener = new LogbackStatusListener(rootRuntimeBeanRegistratorWrapper);
47         statusListener.register();
48     }
49
50     public void updateContext(LogbackModule module) {
51         LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
52
53         List<ch.qos.logback.classic.Logger> loggersBefore = context.getLoggerList();
54
55         createLoggers(context, module, Sets.newHashSet(loggersBefore));
56     }
57
58     private Map<String, Appender<ILoggingEvent>> createConsoleAppenders(LoggerContext context, LogbackModule module) {
59         Map<String, Appender<ILoggingEvent>> appendersMap = new HashMap<>();
60         for (ConsoleAppenderTO appender : module.getConsoleAppenderTO()) {
61             Preconditions.checkState(appendersMap.containsKey(appender.getName()) == false,
62                     "Duplicate appender name %s", appender.getName());
63             ch.qos.logback.core.ConsoleAppender app = new ch.qos.logback.core.ConsoleAppender();
64             app.setContext(context);
65             PatternLayoutEncoder encoder = new PatternLayoutEncoder();
66             encoder.setContext(context);
67             encoder.setPattern(appender.getEncoderPattern());
68             encoder.start();
69             app.setEncoder(encoder);
70             ThresholdFilter filter = new ThresholdFilter();
71             filter.setContext(context);
72             filter.setLevel(appender.getThresholdFilter());
73             filter.start();
74             app.getCopyOfAttachedFiltersList().add(filter);
75             app.setName(appender.getName());
76             app.start();
77             appendersMap.put(app.getName(), app);
78         }
79         return appendersMap;
80     }
81
82     private void createLoggers(LoggerContext context, LogbackModule module,
83             Set<ch.qos.logback.classic.Logger> loggersBefore) {
84
85         Map<String, Appender<ILoggingEvent>> appendersMap = getAppenders(module, context);
86
87         for (LoggerTO logger : module.getLoggerTO()) {
88             classLogger.trace("Setting configuration for logger {}", logger.getLoggerName());
89             final ch.qos.logback.classic.Logger logbackLogger = context.getLogger(logger.getLoggerName());
90
91             Optional<Set<Appender<ILoggingEvent>>> appendersBefore = getAppendersBefore(loggersBefore, logbackLogger);
92             classLogger.trace("Logger {}: Appenders registered before: {}", logger.getLoggerName(),
93                     appendersBefore.isPresent() ? appendersBefore.get() : "NO APPENDERS BEFORE");
94
95             logbackLogger.setLevel(Level.toLevel(logger.getLevel()));
96
97             addNewAppenders(appendersMap, logger, logbackLogger, appendersBefore);
98             removeBeforeAppenders(loggersBefore, logger, logbackLogger, appendersBefore);
99         }
100     }
101
102     private void addNewAppenders(Map<String, Appender<ILoggingEvent>> appendersMap, LoggerTO logger,
103             ch.qos.logback.classic.Logger logbackLogger, Optional<Set<Appender<ILoggingEvent>>> appendersBefore) {
104         for (String appenderName : logger.getAppenders()) {
105             if (appendersMap.containsKey(appenderName)) {
106                 logbackLogger.addAppender(appendersMap.get(appenderName));
107                 classLogger.trace("Logger {}: Adding new appender: {}", logger.getLoggerName(), appenderName);
108             } else {
109                 throw new IllegalStateException("No appender " + appenderName
110                         + " found. This error should have been discovered by validation");
111             }
112         }
113     }
114
115     private void removeBeforeAppenders(Set<ch.qos.logback.classic.Logger> loggersBefore, LoggerTO logger,
116             ch.qos.logback.classic.Logger logbackLogger, Optional<Set<Appender<ILoggingEvent>>> appendersBefore) {
117         if (appendersBefore.isPresent()) {
118             for (Appender<ILoggingEvent> appenderBefore : appendersBefore.get()) {
119                 logbackLogger.detachAppender(appenderBefore);
120                 appenderBefore.stop();
121                 classLogger.trace("Logger {}: Removing old appender: {}", logger.getLoggerName(),
122                         appenderBefore.getName());
123             }
124             loggersBefore.remove(logbackLogger);
125         }
126     }
127
128     private Optional<Set<Appender<ILoggingEvent>>> getAppendersBefore(Set<ch.qos.logback.classic.Logger> loggersBefore,
129             ch.qos.logback.classic.Logger logbackLogger) {
130         if (loggersBefore.contains(logbackLogger)) {
131             Iterator<Appender<ILoggingEvent>> appenderIt = logbackLogger.iteratorForAppenders();
132             Set<Appender<ILoggingEvent>> appendersBefore = Sets.newHashSet();
133             while (appenderIt.hasNext()) {
134                 appendersBefore.add(appenderIt.next());
135             }
136             return Optional.of(appendersBefore);
137         } else
138             return Optional.absent();
139
140     }
141
142     private Map<String, Appender<ILoggingEvent>> getAppenders(LogbackModule module, LoggerContext context) {
143         Map<String, Appender<ILoggingEvent>> appendersMap = new HashMap<>();
144         addAllAppenders(appendersMap, createRollingAppenders(context, module));
145         addAllAppenders(appendersMap, createFileAppenders(context, module));
146         addAllAppenders(appendersMap, createConsoleAppenders(context, module));
147
148         return appendersMap;
149     }
150
151     private void addAllAppenders(Map<String, Appender<ILoggingEvent>> allAppenders,
152             Map<String, Appender<ILoggingEvent>> appendersToAdd) {
153         for (String appenderName : appendersToAdd.keySet()) {
154             Preconditions.checkState(allAppenders.containsKey(appenderName) == false, "Duplicate appender name %s",
155                     appenderName);
156             allAppenders.put(appenderName, appendersToAdd.get(appenderName));
157         }
158     }
159
160     private Map<String, Appender<ILoggingEvent>> createFileAppenders(LoggerContext context, LogbackModule module) {
161         Map<String, Appender<ILoggingEvent>> appendersMap = new HashMap<>();
162         for (FileAppenderTO appender : module.getFileAppenderTO()) {
163             Preconditions.checkState(appendersMap.containsKey(appender.getName()) == false,
164                     "Duplicate appender name %s", appender.getName());
165             ch.qos.logback.core.FileAppender app = new ch.qos.logback.core.FileAppender<>();
166             app.setAppend(appender.getAppend());
167             app.setContext(context);
168             PatternLayoutEncoder encoder = new PatternLayoutEncoder();
169             encoder.setContext(context);
170             encoder.setPattern(appender.getEncoderPattern());
171             encoder.start();
172             app.setEncoder(encoder);
173             app.setFile(appender.getFileName());
174             app.setName(appender.getName());
175             app.start();
176             appendersMap.put(app.getName(), app);
177         }
178
179         return appendersMap;
180     }
181
182     private Map<String, Appender<ILoggingEvent>> createRollingAppenders(LoggerContext context, LogbackModule module) {
183         Map<String, Appender<ILoggingEvent>> appendersMap = new HashMap<>();
184         for (RollingFileAppenderTO appender : module.getRollingFileAppenderTO()) {
185             Preconditions.checkState(appendersMap.containsKey(appender.getName()) == false,
186                     "Duplicate appender name %s", appender.getName());
187             ch.qos.logback.core.rolling.RollingFileAppender app = new ch.qos.logback.core.rolling.RollingFileAppender<>();
188             app.setAppend(appender.getAppend());
189             app.setContext(context);
190             PatternLayoutEncoder encoder = new PatternLayoutEncoder();
191             encoder.setContext(context);
192             encoder.setPattern(appender.getEncoderPattern());
193             encoder.start();
194             app.setEncoder(encoder);
195             app.setFile(appender.getFileName());
196             if (appender.getRollingPolicyType().equals("FixedWindowRollingPolicy")) {
197                 FixedWindowRollingPolicy policy = new FixedWindowRollingPolicy();
198                 policy.setContext(context);
199                 policy.setMaxIndex(appender.getMaxIndex());
200                 policy.setMinIndex(appender.getMinIndex());
201                 policy.setFileNamePattern(appender.getFileNamePattern());
202                 policy.setParent(app);
203                 policy.start();
204                 app.setRollingPolicy(policy);
205             } else if (appender.getRollingPolicyType().equals("TimeBasedRollingPolicy")) {
206                 TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
207                 policy.setContext(context);
208                 policy.setMaxHistory(appender.getMaxHistory());
209                 if (appender.getCleanHistoryOnStart() != null) {
210                     policy.setCleanHistoryOnStart(appender.getCleanHistoryOnStart());
211                 }
212                 policy.setFileNamePattern(appender.getFileNamePattern());
213                 policy.setParent(app);
214                 policy.start();
215                 app.setRollingPolicy(policy);
216             }
217             SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
218             triggeringPolicy.setContext(context);
219             triggeringPolicy.setMaxFileSize(appender.getMaxFileSize());
220             triggeringPolicy.start();
221             app.setTriggeringPolicy(triggeringPolicy);
222             app.setName(appender.getName());
223             app.start();
224             appendersMap.put(app.getName(), app);
225         }
226         return appendersMap;
227     }
228
229     @Override
230     public void close() throws IOException {
231         statusListener.close();
232     }
233 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.