initDB

public class DataConvertor {
private DataBase originDB, targetDB;

private Map<String, DataBase> dataBases = new HashMap<String, DataBase>();

private List<DBUser> users = new ArrayList<DBUser>();

public DataBase getTargetDB() {
return targetDB;
}

public DataBase getOriginDB() {
return originDB;
}

public boolean readConfig(String configfile) {
File file = new File(configfile);
Document doc = null;
if (!file.exists())
return false;
try {
doc = DataConvertor.getDoc(file);
} catch (Exception e) {
e.printStackTrace();
return false;
}
Element root = doc.getRootElement();
Element basesE = root.element("databases");
List<Element> dbelements = basesE.elements("database");
initDataBases(dbelements);
String dbid = root.elementText("origindb");
originDB = dataBases.get(dbid);
dbid = root.elementText("targetdb");
targetDB = dataBases.get(dbid);
initUsers(root);
return true;

}

private void initUsers(Element root) {
List<Element> eusers = root.element("users").elements("user");
for (int i = 0; i < eusers.size(); i++) {
String userName = eusers.get(i).attributeValue("name");
String pwd = eusers.get(i).attributeValue("pwd");
DBUser user = new DBUser();
user.setUserName(userName);
user.setPassword(pwd);
users.add(user);
}
}

private void initDataBases(List<Element> dbelements) {
int size = dbelements.size();
for (int i = 0; i < size; i++) {
Element element = dbelements.get(i);
String dbid = element.attributeValue("id");
String dbtype = element.elementText("dbtype");
String dbip = element.elementText("add");
String dbport = element.elementText("port");
String dbname = element.elementText("dbname");
String userName = element.elementText("username");
String password = element.elementText("password");
DataBase dataBase = DataBaseFactory.getDataBase(dbtype);
dataBase.setId(dbid);
dataBase.setAdd(dbip);
dataBase.setDbName(dbname);
if (!dbport.equals("")) {
int port = Integer.parseInt(dbport);
dataBase.setPort(port);
}
dataBase.setUserName(userName);
dataBase.setPassword(password);
dataBases.put(dbid, dataBase);
}
}

你可能感兴趣的:(init)