View Javadoc

1   /*
2    * $Id: TextFileObjectStore.java 21762 2011-05-03 01:29:28Z mike.schilling $
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
29   * is only suitable for storing simple key value pair strings. This store is backed
30   * by an in-memory store and supports the ability to expire and apply TTL to objects
31   * in the store.
32   */
33  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  
42      /**
43       * {@inheritDoc}
44       */
45      public boolean isPersistent()
46      {
47          return true;
48      }
49  
50      @Override
51      public void initialise() throws InitialisationException
52      {
53          super.initialise();
54          if (encoding == null)
55          {
56              encoding = context.getConfiguration().getDefaultEncoding();
57          }
58  
59          if (directory == null)
60          {
61              directory = context.getConfiguration().getWorkingDirectory() + "/objectstore";
62          }
63  
64          try
65          {
66              File dir = FileUtils.openDirectory(directory);
67              fileStore = new File(dir, name + ".dat");
68              if (fileStore.exists())
69              {
70                  loadFromStore();
71              }
72          }
73          catch (Exception e)
74          {
75              throw new InitialisationException(e, this);
76          }
77      }
78  
79      protected synchronized void loadFromStore() throws Exception
80      {
81          Properties props = new Properties();
82          props.load(new FileInputStream(fileStore));
83  
84          for (Map.Entry<Object, Object> entry : props.entrySet())
85          {
86              super.store(entry.getKey().toString(), entry.getValue().toString());
87          }
88      }
89  
90      @Override
91      public void store(Serializable id, String item) throws ObjectStoreException
92      {
93          super.store(id, item);
94  
95          try
96          {
97              if (output == null)
98              {
99                  output = new FileOutputStream(fileStore, true);
100             }
101 
102             StringBuffer buf = new StringBuffer();
103             buf.append(id).append("=").append(item.toString()).append(IOUtils.LINE_SEPARATOR);
104             output.write(buf.toString().getBytes());
105         }
106         catch (IOException iox)
107         {
108             throw new ObjectStoreException(iox);
109         }
110     }
111 
112     public String getDirectory()
113     {
114         return directory;
115     }
116 
117     public void setDirectory(String directory)
118     {
119         this.directory = directory;
120     }
121 
122     public String getEncoding()
123     {
124         return encoding;
125     }
126 
127     public void setEncoding(String encoding)
128     {
129         this.encoding = encoding;
130     }
131 
132     @Override
133     public synchronized void dispose()
134     {
135         Properties props = new Properties();
136 
137         for (Iterator<?> iterator = super.store.values().iterator(); iterator.hasNext();)
138         {
139             StoredObject<?> storedObject = (StoredObject<?>) iterator.next();
140             props.put(storedObject.getId(), storedObject.getItem());
141         }
142 
143         if (output == null)
144         {
145             try
146             {
147                 output = new FileOutputStream(fileStore, false);
148                 props.store(output, StringUtils.EMPTY);
149                 IOUtils.closeQuietly(output);
150             }
151             catch (IOException e)
152             {
153                 logger.error(e.getMessage(), e);
154             }
155         }
156         else
157         {
158             IOUtils.closeQuietly(output);
159         }
160 
161         super.dispose();
162     }
163 }