File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -379,6 +379,10 @@ public class Options {
379379 * Property used to configure a builder from a Properties object. {@value}, see {@link Builder#token(String) token}.
380380 */
381381 public static final String PROP_TOKEN = PFX + "token" ;
382+ /**
383+ * Property used to configure the token supplier from a Properties object. {@value}, see {@link Builder#tokenSupplier(Supplier) tokenSupplier}.
384+ */
385+ public static final String PROP_TOKEN_SUPPLIER = PFX + "token.supplier" ;
382386 /**
383387 * Property used to configure a builder from a Properties object. {@value}, see {@link Builder#server(String) server}.
384388 */
@@ -882,6 +886,8 @@ public Builder properties(Properties props) {
882886 charArrayProperty (props , PROP_USERNAME , ca -> this .username = ca );
883887 charArrayProperty (props , PROP_PASSWORD , ca -> this .password = ca );
884888 charArrayProperty (props , PROP_TOKEN , ca -> this .tokenSupplier = new DefaultTokenSupplier (ca ));
889+ //noinspection unchecked
890+ classnameProperty (props , PROP_TOKEN_SUPPLIER , o -> this .tokenSupplier = (Supplier <char []>) o );
885891
886892 booleanProperty (props , PROP_SECURE , b -> this .useDefaultTls = b );
887893 booleanProperty (props , PROP_OPENTLS , b -> this .useTrustAllTls = b );
Original file line number Diff line number Diff line change @@ -892,6 +892,16 @@ public void testTokenSupplier() {
892892
893893 connectString = o .buildProtocolConnectOptionsString (serverURI , true , null ).toString ();
894894 assertTrue (connectString .contains ("\" auth_token\" :\" short-lived-token-2\" " ));
895+
896+ Properties properties = new Properties ();
897+ properties .setProperty (PROP_TOKEN_SUPPLIER , TestingDynamicTokenSupplier .class .getCanonicalName ());
898+ o = new Options .Builder ().properties (properties ).build ();
899+
900+ connectString = o .buildProtocolConnectOptionsString (serverURI , true , null ).toString ();
901+ assertTrue (connectString .contains ("\" auth_token\" :\" dynamic-token-1\" " ));
902+
903+ connectString = o .buildProtocolConnectOptionsString (serverURI , true , null ).toString ();
904+ assertTrue (connectString .contains ("\" auth_token\" :\" dynamic-token-2\" " ));
895905 }
896906
897907 @ Test
Original file line number Diff line number Diff line change 1+ // Copyright 2025 The NATS Authors
2+ // Licensed under the Apache License, Version 2.0 (the "License");
3+ // you may not use this file except in compliance with the License.
4+ // You may obtain a copy of the License at:
5+ //
6+ // http://www.apache.org/licenses/LICENSE-2.0
7+ //
8+ // Unless required by applicable law or agreed to in writing, software
9+ // distributed under the License is distributed on an "AS IS" BASIS,
10+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ // See the License for the specific language governing permissions and
12+ // limitations under the License.
13+
14+ package io .nats .client ;
15+
16+ import java .util .concurrent .atomic .AtomicInteger ;
17+ import java .util .function .Supplier ;
18+
19+ public class TestingDynamicTokenSupplier implements Supplier <char []> {
20+ AtomicInteger counter = new AtomicInteger (0 );
21+
22+ @ Override
23+ public char [] get () {
24+ return ("dynamic-token-" + counter .incrementAndGet ()).toCharArray ();
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments