Coverage Report - net.admin4j.exception.ExceptionTrackerCleanupTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionTrackerCleanupTask
75%
9/12
62%
5/8
2.5
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.exception;
 15  
 
 16  
 import java.util.Date;
 17  
 import java.util.Set;
 18  
 import java.util.concurrent.atomic.AtomicInteger;
 19  
 
 20  
 import net.admin4j.config.Admin4JConfiguration;
 21  
 import net.admin4j.dao.DAOFactory;
 22  
 import net.admin4j.deps.commons.lang3.Validate;
 23  
 import net.admin4j.deps.commons.lang3.time.DateUtils;
 24  
 import net.admin4j.entity.ExceptionInfo;
 25  
 
 26  
 /**
 27  
  * Will clean up dated entries in the Exception Tracker so memory usage is contained (e.g. doesn't leak').
 28  
  * @author D. Ashmore
 29  
  * @since 1.0
 30  
  */
 31  15
 class ExceptionTrackerCleanupTask implements Runnable {
 32  
     
 33  
     static final int DEFAULT_PURGE_THRESHOLD_IN_DAYS = 30;  
 34  15
     private AtomicInteger purgeThresholdInDays = new AtomicInteger(DEFAULT_PURGE_THRESHOLD_IN_DAYS);
 35  
 
 36  
     /* (non-Javadoc)
 37  
      * @see java.lang.Thread#run()
 38  
      */
 39  
     public void run() {
 40  
 
 41  
         Date purgeDt = DateUtils.addDays(new Date(), -1 * purgeThresholdInDays.get());
 42  
         
 43  3
         Set<ExceptionInfo> exceptionSet = ExceptionTracker.getExceptionInfoSet();
 44  3
         for (ExceptionInfo eInfo: exceptionSet) {
 45  6
             if (purgeDt.getTime() > eInfo.getLastOccuranceDt().getTime()) {
 46  0
                 ExceptionTracker.purgeException(eInfo);
 47  
             }
 48  
         }
 49  
         
 50  3
         if (Admin4JConfiguration.isExceptionInfoStored()) {
 51  0
             DAOFactory.getExceptionInfoDAO().saveAll(exceptionSet);
 52  
         }
 53  
  
 54  3
     }
 55  
     
 56  
     public void setPurgeThresholdInDays(Integer days) {
 57  
         Validate.notNull(days, "Purge threshhold cannot be null");
 58  0
         Validate.isTrue(days > 0, "Purge threshhold must be larger than zero");
 59  3
         purgeThresholdInDays.set(days);
 60  3
     }
 61  
     
 62  
 }