Frames | No Frames |
1: /** 2: * ======================================== 3: * JFreeReport : a free Java report library 4: * ======================================== 5: * 6: * Project Info: http://reporting.pentaho.org/ 7: * 8: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. 9: * 10: * This library is free software; you can redistribute it and/or modify it under the terms 11: * of the GNU Lesser General Public License as published by the Free Software Foundation; 12: * either version 2.1 of the License, or (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16: * See the GNU Lesser General Public License for more details. 17: * 18: * You should have received a copy of the GNU Lesser General Public License along with this 19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20: * Boston, MA 02111-1307, USA. 21: * 22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 23: * in the United States and other countries.] 24: * 25: * ------------ 26: * $Id: FileConfigStorage.java 3525 2007-10-16 11:43:48Z tmorgner $ 27: * ------------ 28: * (C) Copyright 2000-2005, by Object Refinery Limited. 29: * (C) Copyright 2005-2007, by Pentaho Corporation. 30: */ 31: 32: package org.jfree.report.modules.preferences.filesystem; 33: 34: import java.io.BufferedInputStream; 35: import java.io.BufferedOutputStream; 36: import java.io.File; 37: import java.io.FileInputStream; 38: import java.io.FileOutputStream; 39: import java.io.InputStream; 40: import java.io.OutputStream; 41: import java.util.Enumeration; 42: import java.util.Iterator; 43: import java.util.Properties; 44: 45: import org.jfree.base.config.HierarchicalConfiguration; 46: import org.jfree.base.config.ModifiableConfiguration; 47: import org.jfree.report.modules.preferences.base.ConfigFactory; 48: import org.jfree.report.modules.preferences.base.ConfigStorage; 49: import org.jfree.report.modules.preferences.base.ConfigStoreException; 50: import org.jfree.util.Configuration; 51: 52: 53: /** 54: * The FileConfigStorage is a storage provider that stores its content on the 55: * local filesystem. The directory used contains the data as plain text property 56: * files. 57: * 58: * @author Thomas Morgner 59: */ 60: public class FileConfigStorage implements ConfigStorage 61: { 62: /** The base directory of the storage provider. */ 63: private final File baseDirectory; 64: /** The configuration header text that is appended to all property files. */ 65: private static final String CONFIGHEADER = 66: "part of the jfreereport filesystem config store"; 67: 68: /** 69: * Creates a new file config storage and stores the contents in the given 70: * directory. 71: * 72: * @param baseDirectory the directory that should contain the files. 73: */ 74: public FileConfigStorage(final File baseDirectory) 75: { 76: this.baseDirectory = baseDirectory; 77: } 78: 79: /** 80: * Stores the given properties on the defined path. 81: * <p/> 82: * This implementation stores the data as property files. 83: * 84: * @param configPath the configuration path that specifies where to store the 85: * properties. 86: * @param properties the properties which should be stored. 87: * @throws ConfigStoreException if an error occured. 88: * @see org.jfree.report.modules.misc.configstore.base.ConfigStorage 89: * #storeProperties(java.lang.String, java.util.Properties) 90: */ 91: public void store(final String configPath, final Configuration config) 92: throws ConfigStoreException 93: { 94: if (ConfigFactory.isValidPath(configPath) == false) 95: { 96: throw new IllegalArgumentException("The give path is not valid."); 97: } 98: final Enumeration keys = config.getConfigProperties(); 99: final Properties properties = new Properties(); 100: while (keys.hasMoreElements()) 101: { 102: final String key = (String) keys.nextElement(); 103: final String value = config.getConfigProperty(key); 104: if (value != null && key != null) 105: { 106: properties.put(key, value); 107: } 108: } 109: 110: final File target = new File(baseDirectory, configPath); 111: if (target.exists() == true && target.canWrite() == false) 112: { 113: return; 114: } 115: try 116: { 117: final OutputStream out = new BufferedOutputStream(new FileOutputStream( 118: target)); 119: properties.store(out, CONFIGHEADER); 120: out.close(); 121: } 122: catch (Exception e) 123: { 124: throw new ConfigStoreException("Failed to write config " + configPath, e); 125: } 126: } 127: 128: /** 129: * Loads the properties from the given path, specifying the given properties 130: * as default. 131: * 132: * @param configPath the configuration path from where to load the 133: * properties. 134: * @param defaults the property set that acts as fallback to provide default 135: * values. 136: * @return the loaded properties. 137: * @throws ConfigStoreException if an error occured. 138: */ 139: public Configuration load(final String configPath, 140: final Configuration defaults) 141: throws ConfigStoreException 142: { 143: if (ConfigFactory.isValidPath(configPath) == false) 144: { 145: throw new IllegalArgumentException("The given path is not valid."); 146: } 147: try 148: { 149: final Properties properties = new Properties(); 150: final File target = new File(baseDirectory, configPath); 151: final InputStream in = new BufferedInputStream(new FileInputStream( 152: target)); 153: properties.load(in); 154: in.close(); 155: 156: final ModifiableConfiguration config = new HierarchicalConfiguration(defaults); 157: final Iterator keys = properties.keySet().iterator(); 158: while (keys.hasNext()) 159: { 160: final String key = (String) keys.next(); 161: config.setConfigProperty(key, properties.getProperty(key)); 162: } 163: return config; 164: } 165: catch (Exception e) 166: { 167: throw new ConfigStoreException("Failed to read config" + configPath, e); 168: } 169: } 170: 171: /** 172: * Tests, whether some configuration data exists for the given configuration. 173: * 174: * @param configPath the configuration path to the property storage. 175: * @return true, if there are properties under this path, false otherwise. 176: */ 177: public boolean isAvailable(final String configPath) 178: { 179: if (ConfigFactory.isValidPath(configPath) == false) 180: { 181: throw new IllegalArgumentException("The give path is not valid."); 182: } 183: 184: final File target = new File(baseDirectory, configPath); 185: return target.exists() && target.canRead(); 186: } 187: 188: public String toString() 189: { 190: return "FileConfigStorage={baseDir=" + baseDirectory + "}"; 191: } 192: }