Created
April 19, 2011 14:47
-
-
Save mattupstate/928163 to your computer and use it in GitHub Desktop.
Easily get your cloud environment properties in your Spring context initializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.nobien.cloudfoundry.example.config; | |
import java.util.Properties; | |
import org.cloudfoundry.runtime.env.CloudEnvironment; | |
import org.cloudfoundry.runtime.env.CloudEnvironmentPropertiesFactoryBean; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.context.ApplicationContextInitializer; | |
import org.springframework.core.env.ConfigurableEnvironment; | |
import org.springframework.core.env.PropertiesPropertySource; | |
import org.springframework.web.context.ConfigurableWebApplicationContext; | |
public class ContextInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { | |
private static final Logger logger = LoggerFactory.getLogger(ContextInitializer.class); | |
// The name of a service you created using the vmc command line tool followed by a period. | |
// In this case it is the name of a MongoDB service, but could be MySQL or Redis too. | |
public static final String MONGO_SERVICE_PROPERTY_PREFIX = "nobien-example-mongodb."; | |
// A prefix for your application properties | |
public static final String APPLICATION_PROPERTY_PREFIX = "application."; | |
public void initialize(ConfigurableWebApplicationContext applicationContext) { | |
ConfigurableEnvironment environment = applicationContext.getEnvironment(); | |
// Create the cloud environment object | |
CloudEnvironment cloud = new CloudEnvironment(); | |
// Check the instance info | |
if (cloud.getInstanceInfo() != null) { | |
// Application is running on cloudfoundry.com | |
environment.setActiveProfiles("cloud"); | |
} else { | |
// Application is running somewhere other than cloudfoundry.com | |
environment.setActiveProfiles("development"); | |
} | |
if (environment.acceptsProfiles("development")) { | |
Properties properties = new Properties(); | |
// Create mongo service properties for development environment | |
properties.put(MONGO_SERVICE_PROPERTY_PREFIX + "hostname", "localhost"); | |
properties.put(MONGO_SERVICE_PROPERTY_PREFIX + "port", 27017); | |
properties.put(MONGO_SERVICE_PROPERTY_PREFIX + "username", ""); | |
properties.put(MONGO_SERVICE_PROPERTY_PREFIX + "password", ""); | |
properties.put(MONGO_SERVICE_PROPERTY_PREFIX + "db", "nobiendev"); | |
properties.put(APPLICATION_PROPERTY_PREFIX + "url", "http://local.hello-nobien.cloudfoundry.com:8080/hello-nobien"); | |
environment.getPropertySources().addFirst(new PropertiesPropertySource("properties", properties)); | |
} else if (environment.acceptsProfiles("cloud")) { | |
try { | |
// Use the factory bean to load the application's service properties | |
CloudEnvironmentPropertiesFactoryBean factory = new CloudEnvironmentPropertiesFactoryBean(); | |
Properties properties = factory.getObject(); | |
properties.put(APPLICATION_PROPERTY_PREFIX + "url", "http://hello-nobien.cloudfoundry.com"); | |
environment.getPropertySources().addFirst(new PropertiesPropertySource("properties", properties)); | |
} catch (Exception e) { | |
logger.error("Error loading Cloud properties: " + e.getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment