Search in sources :

Example 1 with ConfigBean

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

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

the class Model2Table method generateOne.

public static String generateOne(final Class<?> entityClass, final boolean isDrop, boolean isCreateFile) {
    StringBuilder sql = new StringBuilder();
    StringBuilder manyMany = new StringBuilder();
    final ORMConfigBean ocb = ORMConfigBeanCache.get(entityClass.getName());
    final String table = ocb.getTable();
    List<Property> properties = ocb.getProperty();
    StringBuilder sb = new StringBuilder();
    StringBuilder fkSb = new StringBuilder();
    final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
    StringBuilder pkSb = new StringBuilder();
    final String pk = ", \n\tPRIMARY KEY (%s)";
    StringBuilder uniqueSb = new StringBuilder();
    String unique = ", \n\tUNIQUE KEY %s (%s)";
    final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
    for (Property p : properties) {
        if (sb.length() > 0)
            sb.append(", \n");
        String col = p.getColumn();
        String type = p.getType();
        String size = null;
        if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
            size = String.format(" (%s) ", p.getSize());
        if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
            uniqueSb.append(String.format(unique, col, col));
        String notNull = "";
        if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
            notNull = " NOT NULL ";
        if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
            if (pkSb.length() > 0)
                pkSb.append(",");
            pkSb.append(col);
        }
        String auto = "";
        if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
            auto = " AUTO_INCREMENT ";
        if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
            final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
            if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
                p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
            final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
            fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
        }
        sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
    }
    sb.append(uniqueSb.toString());
    if (pkSb.length() > 0)
        sb.append(String.format(pk, pkSb.toString()));
    sb.append(fkSb.toString());
    String _sql = String.format(create_table_script, table, sb.toString());
    if (isDrop) {
        _sql = String.format(drop_table_script, table) + _sql;
        ;
    }
    sql.append(_sql);
    try {
        ReflectUtil ru = new ReflectUtil(entityClass);
        for (Field f : ru.getFields()) {
            if (!ClassUtil.isListClass(f))
                continue;
            String name = f.getName();
            Method getter = ru.getGetter(name);
            if (getter == null)
                continue;
            ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
            if (mmAnn == null)
                mmAnn = f.getAnnotation(ManyToMany.class);
            if (mmAnn != null) {
                JoinTable join = getter.getAnnotation(JoinTable.class);
                if (join == null) {
                    join = f.getAnnotation(JoinTable.class);
                    if (join == null)
                        continue;
                }
                JoinColumn[] froms = join.joinColumns();
                if (froms == null || froms.length == 0)
                    continue;
                JoinColumn[] tos = join.inverseJoinColumns();
                if (tos == null || tos.length == 0)
                    continue;
                final String relTable = join.name();
                final String relFrom = froms[0].name();
                final String relTo = tos[0].name();
                final Class<?> targetClass = ClassUtil.getGenericType(f);
                StringBuilder manyManyField = new StringBuilder();
                //handle the many to many 
                manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
                manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
                manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
                manyManyField.append("\n\t").append("PRIMARY KEY (id)");
                String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
                String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
                String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
                manyManyField.append(fk1);
                String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
                manyManyField.append(fk2);
                manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
            }
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    sql.append(manyMany.toString());
    String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
    if (!isCreateFile)
        return script;
    try {
        ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
        File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
        FileWriter writer = new FileWriter(file);
        writer.write(script);
        writer.flush();
        writer.close();
        LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
        return script;
    } catch (IOException e2) {
        e2.printStackTrace();
        return null;
    }
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FileWriter(java.io.FileWriter) ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) IOException(java.io.IOException) IOException(java.io.IOException) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) JoinColumn(javax.persistence.JoinColumn) Property(org.eweb4j.orm.config.bean.Property) File(java.io.File) JoinTable(javax.persistence.JoinTable)

Example 3 with ConfigBean

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

the class Model2Table method generateAll.

public static String generateAll(final boolean isDrop, final boolean isCreateFile) {
    StringBuilder sql = new StringBuilder();
    StringBuilder manyMany = new StringBuilder();
    for (Iterator<Entry<Object, ORMConfigBean>> it = ORMConfigBeanCache.entrySet().iterator(); it.hasNext(); ) {
        final Entry<Object, ORMConfigBean> e = it.next();
        final ORMConfigBean ocb = e.getValue();
        final String table = ocb.getTable();
        List<Property> properties = ocb.getProperty();
        StringBuilder sb = new StringBuilder();
        StringBuilder fkSb = new StringBuilder();
        final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
        StringBuilder pkSb = new StringBuilder();
        final String pk = ", \n\tPRIMARY KEY (%s)";
        StringBuilder uniqueSb = new StringBuilder();
        String unique = ", \n\tUNIQUE KEY %s (%s)";
        final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
        for (Property p : properties) {
            if (sb.length() > 0)
                sb.append(", \n");
            String col = p.getColumn();
            String type = p.getType();
            String size = null;
            if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
                size = String.format(" (%s) ", p.getSize());
            if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
                uniqueSb.append(String.format(unique, col, col));
            String notNull = "";
            if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
                notNull = " NOT NULL ";
            if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
                if (pkSb.length() > 0)
                    pkSb.append(",");
                pkSb.append(col);
            }
            String auto = "";
            if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
                auto = " AUTO_INCREMENT ";
            if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
                final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
                if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
                    p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
                final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
                fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
            }
            sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
        }
        sb.append(uniqueSb.toString());
        if (pkSb.length() > 0)
            sb.append(String.format(pk, pkSb.toString()));
        sb.append(fkSb.toString());
        String _sql = String.format(create_table_script, table, sb.toString());
        if (isDrop) {
            _sql = String.format(drop_table_script, table) + _sql;
            ;
        }
        sql.append(_sql);
        try {
            Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass((String) e.getKey());
            ReflectUtil ru = new ReflectUtil(clazz);
            for (Field f : ru.getFields()) {
                if (!ClassUtil.isListClass(f))
                    continue;
                String name = f.getName();
                Method getter = ru.getGetter(name);
                if (getter == null)
                    continue;
                ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
                if (mmAnn == null)
                    mmAnn = f.getAnnotation(ManyToMany.class);
                if (mmAnn != null) {
                    JoinTable join = getter.getAnnotation(JoinTable.class);
                    if (join == null) {
                        join = f.getAnnotation(JoinTable.class);
                        if (join == null)
                            continue;
                    }
                    JoinColumn[] froms = join.joinColumns();
                    if (froms == null || froms.length == 0)
                        continue;
                    JoinColumn[] tos = join.inverseJoinColumns();
                    if (tos == null || tos.length == 0)
                        continue;
                    final String relTable = join.name();
                    final String relFrom = froms[0].name();
                    final String relTo = tos[0].name();
                    final Class<?> targetClass = ClassUtil.getGenericType(f);
                    StringBuilder manyManyField = new StringBuilder();
                    //handle the many to many 
                    manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
                    manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
                    manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
                    manyManyField.append("\n\t").append("PRIMARY KEY (id)");
                    String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
                    String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
                    String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
                    manyManyField.append(fk1);
                    String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
                    manyManyField.append(fk2);
                    manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
                }
            }
        } catch (Exception e1) {
            continue;
        }
    }
    sql.append(manyMany.toString());
    String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
    if (!isCreateFile)
        return script;
    try {
        ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
        File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
        FileWriter writer = new FileWriter(file);
        writer.write(script);
        writer.flush();
        writer.close();
        LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
        return script;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FileWriter(java.io.FileWriter) ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) IOException(java.io.IOException) IOException(java.io.IOException) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) Entry(java.util.Map.Entry) JoinColumn(javax.persistence.JoinColumn) Property(org.eweb4j.orm.config.bean.Property) File(java.io.File) JoinTable(javax.persistence.JoinTable)

Example 4 with ConfigBean

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

the class DataSourceCreator method create.

public static DataSource create(final DBInfoConfigBean dbInfo) throws Exception {
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    Class<?> cls = Thread.currentThread().getContextClassLoader().loadClass(cb.getOrm().getDataSource());
    DataSource ds = (DataSource) cls.newInstance();
    List<Property> properties = dbInfo.getProperty();
    // 通过反射将配置信息注入到数据源对象中
    for (Property property : properties) {
        String name = property.getKey();
        String value = property.getValue();
        if (null == name || name.trim().length() == 0)
            continue;
        if (null == value || value.trim().length() == 0)
            continue;
        ReflectUtil ru2 = new ReflectUtil(ds);
        Method m2 = ru2.getSetter(name);
        if (m2 == null)
            continue;
        Class<?> clazz = m2.getParameterTypes()[0];
        if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz))
            m2.invoke(ds, Integer.parseInt(value));
        else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz) || double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz))
            m2.invoke(ds, Float.parseFloat(value));
        else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz))
            m2.invoke(ds, Boolean.parseBoolean(value));
        else
            m2.invoke(ds, value);
    }
    return ds;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) Method(java.lang.reflect.Method) Property(org.eweb4j.orm.dao.config.bean.Property) DataSource(javax.sql.DataSource)

Example 5 with ConfigBean

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

the class CheckConfigBean method checkMVCAction.

/**
	 * Check the MVC independent components configuration files
	 * 
	 * @param mvc
	 * @return
	 */
public static String checkMVCAction(ActionConfigBean mvc, 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();
        if (!"".equals(mvc.getClazz())) {
            try {
                Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(mvc.getClazz());
                if (clazz == null) {
                    sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
                } else {
                    if (mvc.getMethod() != null && !"".equals(mvc.getMethod())) {
                        Method m = new ReflectUtil(clazz.newInstance()).getMethod(mvc.getMethod());
                        if (m == null) {
                            sb.append("当前您填写的( method=").append(mvc.getMethod()).append(" )是错误的!它必须是一个有效的方法 ;\n");
                        }
                    }
                }
            } catch (ClassNotFoundException e) {
                sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
            } catch (InstantiationException e) {
                sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个提供无参构造方法的类 ;\n");
            } catch (IllegalAccessException e) {
                sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
            }
        }
        if (!"".equals(sb.toString())) {
            error = "\n<br /><b>" + xmlFile + ":[bean name=" + mvc.getUriMapping() + "]</b>\n" + sb.toString();
        }
    }
    return error;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) 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) Method(java.lang.reflect.Method)

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