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