package com.statusParser;
import java.util.Scanner;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* User: sadvr
* Date: 1/14/14
* Time: 17:04 PM
*/
public class Configuring {
private static int argsLength = 0;
private static final Properties PROP = new Properties();
private static final Scanner IN = new Scanner(System.in);
public void start(String[] args) {
argsLength = args.length;
if (args[1].equals("mysql")) {
load();
mysql(args);
} else {
System.out.println("Unknown config parameter: " + unknownArgs(args, 1));
}
}
public Properties load() { //load properties from file
try {
File file = new File("config.properties");
file.createNewFile();
PROP.load(new FileInputStream("config.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
return PROP;
}
private String unknownArgs(String[] args, int fromParameter) {
String argsString = "";
for (int i = fromParameter; i < argsLength; i++) {
argsString = argsString + args[i] + " ";
}
return argsString;
}
private void mysql(String[] args) {
if (argsLength == 2) { //setup MySQL connection parameters
System.out.print("Конфигурация MySQL...\nserverName: ");
String serverName = IN.nextLine();
System.out.print("Port: ");
String portNumber = IN.nextLine();
System.out.print("Username: ");
String userName = IN.nextLine();
System.out.print("Password: ");
String password = IN.nextLine();
System.out.print("Database: ");
String database = IN.nextLine();
try {
PROP.setProperty("serverName", serverName);
PROP.setProperty("port", portNumber);
PROP.setProperty("userName", userName);
PROP.setProperty("password", password);
PROP.setProperty("database", database);
PROP.store(new FileOutputStream("config.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (argsLength == 3) {
if (args[2].equals("on")) { //set MySQL ON
try {
PROP.setProperty("mysql", "on");
PROP.store(new FileOutputStream("config.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (args[2].equals("off")) { //set MySQL OFF
try {
PROP.setProperty("mysql", "off");
PROP.store(new FileOutputStream("config.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
System.out.println("Unknown mysql parameter: " + unknownArgs(args, 2));
}
} else {
System.out.println("Unknown mysql parameter: " + unknownArgs(args, 2));
}
}
}