/* * Copyright (c) 2016, Gluon * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Gluon, any associated website, nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.gluonhq.cloudlink.sample.whiteboard.mobile.config; import org.springframework.cloud.Cloud; import org.springframework.cloud.CloudException; import org.springframework.cloud.CloudFactory; import org.springframework.cloud.service.ServiceInfo; import org.springframework.cloud.service.common.MysqlServiceInfo; import org.springframework.cloud.service.common.OracleServiceInfo; import org.springframework.cloud.service.common.PostgresqlServiceInfo; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; public class ContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { private static final Logger LOGGER = Logger.getLogger(ContextInitializer.class.getName()); private static final String IN_MEMORY_PROFILE = "in-memory"; private static final Map<Class<? extends ServiceInfo>, String> serviceTypeToProfileName = new HashMap<>(); static { serviceTypeToProfileName.put(PostgresqlServiceInfo.class, "postgres"); serviceTypeToProfileName.put(MysqlServiceInfo.class, "mysql"); serviceTypeToProfileName.put(OracleServiceInfo.class, "oracle"); } @Override public void initialize(ConfigurableApplicationContext applicationContext) { Cloud cloud = getCloud(); ConfigurableEnvironment appEnvironment = applicationContext.getEnvironment(); String[] persistenceProfiles = getCloudProfiles(cloud); if (persistenceProfiles == null) { persistenceProfiles = new String[] { IN_MEMORY_PROFILE }; } for (String persistenceProfile : persistenceProfiles) { appEnvironment.addActiveProfile(persistenceProfile); } } private Cloud getCloud() { try { CloudFactory cloudFactory = new CloudFactory(); return cloudFactory.getCloud(); } catch (CloudException e) { return null; } } private String[] getCloudProfiles(Cloud cloud) { if (cloud == null) { return null; } List<String> profiles = new ArrayList<>(); List<ServiceInfo> serviceInfos = cloud.getServiceInfos(); LOGGER.info("Found serviceInfos: " + StringUtils.collectionToCommaDelimitedString(serviceInfos)); for (ServiceInfo serviceInfo : serviceInfos) { if (serviceTypeToProfileName.containsKey(serviceInfo.getClass())) { profiles.add(serviceTypeToProfileName.get(serviceInfo.getClass())); } } if (profiles.size() > 1) { throw new IllegalStateException( "Only one service of the following types may be bound to this application: " + serviceTypeToProfileName.values().toString() + ". " + "These services are bound to the application: [" + StringUtils.collectionToCommaDelimitedString(profiles) + "]"); } if (profiles.size() > 0) { return createProfileNames(profiles.get(0), "cloud"); } return null; } private String[] createProfileNames(String baseName, String suffix) { String[] profileNames = { baseName, baseName + "-" + suffix }; LOGGER.log(Level.INFO, "Setting profile names: " + Arrays.toString(profileNames)); return profileNames; } }