package iitb.con.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PropertiesConfig {
static Logger log = Logger.getLogger(PropertiesConfig.class.getName());
public static Properties loadProperties(String fileName) {
Properties properties = new Properties();
try {
FileInputStream innputFile = new FileInputStream (fileName);
properties.load(innputFile);
innputFile.close();
} catch (IOException ioException) {
log.log(Level.SEVERE,"Error in reading the file '" + fileName + "'",ioException);
}
return properties;
}
public static Properties loadProperties(Class clazz, String fileName) {
Properties properties = null;
InputStream is = null;
if (clazz != null) {
is = clazz.getResourceAsStream(fileName);
} else {
try {
is = new FileInputStream(fileName);
} catch (FileNotFoundException fe) {
log.log(Level.SEVERE,"Error in reading the file '" + fileName + "'",fe);
return null;
}
}
try {
properties = new Properties();
properties.load(is);
is.close();
return properties;
}catch (IOException ioException) {
log.log(Level.SEVERE,"Error in reading the file '" + fileName + "'",ioException);
return null;
}
}
public static void storeProperties(String fileName, Properties properties) {
try {
FileOutputStream outputFile = new FileOutputStream (fileName) ;
properties.store(outputFile, null);
outputFile.close();
} catch (IOException ioException) {
log.log(Level.SEVERE,"Error in writing the file ",ioException);
}
}
public static void storeProperties(Class clazz, String fileName, Properties properties) {
try {
FileOutputStream outputFile =
new FileOutputStream (resolveName(PropertiesConfig.class, fileName)) ;
properties.store(outputFile, null);
outputFile.close();
} catch (IOException ioException) {
log.log(Level.SEVERE,"Error in writing the file ",ioException);
}
}
private static String resolveName(Class clazz, String name) {
if (name == null) {
return name;
}
if (!name.startsWith("/")) {
Class c = clazz;
while (c.isArray()) {
c = c.getComponentType();
}
String baseName = c.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0, index).replace('.', '/')
+"/"+name;
}
} else {
name = name.substring(1);
}
return name;
}
}