|
| 1 | +/* |
| 2 | + * Copyright 2017-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.dataflow.composedtaskrunner.support; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | + |
| 21 | +import io.pivotal.cfenv.core.CfCredentials; |
| 22 | +import io.pivotal.cfenv.core.CfEnv; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; |
| 27 | +import org.springframework.context.ApplicationListener; |
| 28 | +import org.springframework.core.env.MapPropertySource; |
| 29 | +import org.springframework.core.env.StandardEnvironment; |
| 30 | + |
| 31 | +/** |
| 32 | + * If the composed task runner is running on Cloud Foundry and is bound to a service instance from SCDF for VMware |
| 33 | + * Tanzu, use the values from the service binding credentials to configure the composed task runner. |
| 34 | + * |
| 35 | + * @author Mike Heath |
| 36 | + */ |
| 37 | +public class CfServiceBindingPropertySourceInitializer |
| 38 | + implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { |
| 39 | + |
| 40 | + private static final Logger log = LoggerFactory.getLogger(CfServiceBindingPropertySourceInitializer.class); |
| 41 | + |
| 42 | + public static final String PROPERTY_SOURCE_NAME = "p-dataflow"; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { |
| 46 | + final CfEnv cfEnv = new CfEnv(); |
| 47 | + try { |
| 48 | + final CfCredentials credentials = cfEnv.findCredentialsByLabel("p-dataflow"); |
| 49 | + final HashMap<String, Object> propertiesMap = new HashMap<>(); |
| 50 | + propertiesMap.put("dataflowServerUri", credentials.getString("dataflow-url")); |
| 51 | + propertiesMap.put("oauth2ClientCredentialsClientId", credentials.getString("client-id")); |
| 52 | + propertiesMap.put("oauth2ClientCredentialsClientSecret", credentials.getString("client-secret")); |
| 53 | + propertiesMap.put("oauth2ClientCredentialsTokenUri", credentials.getString("access-token-url")); |
| 54 | + |
| 55 | + final MapPropertySource propertySource = new MapPropertySource(PROPERTY_SOURCE_NAME, propertiesMap); |
| 56 | + event.getEnvironment().getPropertySources() |
| 57 | + .addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, propertySource); |
| 58 | + } catch (IllegalArgumentException e) { |
| 59 | + log.debug("No 'p-dataflow' service binding found in VCAP_SERVICES"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments