Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.agilecoders.wicket.logging.util;
  
  
An ILoggingBarrier checks whether an logging event is allowed to be written to log or not.

Author(s):
miha
 
 public interface ILoggingBarrier {

    
checks a single log event

Parameters:
logObject the log event to check
Returns:
true, if it's allowed to be logged
 
     boolean isAllowed(ClientSideLogObject logObject);

    
checks multiple log events. Please don't use this to iterate over all log events, for this usecase you can use isAllowed(de.agilecoders.wicket.logging.ClientSideLogObject), which is called for each single event.

Parameters:
logObjects the log events to check
Returns:
true, if all loge events are allowed to be logged
 
     boolean isAllowed(Collection<ClientSideLogObjectlogObjects);

    
Default implementation that allows everything.
 
     public static final class AllowAllBarrier implements ILoggingBarrier {
 
         @Override
         public boolean isAllowed(ClientSideLogObject logObject) {
             return true;
         }
 
         @Override
         public boolean isAllowed(Collection<ClientSideLogObjectlogObjects) {
             return true;
         }
     }

    
A special barrier that allows a maximum number of events in a time frame.
 
     public static final class SizeAndTimeFrameBasedBarrier implements ILoggingBarrier {
 
         private final ScheduledExecutorService executor;
         private final AtomicLong counter;
         private final long maxSize;

        
Construct.

Parameters:
maxSize the max number of messages that can be sent during time frame.
period defines the time frame period
timeUnit defines the time frame time unit
 
         public SizeAndTimeFrameBasedBarrier(long maxSizelong periodTimeUnit timeUnit) {
             this. = maxSize;
             this. = new AtomicLong(maxSize);
 
             this. = newScheduledExecutorService();
             this..scheduleAtFixedRate(newOnTimerEventHandler(), periodperiodtimeUnit);
         }

        

Returns:
new timer event handler, default implementation delegates work to onTimerEvent()
 
         protected Runnable newOnTimerEventHandler() {
             return new Runnable() {
                 @Override
                 public void run() {
                     SizeAndTimeFrameBasedBarrier.this.onTimerEvent();
                 }
             };
         }

        
NOTICE: If you override this method, please implement shutdownScheduledExecutorService() too.

Returns:
new scheduled executor service
 
         protected ScheduledExecutorService newScheduledExecutorService() {
             return Executors.newScheduledThreadPool(1);
         }

        
shutdown executor service

Throws:
java.lang.Throwable if executor can't be stopped
        protected void shutdownScheduledExecutorService() throws Throwable {
            this..shutdownNow();
            this..awaitTermination(1, .);
        }

        
this method is triggered after some period of time. The default implementation resets the counter.
        protected void onTimerEvent() {
            .set();
        }
        @Override
        public boolean isAllowed(ClientSideLogObject logObject) {
            return .decrementAndGet() > 0;
        }
        @Override
        public boolean isAllowed(Collection<ClientSideLogObjectlogObjects) {
            return .get() - logObjects.size() > 0;
        }
        @Override
        protected void finalize() throws Throwable {
            try {
                if (!.isShutdown()) {
                    shutdownScheduledExecutorService();
                }
            } catch (Throwable e) {
                // ignore
            } finally {
                super.finalize();
            }
        }
    }
New to GrepCode? Check out our FAQ X