|
12 | 12 | */ |
13 | 13 | package com.ibm.watson.developer_cloud.util; |
14 | 14 |
|
| 15 | +import java.util.Hashtable; |
15 | 16 | import java.util.Map.Entry; |
16 | 17 | import java.util.logging.Level; |
17 | 18 | import java.util.logging.Logger; |
@@ -91,6 +92,9 @@ public String getUsername() { |
91 | 92 | /** The services. */ |
92 | 93 | private static String services; |
93 | 94 |
|
| 95 | + /** The context. */ |
| 96 | + private static Context context; |
| 97 | + |
94 | 98 | /** The Constant USERNAME. */ |
95 | 99 | private static final String USERNAME = "username"; |
96 | 100 |
|
@@ -120,22 +124,35 @@ private CredentialUtils() { |
120 | 124 | } |
121 | 125 |
|
122 | 126 | /** |
123 | | - * Attempt to get the Base64-encoded value through JNDI. |
124 | | - * |
125 | | - * This method should always return null on Android due to the javax functions being unsupported |
| 127 | + * Builds the lookup name to be searched for in JDNI |
| 128 | + * and uses it to call the overloaded JDNI method. |
126 | 129 | * |
127 | 130 | * @param serviceName Name of the bluemix service |
128 | 131 | * @param lookupNameExtension Extension to determine which value should be retrieved through JDNI |
129 | 132 | * @return The encoded desired value |
130 | 133 | */ |
131 | 134 | private static String getJDNIValue(String serviceName, String lookupNameExtension) { |
| 135 | + return getJDNIValue("watson-developer-cloud/" + serviceName + lookupNameExtension); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Attempt to get the Base64-encoded value through JNDI. |
| 140 | + * |
| 141 | + * This method should always return null on Android due to the javax functions being unsupported |
| 142 | + * |
| 143 | + * @param lookupName Key to lookup in JDNI |
| 144 | + * @return The encoded desired value |
| 145 | + */ |
| 146 | + private static String getJDNIValue(String lookupName) { |
132 | 147 | if (!isClassAvailable("javax.naming.Context") || !isClassAvailable("javax.naming.InitialContext")) { |
133 | 148 | log.info("JNDI string lookups is not available."); |
134 | 149 | return null; |
135 | 150 | } |
136 | | - String lookupName = "watson-developer-cloud/" + serviceName + lookupNameExtension; |
| 151 | + |
137 | 152 | try { |
138 | | - Context context = new InitialContext(); |
| 153 | + if (context == null) { |
| 154 | + context = new InitialContext(); |
| 155 | + } |
139 | 156 | return (String) context.lookup(lookupName); |
140 | 157 | } catch (Exception e) { |
141 | 158 | log.fine("JNDI " + lookupName + " not found."); |
@@ -339,4 +356,32 @@ public static String getAPIUrl(String serviceName, String plan) { |
339 | 356 | public static void setServices(String services) { |
340 | 357 | CredentialUtils.services = services; |
341 | 358 | } |
| 359 | + |
| 360 | + /** |
| 361 | + * Sets the context variable for JDNI. This is a utility method for testing. |
| 362 | + * |
| 363 | + * @param env Configuration options for the context |
| 364 | + */ |
| 365 | + public static void setContext(Hashtable<String, String> env) { |
| 366 | + try { |
| 367 | + CredentialUtils.context = new InitialContext(env); |
| 368 | + } catch (Exception e) { |
| 369 | + log.fine("Error setting up JDNI context: " + e.getMessage()); |
| 370 | + } |
| 371 | + } |
| 372 | + |
| 373 | + /** |
| 374 | + * Method for testing the getAPIUrl method that bypasses the VCAP |
| 375 | + * services to ensure retrieval from JDNI. |
| 376 | + * |
| 377 | + * @param serviceName the service name |
| 378 | + * @return the API URL |
| 379 | + */ |
| 380 | + public static String getAPIUrlTest(String serviceName) { |
| 381 | + if ((serviceName == null) || serviceName.isEmpty()) { |
| 382 | + return null; |
| 383 | + } |
| 384 | + |
| 385 | + return getJDNIValue("jdni/watson-developer-cloud/" + serviceName + LOOKUP_NAME_EXTENSION_URL); |
| 386 | + } |
342 | 387 | } |
0 commit comments