Skip to content

Commit 2dd8bb3

Browse files
committed
api: Add newNameResolver() overload and default, best-effort impl.
1 parent 7f39805 commit 2dd8bb3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

api/src/main/java/io/grpc/NameResolver.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,48 @@ public abstract static class Factory {
158158
* cannot be resolved by this factory. The decision should be solely based on the scheme of the
159159
* URI.
160160
*
161+
* <p>This method will eventually be deprecated and removed as part of a migration from {@code
162+
* java.net.URI} to {@code io.grpc.Uri}. Implementations will override {@link
163+
* #newNameResolver(Uri, Args)} instead.
164+
*
161165
* @param targetUri the target URI to be resolved, whose scheme must not be {@code null}
162166
* @param args other information that may be useful
163167
*
164168
* @since 1.21.0
165169
*/
166170
public abstract NameResolver newNameResolver(URI targetUri, final Args args);
167171

172+
/**
173+
* Creates a {@link NameResolver} for the given target URI.
174+
*
175+
* <p>Implementations return {@code null} if 'targetUri' cannot be resolved by this factory. The
176+
* decision should be solely based on the target's scheme.
177+
*
178+
* <p>All {@link NameResolver.Factory} implementations should override this method, as it will
179+
* eventually replace {@link #newNameResolver(URI, Args)}. For backwards compatibility, this
180+
* default implementation delegates to {@link #newNameResolver(URI, Args)} if 'targetUri' can be
181+
* converted to a java.net.URI.
182+
*
183+
* <p>NB: Conversion is not always possible, for example {@code scheme:#frag} is a valid {@link
184+
* Uri} but not a valid {@link URI} because its path is empty. The default implementation throws
185+
* IllegalArgumentException in these cases.
186+
*
187+
* @param targetUri the target URI to be resolved
188+
* @param args other information that may be useful
189+
* @throws IllegalArgumentException if targetUri does not have the expected form
190+
* @since 1.79
191+
*/
192+
public NameResolver newNameResolver(Uri targetUri, final Args args) {
193+
// Not every io.grpc.Uri can be converted but in the ordinary ManagedChannel creation flow,
194+
// any IllegalArgumentException thrown here would happened anyway, just earlier. That's
195+
// because parse/toString is transparent so java.net.URI#create here sees the original target
196+
// string just like it did before the io.grpc.Uri migration.
197+
//
198+
// Throwing IAE shouldn't surprise non-framework callers either. After all, many existing
199+
// Factory impls are picky about targetUri and throw IAE when it doesn't look how they expect.
200+
return newNameResolver(URI.create(targetUri.toString()), args);
201+
}
202+
168203
/**
169204
* Returns the default scheme, which will be used to construct a URI when {@link
170205
* ManagedChannelBuilder#forTarget(String)} is given an authority string instead of a compliant

0 commit comments

Comments
 (0)