-
Notifications
You must be signed in to change notification settings - Fork 18
/
JaxRSClientContainer.java
189 lines (158 loc) · 6.59 KB
/
JaxRSClientContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
* Copyright (c) 2018 Composent, Inc. and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Composent, Inc. - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.provider.jaxrs.client;
import java.io.InvalidObjectException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.ext.ContextResolver;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.provider.internal.jaxrs.client.WebResourceFactory;
import org.eclipse.ecf.provider.jaxrs.ObjectMapperContextResolver;
import org.eclipse.ecf.remoteservice.IRemoteService;
import org.eclipse.ecf.remoteservice.asyncproxy.AsyncReturnUtil;
import org.eclipse.ecf.remoteservice.client.AbstractClientContainer;
import org.eclipse.ecf.remoteservice.client.AbstractRSAClientContainer;
import org.eclipse.ecf.remoteservice.client.AbstractRSAClientService;
import org.eclipse.ecf.remoteservice.client.RemoteServiceClientRegistration;
import org.eclipse.ecf.remoteservice.events.IRemoteCallCompleteEvent;
public class JaxRSClientContainer extends AbstractRSAClientContainer {
public static final int JACKSON_DEFAULT_PRIORITY = Integer
.valueOf(System.getProperty(JaxRSClientContainer.class.getName() + ".jacksonDefaultPriority", String.valueOf(javax.ws.rs.Priorities.USER)));
protected Configuration configuration;
protected int jacksonPriority = JACKSON_DEFAULT_PRIORITY;
protected List<JaxRSClientRemoteService> remoteServices = Collections
.synchronizedList(new ArrayList<JaxRSClientRemoteService>());
public JaxRSClientContainer(ID containerID, Configuration configuration, int jacksonPriority) {
super(containerID);
this.configuration = configuration;
this.jacksonPriority = jacksonPriority;
}
@Override
public void dispose() {
super.dispose();
synchronized (this.remoteServices) {
this.remoteServices.forEach(c -> c.dispose());
}
this.configuration = null;
}
protected class JaxRSClientRemoteService extends AbstractRSAClientService {
protected Map<Class<?>, Object> interfaceProxyMap = Collections
.synchronizedMap(new HashMap<Class<?>, Object>());
protected Client client;
public JaxRSClientRemoteService(AbstractClientContainer container,
RemoteServiceClientRegistration registration) {
super(container, registration);
}
@Override
public void dispose() {
super.dispose();
if (client != null) {
client.close();
client = null;
}
interfaceProxyMap.clear();
}
protected Object getProxyForInterface(Class<?> intfClass) {
return this.interfaceProxyMap.get(intfClass);
}
protected Object invokeViaProxy(RSARemoteCall call) throws Exception {
Method m = call.getReflectMethod();
Object proxy = getProxyForInterface(m.getDeclaringClass());
if (proxy == null)
throw new InvalidObjectException("Proxy for remote method=" + m.getName() + " not found");
return m.invoke(proxy, call.getParameters());
}
@Override
protected Callable<Object> getSyncCallable(RSARemoteCall call) {
return () -> invokeViaProxy(call);
}
@Override
protected Callable<IRemoteCallCompleteEvent> getAsyncCallable(RSARemoteCall call) {
return () -> createRCCESuccess(AsyncReturnUtil.convertAsyncToReturn(invokeViaProxy(call),
call.getReflectMethod().getReturnType(), 0));
}
@Override
public Object getProxy(ClassLoader cl, @SuppressWarnings("rawtypes") Class[] interfaces) throws ECFException {
if (interfaces.length == 0)
throw new ECFException("At least one interface must be provided to create a jaxRSProxy");
try {
// If nothing in the interface -> proxy map
if (this.interfaceProxyMap.size() == 0) {
// Create configuration
Configuration config = createJaxRSClientConfiguration();
// create client
client = createJaxRSClient(config, cl);
// get WebTarget from client
WebTarget webtarget = getJaxRSWebTarget(client);
// For all interfaces create proxy and associate with interface class
// Can then be used at invoke time to get right proxy for given method
// invocation
for (Class<?> clazz : interfaces)
this.interfaceProxyMap.put(clazz, createJaxRSProxy(clazz, webtarget));
}
return super.createProxy(cl, interfaces);
} catch (Exception t) {
ECFException e = new ECFException(t.getMessage());
e.setStackTrace(t.getStackTrace());
throw e;
}
}
protected <T> T createJaxRSProxy(Class<T> interfaceClass, WebTarget webTarget) throws Exception {
return WebResourceFactory.newResource(interfaceClass, webTarget, isOSGIAsync());
}
protected Configuration createJaxRSClientConfiguration() throws ECFException {
return configuration;
}
protected Client createJaxRSClient(Configuration configuration) throws ECFException {
return createJaxRSClient(configuration, this.getClass().getClassLoader());
}
protected Client createJaxRSClient(Configuration configuration, ClassLoader cl) throws ECFException {
ClientBuilder cb = ClientBuilder.newBuilder();
boolean isContextResolverNeeded = true;
if (configuration != null) {
cb.withConfig(configuration);
for (Object registeredInstance : configuration.getInstances()) {
if (registeredInstance instanceof ContextResolver) {
isContextResolverNeeded = false;
}
}
}
if (isContextResolverNeeded)
cb.register(new ObjectMapperContextResolver(), ContextResolver.class);
cb.register(new JaxRSClientJacksonFeature(getRegistration(), cl), jacksonPriority);
return cb.build();
}
protected WebTarget getJaxRSWebTarget(Client client) throws ECFException {
return client.target(getConnectedTarget());
}
protected String getConnectedTarget() {
ID targetID = getConnectedID();
if (targetID == null)
return null;
return targetID.getName();
}
}
@Override
protected IRemoteService createRemoteService(RemoteServiceClientRegistration registration) {
JaxRSClientRemoteService rs = new JaxRSClientRemoteService(this, registration);
this.remoteServices.add(rs);
return rs;
}
}