11package io .nexusrpc .handler ;
22
33import io .nexusrpc .Link ;
4+ import io .nexusrpc .ServiceDefinition ;
45import java .time .Instant ;
56import java .util .*;
67import java .util .stream .Collectors ;
@@ -23,20 +24,25 @@ public static Builder newBuilder(OperationContext context) {
2324 private final Map <String , String > headers ;
2425 // This is not included in equals, hashCode, or toString
2526 private final @ Nullable OperationMethodCanceller methodCanceller ;
26- private final List <Link > links = new ArrayList <>();
2727 private final Instant deadline ;
28+ private final List <Link > links ;
29+ private final @ Nullable ServiceDefinition serviceDefinition ;
2830
2931 private OperationContext (
3032 String service ,
3133 String operation ,
3234 Map <String , String > headers ,
3335 @ Nullable OperationMethodCanceller methodCanceller ,
34- Instant deadline ) {
36+ Instant deadline ,
37+ @ Nullable ServiceDefinition serviceDefinition ,
38+ List <Link > links ) {
3539 this .service = service ;
3640 this .operation = operation ;
3741 this .headers = headers ;
3842 this .methodCanceller = methodCanceller ;
3943 this .deadline = deadline ;
44+ this .serviceDefinition = serviceDefinition ;
45+ this .links = links ;
4046 }
4147
4248 /** Service name for the call. */
@@ -75,6 +81,11 @@ public boolean isMethodCancelled() {
7581 return methodCanceller == null ? null : methodCanceller .getCancellationReason ();
7682 }
7783
84+ /** Get the service definition associated with this operation context, if any. */
85+ public @ Nullable ServiceDefinition getServiceDefinition () {
86+ return serviceDefinition ;
87+ }
88+
7889 /**
7990 * Get the deadline for the operation handler method. This is the time by which the method should
8091 * complete. This is not the operation's deadline.
@@ -136,18 +147,21 @@ public OperationContext removeMethodCancellationListener(
136147
137148 @ Override
138149 public boolean equals (Object o ) {
139- if (this == o ) return true ;
140150 if (o == null || getClass () != o .getClass ()) return false ;
141151 OperationContext that = (OperationContext ) o ;
142152 return Objects .equals (service , that .service )
143153 && Objects .equals (operation , that .operation )
144154 && Objects .equals (headers , that .headers )
145- && Objects .equals (links , that .links );
155+ && Objects .equals (methodCanceller , that .methodCanceller )
156+ && Objects .equals (deadline , that .deadline )
157+ && Objects .equals (links , that .links )
158+ && Objects .equals (serviceDefinition , that .serviceDefinition );
146159 }
147160
148161 @ Override
149162 public int hashCode () {
150- return Objects .hash (service , operation , headers );
163+ return Objects .hash (
164+ service , operation , headers , methodCanceller , deadline , links , serviceDefinition );
151165 }
152166
153167 @ Override
@@ -161,8 +175,14 @@ public String toString() {
161175 + '\''
162176 + ", headers="
163177 + headers
178+ + ", methodCanceller="
179+ + methodCanceller
180+ + ", deadline="
181+ + deadline
164182 + ", links="
165183 + links
184+ + ", serviceDefinition="
185+ + serviceDefinition
166186 + '}' ;
167187 }
168188
@@ -173,15 +193,25 @@ public static class Builder {
173193 private final SortedMap <String , String > headers ;
174194 private @ Nullable OperationMethodCanceller methodCanceller ;
175195 private @ Nullable Instant deadline ;
196+ private @ Nullable ServiceDefinition serviceDefinition ;
197+ // Currently links are not set in the builder, but they need to be passed though to go from
198+ // OperationContext -> Builder
199+ // and back to OperationContext, so we keep them here.
200+ private final List <Link > links ;
176201
177202 private Builder () {
178203 headers = new TreeMap <>(String .CASE_INSENSITIVE_ORDER );
204+ links = new ArrayList <>();
179205 }
180206
181207 private Builder (OperationContext context ) {
182208 service = context .service ;
183209 operation = context .operation ;
184210 headers = new TreeMap <>(context .headers );
211+ methodCanceller = context .methodCanceller ;
212+ deadline = context .deadline ;
213+ serviceDefinition = context .serviceDefinition ;
214+ links = context .links ;
185215 }
186216
187217 /** Set service. Required. */
@@ -219,6 +249,12 @@ public Builder setDeadline(Instant deadline) {
219249 return this ;
220250 }
221251
252+ /** Sets the service definition. */
253+ public Builder setServiceDefinition (ServiceDefinition serviceDefinition ) {
254+ this .serviceDefinition = serviceDefinition ;
255+ return this ;
256+ }
257+
222258 /** Build the context. */
223259 public OperationContext build () {
224260 Objects .requireNonNull (service , "Service required" );
@@ -236,7 +272,9 @@ public OperationContext build() {
236272 operation ,
237273 Collections .unmodifiableMap (new TreeMap <>(normalizedHeaders )),
238274 methodCanceller ,
239- deadline );
275+ deadline ,
276+ serviceDefinition ,
277+ links );
240278 }
241279 }
242280}
0 commit comments