69 lines
1.8 KiB
Java
69 lines
1.8 KiB
Java
package de.mrbesen.youtubecrawler;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Properties;
|
|
import java.util.Map.Entry;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
public class Config {
|
|
|
|
public static Properties prop = new Properties();
|
|
private static Logger log = Logger.getLogger(Config.class.getName());
|
|
private static File conf;
|
|
|
|
private static Map<String,String> properties = new HashMap<String, String>() {
|
|
{
|
|
put("db.host" , "localhost" );
|
|
put("db.port" , "3306" );
|
|
put("db.user" , "ytcrawler" );
|
|
put("db.pw" , "" );
|
|
put("db.dbname" , "ytcrawler" );
|
|
put("youtube.apikey", "" );
|
|
}
|
|
};
|
|
|
|
public Config(final File conf) {
|
|
Config.conf = conf;
|
|
try {
|
|
prop.load(new FileInputStream(conf));
|
|
} catch(IOException e) {
|
|
log.error("can not load Config", e);
|
|
generateNewConfig();
|
|
}
|
|
}
|
|
|
|
private void generateNewConfig() {
|
|
log.fatal("Configuration file " + conf.getName() + " not found or can't be opened!");
|
|
|
|
try {
|
|
// set defaults
|
|
for(Entry<String, String> entr : properties.entrySet()) {
|
|
prop.setProperty(entr.getKey(), entr.getValue());
|
|
}
|
|
|
|
// safe into new configuration file
|
|
prop.store(new FileWriter(conf), "");
|
|
log.info("Created " + conf.getName() + " with default values. "
|
|
+ "Please insert your API key and update the download base url."
|
|
);
|
|
|
|
} catch (IOException ex) {
|
|
// when the new configuration file can't be written to disk
|
|
log.error("Configuration file " + conf.getName()
|
|
+ "could not be created. Please check file writing permissions."
|
|
);
|
|
|
|
} finally {
|
|
// user has to fix this, else the bot won't work at all
|
|
log.info("Will now exit.");
|
|
System.exit(1);
|
|
}
|
|
}
|
|
}
|