浏览代码

优化代码

zhouhao 8 年之前
父节点
当前提交
10ff663865

+ 31 - 0
hsweb-web-controller/src/main/resources/META-INF/spring-configuration-metadata.json

@@ -0,0 +1,31 @@
+{
+  "groups": [
+    {
+      "name": "hsweb.access-logger",
+      "type": "java.lang.String",
+      "sourceType": "java.lang.String"
+    }
+  ],
+  "properties": [
+    {
+      "name": "hsweb.access-logger",
+      "type": "java.lang.String",
+      "sourceType": "java.lang.String"
+    }
+  ],
+  "hints": [
+    {
+      "name": "hsweb.access-logger",
+      "values": [
+        {
+          "value": "on",
+          "description": "enable access logger."
+        },
+        {
+          "value": "off",
+          "description": "enable access logger."
+        }
+      ]
+    }
+  ]
+}

+ 19 - 29
hsweb-web-core/src/main/java/org/hsweb/web/core/Install.java

@@ -4,57 +4,39 @@ import org.hsweb.commons.file.FileUtils;
 import org.hsweb.ezorm.executor.SqlExecutor;
 import org.hsweb.ezorm.render.SqlAppender;
 import org.hsweb.ezorm.render.support.simple.SimpleSQL;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.hsweb.web.core.datasource.DataSourceHolder;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.autoconfigure.AutoConfigureAfter;
-import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
-import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import org.springframework.util.Assert;
 
 import javax.annotation.PostConstruct;
-import javax.sql.DataSource;
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.sql.Connection;
 import java.util.ArrayList;
 import java.util.List;
 
 @Configuration
-@EnableConfigurationProperties({DataSourceProperties.class})
-@AutoConfigureAfter({DataSourceAutoConfiguration.class})
-@ConfigurationProperties(
-        prefix = "spring.datasource"
-)
 public class Install {
-    private static String DATABASE_TYPE = "h2";
-
     /**
      * 获取当前数据库类型
      *
      * @return
      */
     public static String getDatabaseType() {
-        return DATABASE_TYPE;
+        return DataSourceHolder.getActiveDatabaseType().name();
     }
 
-    @Autowired
-    private DataSourceProperties properties;
-
     @Autowired
     private SqlExecutor sqlExecutor;
 
-    private Logger logger = LoggerFactory.getLogger(this.getClass());
-
     @PostConstruct
     public void install() throws Exception {
-        String dc = properties.getDriverClassName();
-        String dbType = dc.contains("mysql") ? "mysql" : dc.contains("oracle") ? "oracle" : dc.contains("h2") ? "h2" : null;
-        DATABASE_TYPE = dbType;
+        String dbType = DataSourceHolder.getActiveDatabaseType().name();
         Assert.notNull(dbType, "不支持的数据库类型");
         try {
             boolean firstInstall = false;
@@ -80,14 +62,22 @@ public class Install {
         }
     }
 
-    protected void execInstallSql(InputStream sqlStream) throws UnsupportedEncodingException {
-        String username = properties.getUsername();
+    protected void execInstallSql(InputStream sqlStream) throws Exception {
+        String username = "";
+        Connection connection = null;
+        try {
+            connection = DataSourceHolder.getActiveSource().getConnection();
+            username = connection.getMetaData().getUserName();
+        } finally {
+            if (null != connection) connection.close();
+        }
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sqlStream, "utf-8"));
         List<String> sqlList = new ArrayList<>();
         SqlAppender tmp = new SqlAppender();
+        String uname = username;
         bufferedReader.lines().forEach((line) -> {
             if (line.startsWith("--")) return;
-            line = line.replace("${jdbc.username}", username);
+            line = line.replace("${jdbc.username}", uname);
             //去除sql中的;
             if (line.endsWith(";"))
                 tmp.add(line.substring(0, line.length() - 1));

+ 39 - 3
hsweb-web-core/src/main/java/org/hsweb/web/core/datasource/DynamicDataSource.java

@@ -17,40 +17,76 @@
 package org.hsweb.web.core.datasource;
 
 import org.hsweb.web.core.utils.ThreadLocalUtils;
-import org.springframework.jca.cci.connection.ConnectionHolder;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
 
-import javax.sql.CommonDataSource;
 import javax.sql.DataSource;
 
+/**
+ * 动态数据源接口,此接口实现多数据源的动态切换
+ *
+ * @see DataSourceHolder
+ */
 public interface DynamicDataSource extends DataSource {
     String DATA_SOURCE_FLAG = "data-source-id";
 
     String DATA_SOURCE_FLAG_LAST = "data-source-id-last";
 
+    /**
+     * 使用上一次调用的数据源
+     */
     static void useLast() {
         use(ThreadLocalUtils.get(DATA_SOURCE_FLAG_LAST));
     }
 
+    /**
+     * 选中参数(数据源ID)对应的数据源,如果数据源不存在,将使用默认数据源
+     *
+     * @param dataSourceId 数据源ID
+     */
     static void use(String dataSourceId) {
         ThreadLocalUtils.put(DATA_SOURCE_FLAG, dataSourceId);
     }
 
+    /**
+     * 获取当前使用的数据源ID,如果不存在则返回null
+     *
+     * @return 数据源ID
+     */
     static String getActiveDataSourceId() {
         return ThreadLocalUtils.get(DATA_SOURCE_FLAG);
     }
 
+    /**
+     * 切换为默认数据源,并指定是否记住上一次选中的数据源
+     *
+     * @param rememberLast 是否记住上一次选中的数据源
+     */
     static void useDefault(boolean rememberLast) {
         if (getActiveDataSourceId() != null && rememberLast)
             ThreadLocalUtils.put(DATA_SOURCE_FLAG_LAST, getActiveDataSourceId());
         ThreadLocalUtils.remove(DATA_SOURCE_FLAG);
     }
 
+    /**
+     * 切换为默认数据源并记住上一次使用的数据源
+     *
+     * @see this#useDefault(boolean)
+     */
     static void useDefault() {
         useDefault(true);
     }
 
+    /**
+     * 获取当前激活的数据源
+     *
+     * @return
+     */
     DataSource getActiveDataSource();
 
+    /**
+     * 获取当前激活数据源的数据库类型
+     *
+     * @return 数据库类型
+     * @see DatabaseType
+     */
     DatabaseType getActiveDataBaseType();
 }

+ 0 - 4
hsweb-web-core/src/main/java/org/hsweb/web/core/logger/Slf4jAccessLoggerPersisting.java

@@ -8,10 +8,6 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-/**
- * Created by zhouhao on 16-4-28.
- */
-@Component
 public class Slf4jAccessLoggerPersisting implements AccessLoggerPersisting {
     protected Logger logger = LoggerFactory.getLogger(this.getClass());
     @Autowired(required = false)

+ 0 - 2
hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceProperties.java

@@ -16,7 +16,6 @@
 
 package org.hsweb.web.datasource.dynamic;
 
-import com.atomikos.datasource.pool.ConnectionPool;
 import com.atomikos.jdbc.AtomikosDataSourceBean;
 import org.hsweb.web.core.datasource.DatabaseType;
 import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -26,7 +25,6 @@ import org.springframework.util.Assert;
 import org.springframework.util.ClassUtils;
 import org.springframework.util.StringUtils;
 
-import java.io.PrintWriter;
 import java.sql.SQLException;
 import java.util.LinkedList;
 import java.util.List;

+ 3 - 2
hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicXaDataSourceImpl.java

@@ -33,8 +33,8 @@ import java.sql.Connection;
 import java.sql.SQLException;
 
 public class DynamicXaDataSourceImpl extends AbstractDataSource implements DynamicDataSource, XADataSource, Closeable {
-    private Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);
-    private   javax.sql.DataSource     defaultDataSource;
+    private Logger               logger            = LoggerFactory.getLogger(DynamicDataSource.class);
+    private javax.sql.DataSource defaultDataSource = null;
     private   DatabaseType             defaultDatabaseType;
     protected DynamicDataSourceService dynamicDataSourceService;
 
@@ -60,6 +60,7 @@ public class DynamicXaDataSourceImpl extends AbstractDataSource implements Dynam
         logger.info("use datasource:{}", sourceId == null ? "default" : sourceId);
         if (sourceId == null || dynamicDataSourceService == null) return defaultDataSource;
         DataSource dataSource = dynamicDataSourceService.getDataSource(sourceId);
+        logger.info("use datasource:{} fail,because its not exists! use default datasource now.", sourceId);
         if (dataSource == null) return defaultDataSource;
         return dataSource;
     }