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 / LogbackModuleFactory.java
1 /**
2  * Generated file
3
4  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
5  * Generated at: Wed Jul 17 15:26:45 CEST 2013
6  *
7  * Do not modifiy this file unless it is present under src/main directory
8  */
9 package org.opendaylight.controller.config.yang.logback.config;
10
11 import ch.qos.logback.classic.Logger;
12 import ch.qos.logback.classic.LoggerContext;
13 import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
14 import ch.qos.logback.classic.spi.ILoggingEvent;
15 import ch.qos.logback.classic.spi.LoggerComparator;
16 import ch.qos.logback.core.Appender;
17 import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
18 import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
19 import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.Lists;
22 import org.apache.commons.lang3.StringUtils;
23 import org.opendaylight.controller.config.api.DependencyResolver;
24 import org.opendaylight.controller.config.api.ModuleIdentifier;
25 import org.osgi.framework.BundleContext;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35
36 /**
37 *
38 */
39 public class LogbackModuleFactory extends
40         org.opendaylight.controller.config.yang.logback.config.AbstractLogbackModuleFactory {
41
42     private static final String INSTANCE_NAME = "singleton";
43     private Map<String, LoggerTO> loggersDTOs;
44     private Map<String, RollingFileAppenderTO> rollingDTOs;
45     private Map<String, ConsoleAppenderTO> consoleDTOs;
46     private Map<String, FileAppenderTO> fileDTOs;
47
48     @Override
49     public LogbackModule instantiateModule(String instanceName,
50             DependencyResolver dependencyResolver, BundleContext bundleContext) {
51         Preconditions.checkArgument(instanceName.equals(INSTANCE_NAME),
52                 "There should be just one instance of logback, named " + INSTANCE_NAME);
53         prepareDTOs();
54         LogbackModule module = new LogbackModule(new ModuleIdentifier(getImplementationName(), INSTANCE_NAME),
55                 dependencyResolver);
56         module.setFileAppenderTO(Lists.newArrayList(fileDTOs.values()));
57         module.setConsoleAppenderTO(Lists.newArrayList(consoleDTOs.values()));
58         module.setRollingFileAppenderTO(Lists.newArrayList(rollingDTOs.values()));
59         module.setLoggerTO(Lists.newArrayList(loggersDTOs.values()));
60         return module;
61     }
62
63     @Override
64     public LogbackModule instantiateModule(String instanceName,
65             DependencyResolver dependencyResolver, LogbackModule oldModule,
66             AutoCloseable oldInstance, BundleContext bundleContext) {
67         Preconditions.checkArgument(instanceName.equals(INSTANCE_NAME),
68                 "There should be just one instance of logback, named " + INSTANCE_NAME);
69         prepareDTOs();
70         LogbackModule module = new LogbackModule(new ModuleIdentifier(getImplementationName(), INSTANCE_NAME),
71                 dependencyResolver, oldModule, oldInstance);
72         module.setConsoleAppenderTO(Lists.newArrayList(consoleDTOs.values()));
73         /*
74          * module.setJCloudsAppender(Lists.newArrayList(jcloudsDTOs.values()));
75          */
76         module.setFileAppenderTO(Lists.newArrayList(fileDTOs.values()));
77         module.setRollingFileAppenderTO(Lists.newArrayList(rollingDTOs.values()));
78         module.setLoggerTO(Lists.newArrayList(loggersDTOs.values()));
79         return module;
80     }
81
82     private void prepareDTOs() {
83         LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
84         this.loggersDTOs = prepareLoggersDTOs(context);
85         prepareAppendersDTOs(context);
86     }
87
88     private void prepareAppendersDTOs(LoggerContext context) {
89         this.rollingDTOs = new HashMap<>();
90         this.consoleDTOs = new HashMap<>();
91         this.fileDTOs = new HashMap<>();
92         ch.qos.logback.core.rolling.RollingFileAppender<ILoggingEvent> rollingApp;
93         ch.qos.logback.core.ConsoleAppender<ILoggingEvent> consoleApp;
94         ch.qos.logback.core.FileAppender<ILoggingEvent> fileApp;
95         Map<Logger, List<Appender<ILoggingEvent>>> appendersAll = new HashMap<>();
96         for (Logger log : context.getLoggerList()) {
97             List<Appender<ILoggingEvent>> appenders = new ArrayList<>();
98             Iterator<Appender<ILoggingEvent>> iter = log.iteratorForAppenders();
99             while (iter.hasNext()) {
100                 Appender<ILoggingEvent> element = iter.next();
101                 appenders.add(element);
102             }
103             appendersAll.put(log, appenders);
104         }
105         for (List<ch.qos.logback.core.Appender<ILoggingEvent>> appEntry : appendersAll.values()) {
106             for (ch.qos.logback.core.Appender<ILoggingEvent> appender : appEntry) {
107                 if (appender instanceof ch.qos.logback.core.rolling.RollingFileAppender<?>) {
108                     RollingFileAppenderTO app = new RollingFileAppenderTO();
109                     rollingApp = (ch.qos.logback.core.rolling.RollingFileAppender<ILoggingEvent>) appender;
110                     app.setAppend(rollingApp.isAppend());
111                     PatternLayoutEncoder encoder = (PatternLayoutEncoder) rollingApp.getEncoder();
112                     app.setEncoderPattern(encoder.getPattern());
113                     app.setFileName(rollingApp.getFile());
114                     if (rollingApp.getRollingPolicy() instanceof FixedWindowRollingPolicy) {
115                         FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) rollingApp
116                                 .getRollingPolicy();
117                         app.setMaxIndex(rollingPolicy.getMaxIndex());
118                         app.setMinIndex(rollingPolicy.getMinIndex());
119                         app.setFileNamePattern(rollingPolicy.getFileNamePattern());
120                         app.setRollingPolicyType("FixedWindowRollingPolicy");
121                     } else if (rollingApp.getRollingPolicy() instanceof TimeBasedRollingPolicy<?>) {
122                         TimeBasedRollingPolicy rollingPolicy = (TimeBasedRollingPolicy) rollingApp.getRollingPolicy();
123                         app.setRollingPolicyType("TimeBasedRollingPolicy");
124                         app.setFileNamePattern(rollingPolicy.getFileNamePattern());
125                         app.setMaxHistory(rollingPolicy.getMaxHistory());
126                         app.setCleanHistoryOnStart(rollingPolicy.isCleanHistoryOnStart());
127                     }
128                     SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = (SizeBasedTriggeringPolicy<ILoggingEvent>) rollingApp
129                             .getTriggeringPolicy();
130                     app.setMaxFileSize(triggeringPolicy.getMaxFileSize());
131                     app.setName(rollingApp.getName());
132                     this.rollingDTOs.put(rollingApp.getName(), app);
133                 } else if (appender instanceof ch.qos.logback.core.FileAppender<?>) {
134                     FileAppenderTO app = new FileAppenderTO();
135                     fileApp = (ch.qos.logback.core.FileAppender<ILoggingEvent>) appender;
136                     app.setName(fileApp.getName());
137                     app.setAppend(fileApp.isAppend());
138                     app.setFileName(fileApp.getFile());
139                     PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileApp.getEncoder();
140                     app.setEncoderPattern(encoder.getPattern());
141                     this.fileDTOs.put(fileApp.getName(), app);
142                 }
143                 if (appender instanceof ch.qos.logback.core.ConsoleAppender<?>) {
144                     ConsoleAppenderTO app = new ConsoleAppenderTO();
145                     consoleApp = (ch.qos.logback.core.ConsoleAppender<ILoggingEvent>) appender;
146                     consoleApp.getCopyOfAttachedFiltersList();
147                     PatternLayoutEncoder encoder = (PatternLayoutEncoder) consoleApp.getEncoder();
148                     app.setEncoderPattern(encoder.getPattern());
149                     app.setName(consoleApp.getName());
150                     app.setThresholdFilter(context.getLogger(Logger.ROOT_LOGGER_NAME).getEffectiveLevel().levelStr);
151                     this.consoleDTOs.put(consoleApp.getName(), app);
152                 }
153             }
154         }
155     }
156
157     private Map<String, LoggerTO> prepareLoggersDTOs(LoggerContext context) {
158         Map<String, LoggerTO> DTOs = new HashMap<>();
159         List<String> appenders = new ArrayList<>();
160         List<org.slf4j.Logger> loggersToBeAdd = removeUnusableLoggers(context.getLoggerList(),
161                 context.getLogger(Logger.ROOT_LOGGER_NAME));
162         for (org.slf4j.Logger log : loggersToBeAdd) {
163             LoggerTO logger = new LoggerTO();
164             if (((Logger) log).getLevel() != null)
165                 logger.setLevel(((Logger) log).getLevel().levelStr);
166             else
167                 logger.setLevel(((Logger) log).getEffectiveLevel().levelStr);
168             logger.setLoggerName(log.getName());
169             Iterator<Appender<ILoggingEvent>> iter = ((Logger) log).iteratorForAppenders();
170             while (iter.hasNext()) {
171                 Appender<ILoggingEvent> element = iter.next();
172                 appenders.add(element.getName());
173             }
174             logger.setAppenders(appenders);
175             DTOs.put(log.getName(), logger);
176             appenders = new ArrayList<>();
177
178         }
179         return DTOs;
180     }
181
182     private List<org.slf4j.Logger> removeUnusableLoggers(List<Logger> loggerList, Logger rootLogger) {
183         Collections.sort(loggerList, new LoggerComparator());
184         Map<String, org.slf4j.Logger> loggersToReturn = new HashMap<>();
185
186         for (org.slf4j.Logger log : loggerList) {
187             boolean shouldAdd = true;
188             for (Entry<String, org.slf4j.Logger> entry : loggersToReturn.entrySet()) {
189                 if (StringUtils.contains(log.getName(), entry.getKey())) {
190                     if (((Logger) log).getLevel() != null
191                             && ((Logger) log).getLevel().equals(((Logger) entry.getValue()).getLevel())
192                             && !((Logger) log).iteratorForAppenders().hasNext()) {
193                         shouldAdd = false;
194                         break;
195                     }
196                     if (((Logger) log).getLevel() == null
197                             && ((Logger) log).getEffectiveLevel().equals(
198                                     ((Logger) entry.getValue()).getEffectiveLevel())
199                             && !((Logger) log).iteratorForAppenders().hasNext()) {
200                         shouldAdd = false;
201                         break;
202                     }
203                 }
204                 if (((Logger) log).getLevel() != null && ((Logger) log).getLevel().equals(rootLogger.getLevel())
205                         && !((Logger) log).iteratorForAppenders().hasNext()) {
206                     shouldAdd = false;
207                     break;
208                 }
209                 if (((Logger) log).getLevel() == null
210                         && ((Logger) log).getEffectiveLevel().equals(rootLogger.getEffectiveLevel())
211                         && !((Logger) log).iteratorForAppenders().hasNext()) {
212                     shouldAdd = false;
213                     break;
214                 }
215             }
216             if (shouldAdd) {
217                 loggersToReturn.put(log.getName(), log);
218             }
219         }
220         return Lists.newArrayList(loggersToReturn.values());
221     }
222
223 }

©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.