Skip to content

Commit 81a2587

Browse files
jansupolsenivam
authored andcommitted
Implemented ClientBuilderListener
Signed-off-by: jansupol <[email protected]>
1 parent 52448e6 commit 81a2587

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

core-client/src/main/java/org/glassfish/jersey/client/JerseyClientBuilder.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,6 +17,9 @@
1717
package org.glassfish.jersey.client;
1818

1919
import java.security.KeyStore;
20+
import java.util.Collections;
21+
import java.util.LinkedList;
22+
import java.util.List;
2023
import java.util.Map;
2124
import java.util.concurrent.ExecutorService;
2225
import java.util.concurrent.ScheduledExecutorService;
@@ -30,8 +33,12 @@
3033

3134
import org.glassfish.jersey.SslConfigurator;
3235
import org.glassfish.jersey.client.internal.LocalizationMessages;
36+
import org.glassfish.jersey.client.spi.ClientBuilderListener;
37+
import org.glassfish.jersey.internal.ServiceFinder;
3338
import org.glassfish.jersey.internal.util.collection.UnsafeValue;
3439
import org.glassfish.jersey.internal.util.collection.Values;
40+
import org.glassfish.jersey.model.internal.RankedComparator;
41+
import org.glassfish.jersey.model.internal.RankedProvider;
3542

3643
/**
3744
* Jersey provider of {@link javax.ws.rs.client.ClientBuilder JAX-RS client builder}.
@@ -45,6 +52,23 @@ public class JerseyClientBuilder extends ClientBuilder {
4552
private SslConfigurator sslConfigurator;
4653
private SSLContext sslContext;
4754

55+
private static final List<ClientBuilderListener> CLIENT_BUILDER_LISTENERS;
56+
57+
static {
58+
final List<RankedProvider<ClientBuilderListener>> listeners = new LinkedList<>();
59+
for (ClientBuilderListener listener : ServiceFinder.find(ClientBuilderListener.class)) {
60+
listeners.add(new RankedProvider<>(listener));
61+
}
62+
listeners.sort(new RankedComparator<>(RankedComparator.Order.ASCENDING));
63+
64+
final List<ClientBuilderListener> sortedList = new LinkedList<>();
65+
for (RankedProvider<ClientBuilderListener> listener : listeners) {
66+
sortedList.add(listener.getProvider());
67+
}
68+
69+
CLIENT_BUILDER_LISTENERS = Collections.unmodifiableList(sortedList);
70+
}
71+
4872
/**
4973
* Create a new custom-configured {@link JerseyClient} instance.
5074
*
@@ -72,6 +96,14 @@ public static JerseyClient createClient(Configuration configuration) {
7296
*/
7397
public JerseyClientBuilder() {
7498
this.config = new ClientConfig();
99+
100+
init(this);
101+
}
102+
103+
private static void init(ClientBuilder builder) {
104+
for (ClientBuilderListener listener : CLIENT_BUILDER_LISTENERS) {
105+
listener.onNewBuilder(builder);
106+
}
75107
}
76108

77109
@Override
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.client.spi;
18+
19+
import org.glassfish.jersey.Beta;
20+
21+
import javax.ws.rs.client.ClientBuilder;
22+
23+
/**
24+
* <p>
25+
* Implementations of this interface will be notified when new ClientBuilder
26+
* instances are being constructed. This will allow implementations to register
27+
* providers on the ClientBuilder, and is intended for global providers.
28+
* </p>
29+
* <p>
30+
* In order for the ClientBuilder to call implementations of this interface,
31+
* the implementation must be specified such that a ServiceLoader can find it -
32+
* i.e. it must be specified in the <code>
33+
* META-INF/services/org.glassfish.jersey.client.spi.ClientBuilderListener
34+
* </code> file in an archive on the current thread's context classloader's
35+
* class path.
36+
* </p>
37+
* <p>
38+
* Note that the <code>onNewBuilder</code> method will be called when the
39+
* ClientBuilder is constructed, not when it's <code>build</code> method is
40+
* invoked. This allows the caller to override global providers if they desire.
41+
* </p>
42+
* <p>
43+
* The ClientBuilderListener are invoked in an order given by it's {@code @Priority}.
44+
* The default is {@code Priorities.USER}.
45+
* </p>
46+
* @since 2.32
47+
*/
48+
// Must not be annotated with @Contract
49+
@Beta
50+
public interface ClientBuilderListener {
51+
void onNewBuilder(ClientBuilder builder);
52+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.client.spi;
18+
19+
import org.glassfish.jersey.client.ClientConfig;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
import javax.annotation.Priority;
24+
import javax.ws.rs.Priorities;
25+
import javax.ws.rs.client.Client;
26+
import javax.ws.rs.client.ClientBuilder;
27+
28+
public class ClientBuilderListenerTest {
29+
30+
public static final String PROPERTY_NAME = "ClientBuilderListenerProperty";
31+
32+
@Priority(Priorities.USER + 1000)
33+
public static class FirstClientBuilderListener implements ClientBuilderListener {
34+
@Override
35+
public void onNewBuilder(ClientBuilder builder) {
36+
builder.withConfig(new ClientConfig().property(PROPERTY_NAME, 60));
37+
}
38+
}
39+
40+
public static class SecondClientBuilderListener implements ClientBuilderListener {
41+
@Override
42+
public void onNewBuilder(ClientBuilder builder) {
43+
builder.withConfig(new ClientConfig().property(PROPERTY_NAME, 50));
44+
}
45+
}
46+
47+
@Priority(Priorities.USER + 2000)
48+
public static class ThirdClientBuilderListener implements ClientBuilderListener {
49+
@Override
50+
public void onNewBuilder(ClientBuilder builder) {
51+
builder.withConfig(new ClientConfig().property(PROPERTY_NAME, 70));
52+
}
53+
}
54+
55+
@Test
56+
public void testClientBuilderListener() {
57+
Client client = ClientBuilder.newClient();
58+
Assert.assertEquals(70, client.getConfiguration().getProperty(PROPERTY_NAME));
59+
}
60+
61+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.glassfish.jersey.client.spi.ClientBuilderListenerTest$ThirdClientBuilderListener
2+
org.glassfish.jersey.client.spi.ClientBuilderListenerTest$FirstClientBuilderListener
3+
org.glassfish.jersey.client.spi.ClientBuilderListenerTest$SecondClientBuilderListener

0 commit comments

Comments
 (0)