Search in sources :

Example 51 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class JRecord method set.

/**
 * 设置数据
 * @param key
 * @param val
 * @param exp
 */
@Override
public void set(String key, T val, long exp) throws HongsException {
    // 序列化值
    byte[] arr = null;
    String str = null;
    if (!bytes) {
        str = Dawn.toString(val);
    } else
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bos)) {
            out.writeObject(val);
            out.flush();
            arr = bos.toByteArray();
        } catch (IOException e) {
            throw new HongsException(e);
        }
    long now = System.currentTimeMillis() / 1000;
    // table.db.open( );
    table.db.ready();
    try (PreparedStatement ps = table.db.prepareStatement("UPDATE `" + table.tableName + "` SET data= ?, xtime= ?, mtime= ? WHERE id = ?")) {
        if (arr != null) {
            ps.setBytes(1, arr);
        } else {
            ps.setString(1, str);
        }
        ps.setLong(2, exp);
        ps.setLong(3, now);
        ps.setString(4, key);
        if (ps.executeUpdate() > 0) {
            return;
        }
    } catch (SQLException e) {
        throw new HongsException(1045, e);
    }
    try (PreparedStatement ps = table.db.prepareStatement("INSERT INTO `" + table.tableName + "` (data, xtime, mtime, id) VALUES (?, ?, ?, ?)")) {
        if (arr != null) {
            ps.setBytes(1, arr);
        } else {
            ps.setString(1, str);
        }
        ps.setLong(2, exp);
        ps.setLong(3, now);
        ps.setString(4, key);
        if (ps.executeUpdate() > 0) {
            return;
        }
    } catch (SQLException e) {
        throw new HongsException(1045, e);
    }
}
Also used : HongsException(io.github.ihongs.HongsException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 52 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class JRecord method del.

/**
 * 清除数据
 * @param exp
 */
@Override
public void del(long exp) throws HongsException {
    // table.db.open( );
    table.db.ready();
    try (PreparedStatement ps = table.db.prepareStatement("DELETE FROM `" + table.tableName + "` WHERE xtime <= ? AND xtime != 0")) {
        ps.setLong(1, exp);
        ps.executeUpdate();
    } catch (SQLException e) {
        throw new HongsException(1045, e);
    }
}
Also used : SQLException(java.sql.SQLException) HongsException(io.github.ihongs.HongsException) PreparedStatement(java.sql.PreparedStatement)

Example 53 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Sesion method close.

@Override
public void close() throws HongsException {
    store();
    IRecord rec = getRecord();
    // 如果数据记录对象可关闭, 关闭之
    if (rec instanceof AutoCloseable) {
        try {
            ((AutoCloseable) rec).close();
        } catch (Exception err) {
            throw new HongsException(err);
        }
    }
}
Also used : HongsException(io.github.ihongs.HongsException) HongsException(io.github.ihongs.HongsException)

Example 54 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class SessFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse rsp, FilterChain flt) throws ServletException, IOException {
    HttpServletRequest raq = (HttpServletRequest) req;
    HttpServletResponse rzp = (HttpServletResponse) rsp;
    /**
     * 对于嵌套相同过滤, 不在内部重复执行;
     * 如外部设置了忽略, 则跳过忽略的路径.
     */
    if ((inside != null && Synt.declare(req.getAttribute(inside), false)) || (patter != null && !patter.matches(ActionDriver.getRecentPath(raq)))) {
        flt.doFilter(req, rsp);
        return;
    }
    try {
        raq = new SessAccess(raq, rzp, this);
        raq.setAttribute(inside, true);
        flt.doFilter(raq, rzp);
    } finally {
        raq.removeAttribute(inside);
        // 最终任务完成后对会话进行保存
        HttpSession ses = raq.getSession(false);
        if (null != ses && ses instanceof Sesion) {
            try {
                ((Sesion) ses).close();
            } catch (HongsException ex) {
                throw new ServletException(ex);
            }
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HongsException(io.github.ihongs.HongsException) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 55 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Unit method makeDocument.

private Document makeDocument() throws HongsException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new HongsException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HongsException(io.github.ihongs.HongsException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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