Allow data source config type to be specified via system property

Signed-off-by: Andrew DeMaria <lostonamountain@gmail.com>
master
Andrew DeMaria 8 years ago
parent aa8ab6e5fe
commit 9df2660d1e
No known key found for this signature in database
GPG Key ID: 0A3F5E91F8364EDF
  1. 4
      libresonic-main/src/main/java/org/libresonic/player/boot/Application.java
  2. 22
      libresonic-main/src/main/java/org/libresonic/player/spring/AdditionalPropertySourceConfigurer.java
  3. 40
      libresonic-main/src/main/java/org/libresonic/player/spring/DatasourceProfileActivatorPropertySource.java
  4. 44
      libresonic-main/src/main/java/org/libresonic/player/spring/LibresonicPropertySourceConfigurer.java

@ -3,7 +3,7 @@ package org.libresonic.player.boot;
import net.sf.ehcache.constructs.web.ShutdownListener; import net.sf.ehcache.constructs.web.ShutdownListener;
import org.directwebremoting.servlet.DwrServlet; import org.directwebremoting.servlet.DwrServlet;
import org.libresonic.player.filter.*; import org.libresonic.player.filter.*;
import org.libresonic.player.spring.AdditionalPropertySourceConfigurer; import org.libresonic.player.spring.LibresonicPropertySourceConfigurer;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
@ -155,7 +155,7 @@ public class Application extends SpringBootServletInitializer {
// Customize the application or call application.sources(...) to add sources // Customize the application or call application.sources(...) to add sources
// Since our example is itself a @Configuration class (via @SpringBootApplication) // Since our example is itself a @Configuration class (via @SpringBootApplication)
// we actually don't need to override this method. // we actually don't need to override this method.
return application.sources(Application.class).web(true).initializers(new AdditionalPropertySourceConfigurer()); return application.sources(Application.class).web(true).initializers(new LibresonicPropertySourceConfigurer());
} }
@Override @Override

@ -1,22 +0,0 @@
package org.libresonic.player.spring;
import org.apache.commons.configuration2.ImmutableConfiguration;
import org.libresonic.player.service.ApacheCommonsConfigurationService;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.env.PropertySource;
import org.springframework.web.context.ConfigurableWebApplicationContext;
public class AdditionalPropertySourceConfigurer implements
ApplicationContextInitializer<ConfigurableWebApplicationContext> {
public void initialize(ConfigurableWebApplicationContext ctx) {
ApacheCommonsConfigurationService configurationService = new ApacheCommonsConfigurationService();
ImmutableConfiguration snapshot = configurationService.getImmutableSnapshot();
PropertySource ps = new DatasourceProfileActivatorPropertySource(new CommonsConfigurationPropertySource(
"libresonic-pre-init-configs",
snapshot));
ctx.getEnvironment().getPropertySources().addLast(ps);
}
}

@ -1,40 +0,0 @@
package org.libresonic.player.spring;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.PropertySource;
public class DatasourceProfileActivatorPropertySource extends PropertySource {
public static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";
public static final String DATASOURCE_CONFIG_TYPE = "database.config.type";
final PropertySource parent;
public DatasourceProfileActivatorPropertySource(PropertySource parent) {
super(parent.getName());
this.parent = parent;
}
@Override
public Object getProperty(String name) {
if(StringUtils.equalsIgnoreCase(name, SPRING_PROFILES_ACTIVE)) {
String appendTo = "";
Object existing = parent.getProperty(SPRING_PROFILES_ACTIVE);
if(existing != null && existing instanceof String) {
appendTo += (String) existing;
}
DataSourceConfigType dataSourceConfigType;
Object rawType = parent.getProperty(DATASOURCE_CONFIG_TYPE);
if(rawType != null && rawType instanceof String) {
dataSourceConfigType = DataSourceConfigType.valueOf(StringUtils.upperCase((String) rawType));
} else {
dataSourceConfigType = DataSourceConfigType.LEGACY;
}
if(StringUtils.isNotBlank(appendTo)) {
appendTo += ",";
}
appendTo += StringUtils.lowerCase(dataSourceConfigType.name());
return appendTo;
} else {
return parent.getProperty(name);
}
}
}

@ -0,0 +1,44 @@
package org.libresonic.player.spring;
import com.google.common.collect.Lists;
import org.apache.commons.configuration2.ImmutableConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.libresonic.player.service.ApacheCommonsConfigurationService;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.env.PropertySource;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import java.util.List;
public class LibresonicPropertySourceConfigurer implements
ApplicationContextInitializer<ConfigurableWebApplicationContext> {
public static final String DATASOURCE_CONFIG_TYPE = "database.config.type";
public void initialize(ConfigurableWebApplicationContext ctx) {
ApacheCommonsConfigurationService configurationService = new ApacheCommonsConfigurationService();
ImmutableConfiguration snapshot = configurationService.getImmutableSnapshot();
PropertySource ps = new CommonsConfigurationPropertySource("libresonic-pre-init-configs", snapshot);
ctx.getEnvironment().getPropertySources().addLast(ps);
addDataSourceProfile(ctx);
}
private void addDataSourceProfile(ConfigurableWebApplicationContext ctx) {
DataSourceConfigType dataSourceConfigType;
String rawType = ctx.getEnvironment().getProperty(DATASOURCE_CONFIG_TYPE);
if(StringUtils.isNotBlank(rawType)) {
dataSourceConfigType = DataSourceConfigType.valueOf(StringUtils.upperCase(rawType));
} else {
dataSourceConfigType = DataSourceConfigType.LEGACY;
}
String dataSourceTypeProfile = StringUtils.lowerCase(dataSourceConfigType.name());
List<String> existingProfiles = Lists.newArrayList(ctx.getEnvironment().getActiveProfiles());
existingProfiles.add(dataSourceTypeProfile);
ctx.getEnvironment().setActiveProfiles(existingProfiles.toArray(new String[0]));
}
}
Loading…
Cancel
Save