0

I have been working on spring/hibernate application, I have @Configuration class that read a property value from a file, then loads a client name from db and returns a datasource for the client.

What I'm looking to do is, get client parameter from the url instead of supplying -DclientName when firing up jetty.

so that it can take the parameter from url and get the url from db and create and set another working datasource to current application.

I'm quite confused at the moment, accept my apologies if you don't understand my question you may ask for another explanation please.

Any reply/help would be much appreciated. Thanks

@Configuration
@EnableTransactionManagement
@ImportResource("classpath:applicationContext.xml")
public class PersistenceConfig {

@Value("${primaryWebDomain}")
private String primaryWebDomain = null;

@Value("${db.driver}")
private String DB_DRIVER = null;

@Value("${db.url}")
private String DB_CONFIG_URL = null;

@Value("${db.user}")
private String DB_USER = null;

@Value("${db.password}")
private String DB_PASSWORD = null;

@Value("${db.client.base.url}")
private String DB_CLIENT_BASE_URL = null;

private String artworkFilePath = null;
private String dataBaseName = null;

@Bean
public ComboPooledDataSource dataSource() throws PropertyVetoException, SQLException {

    initConfigProps();

    ComboPooledDataSource dataSource = new ComboPooledDataSource();

    dataSource.setDriverClass(DB_DRIVER);
    dataSource.setJdbcUrl(DB_CLIENT_BASE_URL + dataBaseName);
    dataSource.setUser(DB_USER);
    dataSource.setPassword(DB_PASSWORD);

    dataSource.setAcquireIncrement(1);
    dataSource.setMinPoolSize(2);
    dataSource.setMaxPoolSize(5);
    dataSource.setMaxIdleTime(300);

    return dataSource;
}

@Bean
public String artworkFilePath() throws SQLException {
    if (artworkFilePath == null) 
        initConfigProps();

    if (System.getProperty("os.name").startsWith("Windows")) 
        return this.artworkFilePath + File.separatorChar + "originals";

    String assetPath = this.artworkFilePath.substring(1).replace("\\".charAt(0), File.separatorChar) + File.separatorChar + "originals";

    return assetPath;
}

@Bean
public String brandworkzApplicationURL() throws SQLException {
    if (primaryWebDomain == null) 
        initConfigProps();

    StringBuilder appUrl = new StringBuilder().append("http://").append(primaryWebDomain).append("/BMS/category/browse.cfm");

    return appUrl.toString();
}

private void initConfigProps() throws SQLException {

    Connection dbConnection = null;
    Statement preparedStatement = null;
    String selectStatement = "SELECT artworkfilepath, databasename FROM bms_applicationVariables WHERE primaryWebDomain ='" + primaryWebDomain + "'";

    try {

        Class.forName(DB_DRIVER);
        dbConnection = DriverManager.getConnection(DB_CONFIG_URL, DB_USER, DB_PASSWORD);
        preparedStatement = dbConnection.createStatement();
        ResultSet rs = preparedStatement.executeQuery(selectStatement);

        while (rs.next()) {
            this.artworkFilePath = rs.getString("artworkfilepath");
            this.dataBaseName = rs.getString("databasename");
        }

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        dbConnection.close();
    }

  }
}
Sayed Ali
  • 71
  • 1
  • 4

1 Answers1

0

As you want to use a different DataSource based on some dynamic action; you probably want to use AbstractRoutingDataSource

Look at this thread for details

Community
  • 1
  • 1
mhan
  • 373
  • 3
  • 11