From 9df2660d1e3a4a40a1a4e5b732bceaa441c95be7 Mon Sep 17 00:00:00 2001 From: Andrew DeMaria Date: Sat, 14 Jan 2017 07:57:52 -0700 Subject: [PATCH] Allow data source config type to be specified via system property Signed-off-by: Andrew DeMaria --- .../libresonic/player/boot/Application.java | 4 +- .../AdditionalPropertySourceConfigurer.java | 22 ---------- ...asourceProfileActivatorPropertySource.java | 40 ----------------- .../LibresonicPropertySourceConfigurer.java | 44 +++++++++++++++++++ 4 files changed, 46 insertions(+), 64 deletions(-) delete mode 100644 libresonic-main/src/main/java/org/libresonic/player/spring/AdditionalPropertySourceConfigurer.java delete mode 100644 libresonic-main/src/main/java/org/libresonic/player/spring/DatasourceProfileActivatorPropertySource.java create mode 100644 libresonic-main/src/main/java/org/libresonic/player/spring/LibresonicPropertySourceConfigurer.java diff --git a/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java b/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java index bd84a820..8d1fa8eb 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java +++ b/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java @@ -3,7 +3,7 @@ package org.libresonic.player.boot; import net.sf.ehcache.constructs.web.ShutdownListener; import org.directwebremoting.servlet.DwrServlet; 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.jdbc.DataSourceAutoConfiguration; 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 // Since our example is itself a @Configuration class (via @SpringBootApplication) // 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 diff --git a/libresonic-main/src/main/java/org/libresonic/player/spring/AdditionalPropertySourceConfigurer.java b/libresonic-main/src/main/java/org/libresonic/player/spring/AdditionalPropertySourceConfigurer.java deleted file mode 100644 index fff66516..00000000 --- a/libresonic-main/src/main/java/org/libresonic/player/spring/AdditionalPropertySourceConfigurer.java +++ /dev/null @@ -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 { - - 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); - } -} diff --git a/libresonic-main/src/main/java/org/libresonic/player/spring/DatasourceProfileActivatorPropertySource.java b/libresonic-main/src/main/java/org/libresonic/player/spring/DatasourceProfileActivatorPropertySource.java deleted file mode 100644 index cbfff912..00000000 --- a/libresonic-main/src/main/java/org/libresonic/player/spring/DatasourceProfileActivatorPropertySource.java +++ /dev/null @@ -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); - } - } -} diff --git a/libresonic-main/src/main/java/org/libresonic/player/spring/LibresonicPropertySourceConfigurer.java b/libresonic-main/src/main/java/org/libresonic/player/spring/LibresonicPropertySourceConfigurer.java new file mode 100644 index 00000000..1798dd0d --- /dev/null +++ b/libresonic-main/src/main/java/org/libresonic/player/spring/LibresonicPropertySourceConfigurer.java @@ -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 { + + 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 existingProfiles = Lists.newArrayList(ctx.getEnvironment().getActiveProfiles()); + existingProfiles.add(dataSourceTypeProfile); + ctx.getEnvironment().setActiveProfiles(existingProfiles.toArray(new String[0])); + } +}