Skip to content

Commit 140bfa4

Browse files
Fix for java.naming.InitialContext that was preventing android users
from using the sdks #167
1 parent 43e5e51 commit 140bfa4

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/main/java/com/ibm/watson/developer_cloud/util/CredentialUtils.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,27 @@ public static String getAPIKey(String serviceName, String plan) {
117117

118118
/**
119119
* Attempt to get the Base64-encoded API key through JNDI
120+
*
120121
* @param serviceName Name of the bluemix service
121122
* @return The encoded API Key
122123
*/
123-
private static String getKeyUsingJNDI(String serviceName){
124-
try{
125-
Context context = new InitialContext();
126-
String lookupName = "watson-developer-cloud/" + serviceName + "/credentials";
127-
String apiKey = (String) context.lookup(lookupName);
128-
return apiKey;
129-
}catch(NamingException e){
130-
//ignore
131-
return null;
124+
private static String getKeyUsingJNDI(String serviceName) {
125+
try {
126+
Class.forName("javax.naming.Context");
127+
try {
128+
Context context = new InitialContext();
129+
String lookupName = "watson-developer-cloud/" + serviceName + "/credentials";
130+
String apiKey = (String) context.lookup(lookupName);
131+
return apiKey;
132+
} catch (NamingException e) {
133+
return null;
134+
}
135+
} catch (ClassNotFoundException exception) {
136+
log.info("JNDI string lookups is not available.");
132137
}
138+
return null;
133139
}
140+
134141
/**
135142
* Gets the <b>VCAP_SERVICES</b> environment variable and return it as a {@link JsonObject}.
136143
*

0 commit comments

Comments
 (0)