|
18 | 18 | * (c) Copyright IBM Corp. 2018 |
19 | 19 | */ |
20 | 20 | package com.eventstreams.samples.env; |
21 | | -import java.io.IOException; |
22 | | -import java.util.Iterator; |
23 | 21 |
|
24 | | -import org.apache.kafka.common.errors.IllegalSaslStateException; |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
25 | 24 | import org.apache.log4j.Level; |
26 | 25 | import org.apache.log4j.Logger; |
27 | 26 |
|
28 | | -import com.fasterxml.jackson.databind.JsonNode; |
29 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Iterator; |
30 | 29 |
|
31 | 30 | public class Environment { |
32 | 31 |
|
33 | 32 | private static final Logger logger = Logger.getLogger(Environment.class); |
34 | | - private static final String SERVICE_NAME = "messagehub"; |
35 | 33 |
|
36 | 34 | public static EventStreamsCredentials getEventStreamsCredentials() { |
37 | 35 | String vcapServices = System.getenv("VCAP_SERVICES"); |
38 | 36 | logger.log(Level.INFO, "VCAP_SERVICES: \n" + vcapServices); |
39 | | - try { |
40 | | - if (vcapServices != null) { |
41 | | - JsonNode mhub = parseVcapServices(vcapServices); |
42 | | - ObjectMapper mapper = new ObjectMapper(); |
43 | | - return mapper.readValue(mhub.toString(), EventStreamsCredentials.class); |
44 | | - } else { |
45 | | - logger.log(Level.ERROR, "VCAP_SERVICES environment variable is null."); |
46 | | - throw new IllegalStateException("VCAP_SERVICES environment variable is null."); |
47 | | - } |
48 | | - } catch (IOException ioe) { |
49 | | - logger.log(Level.ERROR, "VCAP_SERVICES environment variable parses failed."); |
50 | | - throw new IllegalStateException("VCAP_SERVICES environment variable parses failed.", ioe); |
| 37 | + if (vcapServices == null) { |
| 38 | + logger.log(Level.ERROR, "VCAP_SERVICES environment variable is null."); |
| 39 | + throw new IllegalStateException("VCAP_SERVICES environment variable is null."); |
51 | 40 | } |
| 41 | + return transformVcapServices(vcapServices); |
52 | 42 | } |
53 | 43 |
|
54 | | - private static JsonNode parseVcapServices(String vcapServices) throws IOException { |
55 | | - ObjectMapper mapper = new ObjectMapper(); |
56 | | - JsonNode vcapServicesJson = mapper.readValue(vcapServices, JsonNode.class); |
57 | | - |
58 | | - // when running in CloudFoundry VCAP_SERVICES is wrapped into a bigger JSON object |
59 | | - // so it needs to be extracted. We attempt to read the "instance_id" field to identify |
60 | | - // if it has been wrapped |
61 | | - if (vcapServicesJson.get("instance_id") != null) { |
62 | | - return vcapServicesJson; |
63 | | - } else { |
64 | | - String vcapKey = null; |
65 | | - Iterator<String> it = vcapServicesJson.fieldNames(); |
66 | | - // Find the Event Streams service bound to this application. |
67 | | - while (it.hasNext() && vcapKey == null) { |
68 | | - String potentialKey = it.next(); |
69 | | - if (potentialKey.startsWith(SERVICE_NAME)) { |
70 | | - logger.log(Level.WARN, "Using the '" + potentialKey + "' key from VCAP_SERVICES."); |
71 | | - vcapKey = potentialKey; |
| 44 | + private static EventStreamsCredentials transformVcapServices(String vcapServices) { |
| 45 | + try { |
| 46 | + ObjectMapper mapper = new ObjectMapper(); |
| 47 | + JsonNode instanceCredentials = mapper.readValue(vcapServices, JsonNode.class); |
| 48 | + // when running in CloudFoundry VCAP_SERVICES is wrapped into a bigger JSON object |
| 49 | + // so it needs to be extracted. We attempt to read the "instance_id" field to identify |
| 50 | + // if it has been wrapped |
| 51 | + if (instanceCredentials.get("instance_id") == null) { |
| 52 | + Iterator<String> it = instanceCredentials.fieldNames(); |
| 53 | + // Find the Event Streams service bound to this application. |
| 54 | + while (it.hasNext()) { |
| 55 | + String potentialKey = it.next(); |
| 56 | + String messageHubJsonKey = "messagehub"; |
| 57 | + if (potentialKey.startsWith(messageHubJsonKey)) { |
| 58 | + logger.log(Level.WARN, "Using the '" + potentialKey + "' key from VCAP_SERVICES."); |
| 59 | + instanceCredentials = instanceCredentials.get(potentialKey) |
| 60 | + .get(0) |
| 61 | + .get("credentials"); |
| 62 | + break; |
| 63 | + } |
72 | 64 | } |
73 | 65 | } |
74 | | - |
75 | | - if (vcapKey == null) { |
76 | | - throw new IllegalSaslStateException("No EventStreams service bound"); |
77 | | - } else { |
78 | | - return vcapServicesJson.get(vcapKey).get(0).get("credentials"); |
79 | | - } |
| 66 | + return mapper.readValue(instanceCredentials.toString(), EventStreamsCredentials.class); |
| 67 | + } catch (IOException e) { |
| 68 | + logger.log(Level.ERROR, "VCAP_SERVICES environment variable parses failed."); |
| 69 | + throw new IllegalStateException("VCAP_SERVICES environment variable parses failed.", e); |
80 | 70 | } |
81 | 71 | } |
82 | 72 | } |
0 commit comments