Coverage Report - org.mule.util.store.TextFileObjectStore
 
Classes in this File Line Coverage Branch Coverage Complexity
TextFileObjectStore
82%
42/51
88%
14/16
2.5
 
 1  
 /*
 2  
  * $Id: TextFileObjectStore.java 12269 2008-07-10 04:19:03Z dfeist $
 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.lifecycle.InitialisationException;
 13  
 import org.mule.util.FileUtils;
 14  
 import org.mule.util.IOUtils;
 15  
 import org.mule.util.StringUtils;
 16  
 
 17  
 import java.io.File;
 18  
 import java.io.FileInputStream;
 19  
 import java.io.FileNotFoundException;
 20  
 import java.io.FileOutputStream;
 21  
 import java.util.Iterator;
 22  
 import java.util.Map;
 23  
 import java.util.Properties;
 24  
 
 25  
 /**
 26  
  * A Simple object store that stores String objects by key to a text file. This store is only suitable for storing
 27  
  * simple key value pair strings.
 28  
  *
 29  
  * This store is backed by an in-memory store and supports the ability to expire and apply TTL to objects in the store.
 30  
  */
 31  12
 public class TextFileObjectStore extends InMemoryObjectStore
 32  
 {
 33  
 
 34  
     protected File fileStore;
 35  
     protected String directory;
 36  
     protected String encoding;
 37  
 
 38  
     private FileOutputStream output;
 39  
 
 40  
 
 41  
     public void initialise() throws InitialisationException
 42  
     {
 43  12
         super.initialise();
 44  12
         if (encoding == null)
 45  
         {
 46  12
             encoding = context.getConfiguration().getDefaultEncoding();
 47  
         }
 48  
 
 49  12
         if (directory == null)
 50  
         {
 51  0
             directory = context.getConfiguration().getWorkingDirectory() + "/objectstore";
 52  
         }
 53  
 
 54  
         try
 55  
         {
 56  12
             File dir = FileUtils.openDirectory(directory);
 57  12
             fileStore = new File(dir, name + ".dat");
 58  12
             if (fileStore.exists())
 59  
             {
 60  4
                 loadFromStore();
 61  
             }
 62  
         }
 63  0
         catch (Exception e)
 64  
         {
 65  0
             throw new InitialisationException(e, this);
 66  12
         }
 67  
 
 68  12
     }
 69  
 
 70  
     protected synchronized void loadFromStore() throws Exception
 71  
     {
 72  4
         Properties props = new Properties();
 73  4
         props.load(new FileInputStream(fileStore));
 74  4
         for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
 75  
         {
 76  6
             Map.Entry entry = (Map.Entry) iterator.next();
 77  6
             super.storeObject(entry.getKey().toString(), entry.getValue());
 78  6
         }
 79  4
     }
 80  
 
 81  
     /**
 82  
      * Store the given Object.
 83  
      *
 84  
      * @param id the ID to store
 85  
      * @return <code>true</code> if the ID was stored properly, or <code>false</code>
 86  
      *         if it already existed
 87  
      * @throws IllegalArgumentException if the given ID cannot be stored or is
 88  
      *                                  <code>null</code>
 89  
      * @throws Exception                if the store is not available or any other
 90  
      *                                  implementation-specific error occured
 91  
      */
 92  
     //@Override
 93  
     public boolean storeObject(String id, Object item) throws Exception
 94  
     {
 95  32
         if (!(item instanceof String))
 96  
         {
 97  2
             throw new IllegalArgumentException("TextFile store can only be used for storing text entries");
 98  
         }
 99  30
         boolean result = super.storeObject(id, item);
 100  30
         if (output == null)
 101  
         {
 102  6
             output = new FileOutputStream(fileStore, true);
 103  
         }
 104  30
         StringBuffer buf = new StringBuffer();
 105  30
         buf.append(id).append("=").append(item.toString()).append(IOUtils.LINE_SEPARATOR);
 106  30
         output.write(buf.toString().getBytes());
 107  30
         return result;
 108  
     }
 109  
 
 110  
     public String getDirectory()
 111  
     {
 112  0
         return directory;
 113  
     }
 114  
 
 115  
     public void setDirectory(String directory)
 116  
     {
 117  12
         this.directory = directory;
 118  12
     }
 119  
 
 120  
     public String getEncoding()
 121  
     {
 122  0
         return encoding;
 123  
     }
 124  
 
 125  
     public void setEncoding(String encoding)
 126  
     {
 127  0
         this.encoding = encoding;
 128  0
     }
 129  
 
 130  
     public synchronized void dispose()
 131  
     {
 132  12
         Properties props = new Properties();
 133  
 
 134  12
         for (Iterator iterator = super.store.values().iterator(); iterator.hasNext();)
 135  
         {
 136  12
             StoredObject storedObject = (StoredObject) iterator.next();
 137  12
             props.put(storedObject.getId(), storedObject.getItem());
 138  12
         }
 139  
 
 140  12
         if (output == null)
 141  
         {
 142  
             try
 143  
             {
 144  6
                 output = new FileOutputStream(fileStore, false);
 145  6
                 props.save(output, StringUtils.EMPTY);
 146  6
                 IOUtils.closeQuietly(output);
 147  
             }
 148  0
             catch (FileNotFoundException e)
 149  
             {
 150  0
                 logger.error(e.getMessage(), e);
 151  6
             }
 152  
         }
 153  
         else 
 154  
         {
 155  6
             IOUtils.closeQuietly(output);
 156  
         }
 157  
         
 158  
         
 159  12
         super.dispose();
 160  12
     }
 161  
 }