package controller.operating;
/**
* Como usar:
*
* Criar um vetor String de 6 posi��es ou mais
* private String[] drive = new String[6];
*
* Instanciar um objeto da classe
* GerenciadorConfiguracoesDriverXml driverConfig = new GerenciadorConfiguracoesDriverXml();
*
* Usar o getConfigureConection para jogar a leitura do xml para drive
* drive = driverConfig.getConfigureConnection();
*/
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import util.DriverConexao;
public class GerenciadorConfiguracoesDriverXml {
private final String fileName = "driver.xml";
private String xmlDriverName = "driverName";
private String xmlDriverServerName = "serverName";
private String xmlDriverDataBase = "dataBase";
private String xmlDriverUserName = "driverUserName";
private String xmlDriverPassword = "driverPassword";
private Map<String, Object> configurations;
private String driverName, driverServerName, driverDataBase, driverUserName, driverPassword;
//private String[] drive = new String[5];
private DriverConexao driver;
public GerenciadorConfiguracoesDriverXml() {
loadConfigurations();
}
@SuppressWarnings("unchecked")
private void loadConfigurations() {
try {
XMLDecoder decoder = new XMLDecoder( new FileInputStream(fileName) );
Map readObject = (Map) decoder.readObject();
configurations = readObject;
decoder.close();
} catch (FileNotFoundException ex) {
configurations = new HashMap<String, Object>();
}
}
@SuppressWarnings("unchecked")
public boolean addConfiguration(String configName, Object value) {
configurations.put(configName, value);
try {
XMLEncoder encoder = new XMLEncoder( new FileOutputStream(fileName) );
encoder.writeObject(configurations);
encoder.flush();
encoder.close();
return true;
} catch (FileNotFoundException ex) {
return false;
}
}
public Object getConfiguration(String configName) {
Object config = configurations.get(configName);
if(config == null)
return "";
else
return config;
}
public DriverConexao getConfigureConnection() {
GerenciadorConfiguracoesDriverXml driverConfig = new GerenciadorConfiguracoesDriverXml();
driverName = (String) driverConfig.getConfiguration(xmlDriverName);
driverServerName = (String) driverConfig.getConfiguration(xmlDriverServerName);
driverDataBase = (String) driverConfig.getConfiguration(xmlDriverDataBase);
driverUserName = (String) driverConfig.getConfiguration(xmlDriverUserName);
driverPassword = (String) driverConfig.getConfiguration(xmlDriverPassword);
// org.gjt.mm.mysql.Driver;localhost;Desafio;jdbc:mysql://localhost/Desafio;root;root;
driver = new DriverConexao(
driverName,
driverServerName,
driverDataBase,
driverUserName,
driverPassword);
return driver;
}
public void setConfigureConnection(DriverConexao newDrive){
GerenciadorConfiguracoesDriverXml driverConfig = new GerenciadorConfiguracoesDriverXml();
driverConfig.addConfiguration(xmlDriverName, newDrive.getName());
driverConfig.addConfiguration(xmlDriverServerName, newDrive.getServerName());
driverConfig.addConfiguration(xmlDriverDataBase, newDrive.getDataBase());
driverConfig.addConfiguration(xmlDriverUserName, newDrive.getUserName());
driverConfig.addConfiguration(xmlDriverPassword, newDrive.getPassword());
}
public static void main(String args[]){
DriverConexao newDrive = new DriverConexao(
"org.gjt.mm.mysql.Driver",
"localhost",
"etadg",
"root",
"root");
GerenciadorConfiguracoesDriverXml set = new GerenciadorConfiguracoesDriverXml();
set.setConfigureConnection(newDrive);
}
}