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

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