Browse Source

minor refactoring

jrivard@gmail.com 6 years ago
parent
commit
5b2487455e

+ 15 - 14
server/src/main/java/password/pwm/util/logging/LocalDBLogger.java

@@ -43,6 +43,7 @@ import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 import java.util.Queue;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.Executors;
@@ -75,17 +76,22 @@ public class LocalDBLogger implements PwmService
 
     private static final String STORAGE_FORMAT_VERSION = "3";
 
-    public LocalDBLogger( final PwmApplication pwmApplication, final LocalDB localDB, final LocalDBLoggerSettings settings )
+    public LocalDBLogger(
+            final PwmApplication pwmApplication,
+            final LocalDB localDB,
+            final LocalDBLoggerSettings settings
+    )
             throws LocalDBException
     {
-        if ( localDB == null )
-        {
-            throw new NullPointerException( "localDB can not be null" );
-        }
+        Objects.requireNonNull( localDB, "localDB can not be null" );
 
         final Instant startTime = Instant.now();
         status = STATUS.OPENING;
-        this.settings = settings;
+
+        this.settings = settings == null
+                ? LocalDBLoggerSettings.builder().build().applyValueChecks()
+                : settings.applyValueChecks();
+
         this.localDB = localDB;
         this.localDBListQueue = LocalDBStoredQueue.createLocalDBStoredQueue(
                 pwmApplication,
@@ -93,19 +99,14 @@ public class LocalDBLogger implements PwmService
                 LocalDB.DB.EVENTLOG_EVENTS
         );
 
-        if ( settings.getMaxEvents() == 0 )
+        if ( this.settings.getMaxEvents() == 0 )
         {
             LOGGER.info( () -> "maxEvents set to zero, clearing LocalDBLogger history and LocalDBLogger will remain closed" );
             localDBListQueue.clear();
             throw new IllegalArgumentException( "maxEvents=0, will remain closed" );
         }
 
-        if ( localDB == null )
-        {
-            throw new IllegalArgumentException( "LocalDB is not available" );
-        }
-
-        eventQueue = new ArrayBlockingQueue<>( settings.getMaxBufferSize(), true );
+        eventQueue = new ArrayBlockingQueue<>( this.settings.getMaxBufferSize(), true );
 
         if ( pwmApplication != null )
         {
@@ -137,7 +138,7 @@ public class LocalDBLogger implements PwmService
         cleanerService.scheduleAtFixedRate( new CleanupTask(), 0, 1, TimeUnit.MINUTES );
         writerService.scheduleWithFixedDelay( new FlushTask(), 0, 103, TimeUnit.MILLISECONDS );
 
-        cleanOnWriteFlag.set( eventQueue.size() >= settings.getMaxEvents() );
+        cleanOnWriteFlag.set( eventQueue.size() >= this.settings.getMaxEvents() );
 
         final TimeDuration timeDuration = TimeDuration.fromCurrent( startTime );
         LOGGER.info( () -> "open in " + timeDuration.asCompactString() + ", " + debugStats() );

+ 34 - 105
server/src/main/java/password/pwm/util/logging/LocalDBLoggerSettings.java

@@ -22,6 +22,8 @@
 
 package password.pwm.util.logging;
 
+import lombok.Builder;
+import lombok.Data;
 import password.pwm.AppProperty;
 import password.pwm.config.Configuration;
 import password.pwm.config.PwmSetting;
@@ -32,68 +34,46 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
+@Data
+@Builder( toBuilder = true )
 public class LocalDBLoggerSettings implements Serializable
 {
+    @Builder.Default
     static final int MINIMUM_MAXIMUM_EVENTS = 100;
+
+    @Builder.Default
     static final TimeDuration MINIMUM_MAX_AGE = TimeDuration.HOUR;
 
-    private final int maxEvents;
-    private final TimeDuration maxAge;
-    private final Set<Flag> flags;
-    private final int maxBufferSize;
-    private final TimeDuration maxBufferWaitTime;
-    private final int maxTrimSize;
+    @Builder.Default
+    private int maxEvents = 1000 * 1000;
 
-    public enum Flag
-    {
-        DevDebug,
-    }
+    @Builder.Default
+    private TimeDuration maxAge = TimeDuration.of( 7, TimeDuration.Unit.DAYS );
 
-    private LocalDBLoggerSettings(
-            final int maxEvents,
-            final TimeDuration maxAge,
-            final Set<Flag> flags,
-            final int maxBufferSize,
-            final TimeDuration maxBufferWaitTime,
-            final int maxTrimSize
-    )
-    {
-        this.maxEvents = maxEvents < 1 ? 0 : Math.max( MINIMUM_MAXIMUM_EVENTS, maxEvents );
-        this.maxAge = maxAge == null || maxAge.isShorterThan( MINIMUM_MAX_AGE ) ? MINIMUM_MAX_AGE : maxAge;
-        this.flags = flags == null ? Collections.<Flag>emptySet() : Collections.unmodifiableSet( flags );
-        this.maxBufferSize = maxBufferSize;
-        this.maxBufferWaitTime = maxBufferWaitTime;
-        this.maxTrimSize = maxTrimSize;
-    }
+    @Builder.Default
+    private Set<Flag> flags = Collections.emptySet();
 
-    public int getMaxEvents( )
-    {
-        return maxEvents;
-    }
+    @Builder.Default
+    private int maxBufferSize = 1000;
 
-    public TimeDuration getMaxBufferWaitTime( )
-    {
-        return maxBufferWaitTime;
-    }
+    @Builder.Default
+    private TimeDuration maxBufferWaitTime = TimeDuration.of( 1, TimeDuration.Unit.MINUTES );
 
-    public TimeDuration getMaxAge( )
-    {
-        return maxAge;
-    }
+    @Builder.Default
+    private int maxTrimSize = 501;
 
-    public Set<Flag> getFlags( )
-    {
-        return flags;
-    }
 
-    public int getMaxBufferSize( )
+    public enum Flag
     {
-        return maxBufferSize;
+        DevDebug,
     }
 
-    public int getMaxTrimSize( )
+    LocalDBLoggerSettings applyValueChecks()
     {
-        return maxTrimSize;
+        return toBuilder()
+                .maxEvents( maxEvents < 1 ? 0 : Math.max( MINIMUM_MAXIMUM_EVENTS, maxEvents ) )
+                .maxAge( maxAge == null || maxAge.isShorterThan( MINIMUM_MAX_AGE ) ? MINIMUM_MAX_AGE : maxAge )
+                .build();
     }
 
     public static LocalDBLoggerSettings fromConfiguration( final Configuration configuration )
@@ -113,64 +93,13 @@ public class LocalDBLoggerSettings implements Serializable
         );
         final int maxTrimSize = Integer.parseInt( configuration.readAppProperty( AppProperty.LOCALDB_LOGWRITER_MAX_TRIM_SIZE ) );
 
-        return new Builder()
-                .setMaxEvents( maxEvents )
-                .setMaxAge( maxAge )
-                .setFlags( flags )
-                .setMaxBufferSize( maxBufferSize )
-                .setMaxBufferWaitTime( maxBufferWaitTime )
-                .setMaxTrimSize( maxTrimSize )
-                .createLocalDBLoggerSettings();
-    }
-
-    public static class Builder
-    {
-        private int maxEvents = 1 * 1000 * 1000;
-        private TimeDuration maxAge = TimeDuration.of( 7, TimeDuration.Unit.DAYS );
-        private Set<Flag> flags = Collections.emptySet();
-        private int maxBufferSize = 1000;
-        private TimeDuration maxBufferWaitTime = TimeDuration.of( 1, TimeDuration.Unit.MINUTES );
-        private int maxTrimSize = 501;
-
-        public Builder setMaxEvents( final int maxEvents )
-        {
-            this.maxEvents = maxEvents;
-            return this;
-        }
-
-        public Builder setMaxAge( final TimeDuration maxAge )
-        {
-            this.maxAge = maxAge;
-            return this;
-        }
-
-        public Builder setFlags( final Set<Flag> flags )
-        {
-            this.flags = flags;
-            return this;
-        }
-
-        public Builder setMaxTrimSize( final int maxTrimSize )
-        {
-            this.maxTrimSize = maxTrimSize;
-            return this;
-        }
-
-        public Builder setMaxBufferSize( final int maxBufferSize )
-        {
-            this.maxBufferSize = maxBufferSize;
-            return this;
-        }
-
-        public Builder setMaxBufferWaitTime( final TimeDuration maxBufferWaitTime )
-        {
-            this.maxBufferWaitTime = maxBufferWaitTime;
-            return this;
-        }
-
-        public LocalDBLoggerSettings createLocalDBLoggerSettings( )
-        {
-            return new LocalDBLoggerSettings( maxEvents, maxAge, flags, maxBufferSize, maxBufferWaitTime, maxTrimSize );
-        }
+        return LocalDBLoggerSettings.builder()
+                .maxEvents( maxEvents )
+                .maxAge( maxAge )
+                .flags( flags )
+                .maxBufferSize( maxBufferSize )
+                .maxBufferWaitTime( maxBufferWaitTime )
+                .maxTrimSize( maxTrimSize )
+                .build().applyValueChecks();
     }
 }

+ 5 - 5
server/src/test/java/password/pwm/util/localdb/LocalDBLoggerExtendedTest.java

@@ -98,11 +98,11 @@ public class LocalDBLoggerExtendedTest
 
         // open localDBLogger based on configuration settings;
         {
-            final LocalDBLoggerSettings settings = new LocalDBLoggerSettings.Builder()
-                    .setMaxEvents( Integer.MAX_VALUE )
-                    .setMaxAge( TimeDuration.of( 1, TimeDuration.Unit.MINUTES ) )
-                    .setFlags( Collections.emptySet() )
-                    .createLocalDBLoggerSettings();
+            final LocalDBLoggerSettings settings = LocalDBLoggerSettings.builder()
+                    .maxEvents( Integer.MAX_VALUE )
+                    .maxAge( TimeDuration.of( 1, TimeDuration.Unit.MINUTES ) )
+                    .flags( Collections.emptySet() )
+                    .build();
             localDBLogger = new LocalDBLogger( null, localDB, settings );
         }