Coverage Report - org.mule.util.store.AbstractMonitoredObjectStore
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractMonitoredObjectStore
75%
27/36
50%
4/8
1.333
 
 1  
 /*
 2  
  * $Id: AbstractMonitoredObjectStore.java 12058 2008-06-16 12:02:56Z rossmason $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 package org.mule.util.store;
 11  
 
 12  
 import org.mule.api.MuleContext;
 13  
 import org.mule.api.store.ObjectStore;
 14  
 import org.mule.api.context.MuleContextAware;
 15  
 import org.mule.api.lifecycle.Initialisable;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.api.lifecycle.Disposable;
 18  
 import org.mule.config.i18n.CoreMessages;
 19  
 import org.mule.util.concurrent.DaemonThreadFactory;
 20  
 import org.mule.util.UUID;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
 25  
 import edu.emory.mathcs.backport.java.util.concurrent.ScheduledThreadPoolExecutor;
 26  
 
 27  
 /**
 28  
  * TODO
 29  
  */
 30  18
 public abstract class AbstractMonitoredObjectStore implements ObjectStore, Runnable, MuleContextAware, Initialisable, Disposable
 31  
 {
 32  18
     protected final Log logger = LogFactory.getLog(this.getClass());
 33  
 
 34  
     /**
 35  
      * Default constructor for IdempotentInMemoryMessageIdStore.
 36  
      *
 37  
      * @param name               a name for this store, can be used for logging and identification
 38  
      * purposes
 39  
      * @param maxEntries         the maximum number of entries that this store keeps around.
 40  
      * Specify <em>-1</em> if the store is supposed to be "unbounded".
 41  
      * @param entryTTL           the time-to-live for each message ID, specified in seconds, or
 42  
      * <em>-1</em> for entries that should never expire. <b>DO NOT</b>
 43  
      * combine this with an unbounded store!
 44  
      * @param expirationInterval the interval for periodic bounded size enforcement and
 45  
      * entry expiration, specified in seconds. Arbitrary positive values
 46  
      * between 1 second and several hours or days are possible, but should be
 47  
      * chosen carefully according to the expected message rate to prevent
 48  
      * OutOfMemory conditions.
 49  
      */
 50  
     protected ScheduledThreadPoolExecutor scheduler;
 51  18
     protected int maxEntries = 4000;
 52  18
     protected int entryTTL = -1;
 53  18
     protected int expirationInterval = 1000;
 54  
     protected String name;
 55  
     protected MuleContext context;
 56  
 
 57  
 
 58  
     public void initialise() throws InitialisationException
 59  
     {
 60  18
         if (name == null)
 61  
         {
 62  0
             name = UUID.getUUID();
 63  
             //throw new IllegalArgumentException(CoreMessages.propertyHasInvalidValue("storeId", "null").getMessage());
 64  
         }
 65  
 
 66  18
         if (expirationInterval <= 0)
 67  
         {
 68  0
             throw new IllegalArgumentException(CoreMessages.propertyHasInvalidValue("expirationInterval",
 69  
                     new Integer(expirationInterval)).toString());
 70  
         }
 71  
 
 72  18
         if (scheduler == null)
 73  
         {
 74  18
             this.scheduler = new ScheduledThreadPoolExecutor(1);
 75  18
             scheduler.setThreadFactory(new DaemonThreadFactory(name + "-Monitor"));
 76  18
             scheduler.scheduleWithFixedDelay(this, 0, expirationInterval, TimeUnit.MILLISECONDS);
 77  
         }
 78  18
     }
 79  
 
 80  
     public final void run()
 81  
     {
 82  89
         expire();
 83  89
     }
 84  
 
 85  
     /**
 86  
      * A lifecycle method where implementor should free up any resources. If an
 87  
      * exception is thrown it should just be logged and processing should continue.
 88  
      * This method should not throw Runtime exceptions.
 89  
      */
 90  
     public void dispose()
 91  
     {
 92  12
         if(scheduler!=null)
 93  
         {
 94  12
             scheduler.shutdown();
 95  
         }
 96  12
     }
 97  
 
 98  
     public void setEntryTTL(int entryTTL)
 99  
     {
 100  18
         this.entryTTL = entryTTL;
 101  18
     }
 102  
 
 103  
     public void setExpirationInterval(int expirationInterval)
 104  
     {
 105  18
         this.expirationInterval = expirationInterval;
 106  18
     }
 107  
 
 108  
     public void setMaxEntries(int maxEntries)
 109  
     {
 110  18
         this.maxEntries = maxEntries;
 111  18
     }
 112  
 
 113  
     public void setScheduler(ScheduledThreadPoolExecutor scheduler)
 114  
     {
 115  0
         this.scheduler = scheduler;
 116  0
     }
 117  
 
 118  
     public void setName(String id)
 119  
     {
 120  18
         this.name = id;
 121  18
     }
 122  
 
 123  
     public void setMuleContext(MuleContext context)
 124  
     {
 125  12
         this.context = context;
 126  12
     }
 127  
 
 128  
     public int getEntryTTL()
 129  
     {
 130  0
         return entryTTL;
 131  
     }
 132  
 
 133  
     public int getExpirationInterval()
 134  
     {
 135  0
         return expirationInterval;
 136  
     }
 137  
 
 138  
     public int getMaxEntries()
 139  
     {
 140  0
         return maxEntries;
 141  
     }
 142  
 
 143  
     public String getName()
 144  
     {
 145  0
         return name;
 146  
     }
 147  
 
 148  
     public ScheduledThreadPoolExecutor getScheduler()
 149  
     {
 150  0
         return scheduler;
 151  
     }
 152  
 
 153  
     protected abstract void expire();
 154  
 
 155  
 }