View Javadoc

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  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          super.initialise();
44          if (encoding == null)
45          {
46              encoding = context.getConfiguration().getDefaultEncoding();
47          }
48  
49          if (directory == null)
50          {
51              directory = context.getConfiguration().getWorkingDirectory() + "/objectstore";
52          }
53  
54          try
55          {
56              File dir = FileUtils.openDirectory(directory);
57              fileStore = new File(dir, name + ".dat");
58              if (fileStore.exists())
59              {
60                  loadFromStore();
61              }
62          }
63          catch (Exception e)
64          {
65              throw new InitialisationException(e, this);
66          }
67  
68      }
69  
70      protected synchronized void loadFromStore() throws Exception
71      {
72          Properties props = new Properties();
73          props.load(new FileInputStream(fileStore));
74          for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
75          {
76              Map.Entry entry = (Map.Entry) iterator.next();
77              super.storeObject(entry.getKey().toString(), entry.getValue());
78          }
79      }
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          if (!(item instanceof String))
96          {
97              throw new IllegalArgumentException("TextFile store can only be used for storing text entries");
98          }
99          boolean result = super.storeObject(id, item);
100         if (output == null)
101         {
102             output = new FileOutputStream(fileStore, true);
103         }
104         StringBuffer buf = new StringBuffer();
105         buf.append(id).append("=").append(item.toString()).append(IOUtils.LINE_SEPARATOR);
106         output.write(buf.toString().getBytes());
107         return result;
108     }
109 
110     public String getDirectory()
111     {
112         return directory;
113     }
114 
115     public void setDirectory(String directory)
116     {
117         this.directory = directory;
118     }
119 
120     public String getEncoding()
121     {
122         return encoding;
123     }
124 
125     public void setEncoding(String encoding)
126     {
127         this.encoding = encoding;
128     }
129 
130     public synchronized void dispose()
131     {
132         Properties props = new Properties();
133 
134         for (Iterator iterator = super.store.values().iterator(); iterator.hasNext();)
135         {
136             StoredObject storedObject = (StoredObject) iterator.next();
137             props.put(storedObject.getId(), storedObject.getItem());
138         }
139 
140         if (output == null)
141         {
142             try
143             {
144                 output = new FileOutputStream(fileStore, false);
145                 props.save(output, StringUtils.EMPTY);
146                 IOUtils.closeQuietly(output);
147             }
148             catch (FileNotFoundException e)
149             {
150                 logger.error(e.getMessage(), e);
151             }
152         }
153         else 
154         {
155             IOUtils.closeQuietly(output);
156         }
157         
158         
159         super.dispose();
160     }
161 }