GeezerVison is showing github.com — Check if JNDI is available before using · einsnull/java-sdk@163dade
File tree
Expand file treeCollapse file tree
Open diff view settings
src/main/java/com/ibm/watson/developer_cloud/util
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/main/java/com/ibm/watson/developer_cloud/util/CredentialUtils.java +19-12Lines changed: 19 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,20 +74,27 @@ public class CredentialUtils { |
74 | 74 | * @return The encoded API Key |
75 | 75 | */ |
76 | 76 | private static String getKeyUsingJNDI(String serviceName) { |
77 | | - try { |
78 | | - Class.forName("javax.naming.Context"); |
79 | | - try { |
80 | | - Context context = new InitialContext(); |
81 | | - String lookupName = "watson-developer-cloud/" + serviceName + "/credentials"; |
82 | | - String apiKey = (String) context.lookup(lookupName); |
83 | | - return apiKey; |
84 | | - } catch (NamingException e) { |
85 | | - return null; |
86 | | - } |
87 | | - } catch (ClassNotFoundException exception) { |
| 77 | + if (!isClassAvailable("javax.naming.Context")) { |
88 | 78 | log.info("JNDI string lookups is not available."); |
| 79 | + return null; |
| 80 | + } |
| 81 | + try { |
| 82 | + Context context = new InitialContext(); |
| 83 | + String lookupName = "watson-developer-cloud/" + serviceName + "/credentials"; |
| 84 | + String apiKey = (String) context.lookup(lookupName); |
| 85 | + return apiKey; |
| 86 | + } catch (NamingException e) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static boolean isClassAvailable(String className) { |
| 92 | + try { |
| 93 | + Class.forName(className); |
| 94 | + return true; |
| 95 | + } catch (Throwable e) { |
| 96 | + return false; |
89 | 97 | } |
90 | | - return null; |
91 | 98 | } |
92 | 99 |
|
93 | 100 | /** |
|