Skip to content

Commit 862ab6f

Browse files
committed
Enhancement Request 37169695 - [36401381->24.09.1] ENH: Improve the NSLookup -name option exception handling (ce-main->ce-24.09)
Remote remote.full on coherence-ce/release/coherence-ce-v24.09 success, changes 111926, synced @111936, job.9.20241015142728.4681 [git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v24.09/": change = 111941]
1 parent 6beae34 commit 862ab6f

File tree

2 files changed

+138
-3
lines changed
  • prj

2 files changed

+138
-3
lines changed

prj/coherence-discovery/src/main/java/com/tangosol/discovery/NSLookup.java

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,13 +1013,25 @@ public void accept(String sClusterFound, String sResult)
10131013
sCluster = sTok.nextToken().trim();
10141014
try (Connection conn2 = Connection.open(sCluster, socketAddr, cTimeoutMillis))
10151015
{
1016-
System.out.println("Cluster " + sCluster + ":\t" + conn2.lookup(NS_STRING_PREFIX + sName));
1016+
String sResultLookup = conn2.lookup(NS_STRING_PREFIX + sName);
1017+
1018+
System.out.println("Cluster " + sCluster + ":\t" + sResultLookup);
1019+
if (sResultLookup == null)
1020+
{
1021+
reportNameWarning(sName);
1022+
}
10171023
}
10181024
}
10191025
}
10201026
else
10211027
{
1022-
System.out.println(conn.lookup(NS_STRING_PREFIX + sName));
1028+
String sResultLookup = conn.lookup(NS_STRING_PREFIX + sName);
1029+
1030+
System.out.println(sResultLookup);
1031+
if (sResultLookup == null)
1032+
{
1033+
reportNameWarning(sName);
1034+
}
10231035
}
10241036
}
10251037
ex = null;
@@ -1045,7 +1057,15 @@ public void accept(String sClusterFound, String sResult)
10451057
*/
10461058
protected static void showInstructions()
10471059
{
1048-
String sClass = NSLookup.class.getCanonicalName();
1060+
String sClass = NSLookup.class.getCanonicalName();
1061+
StringBuffer sbLookupNames = new StringBuffer("Valid predefined lookup names for -" + COMMAND_NAME + " option: ");
1062+
1063+
for (String sName : VALID_PREDEFINED_LOOKUP_NAMES)
1064+
{
1065+
sbLookupNames.append(" " + sName + ",");
1066+
}
1067+
sbLookupNames.setCharAt(sbLookupNames.length() - 1, '.');
1068+
10491069
System.out.println();
10501070
System.out.println("java " + sClass + " <commands ...>");
10511071
System.out.println();
@@ -1058,11 +1078,66 @@ protected static void showInstructions()
10581078
System.out.println("\t-" + COMMAND_PORT + " the cluster port; default " + DEFAULT_CLUSTERPORT);
10591079
System.out.println("\t-" + COMMAND_TIMEOUT + " the timeout (in seconds) of the lookup request; default " + (DEFAULT_TIMEOUT/1000));
10601080
System.out.println();
1081+
System.out.println(sbLookupNames.toString());
1082+
System.out.println();
10611083
System.out.println("Example:");
10621084
System.out.println("\tjava " + sClass + " -" + COMMAND_HOST + " host.mycompany.com -" + COMMAND_NAME + " " + JMX_CONNECTOR_URL);
10631085
System.out.println();
10641086
}
10651087

1088+
/**
1089+
* Descriptive error feedback for a failed lookup of provided -name parameter.
1090+
*
1091+
* @param sName value passed to -name argument
1092+
*
1093+
* @return null if no feedback or descriptive feedback
1094+
*/
1095+
protected static String validateName(String sName)
1096+
{
1097+
for (String s : VALID_PREDEFINED_LOOKUP_NAMES)
1098+
{
1099+
if (s.equals(sName))
1100+
{
1101+
return null;
1102+
}
1103+
}
1104+
1105+
StringBuffer sb = new StringBuffer();
1106+
1107+
sb.append("Warning: provided -name parameter of \"" + sName + "\"");
1108+
sb.append(" does not match predefined Name Service lookups of ");
1109+
for (int i =0; i < VALID_PREDEFINED_LOOKUP_NAMES.length; i++)
1110+
{
1111+
sb.append("\"" + VALID_PREDEFINED_LOOKUP_NAMES[i] + "\"");
1112+
if (i + 1 < VALID_PREDEFINED_LOOKUP_NAMES.length)
1113+
{
1114+
sb.append(",");
1115+
}
1116+
else
1117+
{
1118+
sb.append(".");
1119+
}
1120+
}
1121+
sb.append(System.lineSeparator());
1122+
sb.append("warning: If looking up an extend proxy service, verify the scoped proxy service name is correct.");
1123+
return sb.toString();
1124+
}
1125+
1126+
/**
1127+
* Reports {@code -name} parameter warning when appropriate to standard error.
1128+
*
1129+
* @param sName Name Service looked up
1130+
*/
1131+
protected static void reportNameWarning(String sName)
1132+
{
1133+
String sWarning = validateName(sName);
1134+
1135+
if (sWarning != null)
1136+
{
1137+
System.err.println(sWarning);
1138+
}
1139+
}
1140+
10661141
// ----- static data members -------------------------------------------
10671142

10681143
public static final String COMMAND_HELP = "?";
@@ -1117,6 +1192,13 @@ protected static void showInstructions()
11171192
*/
11181193
public static final String HTTP_HEALTH_URL = "health/HTTPHealthURL";
11191194

1195+
/**
1196+
* Cluster info lookup name.
1197+
*
1198+
* @since 25.03
1199+
*/
1200+
public static final String CLUSTER_INFO = "Cluster/info";
1201+
11201202
/**
11211203
* The gRPC Proxy lookup name.
11221204
*
@@ -1126,6 +1208,14 @@ protected static void showInstructions()
11261208
// This value here must match the fully scoped name used for the gRPC proxy in the
11271209
// grpc-proxy-cache-config.xml file and in the GrpcDependencies.DEFAULT_PROXY_NAME field
11281210

1211+
/**
1212+
* Valid predefined lookup names.
1213+
*
1214+
* @since 25.03
1215+
*/
1216+
public static final String[] VALID_PREDEFINED_LOOKUP_NAMES =
1217+
{JMX_CONNECTOR_URL, HTTP_MANAGEMENT_URL, HTTP_METRICS_URL, HTTP_HEALTH_URL, GRPC_PROXY_URL, CLUSTER_INFO};
1218+
11291219
/**
11301220
* Default timeout in milliseconds
11311221
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* https://oss.oracle.com/licenses/upl.
6+
*/
7+
8+
package com.tangosol.discovery;
9+
10+
import org.junit.Test;
11+
12+
import static org.junit.Assert.*;
13+
14+
15+
/**
16+
* Unit test of the NSLookup class.
17+
*
18+
* @author jf 2024.10.07
19+
*/
20+
public class NSLookupTest
21+
{
22+
@Test
23+
public void shouldReportNameWarning()
24+
{
25+
for (String sName : NSLookup.VALID_PREDEFINED_LOOKUP_NAMES)
26+
{
27+
StringBuffer sbTypo = new StringBuffer(sName);
28+
29+
sbTypo.setCharAt(0, Character.toUpperCase(sName.charAt(0)));
30+
sbTypo.setCharAt(1, Character.toLowerCase(sName.charAt(1)));
31+
sbTypo.setCharAt(2, Character.toUpperCase(sName.charAt(2)));
32+
assertNotNull("assert predefined lookup name " + sbTypo + " with injected typo results in a warning",
33+
NSLookup.validateName(sbTypo.toString()));
34+
}
35+
}
36+
37+
@Test
38+
public void mustNotReportNameWarning()
39+
{
40+
for (String sName : NSLookup.VALID_PREDEFINED_LOOKUP_NAMES)
41+
{
42+
assertNull("assert no warning for predefined lookup names", NSLookup.validateName(sName));
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)