Search in sources :

Example 6 with DBInfoConfigBean

use of org.eweb4j.orm.dao.config.bean.DBInfoConfigBean in project eweb4j-framework by laiweiwei.

the class DAOFactory method getDivPageDAO.

/**
	 * 创建DivPageDAOImpl实例
	 * 
	 * @param dsName
	 *            数据库配置信息Bean的名字,默认下在eweb4j-dbInfo-config.xml文件中<name></name>
	 *            找到这个值
	 * @return
	 */
public static DivPageDAO getDivPageDAO(String dsName) {
    DataSource ds = DataSourceWrapCache.get(dsName);
    DBInfoConfigBean dcb = DBInfoConfigBeanCache.get(dsName);
    return new DivPageDAOImpl(ds, dcb.getDataBaseType());
}
Also used : DivPageDAOImpl(org.eweb4j.orm.dao.select.DivPageDAOImpl) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) DataSource(javax.sql.DataSource)

Example 7 with DBInfoConfigBean

use of org.eweb4j.orm.dao.config.bean.DBInfoConfigBean in project eweb4j-framework by laiweiwei.

the class DAOFactory method getInsertDAO.

public static InsertDAO getInsertDAO() {
    DataSource ds = DataSourceWrapCache.get();
    DBInfoConfigBean dcb = DBInfoConfigBeanCache.get();
    return new InsertDAOImpl(ds, dcb.getDataBaseType());
}
Also used : InsertDAOImpl(org.eweb4j.orm.dao.insert.InsertDAOImpl) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) DataSource(javax.sql.DataSource)

Example 8 with DBInfoConfigBean

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

use of org.eweb4j.orm.dao.config.bean.DBInfoConfigBean 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 DBInfoConfigBean

use of org.eweb4j.orm.dao.config.bean.DBInfoConfigBean in project eweb4j-framework by laiweiwei.

the class DAOFactory method getInsertDAO.

/**
	 * 创建InsertDAOImpl实例
	 * 
	 * @param dsName
	 *            数据库配置信息Bean的名字,默认下在eweb4j-dbInfo-config.xml文件中<name></name>
	 *            找到这个值
	 * @return
	 */
public static InsertDAO getInsertDAO(String dsName) {
    DataSource ds = DataSourceWrapCache.get(dsName);
    DBInfoConfigBean dcb = DBInfoConfigBeanCache.get(dsName);
    return new InsertDAOImpl(ds, dcb.getDataBaseType());
}
Also used : InsertDAOImpl(org.eweb4j.orm.dao.insert.InsertDAOImpl) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) DataSource(javax.sql.DataSource)

Aggregations

DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)13 DataSource (javax.sql.DataSource)9 ConfigBean (org.eweb4j.config.bean.ConfigBean)4 File (java.io.File)2 Property (org.eweb4j.orm.dao.config.bean.Property)2 InsertDAOImpl (org.eweb4j.orm.dao.insert.InsertDAOImpl)2 DivPageDAOImpl (org.eweb4j.orm.dao.select.DivPageDAOImpl)2 SearchDAOImpl (org.eweb4j.orm.dao.select.SearchDAOImpl)2 SelectDAOImpl (org.eweb4j.orm.dao.select.SelectDAOImpl)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 CheckConfigBean (org.eweb4j.config.CheckConfigBean)1 DBInfoXmlFiles (org.eweb4j.config.bean.DBInfoXmlFiles)1 ListenerBean (org.eweb4j.config.bean.ListenerBean)1 Listeners (org.eweb4j.config.bean.Listeners)1 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)1