| 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 10756 2009-12-02 15:58:24Z 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.report.modules.preferences.base.ConfigFactory; |
| 46 |
import org.jfree.report.modules.preferences.base.ConfigStorage; |
| 47 |
import org.jfree.report.modules.preferences.base.ConfigStoreException; |
| 48 |
import org.pentaho.reporting.libraries.base.config.Configuration; |
| 49 |
import org.pentaho.reporting.libraries.base.config.HierarchicalConfiguration; |
| 50 |
import org.pentaho.reporting.libraries.base.config.ModifiableConfiguration; |
| 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 config the properties which should be stored. |
| 87 |
* @throws ConfigStoreException if an error occured. |
| 88 |
*/ |
| 89 |
public void store(final String configPath, final Configuration config) |
| 90 |
throws ConfigStoreException |
| 91 |
{ |
| 92 |
if (ConfigFactory.isValidPath(configPath) == false) |
| 93 |
{ |
| 94 |
throw new IllegalArgumentException("The give path is not valid."); |
| 95 |
} |
| 96 |
final Enumeration keys = config.getConfigProperties(); |
| 97 |
final Properties properties = new Properties(); |
| 98 |
while (keys.hasMoreElements()) |
| 99 |
{ |
| 100 |
final String key = (String) keys.nextElement(); |
| 101 |
final String value = config.getConfigProperty(key); |
| 102 |
if (value != null && key != null) |
| 103 |
{ |
| 104 |
properties.put(key, value); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
final File target = new File(baseDirectory, configPath); |
| 109 |
if (target.exists() == true && target.canWrite() == false) |
| 110 |
{ |
| 111 |
return; |
| 112 |
} |
| 113 |
try |
| 114 |
{ |
| 115 |
final OutputStream out = new BufferedOutputStream(new FileOutputStream( |
| 116 |
target)); |
| 117 |
properties.store(out, CONFIGHEADER); |
| 118 |
out.close(); |
| 119 |
} |
| 120 |
catch (Exception e) |
| 121 |
{ |
| 122 |
throw new ConfigStoreException("Failed to write config " + configPath, e); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Loads the properties from the given path, specifying the given properties |
| 128 |
* as default. |
| 129 |
* |
| 130 |
* @param configPath the configuration path from where to load the |
| 131 |
* properties. |
| 132 |
* @param defaults the property set that acts as fallback to provide default |
| 133 |
* values. |
| 134 |
* @return the loaded properties. |
| 135 |
* @throws ConfigStoreException if an error occured. |
| 136 |
*/ |
| 137 |
public Configuration load(final String configPath, |
| 138 |
final Configuration defaults) |
| 139 |
throws ConfigStoreException |
| 140 |
{ |
| 141 |
if (ConfigFactory.isValidPath(configPath) == false) |
| 142 |
{ |
| 143 |
throw new IllegalArgumentException("The given path is not valid."); |
| 144 |
} |
| 145 |
try |
| 146 |
{ |
| 147 |
final Properties properties = new Properties(); |
| 148 |
final File target = new File(baseDirectory, configPath); |
| 149 |
final InputStream in = new BufferedInputStream(new FileInputStream( |
| 150 |
target)); |
| 151 |
properties.load(in); |
| 152 |
in.close(); |
| 153 |
|
| 154 |
final ModifiableConfiguration config = new HierarchicalConfiguration(defaults); |
| 155 |
final Iterator keys = properties.keySet().iterator(); |
| 156 |
while (keys.hasNext()) |
| 157 |
{ |
| 158 |
String key = (String) keys.next(); |
| 159 |
config.setConfigProperty(key, properties.getProperty(key)); |
| 160 |
} |
| 161 |
return config; |
| 162 |
} |
| 163 |
catch (Exception e) |
| 164 |
{ |
| 165 |
throw new ConfigStoreException("Failed to read config" + configPath, e); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Tests, whether some configuration data exists for the given configuration. |
| 171 |
* |
| 172 |
* @param configPath the configuration path to the property storage. |
| 173 |
* @return true, if there are properties under this path, false otherwise. |
| 174 |
*/ |
| 175 |
public boolean isAvailable(final String configPath) |
| 176 |
{ |
| 177 |
if (ConfigFactory.isValidPath(configPath) == false) |
| 178 |
{ |
| 179 |
throw new IllegalArgumentException("The give path is not valid."); |
| 180 |
} |
| 181 |
|
| 182 |
final File target = new File(baseDirectory, configPath); |
| 183 |
return target.exists() && target.canRead(); |
| 184 |
} |
| 185 |
|
| 186 |
public String toString() |
| 187 |
{ |
| 188 |
return "FileConfigStorage={baseDir=" + baseDirectory + "}"; |
| 189 |
} |
| 190 |
} |