All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.jackson.env.CloudFoundryVcapServicesPropertySourceLoader Maven / Gradle / Ivy

There is a newer version: 4.6.5
Show newest version
/*
 * Copyright 2017-2019 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.jackson.env;

import com.fasterxml.jackson.core.JsonParseException;
import io.micronaut.context.env.MapPropertySource;
import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.io.ResourceLoader;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 

A {@link io.micronaut.context.env.PropertySourceLoader} that reads from the environment variable VCAP_SERVICES * which is used by CloudFoundry.

* * @author Fabian Nonnenmacher * @since 2.0 */ public class CloudFoundryVcapServicesPropertySourceLoader extends EnvJsonPropertySourceLoader { /** * Position for the system property source loader in the chain. */ public static final int POSITION = EnvJsonPropertySourceLoader.POSITION + 11; private static final String VCAP_SERVICES = "VCAP_SERVICES"; @Override public int getOrder() { return POSITION; } @Override protected String getEnvValue() { return System.getenv(VCAP_SERVICES); } @Override public Set getExtensions() { return Collections.singleton(VCAP_SERVICES); } @Override protected Optional readInput(ResourceLoader resourceLoader, String fileName) { if (fileName.equals("application." + VCAP_SERVICES)) { return getEnvValueAsStream(); } return Optional.empty(); } @Override protected void processInput(String name, InputStream input, Map finalMap) throws IOException { try { Map map = readJsonAsMap(input); processVcapServices(finalMap, map); } catch (JsonParseException e) { throw new ConfigurationException("Could not parse '" + VCAP_SERVICES + "': " + e.getMessage(), e); } } private void processVcapServices(Map finalMap, Map vcapServices) { if (vcapServices != null) { for (Object services : vcapServices.values()) { @SuppressWarnings("unchecked") List list = (List) services; for (Object object : list) { @SuppressWarnings("unchecked") Map service = (Map) object; String key = (String) service.get("name"); if (key == null) { key = (String) service.get("label"); } processMap(finalMap, service, "vcap.services." + key + "."); } } } } @Override protected MapPropertySource createPropertySource(String name, Map map, int order) { return super.createPropertySource("cloudfoundry-vcap-services", map, order); } }