Search in sources :

Example 26 with HongsException

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;
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) HongsException(io.github.ihongs.HongsException)

Example 27 with HongsException

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);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HongsException(io.github.ihongs.HongsException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 28 with HongsException

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);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HongsException(io.github.ihongs.HongsException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 29 with HongsException

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;
}
Also used : HongsException(io.github.ihongs.HongsException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) HongsExemption(io.github.ihongs.HongsExemption) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 30 with HongsException

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);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HongsException(io.github.ihongs.HongsException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

HongsException (io.github.ihongs.HongsException)138 Map (java.util.Map)77 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)31 IOException (java.io.IOException)29 Set (java.util.Set)26 HashSet (java.util.HashSet)25 ArrayList (java.util.ArrayList)24 List (java.util.List)20 HongsExemption (io.github.ihongs.HongsExemption)14 Action (io.github.ihongs.action.anno.Action)14 LinkedHashSet (java.util.LinkedHashSet)14 SQLException (java.sql.SQLException)13 FormSet (io.github.ihongs.action.FormSet)12 Table (io.github.ihongs.db.Table)12 FileNotFoundException (java.io.FileNotFoundException)11 CoreConfig (io.github.ihongs.CoreConfig)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 Iterator (java.util.Iterator)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9