Coverage Report - org.codeforamerica.open311.internals.caching.RegularJavaCache
 
Classes in this File Line Coverage Branch Coverage Complexity
RegularJavaCache
70%
19/27
50%
5/10
2.8
 
 1  
 package org.codeforamerica.open311.internals.caching;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.FileOutputStream;
 6  
 import java.io.IOException;
 7  
 import java.util.Properties;
 8  
 
 9  
 import org.codeforamerica.open311.internals.platform.PlatformManager;
 10  
 
 11  
 /**
 12  
  * Basic implementation of the {@link Cache} interface. It uses a properties
 13  
  * file to save the data. Singleton class.
 14  
  * 
 15  
  * @author Santiago MunĂ­n <santimunin@gmail.com>
 16  
  * 
 17  
  */
 18  
 public class RegularJavaCache extends AbstractCache {
 19  
         private Properties properties;
 20  
         /**
 21  
          * Unique instance of the class.
 22  
          */
 23  1
         private static RegularJavaCache instance = new RegularJavaCache();
 24  
 
 25  
         /**
 26  
          * Returns the unique instance of the class.
 27  
          */
 28  
         public static RegularJavaCache getInstance() {
 29  23
                 return instance;
 30  
         }
 31  
 
 32  
         private RegularJavaCache() {
 33  1
                 super();
 34  
                 try {
 35  1
                         File cacheFile = new File(FILE);
 36  1
                         if (!cacheFile.exists()) {
 37  0
                                 cacheFile.createNewFile();
 38  
                         }
 39  1
                         this.properties = new Properties();
 40  1
                         this.properties.load(new FileInputStream(cacheFile));
 41  0
                 } catch (IOException e) {
 42  0
                         PlatformManager.getInstance().buildLogger()
 43  
                                         .logError("Error loading the cache: " + e.getMessage());
 44  0
                         throw new Error("Couldn't create/load the cache file.");
 45  1
                 }
 46  1
         }
 47  
 
 48  
         @Override
 49  
         protected void saveProperty(String key, String value) {
 50  12
                 if (key != null && key.length() > 0 && value != null
 51  
                                 && value.length() > 0) {
 52  
                         try {
 53  12
                                 properties.setProperty(key, value);
 54  12
                                 properties.store(new FileOutputStream(FILE), null);
 55  0
                         } catch (IOException e) {
 56  0
                                 PlatformManager.getInstance().buildLogger()
 57  
                                                 .logError("Error saving a property: " + e.getMessage());
 58  12
                         }
 59  
                 }
 60  12
         }
 61  
 
 62  
         @Override
 63  
         protected String getProperty(String key) {
 64  33
                 return properties.getProperty(key);
 65  
         }
 66  
 
 67  
         @Override
 68  
         public void deleteCache() {
 69  
                 try {
 70  9
                         properties.clear();
 71  9
                         properties.store(new FileOutputStream(FILE), null);
 72  0
                 } catch (IOException e) {
 73  0
                         PlatformManager.getInstance().buildLogger()
 74  
                                         .logError("Error deleting the cache: " + e.getMessage());
 75  9
                 }
 76  9
         }
 77  
 
 78  
 }