Coverage Report - org.homeunix.thecave.buddi.plugin.api.PreferenceAccess
 
Classes in this File Line Coverage Branch Coverage Complexity
PreferenceAccess
4%
1/21
0%
0/2
2.167
 
 1  
 /*
 2  
  * Created on Sep 7, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.api;
 5  
 
 6  
 import java.util.List;
 7  
 
 8  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 9  
 import org.homeunix.thecave.buddi.plugin.api.exception.PluginException;
 10  
 import org.homeunix.thecave.buddi.util.BuddiCryptoFactory;
 11  
 
 12  
 /**
 13  
  * An abstract class which provides plugins the functionality of storing and
 14  
  * reading preferences in the main Buddi preferences file. 
 15  
  * @author wyatt
 16  
  *
 17  
  */
 18  18360
 public abstract class PreferenceAccess {
 19  
         /**
 20  
          * Loads the value which is associated with the given key.
 21  
          * @param key The key to read.
 22  
          * @return The value associated with the given key, or null if there was no 
 23  
          * such value set.
 24  
          */
 25  
         public String getPreference(String key){
 26  0
                 return PrefsModel.getInstance().getPluginPreference(key);
 27  
         }
 28  
         
 29  
         /**
 30  
          * Stores the value associated with a given key.
 31  
          * @param key The key to save.  In order to ensure that you do not
 32  
          * overwrite the preferences for another plugin, you must use
 33  
          * the key format as follows:
 34  
          * 
 35  
          * "package.Plugin.property"
 36  
          * 
 37  
          * For instance, use the key "com.example.buddi.ImportFoo.LAST_EXECUTE_DAY" 
 38  
          * instead of the key "LAST_EXECUTE_DAY".
 39  
          * @param value The value to store.  Can be any valid String.
 40  
          */
 41  
         public void putPreference(String key, String value){
 42  0
                 PrefsModel.getInstance().putPluginPreference(key, value);
 43  0
         }
 44  
         
 45  
         /**
 46  
          * Sets the given list of strings in the preferences map, using the given key.
 47  
          * 
 48  
          * @param key The key to save.  In order to ensure that you do not
 49  
          * overwrite the preferences for another plugin, you must use
 50  
          * the key format as follows:
 51  
          * 
 52  
          * "package.Plugin.property"
 53  
          * 
 54  
          * For instance, use the key "com.example.buddi.ImportFoo.LAST_EXECUTE_DAY" 
 55  
          * instead of the key "LAST_EXECUTE_DAY".
 56  
          * @param values The list of values to store
 57  
          */
 58  
         public void putListPreference(String key, List<String> values){
 59  0
                 PrefsModel.getInstance().putPluginListPreference(key, values);
 60  0
         }
 61  
         
 62  
         /**
 63  
          * Returns the list associated with the given key.
 64  
          * @param key The key to save.  In order to ensure that you do not
 65  
          * overwrite the preferences for another plugin, you must use
 66  
          * the key format as follows:
 67  
          * 
 68  
          * "package.Plugin.property"
 69  
          * 
 70  
          * For instance, use the key "com.example.buddi.ImportFoo.LAST_EXECUTE_DAY" 
 71  
          * instead of the key "LAST_EXECUTE_DAY".
 72  
 
 73  
          * @return
 74  
          */
 75  
         public List<String> getListPreference(String key){
 76  0
                 return PrefsModel.getInstance().getPluginListPreference(key);
 77  
         }
 78  
         
 79  
         /**
 80  
          * Loads the value which is associated with the given key, after decrypting 
 81  
          * the value using the given password.
 82  
          * @param key
 83  
          * @param password
 84  
          * @return
 85  
          */
 86  
         public String getSecurePreference(String key, char[] password) throws PluginException {
 87  
                 try {
 88  0
                         BuddiCryptoFactory crypto = new BuddiCryptoFactory();
 89  0
                         String ciphertext = PrefsModel.getInstance().getPluginPreference(key);
 90  0
                         if (ciphertext == null)
 91  0
                                 return null;
 92  0
                         return crypto.getDecryptedString(ciphertext, password);
 93  
                         
 94  
                 }
 95  0
                 catch (Exception e){
 96  0
                         throw new PluginException("Error getting encrypted preference from key " + key, e);
 97  
                 }
 98  
         }
 99  
         
 100  
         /**
 101  
          * Stores the value, encrypted using the given password, into the preferences
 102  
          * file using the given key.
 103  
          * @param key
 104  
          * @param value
 105  
          * @param password
 106  
          */
 107  
         public void putSecurePreference(String key, String value, char[] password) throws PluginException {
 108  
                 try {
 109  0
                         BuddiCryptoFactory crypto = new BuddiCryptoFactory();
 110  0
                         String encrypted = crypto.getEncryptedString(value, password);
 111  0
                         PrefsModel.getInstance().putPluginPreference(key, encrypted);
 112  
                 }
 113  0
                 catch (Exception e){
 114  0
                         throw new PluginException("Error putting encrypted preference to key " + key, e);
 115  0
                 }
 116  0
         }
 117  
 }