org.omnifaces.persistence.datasource.SwitchableCommonDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of omnipersistence Show documentation
Show all versions of omnipersistence Show documentation
Utilities for JPA, JDBC and DataSources
package org.omnifaces.persistence.datasource;
import static org.omnifaces.utils.properties.PropertiesUtils.loadPropertiesFromClasspath;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import javax.sql.CommonDataSource;
public class SwitchableCommonDataSource extends CommonDataSourceWrapper {
private boolean init;
private String configFile;
private Map tempValues = new HashMap<>();
@Override
public void set(String name, Object value) {
if (init) {
super.set(name, value);
} else {
tempValues.put(name, value);
}
}
@SuppressWarnings("unchecked")
@Override
public T get(String name) {
if (init) {
return super.get(name);
} else {
return (T) tempValues.get(name);
}
}
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
// Nasty, but there's not an @PostConstruct equivalent on a DataSource that's called
// when all properties have been set.
doInit();
}
public void doInit() {
// Get the properties that were defined separately from the @DataSourceDefinition/data-source element
ServiceLoader loader = ServiceLoader.load(PropertiesFileLoader.class);
if (!loader.iterator().hasNext()) {
loader = ServiceLoader.load(PropertiesFileLoader.class, SwitchableCommonDataSource.class.getClassLoader());
}
Map properties = new HashMap<>();
if (!loader.iterator().hasNext()) {
// No service loader was specified for loading the configfile.
// Try the fallback default location of META-INF on the classpath
properties.putAll(loadPropertiesFromClasspath("META-INF/" + configFile));
} else {
for (PropertiesFileLoader propertiesFileLoader : loader) {
properties.putAll(propertiesFileLoader.loadFromFile(configFile));
}
}
// Get & check the most important property; the class name of the data source that we wrap.
String className = properties.get("className");
if (className == null) {
throw new IllegalStateException("Required parameter 'className' missing.");
}
initDataSource(newInstance(className));
// Set the properties on the wrapped data source that were already set on this class before doInit()
// was possible.
for (Entry property : tempValues.entrySet()) {
super.set(property.getKey(), property.getValue());
}
// Set the properties on the wrapped data source that were loaded from the external file.
for (Entry property : properties.entrySet()) {
if (!property.getKey().equals("className")) {
setWithConversion(property.getKey(), property.getValue());
}
}
// After this properties will be set directly on the wrapped data source instance.
init = true;
}
private CommonDataSource newInstance(String className) {
try {
return (CommonDataSource) Class.forName(className).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy