diff --git a/WebContent/WEB-INF/applicationContext.xml b/WebContent/WEB-INF/applicationContext.xml index c51933e551..4bfc559f4c 100644 --- a/WebContent/WEB-INF/applicationContext.xml +++ b/WebContent/WEB-INF/applicationContext.xml @@ -228,10 +228,9 @@ - - - - + + + diff --git a/src/com/serotonin/mango/MangoContextListener.java b/src/com/serotonin/mango/MangoContextListener.java index e265430887..bce8617457 100644 --- a/src/com/serotonin/mango/MangoContextListener.java +++ b/src/com/serotonin/mango/MangoContextListener.java @@ -444,10 +444,10 @@ private void constantsInitialize(ServletContext ctx) { // Database. // private void databaseInitialize(ServletContext ctx) { - DatabaseAccess databaseAccess = DatabaseAccess - .createDatabaseAccess(ctx); + DatabaseAccess databaseAccess = ApplicationBeans + .getBean("databaseAccess", DatabaseAccess.class); ctx.setAttribute(Common.ContextKeys.DATABASE_ACCESS, databaseAccess); - databaseAccess.initialize(); + databaseAccess.initialize(ctx); } private void databaseTerminate(ContextWrapper ctx) { diff --git a/src/com/serotonin/mango/db/BasePooledAccess.java b/src/com/serotonin/mango/db/BasePooledAccess.java index 13a56d73fb..1efa0a9d44 100644 --- a/src/com/serotonin/mango/db/BasePooledAccess.java +++ b/src/com/serotonin/mango/db/BasePooledAccess.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.List; +import javax.naming.InitialContext; +import javax.naming.NamingException; import javax.servlet.ServletContext; import javax.sql.DataSource; @@ -33,22 +35,16 @@ import com.serotonin.ShouldNeverHappenException; import com.serotonin.db.spring.ExtendedJdbcTemplate; import com.serotonin.mango.Common; -import org.scada_lts.web.beans.ApplicationBeans; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; /** * @author Matthew Lohbihler */ abstract public class BasePooledAccess extends DatabaseAccess { - private final Log log = LogFactory.getLog(BasePooledAccess.class); + private final static Log log = LogFactory.getLog(BasePooledAccess.class); protected DataSource dataSource; protected boolean dataSourceFound = false; - public BasePooledAccess(ServletContext ctx) - { - super(ctx); - } @SuppressWarnings("deprecation") @Override @@ -56,23 +52,23 @@ protected void initializeImpl(String propertyPrefix, String dataSourceName) { log.info("Initializing pooled connection manager"); - boolean datasource = Common.getEnvironmentProfile().getBoolean(propertyPrefix + "db.datasource", false); - - if(datasource) { - try { - log.info("Looking for Datasource: " + dataSourceName); - dataSource = ApplicationBeans.getBean("databaseSource", DataSource.class); - if(dataSource == null) { - log.info("Datasource not found! dataSourceName: " + dataSourceName); - } else { - try (Connection conn = dataSource.getConnection()) { - log.info("DataSource meta: " + conn.getMetaData().getDatabaseProductName() + " " + conn.getMetaData().getDatabaseProductVersion()); - dataSourceFound = true; - } + if(Common.getEnvironmentProfile().getString(propertyPrefix + "db.datasource", "false").equals("true")) + { + try + { + log.info("Looking for Datasource: " + Common.getEnvironmentProfile().getString(propertyPrefix + "db.datasourceName")); + dataSource = (DataSource) new InitialContext().lookup(Common.getEnvironmentProfile().getString(propertyPrefix + "db.datasourceName")); + try (Connection conn = dataSource.getConnection()) { + log.info("DataSource meta: " + conn.getMetaData().getDatabaseProductName() + " " + conn.getMetaData().getDatabaseProductVersion()); + dataSourceFound = true; } - } catch(IllegalArgumentException | NoSuchBeanDefinitionException e) { + } + catch(NamingException e) + { log.info("Datasource not found!" + e.getLocalizedMessage()); - } catch(SQLException e) { + } + catch(SQLException e) + { log.error("SQL Exception: " + e.getLocalizedMessage()); } } @@ -126,7 +122,7 @@ public void runScript(String[] script, OutputStream out) } } - protected void createSchema(String scriptFile) + protected void createSchema(String scriptFile, ServletContext ctx) { BufferedReader in = new BufferedReader(new InputStreamReader(ctx.getResourceAsStream(scriptFile))); diff --git a/src/com/serotonin/mango/db/DatabaseAccess.java b/src/com/serotonin/mango/db/DatabaseAccess.java index 7bac36ff90..1b4bb05065 100644 --- a/src/com/serotonin/mango/db/DatabaseAccess.java +++ b/src/com/serotonin/mango/db/DatabaseAccess.java @@ -41,44 +41,44 @@ import com.serotonin.util.StringUtils; abstract public class DatabaseAccess { - private final Log log = LogFactory.getLog(DatabaseAccess.class); + private final static Log log = LogFactory.getLog(DatabaseAccess.class); public enum DatabaseType { DERBY { @Override - DatabaseAccess getImpl(ServletContext ctx) { - return new DerbyAccess(ctx); + DatabaseAccess getImpl() { + return new DerbyAccess(); } }, MSSQL { @Override - DatabaseAccess getImpl(ServletContext ctx) { - return new MSSQLAccess(ctx); + DatabaseAccess getImpl() { + return new MSSQLAccess(); } }, MYSQL { @Override - DatabaseAccess getImpl(ServletContext ctx) { - return new MySQLAccess(ctx); + DatabaseAccess getImpl() { + return new MySQLAccess(); } }, POSTGRES { @Override - DatabaseAccess getImpl(ServletContext ctx) { - return new PostgreSQLAccess(ctx); + DatabaseAccess getImpl() { + return new PostgreSQLAccess(); } }, ORACLE11G { @Override - DatabaseAccess getImpl(ServletContext ctx) { - return new Oracle11GAccess(ctx); + DatabaseAccess getImpl() { + return new Oracle11GAccess(); } }; - abstract DatabaseAccess getImpl(ServletContext ctx); + abstract DatabaseAccess getImpl(); } - public static DatabaseAccess createDatabaseAccess(ServletContext ctx) { + public static DatabaseAccess createDatabaseAccess() { String type = Common.getEnvironmentProfile().getString("db.type", "derby"); @@ -87,35 +87,23 @@ public static DatabaseAccess createDatabaseAccess(ServletContext ctx) { if (dt == null) throw new IllegalArgumentException("Unknown database type: " + type); - return dt.getImpl(ctx); + return dt.getImpl(); } public static DatabaseAccess getDatabaseAccess() { return Common.ctx.getDatabaseAccess(); } - protected final ServletContext ctx; - - protected DatabaseAccess(ServletContext ctx) { - this.ctx = ctx; + protected DatabaseAccess() { + initDataSource(); } - public void initialize() { - if (Common.getEnvironmentProfile().getString("db.datasource", "false") - .equals("true")) { - initializeImpl( - "", - Common.getEnvironmentProfile().getString( - "db.datasourceName")); - } else { - initializeImpl(""); - } - + public void initialize(ServletContext ctx) { ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate(); ejt.setDataSource(getDataSource()); try { - if (newDatabaseCheck(ejt)) { + if (newDatabaseCheck(ejt, ctx)) { // Check if we should convert from another database. String convertTypeStr = null; try { @@ -133,7 +121,7 @@ public void initialize() { throw new IllegalArgumentException( "Unknown convert database type: " + convertType); - DatabaseAccess sourceAccess = convertType.getImpl(ctx); + DatabaseAccess sourceAccess = convertType.getImpl(); sourceAccess.initializeImpl("convert."); DBConvert convert = new DBConvert(); @@ -184,6 +172,18 @@ public void initialize() { postInitialize(ejt); } + private void initDataSource() { + if (Common.getEnvironmentProfile().getString("db.datasource", "false") + .equals("true")) { + initializeImpl( + "", + Common.getEnvironmentProfile().getString( + "db.datasourceName")); + } else { + initializeImpl(""); + } + } + abstract public DatabaseType getType(); abstract public void terminate(); @@ -206,7 +206,7 @@ protected void postInitialize( // no op - override as necessary } - abstract protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt); + abstract protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt, ServletContext ctx); abstract public void runScript(String[] script, final OutputStream out) throws Exception; diff --git a/src/com/serotonin/mango/db/DerbyAccess.java b/src/com/serotonin/mango/db/DerbyAccess.java index d09a12c689..7faf7c125a 100644 --- a/src/com/serotonin/mango/db/DerbyAccess.java +++ b/src/com/serotonin/mango/db/DerbyAccess.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.nio.file.Paths; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; @@ -38,6 +39,8 @@ import org.apache.commons.logging.LogFactory; import org.apache.derby.jdbc.EmbeddedXADataSource40; import org.apache.derby.tools.ij; +import org.scada_lts.utils.PathSecureUtils; +import org.scada_lts.utils.UploadFileUtils; import org.springframework.jdbc.CannotGetJdbcConnectionException; import org.springframework.jdbc.core.CallableStatementCreator; import org.springframework.jdbc.datasource.DataSourceUtils; @@ -58,10 +61,6 @@ public class DerbyAccess extends DatabaseAccess { private EmbeddedXADataSource40 dataSource; - public DerbyAccess(ServletContext ctx) { - super(ctx); - } - @Override public DatabaseType getType() { return DatabaseType.DERBY; @@ -92,7 +91,7 @@ private String getUrl(String propertyPrefix) { String name = Common.getEnvironmentProfile().getString(propertyPrefix + "db.url", "~/../../mangoDB"); if (name.startsWith("~")) - name = ctx.getRealPath(name.substring(1)); + name = UploadFileUtils.getAbsoluteResourcePath(name.substring(1)).toString(); return name; } @@ -123,7 +122,7 @@ public DataSource getDataSource() { } @Override - protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { + protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt, ServletContext ctx) { int count = ejt.queryForInt("select count(1) from sys.systables where tablename='USERS'", 0); if (count == 0) { // The users table wasn't found, so assume that this is a new Mango instance. diff --git a/src/com/serotonin/mango/db/MSSQLAccess.java b/src/com/serotonin/mango/db/MSSQLAccess.java index 93fba4fce9..b13fc7e2da 100644 --- a/src/com/serotonin/mango/db/MSSQLAccess.java +++ b/src/com/serotonin/mango/db/MSSQLAccess.java @@ -27,9 +27,6 @@ import com.serotonin.db.spring.ExtendedJdbcTemplate; public class MSSQLAccess extends BasePooledAccess { - public MSSQLAccess(ServletContext ctx) { - super(ctx); - } @Override public DatabaseType getType() { @@ -42,7 +39,7 @@ protected String getDriverClassName() { } @Override - protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { + protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt, ServletContext ctx) { try { ejt.execute("select count(*) from users"); } @@ -51,7 +48,7 @@ protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { SQLException se = (SQLException) e.getCause(); if ("S0002".equals(se.getSQLState())) { // This state means a missing table. Assume that the schema needs to be created. - createSchema("/WEB-INF/db/createTables-mssql.sql"); + createSchema("/WEB-INF/db/createTables-mssql.sql", ctx); return true; } } diff --git a/src/com/serotonin/mango/db/MySQLAccess.java b/src/com/serotonin/mango/db/MySQLAccess.java index 8c0b331385..f2e288f263 100644 --- a/src/com/serotonin/mango/db/MySQLAccess.java +++ b/src/com/serotonin/mango/db/MySQLAccess.java @@ -32,10 +32,6 @@ public class MySQLAccess extends BasePooledAccess { private static final Log LOG = LogFactory.getLog(MySQLAccess.class); - - public MySQLAccess(ServletContext ctx) { - super(ctx); - } @Override protected void initializeImpl(String propertyPrefix) { @@ -75,7 +71,7 @@ protected String getDriverClassName() { } @Override - protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { + protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt, ServletContext ctx) { // To test old shema /*try { diff --git a/src/com/serotonin/mango/db/Oracle11GAccess.java b/src/com/serotonin/mango/db/Oracle11GAccess.java index 4574f92f2c..db4d4d93cf 100644 --- a/src/com/serotonin/mango/db/Oracle11GAccess.java +++ b/src/com/serotonin/mango/db/Oracle11GAccess.java @@ -51,10 +51,6 @@ public int getCode() { private static final String ORACLE_DRIVER_CLASS = "oracle.jdbc.OracleDriver"; private final Log log = LogFactory.getLog(Oracle11GAccess.class); - public Oracle11GAccess(ServletContext ctx) { - super(ctx); - } - @Override protected void initializeImpl(String propertyPrefix) { super.initializeImpl(propertyPrefix); @@ -81,7 +77,7 @@ protected String getDriverClassName() { } @Override - protected void createSchema(String scriptFile) { + protected void createSchema(String scriptFile, ServletContext ctx) { // Create MySql Connection try { @@ -112,7 +108,7 @@ protected void createSchema(String scriptFile) { } @Override - protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { + protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt, ServletContext ctx) { try { ejt.execute("select count(*) from users"); } catch (DataAccessException e) { @@ -123,7 +119,7 @@ protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { .getCode()) { // This state means a missing table. Assume that the schema // needs to be created. - createSchema(CREATE_SCHEMA_SCRIPT_PATH); + createSchema(CREATE_SCHEMA_SCRIPT_PATH, ctx); return true; } } diff --git a/src/com/serotonin/mango/db/PostgreSQLAccess.java b/src/com/serotonin/mango/db/PostgreSQLAccess.java index 1015c206b0..9d87632a5c 100644 --- a/src/com/serotonin/mango/db/PostgreSQLAccess.java +++ b/src/com/serotonin/mango/db/PostgreSQLAccess.java @@ -34,10 +34,6 @@ import com.serotonin.mango.Common; public class PostgreSQLAccess extends BasePooledAccess { - public PostgreSQLAccess(ServletContext ctx) { - super(ctx); - } - @Override protected void initializeImpl(String propertyPrefix) { super.initializeImpl(propertyPrefix); @@ -76,7 +72,7 @@ protected String getDriverClassName() { } @Override - protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { + protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt, ServletContext ctx) { try { //ejt.execute("select count(*) from users"); Class.forName(getDriverClassName()); @@ -97,7 +93,7 @@ protected boolean newDatabaseCheck(ExtendedJdbcTemplate ejt) { conn = DriverManager.getConnection(Common.getEnvironmentProfile().getString("db.url"), Common.getEnvironmentProfile().getString("db.username"), Common.getEnvironmentProfile().getString("db.password")); - createSchema("/WEB-INF/db/createTables-postgresql.sql"); + createSchema("/WEB-INF/db/createTables-postgresql.sql", ctx); conn.close(); return true; } diff --git a/src/org/scada_lts/utils/UploadFileUtils.java b/src/org/scada_lts/utils/UploadFileUtils.java index a6915a031f..ae4b9df247 100644 --- a/src/org/scada_lts/utils/UploadFileUtils.java +++ b/src/org/scada_lts/utils/UploadFileUtils.java @@ -362,7 +362,7 @@ else if (graphic.isDynamicImage()) } } - private static Path getAbsoluteResourcePath(String path) { + public static Path getAbsoluteResourcePath(String path) { Path normalizedPath = normalizePath(path); if (!path.equals(normalizedPath.toString())) { Path basePath = basePath(); diff --git a/test/br/org/scadabr/db/AbstractMySQLDependentTest.java b/test/br/org/scadabr/db/AbstractMySQLDependentTest.java index 6420f14fba..e512e271a6 100644 --- a/test/br/org/scadabr/db/AbstractMySQLDependentTest.java +++ b/test/br/org/scadabr/db/AbstractMySQLDependentTest.java @@ -18,7 +18,7 @@ public abstract class AbstractMySQLDependentTest extends @Before public void createMySQLAccess() throws SQLException { - mysqlAccess = new MySQLAccess(this.getServletContextStub()); + mysqlAccess = new MySQLAccess(); this.putAttributeInServletContext(Common.ContextKeys.DATABASE_ACCESS, mysqlAccess); } @@ -33,12 +33,12 @@ public void shutdownIfNecessary() { protected final void useScenario(DatabaseScenario scenario) { boolean requireInitializationAfterSetup = scenario instanceof RequireInitializationAfterSetup; if (!requireInitializationAfterSetup) { - mysqlAccess.initialize(); + mysqlAccess.initialize(this.getServletContextStub()); } scenario.setupScenario(mysqlAccess); if (requireInitializationAfterSetup) { - mysqlAccess.initialize(); + mysqlAccess.initialize(this.getServletContextStub()); } } diff --git a/test/com/serotonin/mango/db/MySQLAccessTest.java b/test/com/serotonin/mango/db/MySQLAccessTest.java index 187d0daaab..3402bd7152 100644 --- a/test/com/serotonin/mango/db/MySQLAccessTest.java +++ b/test/com/serotonin/mango/db/MySQLAccessTest.java @@ -90,7 +90,7 @@ public void afterInitializeWithPopulatedDatabaseShouldNotOverrideData() { user.setHomeUrl("My home URL"); userDao.saveUser(user); - mysqlAccess.initialize(); + mysqlAccess.initialize(this.getServletContextStub()); User userAfterSecondInitialization = userDao.getUser("admin"); assertEquals("My home URL", userAfterSecondInitialization.getHomeUrl());