Skip to content

Commit

Permalink
#2718 Mapping path static Resources - refactoring:
Browse files Browse the repository at this point in the history
 - Separation of the method that calls migration from the method that creates DataSource based on the configuration in the env.properties file.
 - Transferring the configuration to applicationContext.xml
  • Loading branch information
Limraj committed Jan 7, 2024
1 parent 29b028b commit be78cfb
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 96 deletions.
7 changes: 3 additions & 4 deletions WebContent/WEB-INF/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@
<property name="locations" value="classpath:env.properties"/>
</bean>

<bean id="databaseSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="expectedType" value="javax.sql.DataSource"/>
<property name="jndiName" value="${db.datasourceName}"/>
</bean>
<bean id="databaseAccess" class="com.serotonin.mango.db.DatabaseAccess" factory-method="createDatabaseAccess" />

<bean id="databaseSource" factory-bean="databaseAccess" factory-method="getDataSource" />

<bean id="mangoContextListener" class="com.serotonin.mango.MangoContextListener" />

Expand Down
6 changes: 3 additions & 3 deletions src/com/serotonin/mango/MangoContextListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 19 additions & 23 deletions src/com/serotonin/mango/db/BasePooledAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,46 +35,40 @@
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
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());
}
}
Expand Down Expand Up @@ -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)));

Expand Down
64 changes: 32 additions & 32 deletions src/com/serotonin/mango/db/DatabaseAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions src/com/serotonin/mango/db/DerbyAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions src/com/serotonin/mango/db/MSSQLAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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");
}
Expand All @@ -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;
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/com/serotonin/mango/db/MySQLAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions src/com/serotonin/mango/db/Oracle11GAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -81,7 +77,7 @@ protected String getDriverClassName() {
}

@Override
protected void createSchema(String scriptFile) {
protected void createSchema(String scriptFile, ServletContext ctx) {

// Create MySql Connection
try {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down
Loading

0 comments on commit be78cfb

Please sign in to comment.