/src/main/java/org/cloudfoundry/samples/music/config/SpringApplicationContextInitializer.java

https://gitlab.com/cgshome/spring-music · Java · 127 lines · 101 code · 26 blank · 0 comment · 15 complexity · 4026ce143756de6c5ad2993a7fd47068 MD5 · raw file

  1. package org.cloudfoundry.samples.music.config;
  2. import org.apache.commons.logging.Log;
  3. import org.apache.commons.logging.LogFactory;
  4. import org.springframework.cloud.Cloud;
  5. import org.springframework.cloud.CloudException;
  6. import org.springframework.cloud.CloudFactory;
  7. import org.springframework.cloud.service.ServiceInfo;
  8. import org.springframework.cloud.service.common.MongoServiceInfo;
  9. import org.springframework.cloud.service.common.MysqlServiceInfo;
  10. import org.springframework.cloud.service.common.OracleServiceInfo;
  11. import org.springframework.cloud.service.common.PostgresqlServiceInfo;
  12. import org.springframework.cloud.service.common.RedisServiceInfo;
  13. import org.springframework.context.ApplicationContextInitializer;
  14. import org.springframework.core.env.ConfigurableEnvironment;
  15. import org.springframework.util.StringUtils;
  16. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  17. import java.util.*;
  18. public class SpringApplicationContextInitializer implements ApplicationContextInitializer<AnnotationConfigWebApplicationContext> {
  19. private static final Log logger = LogFactory.getLog(SpringApplicationContextInitializer.class);
  20. private static final Map<Class<? extends ServiceInfo>, String> serviceTypeToProfileName =
  21. new HashMap<Class<? extends ServiceInfo>, String>();
  22. private static final List<String> validLocalProfiles = Arrays.asList("mysql", "postgres", "mongodb", "redis");
  23. public static final String IN_MEMORY_PROFILE = "in-memory";
  24. static {
  25. serviceTypeToProfileName.put(MongoServiceInfo.class, "mongodb");
  26. serviceTypeToProfileName.put(PostgresqlServiceInfo.class, "postgres");
  27. serviceTypeToProfileName.put(MysqlServiceInfo.class, "mysql");
  28. serviceTypeToProfileName.put(RedisServiceInfo.class, "redis");
  29. serviceTypeToProfileName.put(OracleServiceInfo.class, "oracle");
  30. }
  31. @Override
  32. public void initialize(AnnotationConfigWebApplicationContext applicationContext) {
  33. Cloud cloud = getCloud();
  34. ConfigurableEnvironment appEnvironment = applicationContext.getEnvironment();
  35. String[] persistenceProfiles = getCloudProfile(cloud);
  36. if (persistenceProfiles == null) {
  37. persistenceProfiles = getActiveProfile(appEnvironment);
  38. }
  39. if (persistenceProfiles == null) {
  40. persistenceProfiles = new String[] { IN_MEMORY_PROFILE };
  41. }
  42. for (String persistenceProfile : persistenceProfiles) {
  43. appEnvironment.addActiveProfile(persistenceProfile);
  44. }
  45. }
  46. public String[] getCloudProfile(Cloud cloud) {
  47. if (cloud == null) {
  48. return null;
  49. }
  50. List<String> profiles = new ArrayList<String>();
  51. List<ServiceInfo> serviceInfos = cloud.getServiceInfos();
  52. logger.info("Found serviceInfos: " + StringUtils.collectionToCommaDelimitedString(serviceInfos));
  53. for (ServiceInfo serviceInfo : serviceInfos) {
  54. if (serviceTypeToProfileName.containsKey(serviceInfo.getClass())) {
  55. profiles.add(serviceTypeToProfileName.get(serviceInfo.getClass()));
  56. }
  57. }
  58. if (profiles.size() > 1) {
  59. throw new IllegalStateException(
  60. "Only one service of the following types may be bound to this application: " +
  61. serviceTypeToProfileName.values().toString() + ". " +
  62. "These services are bound to the application: [" +
  63. StringUtils.collectionToCommaDelimitedString(profiles) + "]");
  64. }
  65. if (profiles.size() > 0) {
  66. return createProfileNames(profiles.get(0), "cloud");
  67. }
  68. return null;
  69. }
  70. private Cloud getCloud() {
  71. try {
  72. CloudFactory cloudFactory = new CloudFactory();
  73. return cloudFactory.getCloud();
  74. } catch (CloudException ce) {
  75. return null;
  76. }
  77. }
  78. private String[] getActiveProfile(ConfigurableEnvironment appEnvironment) {
  79. List<String> serviceProfiles = new ArrayList<String>();
  80. for (String profile : appEnvironment.getActiveProfiles()) {
  81. if (validLocalProfiles.contains(profile)) {
  82. serviceProfiles.add(profile);
  83. }
  84. }
  85. if (serviceProfiles.size() > 1) {
  86. throw new IllegalStateException("Only one active Spring profile may be set among the following: " +
  87. validLocalProfiles.toString() + ". " +
  88. "These profiles are active: [" +
  89. StringUtils.collectionToCommaDelimitedString(serviceProfiles) + "]");
  90. }
  91. if (serviceProfiles.size() > 0) {
  92. return createProfileNames(serviceProfiles.get(0), "local");
  93. }
  94. return null;
  95. }
  96. private String[] createProfileNames(String baseName, String suffix) {
  97. String[] profileNames = {baseName, baseName + "-" + suffix};
  98. logger.info("Setting profile names: " + StringUtils.arrayToCommaDelimitedString(profileNames));
  99. return profileNames;
  100. }
  101. }