Search in sources :

Example 1 with XMLReader

use of org.eweb4j.util.xml.XMLReader in project eweb4j-framework by laiweiwei.

the class DAOConfig method check.

public static synchronized String check() {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if (cb == null)
        return null;
    DBInfoXmlFiles dbInfoXmlFiles = cb.getOrm().getDbInfoXmlFiles();
    if (dbInfoXmlFiles == null)
        return ConfigInfoCons.CANNOT_READ_CONFIG_FILE;
    for (String filePath : dbInfoXmlFiles.getPath()) {
        if (filePath == null || filePath.length() == 0)
            continue;
        File configFile = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
        try {
            XMLReader reader = BeanXMLUtil.getBeanXMLReader(configFile);
            reader.setBeanName("dataSource");
            reader.setClass("dataSource", DBInfoConfigBean.class);
            DBInfoConfigBean dcb = reader.readOne();
            if (dcb == null) {
                error = rebuildXmlFile(configFile, ConfigInfoCons.REPAIR_FILE_INFO);
            } else {
                String error1 = CheckConfigBean.checkORMDBInfo(dcb, filePath);
                if (error1 == null) {
                    if (DBInfoConfigBeanCache.get(dcb.getDsName()) == null) {
                        DBInfoConfigBeanCache.add(dcb.getDsName(), dcb);
                    }
                    DataSourceWrap dsw = null;
                    if (DataSourceWrapCache.containsKey(dcb.getDsName())) {
                        dsw = (DataSourceWrap) DataSourceWrapCache.get(dcb.getDsName());
                        if (dsw == null) {
                            DataSourceWrapCache.remove(dcb.getDsName());
                            log.debug("remove dsName->" + dcb.getDsName());
                        } else
                            log.debug("DataSource -> " + dcb.getDsName() + " is alive !");
                    }
                    if (dsw == null)
                        dsw = new DataSourceWrap(dcb.getDsName(), DataSourceCreator.create(dcb));
                    String error2 = dsw.getConnection() == null ? ConfigInfoCons.CANNOT_GET_DB_CON : null;
                    if (error2 != null)
                        if (error == null)
                            error = error2;
                        else
                            error += error2;
                    else {
                        String info = dcb.getDsName() + "." + ConfigInfoCons.READ_CONFIG_INFO_SUCCESS;
                        log.debug(info);
                        // 此步也是为了共存多个数据源
                        if (!DataSourceWrapCache.containsKey(dcb.getDsName())) {
                            log.debug("init put ds to cache...");
                            DataSourceWrapCache.put(dcb.getDsName(), dsw);
                        }
                    }
                } else if (error == null)
                    error = error1;
                else
                    error += error1;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            error = rebuildXmlFile(configFile, CommonUtil.getExceptionString(e));
        }
    }
    if (error != null)
        DBInfoConfigBeanCache.clear();
    return error;
}
Also used : DataSourceWrap(org.eweb4j.orm.jdbc.DataSourceWrap) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) CheckConfigBean(org.eweb4j.config.CheckConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) File(java.io.File) XMLReader(org.eweb4j.util.xml.XMLReader) DBInfoXmlFiles(org.eweb4j.config.bean.DBInfoXmlFiles)

Example 2 with XMLReader

use of org.eweb4j.util.xml.XMLReader in project eweb4j-framework by laiweiwei.

the class EWeb4JConfig method startByAbFile.

private static synchronized String startByAbFile(String aStartXmlPath) {
    String startXmlPath = aStartXmlPath;
    String error = null;
    File file = null;
    boolean readXml = true;
    if (ConfigConstant.SUCCESS_START.equals(String.valueOf(SingleBeanCache.get(ConfigConstant.SUCCESS_START)))) {
        ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
        String reload = (cb == null ? "true" : cb.getReload());
        if ("true".equals(reload) || "1".equals(reload)) {
            // 如果开了DEBUG,清空缓存,重新读取配置文件
            SingleBeanCache.clear();
            ORMConfigBeanCache.clear();
            IOCConfigBeanCache.clear();
            ActionConfigBeanCache.clear();
            log.debug("EWeb4J clear cache");
            readXml = true;
        } else {
            // 否则,不需要读取配置文件
            readXml = false;
        }
    }
    if (readXml) {
        // 1.读取配置文件
        try {
            file = new File(startXmlPath);
            ConfigBean cb = null;
            boolean readFile = true;
            final String check = ConfigConstant.CHECK_START_FILE_EXIST;
            if (!file.exists()) {
                if (!"true".equals(check) && !"1".equals(check)) {
                    log.warn("Skip the Start Configuation file !!!");
                    cb = ConfigBeanCreator.create();
                    readFile = false;
                }
            }
            if (readFile) {
                XMLReader reader = BeanXMLUtil.getBeanXMLReader(file);
                reader.setBeanName("eweb4j");
                reader.setClass("eweb4j", ConfigBean.class);
                cb = reader.readOne();
            }
            error = _start(error, cb);
        } catch (Throwable e) {
            // 重写配置文件
            try {
                // 保存为备份文件
                FileUtil.copy(file, new File(startXmlPath + ".back" + "_" + CommonUtil.getNowTime("MMddHHmmss")));
                XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(file, ConfigBeanCreator.create());
                writer.setBeanName("eweb4j");
                writer.setClass("eweb4j", ConfigBean.class);
                writer.write();
                String info = "configuration error, now it has repaired.";
                error = CommonUtil.getNowTime() + "EWeb4JConfig : " + info + "exception:" + CommonUtil.getExceptionString(e);
                log.error(info, e);
            } catch (Throwable e1) {
                String info = "can not write any configuration";
                error = CommonUtil.getNowTime() + "EWeb4JConfig : " + info + "exception:" + CommonUtil.getExceptionString(e1);
                log.fatal(info, e);
            }
        }
        _done(error);
    }
    return error;
}
Also used : DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) File(java.io.File) XMLWriter(org.eweb4j.util.xml.XMLWriter) XMLReader(org.eweb4j.util.xml.XMLReader)

Example 3 with XMLReader

use of org.eweb4j.util.xml.XMLReader in project eweb4j-framework by laiweiwei.

the class ActionConfig method check.

/** 涉及到文件IO,需要同步保证正确性 */
public static synchronized String check() {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if (cb == null)
        return null;
    // eweb4j-start-config.xml中<mvc>节点里的actionXmlFile
    List<String> xmlFilePaths = cb.getMvc().getActionXmlFiles().getPath();
    for (String filePath : xmlFilePaths) {
        if (filePath == null || filePath.length() == 0)
            continue;
        // 解决中文文件夹名
        File configFile = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
        try {
            XMLReader reader = BeanXMLUtil.getBeanXMLReader(configFile);
            reader.setBeanName("action");
            reader.setClass("action", ActionConfigBean.class);
            List<ActionConfigBean> mvcList = reader.read();
            if (mvcList == null || mvcList.isEmpty()) {
                error = rebuildXmlFile(configFile, ConfigErrCons.CANNOT_READ_CONFIG_INFO);
            } else {
                for (Iterator<ActionConfigBean> it = mvcList.iterator(); it.hasNext(); ) {
                    ActionConfigBean mvc = it.next();
                    // 检查MVC.Action配置是否有错误
                    String error1 = CheckConfigBean.checkMVCAction(mvc, filePath);
                    if (error1 != null)
                        if (error != null)
                            error += error1;
                        else
                            error = error1;
                    // 检查MVC.Action中的Result部分配置是否有错误
                    String error2 = CheckConfigBean.checkMVCResultPart(mvc.getResult(), mvc.getUriMapping(), filePath);
                    if (error2 != null)
                        if (error != null)
                            error += error2;
                        else
                            error = error2;
                    // 检查MVC.Action.Validator中的部分配置是否有错误
                    String error4 = CheckConfigBean.checkMVCValidator(mvc.getValidator(), mvc.getUriMapping(), filePath);
                    if (error4 != null)
                        if (error != null)
                            error += error4;
                        else
                            error = error4;
                }
                // 如果没有任何错误,将Action的配置信息放入缓存,供框架运行期使用
                if (error == null)
                    for (Iterator<ActionConfigBean> it = mvcList.iterator(); it.hasNext(); ) {
                        ActionConfigBean mvc = it.next();
                        if (!"".equals(mvc.getClazz()))
                            if (!"".equals(mvc.getUriMapping()))
                                ActionConfigBeanCache.add(mvc.getUriMapping(), mvc);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
            error = rebuildXmlFile(configFile, CommonUtil.getExceptionString(e));
        }
    }
    // 如果有错误,清空缓存
    if (error != null)
        ActionConfigBeanCache.clear();
    return error;
}
Also used : Iterator(java.util.Iterator) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) CheckConfigBean(org.eweb4j.config.CheckConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) File(java.io.File) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) XMLReader(org.eweb4j.util.xml.XMLReader)

Example 4 with XMLReader

use of org.eweb4j.util.xml.XMLReader in project eweb4j-framework by laiweiwei.

the class InterceptorConfig method check.

public static synchronized String check() {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if (cb != null) {
        for (String filePath : cb.getMvc().getInterXmlFiles().getPath()) {
            if (filePath != null && filePath.length() > 0) {
                File configFile = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
                try {
                    XMLReader reader = BeanXMLUtil.getBeanXMLReader(configFile);
                    reader.setBeanName("interceptor");
                    reader.setClass("interceptor", InterConfigBean.class);
                    List<InterConfigBean> interList = reader.read();
                    if (interList == null || interList.isEmpty()) {
                        error = rebuildXmlFile(configFile, ConfigErrCons.CANNOT_READ_CONFIG_INFO);
                    } else {
                        for (Iterator<InterConfigBean> it = interList.iterator(); it.hasNext(); ) {
                            InterConfigBean inter = it.next();
                            String error1 = CheckConfigBean.checkMVCInterceptor(inter, filePath);
                            if (error1 != null) {
                                if (error != null) {
                                    error += error1;
                                } else {
                                    error = error1;
                                }
                            }
                        }
                        if (error == null) {
                            for (Iterator<InterConfigBean> it = interList.iterator(); it.hasNext(); ) {
                                InterConfigBean inter = it.next();
                                if (!"".equals(inter.getClazz())) {
                                    InterConfigBeanCache.add(inter);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    error = rebuildXmlFile(configFile, CommonUtil.getExceptionString(e));
                }
            }
        }
        if (error != null) {
            InterConfigBeanCache.clear();
        }
    }
    return error;
}
Also used : InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) CheckConfigBean(org.eweb4j.config.CheckConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) File(java.io.File) XMLReader(org.eweb4j.util.xml.XMLReader)

Example 5 with XMLReader

use of org.eweb4j.util.xml.XMLReader in project eweb4j-framework by laiweiwei.

the class IOCConfig method check.

public static synchronized String check() {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if (cb == null)
        return null;
    List<String> iocXmlFilePaths = cb.getIoc().getIocXmlFiles().getPath();
    log.debug("ioc xml files size -> " + iocXmlFilePaths.size());
    for (String filePath : iocXmlFilePaths) {
        if (filePath == null || filePath.length() == 0)
            continue;
        File configFile = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
        log.debug("check ioc file -> " + configFile.getAbsolutePath());
        try {
            XMLReader reader = BeanXMLUtil.getBeanXMLReader(configFile);
            reader.setBeanName("ioc");
            reader.setClass("ioc", IOCConfigBean.class);
            List<IOCConfigBean> iocList = reader.read();
            if (iocList == null || iocList.isEmpty()) {
                error = rebuildXmlFile(ConfigInfoCons.CANNOT_READ_CONFIG_FILE, configFile);
            } else {
                log.debug("read from the ioc file there has ->" + iocList.size() + " beans will be checked ");
                for (IOCConfigBean ioc : iocList) {
                    String error1 = CheckConfigBean.checkIOC(ioc, filePath);
                    if (error1 != null)
                        if (error == null)
                            error = error1;
                        else
                            error += error1;
                    String error2 = CheckConfigBean.checkIOCJnject(ioc.getInject(), iocList, ioc.getId(), filePath);
                    if (error2 != null)
                        if (error == null)
                            error = error2;
                        else
                            error += error2;
                }
                if (error == null) {
                    for (IOCConfigBean ioc : iocList) IOCConfigBeanCache.add(ioc.getId(), ioc);
                    log.debug(configFile.getAbsolutePath() + " beans has checked success ");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            error = rebuildXmlFile(error + "|" + CommonUtil.getExceptionString(e), configFile);
        }
    }
    if (error != null)
        IOCConfigBeanCache.clear();
    return error;
}
Also used : CheckConfigBean(org.eweb4j.config.CheckConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) File(java.io.File) XMLReader(org.eweb4j.util.xml.XMLReader)

Aggregations

File (java.io.File)6 ConfigBean (org.eweb4j.config.bean.ConfigBean)6 XMLReader (org.eweb4j.util.xml.XMLReader)6 CheckConfigBean (org.eweb4j.config.CheckConfigBean)5 DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)2 Iterator (java.util.Iterator)1 DBInfoXmlFiles (org.eweb4j.config.bean.DBInfoXmlFiles)1 IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)1 ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)1 InterConfigBean (org.eweb4j.mvc.config.bean.InterConfigBean)1 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)1 DataSourceWrap (org.eweb4j.orm.jdbc.DataSourceWrap)1 XMLWriter (org.eweb4j.util.xml.XMLWriter)1