/*-
* Copyright © 2010 Diamond Light Source Ltd.
*
* This file is part of GDA.
*
* GDA is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 3 as published by the Free
* Software Foundation.
*
* GDA is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with GDA. If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.gda.util.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.util.Iterator;
import java.util.Properties;
public final class PropUtils {
////////////////////////////////////////////////////////////////////////
public final static Properties loadProperties(final String path) throws IOException {
final File file = new File(path);
return PropUtils.loadProperties(file);
}
////////////////////////////////////////////////////////////////////////
public final static Properties loadProperties(final File file) throws IOException {
final Properties fileProps = new Properties();
if (!file.exists()) return fileProps;
InputStream stream = null;
try {
stream = new FileInputStream(file);
final BufferedInputStream in = new BufferedInputStream(stream);
fileProps.load(in);
}
finally {
IOUtils.close(stream,"Loading properties for file "+IOUtils.fileInfo(file));
}
return fileProps;
}
////////////////////////////////////////////////////////////////////////
public final static Properties storeProperties(final Properties props,
final String path) throws IOException{
final File file = new File(path);
return PropUtils.storeProperties(props, file);
}
////////////////////////////////////////////////////////////////////////
public final static Properties storeProperties(final Properties props,
final File file) throws IOException {
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
if (!file.exists()) file.createNewFile();
BufferedOutputStream out = null;
try {
final FileOutputStream os = new FileOutputStream(file);
out = new BufferedOutputStream(os);
props.store(out, "GDA/SDA Properties. Please do not edit this file.");
}
finally {
IOUtils.close(out,"Storing properties for file "+IOUtils.fileInfo(file));
}
return props;
}
public final static Properties storeKeys(final Properties props,
final File file) throws IOException{
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
if (!file.exists()) file.createNewFile();
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(file));
final Iterator<?> it = props.keySet().iterator();
while(it.hasNext()) out.println((String)it.next());
} finally {
IOUtils.close(out,"Storing keys for file "+IOUtils.fileInfo(file));
}
return props;
}
public static final Properties loadProperties(final HttpURLConnection connection) throws IOException {
BufferedInputStream in = null;
final Properties urlProps = new Properties();
try {
final InputStream stream = connection.getInputStream();
in = new BufferedInputStream(stream);
urlProps.load(in);
} finally {
if (in!=null) {
IOUtils.close(in,"Loading properties from URL");
connection.disconnect(); // Must do for HTTP because servers have limited connections
}
}
return urlProps;
}
}