use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class DB method getModel.
/**
* 通过表名获取表模型
* 表名可以为"库名.表名"
* @param tableName table 对象的 name
* @return 指定表模型
* @throws io.github.ihongs.HongsException
*/
public Model getModel(String tableName) throws HongsException {
/**
* 表名可以是"数据库.表名"
* 用于引用另一个库中的表
*/
int pos = tableName.lastIndexOf('.');
if (pos > 0) {
String db = tableName.substring(0, pos);
tableName = tableName.substring(pos + 1);
return DB.getInstance(db).getModel(tableName);
}
if (this.modelObjects.containsKey(tableName)) {
return this.modelObjects.get(tableName);
}
if (!this.tableConfigs.containsKey(tableName)) {
throw new HongsException(1026, "Can not find config for table '" + this.name + "." + tableName + "'.");
}
/**
* 读取库指定的modelClass
*/
Map<String, String> tcfg = this.tableConfigs.get(tableName);
// this.tableConfigs.remove(tableName);
String mcls = this.modelClass;
/**
* 就近原则:
* 如果表配置中有设置model则采用表配置中的
*/
if (tcfg.containsKey("model")) {
mcls = tcfg.get("model");
}
/**
* 如果class为空则直接使用默认的Table
*/
if (mcls == null || mcls.length() == 0) {
Model mobj = new Model(this.getTable(tableName));
this.modelObjects.put(tableName, mobj);
return mobj;
}
CoreLogger.trace("DB: modelClass({}) for table({}.{}) has been defined, try to get it", mcls, this.name, tableName);
/**
* 获取指定的Table类
*/
Class cls;
try {
cls = Class.forName(mcls);
} catch (ClassNotFoundException ex) {
throw new HongsException(1037, ex);
}
/**
* 获取构造器
*/
Constructor cst;
try {
cst = cls.getConstructor(new Class[] { Table.class });
} catch (NoSuchMethodException ex) {
throw new HongsException(1038, ex);
} catch (SecurityException ex) {
throw new HongsException(1038, ex);
}
/**
* 获取表实例
*/
Model mobj;
try {
mobj = (Model) cst.newInstance(new Object[] { this.getTable(tableName) });
} catch (InstantiationException ex) {
throw new HongsException(1039, ex);
} catch (IllegalAccessException ex) {
throw new HongsException(1039, ex);
} catch (IllegalArgumentException ex) {
throw new HongsException(1039, ex);
} catch (InvocationTargetException ex) {
throw new HongsException(1039, ex);
}
this.modelObjects.put(tableName, mobj);
return mobj;
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class DBConfig method parseByFile.
/**
* 根据文件解析配置
*
* @param df
* @return 配置对象
* @throws io.github.ihongs.HongsException
*/
public static DBConfig parseByFile(File df) throws HongsException {
Document doc;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbn = dbf.newDocumentBuilder();
doc = dbn.parse(df);
} catch (ParserConfigurationException ex) {
throw new HongsException(1062, ex);
} catch (SAXException ex) {
throw new HongsException(1062, ex);
} catch (IOException ex) {
throw new HongsException(1064, ex);
}
return new DBConfig(doc);
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class DBConfig method parseBySource.
/**
* 根据输入流解析配置
*
* @param ds
* @return 配置对象
* @throws io.github.ihongs.HongsException
*/
public static DBConfig parseBySource(InputSource ds) throws HongsException {
Document doc;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbn = dbf.newDocumentBuilder();
doc = dbn.parse(ds);
} catch (ParserConfigurationException ex) {
throw new HongsException(1062, ex);
} catch (SAXException ex) {
throw new HongsException(1062, ex);
} catch (IOException ex) {
throw new HongsException(1065, ex);
}
return new DBConfig(doc);
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class DBConfig method imports.
@Override
protected void imports() throws HongsException {
InputStream is;
String fn;
DBConfig cp;
try {
fn = Core.CONF_PATH + "/" + name + Cnst.DB_EXT + ".xml";
is = new FileInputStream(fn);
} catch (FileNotFoundException ex) {
fn = name.contains(".") || name.contains("/") ? name + Cnst.DB_EXT + ".xml" : Cnst.CONF_PACK + "/" + name + Cnst.DB_EXT + ".xml";
is = this.getClass().getClassLoader().getResourceAsStream(fn);
if (is == null) {
throw new HongsExemption(826, "Can not find the config file '" + name + Cnst.DB_EXT + ".xml'.");
}
}
try {
cp = parseByStream(is);
} finally {
try {
is.close();
} catch (IOException ex) {
throw new HongsException(ex);
}
}
this.link = cp.link;
this.source = cp.source;
this.origin = cp.origin;
this.dbClass = cp.dbClass;
this.tableClass = cp.tableClass;
this.modelClass = cp.modelClass;
this.tablePrefix = cp.tablePrefix;
this.tableSuffix = cp.tableSuffix;
this.tableConfigs = cp.tableConfigs;
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class DBConfig method parseByStream.
/**
* 根据输入流解析配置
*
* @param ds
* @return 配置对象
* @throws io.github.ihongs.HongsException
*/
public static DBConfig parseByStream(InputStream ds) throws HongsException {
Document doc;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbn = dbf.newDocumentBuilder();
doc = dbn.parse(ds);
} catch (ParserConfigurationException ex) {
throw new HongsException(1062, ex);
} catch (SAXException ex) {
throw new HongsException(1062, ex);
} catch (IOException ex) {
throw new HongsException(1065, ex);
}
return new DBConfig(doc);
}
Aggregations