Search in sources :

Example 26 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class ServerCmdlet method start.

@Cmdlet("start")
public static void start(String[] args) throws HongsException {
    int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
    String conf = Core.CORE_PATH + File.separator + "web.xml";
    if (!(new File(conf)).exists()) {
        conf = Core.CONF_PATH + File.separator + "web.xml";
    }
    String serd = Core.DATA_PATH + File.separator + "server";
    File ppid = new File(serd + File.separator + port + ".pid");
    File ppcd = ppid.getParentFile();
    // 检查进程
    if (ppid.exists() == true) {
        CmdletHelper.println("Process already exists!");
        return;
    }
    if (ppcd.exists() == false) {
        ppcd.mkdirs();
    }
    try {
        String pid = ManagementFactory.getRuntimeMXBean().getName().split("@", 2)[0];
        FileWriter dip = new FileWriter(ppid, true);
        dip.write(pid);
        dip.close();
    } catch (IOException e) {
        throw new HongsException.Common(e);
    }
    // 构建应用
    Server server;
    WebAppContext webapp;
    server = new Server(port);
    webapp = new WebAppContext();
    webapp.setDescriptor(conf);
    webapp.setContextPath(Core.BASE_HREF);
    webapp.setResourceBase(Core.BASE_PATH);
    webapp.setParentLoaderPriority(true);
    server.setHandler(webapp);
    // 外部配置
    CoreConfig c = CoreConfig.getInstance("defines");
    for (Map.Entry t : c.entrySet()) {
        String k = (String) t.getKey();
        String v = (String) t.getValue();
        if (k.startsWith("jetty.attr.")) {
            webapp.setAttribute(k.substring(11), v);
        } else if (k.startsWith("jetty.para.")) {
            webapp.setInitParameter(k.substring(11), v);
        }
    }
    /**
     * 初始设置
     * 光能外部配置参数还不够方便
     * 可能需要替换 JSP 解析器或 Session 容器
     * 可以设置 jetty.init 来注入 Initer 对象
     */
    String xs = c.getProperty("jetty.init");
    if (null != xs) {
        String[] xa = xs.split(";");
        for (String xn : xa) {
            xn = xn.trim();
            if ("".equals(xn)) {
                continue;
            }
            try {
                ((Initer) Class.forName(xn).newInstance()).init(webapp);
            } catch (ClassNotFoundException ex) {
                throw new HongsError.Common(ex);
            } catch (InstantiationException ex) {
                throw new HongsError.Common(ex);
            } catch (IllegalAccessException ex) {
                throw new HongsError.Common(ex);
            }
        }
    }
    // 停止机制
    Runtime.getRuntime().addShutdownHook(new Stoper(server, ppid));
    try {
        server.start();
        server.join();
    } catch (Exception e) {
        throw new HongsException.Common(e);
    } catch (Error e) {
        throw new HongsError.Common(e);
    }
}
Also used : HongsError(app.hongs.HongsError) Server(org.eclipse.jetty.server.Server) CoreConfig(app.hongs.CoreConfig) FileWriter(java.io.FileWriter) HongsError(app.hongs.HongsError) IOException(java.io.IOException) IOException(java.io.IOException) HongsException(app.hongs.HongsException) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HongsException(app.hongs.HongsException) File(java.io.File) Map(java.util.Map) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 27 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class SystemCmdlet method exec.

/**
 * 维护命令
 * @param args
 * @throws HongsException
 */
@Cmdlet("__main__")
public static void exec(String[] args) throws HongsException {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "date:s");
    args = (String[]) opts.remove("");
    if (0 == args.length) {
        System.err.println("Serve name required!");
        return;
    }
    String fn = args[0];
    File fu = new File(fn);
    Date dt = new Date();
    // 日期参数
    if (opts.containsKey("date")) {
        dt = getTin((String) opts.get("date"), dt);
    }
    // 相对且不存在则看作内部目录
    if (!fu.isAbsolute() && !fu.exists()) {
        fn = Core.CORE_PATH + File.separator + "bin" + File.separator + fn;
        fu = new File(fn);
    }
    // 获取目录下全部待执行的文件
    List<File> fxs = new ArrayList();
    if (!fu.isFile()) {
        File[] fus = fu.listFiles();
        for (File fo : fus) {
            if (!fo.isFile() || fo.getName().startsWith(".")) {
                continue;
            }
            fxs.add(fo);
        }
        Collections.sort(fxs, new Sorter());
    } else {
        fxs.add(fu);
    }
    Logger lgr = new Logger();
    String act = Core.ACTION_NAME.get();
    long now = Core.ACTION_TIME.get();
    // 逐个执行
    for (File fo : fxs) {
        Matcher met = filNamPatt.matcher(fo.getName());
        if (!met.matches()) {
            continue;
        }
        String ent = met.group(2).trim();
        String ext = met.group(3).trim();
        try {
            if ("cmd.xml".equals(ext)) {
                SystemCmdlet.runCmd(dt, fo, lgr);
            } else if (/**/
            "sql".equals(ext)) {
                runSql(dt, fo, ent);
            }
        } catch (Exception ex) {
            lgr.error(ex);
        } catch (Error ex) {
            lgr.error(ex);
        } finally {
            // 放回名称和开始时间
            // 避免时间或日志模糊
            Core.ACTION_NAME.set(act);
            Core.ACTION_TIME.set(now);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) CoreLogger(app.hongs.CoreLogger) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HongsException(app.hongs.HongsException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) File(java.io.File) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 28 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class SystemCmdlet method runSql.

private static void runSql(Date dt, File fo, String fn) throws HongsException {
    // 读取代码
    String sql;
    byte[] buf;
    try {
        FileInputStream in = new FileInputStream(fo);
        buf = new byte[in.available()];
        in.read(buf);
        sql = new String(buf, "UTF-8");
    } catch (FileNotFoundException ex) {
        throw new HongsException.Common(ex);
    } catch (IOException ex) {
        throw new HongsException.Common(ex);
    }
    // 解析配置
    Date dzt = dt;
    String dbn = fn;
    Matcher mat = sqlDnmPatt.matcher(sql);
    while (mat.find()) {
        String key = mat.group(1);
        if ("DB".equals(key)) {
            dbn = mat.group(2).trim();
        } else if ("DT".equals(key)) {
            dzt = getTim(mat.group(2).trim(), dt);
        }
    }
    // 清理注释
    sql = sqlCmnPatt.matcher(sql).replaceAll("");
    // 设置时间
    sql = repTim(sql, dzt);
    CmdletHelper.println("Run '" + fo.getName() + "' for '" + dbn + "'");
    // 逐条执行
    String[] a = sql.split(";\\s*[\r\n]");
    DB db = DB.getInstance(dbn);
    long st = System.currentTimeMillis();
    int al = a.length;
    int ok = 0;
    int er = 0;
    for (String s : a) {
        s = s.trim();
        try {
            if (0 < s.length()) {
                db.execute(s);
            }
            CmdletHelper.progres(st, al, ++ok, er);
        } catch (HongsException ex) {
            CmdletHelper.progres(st, al, ok, ++er);
            CmdletHelper.progred();
            throw ex;
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) HongsException(app.hongs.HongsException) DB(app.hongs.db.DB)

Example 29 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class DBDeff method syncSlaver.

/**
 * 同步从数据库
 * @param slaver
 * @param tablePrefix 从库表前缀
 * @param tableSuffix 从库表后缀
 * @param delExtraTables 删除多余的表
 * @param delExtraFields 删除多余的字段
 * @throws app.hongs.HongsException
 */
public void syncSlaver(DB slaver, String tablePrefix, String tableSuffix, boolean delExtraTables, boolean delExtraFields) throws HongsException {
    List<String> sqls = this.deffSlaver(slaver, tablePrefix, tableSuffix, delExtraTables, delExtraFields);
    DB sdb = slaver;
    sdb.begin();
    try {
        for (String sql : sqls) {
            sdb.execute(sql);
        }
        sdb.commit();
    } catch (HongsException ex) {
        sdb.revert();
        throw ex;
    }
}
Also used : HongsException(app.hongs.HongsException) DB(app.hongs.db.DB)

Example 30 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class TableDesc method getInstance.

public static TableDesc getInstance(Table table) throws HongsException {
    TableDesc desc = new TableDesc();
    try {
        List rows;
        Iterator it;
        /**
         * 组织字段描述
         */
        rows = table.db.fetchAll("SHOW FULL COLUMNS FROM `" + table.tableName + "`");
        it = rows.iterator();
        while (it.hasNext()) {
            Map row = (Map) it.next();
            String dfn = desc.getDefine(row);
            String col = (String) row.get("Field");
            desc.addColumn(col, dfn);
        }
        /**
         * 获取索引字段
         */
        rows = table.db.fetchAll("SHOW INDEXES FROM `" + table.tableName + "`");
        it = rows.iterator();
        while (it.hasNext()) {
            Map row = (Map) it.next();
            String key = (String) row.get("Key_name");
            String col = (String) row.get("Column_name");
            if ("PRIMARY".equals(key)) {
                desc.addPriCol(col);
            } else if ("0".equals(row.get("Non_unique"))) {
                desc.addUniKey(col, key);
            } else {
                desc.addIdxKey(col, key);
            }
        }
    } catch (HongsException ex) {
        if (ex.getErrno() == 0x1047) {
            String msg = ex.getMessage();
            if (msg.startsWith("Ex1047: Table ") && msg.endsWith(" doesn't exist")) {
                return desc;
            }
        }
        throw ex;
    }
    return desc;
}
Also used : HongsException(app.hongs.HongsException) Iterator(java.util.Iterator) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

HongsException (app.hongs.HongsException)89 Map (java.util.Map)42 HashMap (java.util.HashMap)34 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)15 LinkedHashMap (java.util.LinkedHashMap)15 Set (java.util.Set)15 List (java.util.List)13 File (java.io.File)11 SQLException (java.sql.SQLException)10 FileNotFoundException (java.io.FileNotFoundException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 PreparedStatement (java.sql.PreparedStatement)8 Iterator (java.util.Iterator)8 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 HongsExpedient (app.hongs.HongsExpedient)7 FormSet (app.hongs.action.FormSet)7 Table (app.hongs.db.Table)7