From 4ba5699f323e6cd51e45ca435edd86dc588005d3 Mon Sep 17 00:00:00 2001 From: Tommy Wang Date: Wed, 29 Sep 2010 23:29:35 +0800 Subject: [PATCH] =?utf8?q?=E5=B0=86dbcp=E7=9A=84=E9=85=8D=E7=BD=AE?= =?utf8?q?=E5=B9=B6=E5=85=A5jibu.properties=E4=B8=AD=EF=BC=8C=E9=80=9A?= =?utf8?q?=E8=BF=87JibuConfig=E7=B1=BB=E6=9D=A5=E6=87=92=E5=8A=A0=E8=BD=BD?= =?utf8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- jibu-core/pom.xml | 1 - .../java/org/gaixie/jibu/config/JibuConfig.java | 62 ++++++++++++++++++++++ .../java/org/gaixie/jibu/config/package-info.java | 6 +++ .../org/gaixie/jibu/utils/ConnectionUtils.java | 11 ++-- jibu-core/src/main/resources/dbcp.properties | 13 ----- jibu-core/src/main/resources/jibu.properties | 14 +++++ .../test/java/org/gaixie/jibu/CoreTestSupport.java | 18 ++----- .../jibu/security/servlet/GuiceServletConfig.java | 4 +- 8 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 jibu-core/src/main/java/org/gaixie/jibu/config/JibuConfig.java create mode 100644 jibu-core/src/main/java/org/gaixie/jibu/config/package-info.java delete mode 100644 jibu-core/src/main/resources/dbcp.properties diff --git a/jibu-core/pom.xml b/jibu-core/pom.xml index 7243a17..dd21797 100644 --- a/jibu-core/pom.xml +++ b/jibu-core/pom.xml @@ -39,7 +39,6 @@ dbscripts/** - dbcp.properties jibu.properties logback.xml diff --git a/jibu-core/src/main/java/org/gaixie/jibu/config/JibuConfig.java b/jibu-core/src/main/java/org/gaixie/jibu/config/JibuConfig.java new file mode 100644 index 0000000..6542f1c --- /dev/null +++ b/jibu-core/src/main/java/org/gaixie/jibu/config/JibuConfig.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.gaixie.jibu.config; + +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Jibu 配置,加载 jibu.properties 配置文件。 + *

+ */ +public class JibuConfig { + private static final Logger logger = LoggerFactory.getLogger(JibuConfig.class); + private static String PROPERTIES_FILE = "jibu.properties"; + private static Properties prop; + + static { + prop = new Properties(); + try { + prop.load(JibuConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE)); + logger.info("Successfully loaded "+PROPERTIES_FILE+"."); + } catch (Exception e) { + logger.error("Failed to load file: "+PROPERTIES_FILE+".", e); + } + } + + // 这个类就不用这样实例化了。:-) + private JibuConfig() {} + + /** + * Retrieve a property value + * @param key Name of the property + * @return String Value of property requested, null if not found + */ + public static String getProperty(String key) { + return prop.getProperty(key); + } + + /** + * 得到 jibu 的当前配置。 + * @return Properties + */ + public static Properties getProperties() { + return prop; + } +} \ No newline at end of file diff --git a/jibu-core/src/main/java/org/gaixie/jibu/config/package-info.java b/jibu-core/src/main/java/org/gaixie/jibu/config/package-info.java new file mode 100644 index 0000000..a5b4bf4 --- /dev/null +++ b/jibu-core/src/main/java/org/gaixie/jibu/config/package-info.java @@ -0,0 +1,6 @@ +/** + * 提供 Jibu 加载系统配置使用的类。 + *

+ * + */ +package org.gaixie.jibu.config; \ No newline at end of file diff --git a/jibu-core/src/main/java/org/gaixie/jibu/utils/ConnectionUtils.java b/jibu-core/src/main/java/org/gaixie/jibu/utils/ConnectionUtils.java index 04778ea..c8c33ed 100644 --- a/jibu-core/src/main/java/org/gaixie/jibu/utils/ConnectionUtils.java +++ b/jibu-core/src/main/java/org/gaixie/jibu/utils/ConnectionUtils.java @@ -16,15 +16,14 @@ */ package org.gaixie.jibu.utils; -import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; -import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; +import org.gaixie.jibu.config.JibuConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,16 +33,12 @@ import org.slf4j.LoggerFactory; */ public class ConnectionUtils { private static final Logger logger = LoggerFactory.getLogger(ConnectionUtils.class); - private static String PROPERTIES_FILE = "dbcp.properties"; private static DataSource dataSource = null; static { - Properties prop = new Properties(); try { - prop.load(ConnectionUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE)); - dataSource = BasicDataSourceFactory.createDataSource(prop); - } catch (IOException ioe) { - logger.error("Failed to load file: "+PROPERTIES_FILE, ioe); + dataSource = BasicDataSourceFactory.createDataSource(JibuConfig.getProperties()); + logger.info("Successfully created datasource."); } catch (Exception e) { logger.error("Create DataSource failed.", e); } diff --git a/jibu-core/src/main/resources/dbcp.properties b/jibu-core/src/main/resources/dbcp.properties deleted file mode 100644 index 31d3c93..0000000 --- a/jibu-core/src/main/resources/dbcp.properties +++ /dev/null @@ -1,13 +0,0 @@ -## Derby -driverClassName=org.apache.derby.jdbc.EmbeddedDriver -url=jdbc:derby:memory:jibudb;create=true -username= -password= - -## HSQLDB -#driverClassName=org.hsqldb.jdbcDriver -#url=jdbc:hsqldb:mem:jibudb -#username=sa -#password= - -defaultAutoCommit=false diff --git a/jibu-core/src/main/resources/jibu.properties b/jibu-core/src/main/resources/jibu.properties index a1146fd..360f0a8 100644 --- a/jibu-core/src/main/resources/jibu.properties +++ b/jibu-core/src/main/resources/jibu.properties @@ -1,2 +1,16 @@ ## Derby , PostgreSQL databaseType = Derby + +## Derby +driverClassName=org.apache.derby.jdbc.EmbeddedDriver +url=jdbc:derby:memory:jibudb;create=true +username= +password= + +## HSQLDB +#driverClassName=org.hsqldb.jdbcDriver +#url=jdbc:hsqldb:mem:jibudb +#username=sa +#password= + +defaultAutoCommit=false diff --git a/jibu-core/src/test/java/org/gaixie/jibu/CoreTestSupport.java b/jibu-core/src/test/java/org/gaixie/jibu/CoreTestSupport.java index 3ba7f6b..9912468 100644 --- a/jibu-core/src/test/java/org/gaixie/jibu/CoreTestSupport.java +++ b/jibu-core/src/test/java/org/gaixie/jibu/CoreTestSupport.java @@ -19,9 +19,7 @@ package org.gaixie.jibu; import com.google.inject.Guice; import com.google.inject.Injector; -import java.io.IOException; -import java.util.Properties; - +import org.gaixie.jibu.config.JibuConfig; import org.gaixie.jibu.security.dao.SecurityDAOModule; import org.gaixie.jibu.security.service.SecurityServiceModule; @@ -32,25 +30,15 @@ import org.gaixie.jibu.security.service.SecurityServiceModule; * 测试多种类型的数据库。 */ public class CoreTestSupport { - private static String PROPERTIES_FILE = "jibu.properties"; - private String databaseType = "Derby"; private static Injector injector; public synchronized Injector getInjector() { if(injector != null){ return injector; } - Properties prop = new Properties(); - try { - prop.load(CoreTestSupport.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE)); - if(prop.containsKey("databaseType")) - databaseType = prop.getProperty("databaseType"); - - injector = Guice.createInjector(new SecurityServiceModule(), + String databaseType = JibuConfig.getProperty("databaseType"); + injector = Guice.createInjector(new SecurityServiceModule(), new SecurityDAOModule(databaseType)); - } catch (IOException ioe) { - System.out.println("Failed to load file: "+PROPERTIES_FILE); - } return injector; } } diff --git a/jibu-web/jibu-core-extjs/src/main/java/org/gaixie/jibu/security/servlet/GuiceServletConfig.java b/jibu-web/jibu-core-extjs/src/main/java/org/gaixie/jibu/security/servlet/GuiceServletConfig.java index 5cb3279..d206168 100644 --- a/jibu-web/jibu-core-extjs/src/main/java/org/gaixie/jibu/security/servlet/GuiceServletConfig.java +++ b/jibu-web/jibu-core-extjs/src/main/java/org/gaixie/jibu/security/servlet/GuiceServletConfig.java @@ -20,6 +20,7 @@ import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.servlet.GuiceServletContextListener; +import org.gaixie.jibu.config.JibuConfig; import org.gaixie.jibu.security.dao.SecurityDAOModule; import org.gaixie.jibu.security.interceptor.SecurityInterceptorModule; import org.gaixie.jibu.security.service.SecurityServiceModule; @@ -50,9 +51,10 @@ public class GuiceServletConfig extends GuiceServletContextListener { * @return 实例化的 Injector */ @Override protected Injector getInjector() { + String databaseType = JibuConfig.getProperty("databaseType"); return Guice.createInjector(new SecurityServletModule(), new SecurityInterceptorModule(), new SecurityServiceModule(), - new SecurityDAOModule()); + new SecurityDAOModule(databaseType)); } } \ No newline at end of file -- 2.11.4.GIT