Fix breakage introduced by logback bump
[controller.git] / opendaylight / config / logback-config / src / main / java / org / opendaylight / controller / config / yang / logback / config / LogbackModuleFactory.java
index bd19d5e..b79e3d3 100644 (file)
@@ -25,9 +25,12 @@ import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
 import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
 import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
+import ch.qos.logback.core.util.FileSize;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -136,7 +139,7 @@ public class LogbackModuleFactory extends
                     }
                     SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = (SizeBasedTriggeringPolicy<ILoggingEvent>) rollingApp
                             .getTriggeringPolicy();
-                    app.setMaxFileSize(triggeringPolicy.getMaxFileSize());
+                    app.setMaxFileSize(getMaxFileSize(triggeringPolicy).toString());
                     app.setName(rollingApp.getName());
                     this.rollingDTOs.put(rollingApp.getName(), app);
                 } else if (appender instanceof ch.qos.logback.core.FileAppender<?>) {
@@ -240,4 +243,26 @@ public class LogbackModuleFactory extends
         return defaultModules;
     }
 
+    // Ugly hack to deal with logback changing its api
+    private static final Field MAX_FILE_SIZE_FIELD;
+    static {
+        Field f;
+        try {
+            f = SizeBasedTriggeringPolicy.class.getDeclaredField("maxFileSize");
+            f.setAccessible(true);
+        } catch (NoSuchFieldException | SecurityException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+
+        MAX_FILE_SIZE_FIELD = f;
+    }
+
+    @VisibleForTesting
+    static FileSize getMaxFileSize(SizeBasedTriggeringPolicy<?> policy) {
+        try {
+            return (FileSize) MAX_FILE_SIZE_FIELD.get(policy);
+        } catch (IllegalArgumentException | IllegalAccessException e) {
+            throw new IllegalStateException("Cannot get maxFileSize field", e);
+        }
+    }
 }