Skip to content

Commit

Permalink
springboot/helper: added SpringDataSourceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrugz committed Dec 3, 2024
1 parent 6d40480 commit 41df86e
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tbrugz.queryon.springboot;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import tbrugz.sqldump.util.DataSourceProvider;

@Configuration
public class SpringDataSourceProvider implements DataSourceProvider, ApplicationContextAware {

private static final Log log = LogFactory.getLog(SpringDataSourceProvider.class);

static ApplicationContext ctx;

public SpringDataSourceProvider() {
log.debug("SpringDataSourceProvider()");
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
log.info("setApplicationContext(), ctx = "+applicationContext);
ctx = applicationContext;
}

@Override
public DataSource getDataSource(String name) {
if(ctx == null) {
String message = "getDataSource: ApplicationContext is null ( make sure to @Import(SpringDataSourceProvider.class) )";
log.error(message);
throw new IllegalStateException(message);
}
if(name==null || name.isEmpty()) {
log.info("getDataSource (empty name)");
return ctx.getBean(DataSource.class);
}
log.info("getDataSource: name = "+name);
return ctx.getBean(name, DataSource.class);
}

}

0 comments on commit 41df86e

Please sign in to comment.