Search in sources :

Example 6 with ConfigBean

use of org.eweb4j.config.bean.ConfigBean in project eweb4j-framework by laiweiwei.

the class CheckConfigBean method checkMVCResultPart.

/**
	 * Check the Result part of MVC components configuration
	 * 
	 * @param rList
	 * @return
	 */
public static String checkMVCResultPart(List<ResultConfigBean> rList, String beanID, String xmlFile) {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if ("true".equalsIgnoreCase(cb.getMvc().getOpen()) || "1".equals(cb.getMvc().getOpen())) {
        StringBuilder sb = new StringBuilder();
        for (Iterator<ResultConfigBean> it = rList.iterator(); it.hasNext(); ) {
            ResultConfigBean r = it.next();
            if (!"forward".equalsIgnoreCase(r.getType()) && !"redirect".equalsIgnoreCase(r.getType()) && !"out".equalsIgnoreCase(r.getType()) && !"action".equalsIgnoreCase(r.getType()) && !"".equals(r.getType())) {
                sb.append("当前您填写的:( type=").append(r.getType()).append(" )是错误的!它只能填写为:forward|redirect|out|action|留空  中的一种 ;").append("\n");
            }
        }
        if (!"".equals(sb.toString())) {
            error = "\n<br /><b>" + xmlFile + "[bean name=" + beanID + "][result]:</b>\n" + sb.toString();
        }
    }
    return error;
}
Also used : ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean)

Example 7 with ConfigBean

use of org.eweb4j.config.bean.ConfigBean in project eweb4j-framework by laiweiwei.

the class CheckConfigBean method checkIOCJnject.

/**
	 * Check the IOC component Inject part configuration
	 * 
	 * @param inject
	 * @param iocList
	 * @return
	 */
public static String checkIOCJnject(List<Injection> inject, List<IOCConfigBean> iocList, String beanID, String xmlFile) {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if ("true".equalsIgnoreCase(cb.getIoc().getOpen()) || "1".equals(cb.getIoc().getOpen())) {
        StringBuilder sb = new StringBuilder();
        for (Injection inj : inject) {
            if (inj.getRef() != null && !"".equals(inj.getRef())) {
                boolean flag = false;
                for (IOCConfigBean iocBean : iocList) {
                    if (!iocBean.getId().equals(inj.getRef())) {
                        flag = true;
                    } else {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    sb.append("当前您填写的 :( ref=").append(inj.getRef()).append(" )是错误的!它必须是本文件中某个bean的id值 ;\n");
                }
            }
            if (!"".equals(inj.getType()) && inj.getType() != null) {
                if (!"int".equalsIgnoreCase(inj.getType()) && !"String".equalsIgnoreCase(inj.getType()) && !"boolean".equalsIgnoreCase(inj.getType()) && !"long".equalsIgnoreCase(inj.getType()) && !"double".equalsIgnoreCase(inj.getType()) && !"float".equalsIgnoreCase(inj.getType())) {
                    String type = inj.getType();
                    try {
                        Thread.currentThread().getContextClassLoader().loadClass(type);
                    } catch (ClassNotFoundException e) {
                        sb.append("当前您填写的:( type=").append(inj.getType()).append(" )是错误的!它只能填写为:int|String|long|float|boolean|double 中的一种 或者有效的类名 ;").append("\n");
                    }
                }
            }
            if ("int".equalsIgnoreCase(inj.getType())) {
                if (!inj.getValue().matches(RegexList.integer_regexp)) {
                    sb.append("当前您填写的:( value=").append(inj.getValue()).append(" )是错误的!它必须是整数! ;").append("\n");
                }
            }
        }
        if (!"".equals(sb.toString())) {
            error = "\n<br /><b>" + xmlFile + "[bean id=" + beanID + "][inject]:</b>\n" + sb.toString();
        }
    }
    return error;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean) Injection(org.eweb4j.ioc.config.bean.Injection) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean)

Example 8 with ConfigBean

use of org.eweb4j.config.bean.ConfigBean 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 9 with ConfigBean

use of org.eweb4j.config.bean.ConfigBean in project eweb4j-framework by laiweiwei.

the class EWeb4JConfig method _start.

private static String _start(String error, ConfigBean cb) {
    if (cb == null) {
        error = " can not read any configuration info! But now have bean repaired, please restart.";
    } else {
        StringBuilder infos = new StringBuilder("EWeb4JConfig.start \n");
        infos.append("start-config-xml-path --> ").append(ConfigConstant.START_FILE_PATH()).append("\n");
        infos.append("${RootPath} --> ").append(ConfigConstant.ROOT_PATH).append("\n");
        infos.append(cb).append("\n");
        log.debug(infos.toString());
        // 检查配置信息格式是否填写正确
        String error1 = CheckConfigBean.checkEWeb4JConfigBean(cb);
        if (error1 != null)
            error = error1;
        String error2 = CheckConfigBean.checkEWeb4JIOCPart(cb.getIoc());
        if (error2 != null)
            if (error == null)
                error = error2;
            else
                error += error2;
        String error3 = CheckConfigBean.checkIOCXml(cb.getIoc().getIocXmlFiles());
        if (error3 != null)
            if (error == null)
                error = error3;
            else
                error += error3;
        String error4 = CheckConfigBean.checkEWeb4JORMPart(cb.getOrm());
        if (error4 != null)
            if (error == null)
                error = error4;
            else
                error += error4;
        String error5 = CheckConfigBean.checkORMXml(cb.getOrm().getOrmXmlFiles());
        if (error5 != null)
            if (error == null)
                error = error5;
            else
                error += error5;
        String error6 = CheckConfigBean.checkEWeb4JMVCPart(cb.getMvc());
        if (error6 != null)
            if (error == null)
                error = error6;
            else
                error += error6;
        String error7 = CheckConfigBean.checkMVCActionXmlFile(cb.getMvc().getActionXmlFiles());
        if (error7 != null)
            if (error == null)
                error = error7;
            else
                error += error7;
        String error8 = CheckConfigBean.checkInter(cb.getMvc().getInterXmlFiles());
        if (error8 != null)
            if (error == null)
                error = error8;
            else
                error += error8;
        if (error == null) {
            // 验证通过,将读取到的信息放入缓存池中
            SingleBeanCache.add(ConfigBean.class.getName(), cb);
            // ------log-------
            String info = "EWeb4J start configuration info have bean validated and pushed to the cache. ";
            log.debug(info);
            // ------log-------
            // 继续验证其他组件配置信息
            // properties
            String error13 = null;
            try {
                for (org.eweb4j.config.bean.Prop f : cb.getProperties().getFile()) {
                    error13 = Props.readProperties(f, true);
                    if (error13 != null)
                        if (error == null)
                            error = error13;
                        else
                            error += error13;
                }
            } catch (Throwable e) {
                log.warn(e.toString(), e);
                if (error == null)
                    error = e.toString();
                else
                    error += e.toString();
            }
            if (error == null)
                log.debug("properties module -> ok");
            if (error == null && ("true".equals(cb.getIoc().getOpen()) || "1".equals(cb.getIoc().getOpen()))) {
                String error10 = IOCConfig.check();
                if (error10 != null)
                    error = error10;
                if (error == null)
                    log.debug("ioc module -> ok");
            }
            if (error == null && ("true".equals(cb.getOrm().getOpen()) || "1".equals(cb.getOrm().getOpen()))) {
                // check the db connection
                String error14 = DAOConfig.check();
                if (error14 != null)
                    error = error14;
                if (error == null) {
                    log.debug("orm.dao module -> ok");
                    // read jpa annotation
                    String error10 = new PojoAnnotationConfig().readAnnotation(cb.getOrm().getScanPojoPackage().getPath());
                    if (error10 != null)
                        error = error10;
                    if (error == null) {
                        log.debug("orm.pojo.annotation module -> ok");
                        // read orm xml file
                        String error11 = ORMConfig.check();
                        if (error11 != null)
                            error = error11;
                        if (error == null)
                            log.debug("orm.pojo.xml module -> ok");
                        // generate ddl
                        if (error == null && ("true".equals(cb.getOrm().getDdl().getGenerate()) || "1".equals(cb.getOrm().getDdl().getGenerate()))) {
                            log.debug("ddl.generate -> true");
                            DBInfoConfigBean dcb = DBInfoConfigBeanCache.get(cb.getOrm().getDdl().getDs());
                            if (DBType.MYSQL_DB.equals(dcb.getDataBaseType())) {
                                File sqlFile = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
                                if ("1".equals(cb.getOrm().getDdl().getOverride()) || "true".equals(cb.getOrm().getDdl().getOverride()) || !sqlFile.exists()) {
                                    String sql = Model2Table.generate();
                                    if (sql == null) {
                                        error = "can not generate ddl file";
                                    } else
                                        log.debug("ddl.generate execute success -> " + sqlFile.getAbsolutePath());
                                } else {
                                    log.warn("ddl.generate do not need to execute ->" + sqlFile.getAbsolutePath() + " is exists !");
                                }
                            } else {
                                log.warn("sorry only mysql db can use the ddl feature !");
                            }
                            if (error == null)
                                log.debug("orm.ddl.generate module -> ok");
                        }
                        // run ddl
                        if (error == null && ("true".equals(cb.getOrm().getDdl().getRun()) || "1".equals(cb.getOrm().getDdl().getRun()))) {
                            log.debug("ddl.run -> true");
                            DBInfoConfigBean dcb = DBInfoConfigBeanCache.get(cb.getOrm().getDdl().getDs());
                            if (dcb != null) {
                                if (DBType.MYSQL_DB.equals(dcb.getDataBaseType())) {
                                    File sqlFile = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
                                    StringBuilder builder = new StringBuilder();
                                    BufferedReader sql_reader = null;
                                    try {
                                        sql_reader = new BufferedReader(new InputStreamReader(new FileInputStream(sqlFile)));
                                        String line = null;
                                        while ((line = sql_reader.readLine()) != null) {
                                            builder.append(line);
                                        }
                                        final String[] sqls = builder.toString().split(";");
                                        final UpdateDAO dao = DAOFactory.getUpdateDAO(cb.getOrm().getDdl().getDs());
                                        Transaction.execute(new Trans() {

                                            public void run(Object... args) throws Exception {
                                                for (String sql : sqls) {
                                                    log.debug("ddl run -> " + sql);
                                                    dao.updateBySQL(sql);
                                                }
                                            }
                                        });
                                    } catch (Exception e) {
                                        String _error13 = CommonUtil.getExceptionString(e);
                                        if (_error13 != null)
                                            error = _error13;
                                    } finally {
                                        if (sql_reader != null) {
                                            try {
                                                sql_reader.close();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                } else {
                                    log.warn("sorry only mysql db can use the ddl feature !");
                                }
                            } else {
                                log.error("ddl.ds -> " + cb.getOrm().getDdl().getDs() + " not found !");
                            }
                            if (error == null)
                                log.debug("orm.ddl.run module -> ok");
                        }
                    //end orm.ddl.run
                    }
                // end if error == null -> pojo.annotation
                }
                if (error == null)
                    log.debug("orm module -> ok ");
            }
            if (error == null && ("true".equals(cb.getMvc().getOpen()) || "1".equals(cb.getMvc().getOpen()))) {
                String error20 = new ActionAnnotationConfig().readAnnotation(cb.getMvc().getScanActionPackage().getPath());
                if (error20 != null)
                    error += error20;
                if (error == null) {
                    log.debug("mvc.action.annotation module -> ok");
                    String error11 = ActionConfig.check();
                    if (error11 != null)
                        error = error11;
                    if (error == null) {
                        log.debug("mvc.action.xml module -> ok");
                        String error12 = new InterceptorAnnotationConfig().readAnnotation(cb.getMvc().getScanInterceptorPackage().getPath());
                        if (error12 != null)
                            error = error12;
                        if (error == null) {
                            log.debug("mvc.action.interceptor.annotation module -> ok");
                            String error21 = InterceptorConfig.check();
                            if (error21 != null)
                                error = error21;
                            if (error == null)
                                log.debug("mvc.action.interceptor.xml module -> ok");
                        }
                    // end if interceptor annotation ok
                    }
                // end if action xml ok
                }
                if (error == null)
                    log.debug("mvc module -> ok");
            }
            // end mvc module
            //CallBack after startup
            Listeners listeners = cb.getListeners();
            if (listeners != null && listeners.getListener() != null && !listeners.getListener().isEmpty()) {
                for (ListenerBean lb : listeners.getListener()) {
                    String clazz = lb.getClazz();
                    try {
                        EWeb4JListener listener = (EWeb4JListener) CommonUtil.loadClass(clazz).newInstance();
                        listener.onStartup();
                        log.debug("listener->" + listener + ".onStartup execute...");
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    // end if error == null
    }
    return error;
}
Also used : InputStreamReader(java.io.InputStreamReader) UpdateDAO(org.eweb4j.orm.dao.update.UpdateDAO) ListenerBean(org.eweb4j.config.bean.ListenerBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) Listeners(org.eweb4j.config.bean.Listeners) ActionAnnotationConfig(org.eweb4j.mvc.config.ActionAnnotationConfig) PojoAnnotationConfig(org.eweb4j.orm.config.PojoAnnotationConfig) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) BufferedReader(java.io.BufferedReader) InterceptorAnnotationConfig(org.eweb4j.mvc.config.InterceptorAnnotationConfig) File(java.io.File) Trans(org.eweb4j.orm.jdbc.transaction.Trans)

Example 10 with ConfigBean

use of org.eweb4j.config.bean.ConfigBean 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)

Aggregations

ConfigBean (org.eweb4j.config.bean.ConfigBean)28 DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)13 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)12 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)12 File (java.io.File)10 IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)10 ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)10 InterConfigBean (org.eweb4j.mvc.config.bean.InterConfigBean)10 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)9 ParamConfigBean (org.eweb4j.mvc.config.bean.ParamConfigBean)9 ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)9 ValidatorConfigBean (org.eweb4j.mvc.config.bean.ValidatorConfigBean)9 XMLReader (org.eweb4j.util.xml.XMLReader)6 CheckConfigBean (org.eweb4j.config.CheckConfigBean)5 Method (java.lang.reflect.Method)4 Prop (org.eweb4j.config.bean.Prop)4 ReflectUtil (org.eweb4j.util.ReflectUtil)4 IOException (java.io.IOException)3 LogsConfigBean (org.eweb4j.config.bean.LogsConfigBean)3 FileInputStream (java.io.FileInputStream)2