Skip to content

Commit

Permalink
Use CXF JAX-RS client builder, if available (openhab#7790)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <[email protected]>
  • Loading branch information
kaikreuzer authored and andrewfg committed Aug 31, 2020
1 parent 0a8ee1e commit b16b875
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.ws.rs.client.ClientBuilder;

import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
Expand All @@ -40,6 +42,7 @@
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -56,10 +59,26 @@ public class LaMetricTimeHandlerFactory extends BaseThingHandlerFactory {
Stream.of(THING_TYPE_DEVICE, THING_TYPE_CLOCK_APP, THING_TYPE_COUNTDOWN_APP, THING_TYPE_RADIO_APP,
THING_TYPE_STOPWATCH_APP, THING_TYPE_WEATHER_APP).collect(Collectors.toSet()));

// TODO: Those constants are Jersey specific - once we move away from Jersey,
// this can be removed and the client builder creation simplified.
public static final String READ_TIMEOUT_JERSEY = "jersey.config.client.readTimeout";
public static final String CONNECT_TIMEOUT_JERSEY = "jersey.config.client.connectTimeout";

public static final String READ_TIMEOUT = "http.receive.timeout";
public static final String CONNECT_TIMEOUT = "http.connection.timeout";

private static final int EVENT_STREAM_CONNECT_TIMEOUT = 10000;
private static final int EVENT_STREAM_READ_TIMEOUT = 10000;

private final Logger logger = LoggerFactory.getLogger(LaMetricTimeHandlerFactory.class);

private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();

@Reference(cardinality = ReferenceCardinality.OPTIONAL)
private ClientBuilder injectedClientBuilder;

private ClientBuilder clientBuilder;

private StateDescriptionOptionsProvider stateDescriptionProvider;

@Override
Expand All @@ -74,7 +93,8 @@ protected ThingHandler createHandler(Thing thing) {
if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
logger.debug("Creating handler for LaMetric Time device {}", thing);

LaMetricTimeHandler deviceHandler = new LaMetricTimeHandler((Bridge) thing, stateDescriptionProvider);
LaMetricTimeHandler deviceHandler = new LaMetricTimeHandler((Bridge) thing, stateDescriptionProvider,
getClientBuilder());
registerAppDiscoveryService(deviceHandler);

return deviceHandler;
Expand Down Expand Up @@ -141,4 +161,25 @@ protected void setDynamicStateDescriptionProvider(StateDescriptionOptionsProvide
protected void unsetDynamicStateDescriptionProvider(StateDescriptionOptionsProvider provider) {
this.stateDescriptionProvider = null;
}

private synchronized ClientBuilder getClientBuilder() {
if (clientBuilder == null) {
try {
clientBuilder = ClientBuilder.newBuilder();
clientBuilder.property(CONNECT_TIMEOUT_JERSEY, EVENT_STREAM_CONNECT_TIMEOUT);
clientBuilder.property(READ_TIMEOUT_JERSEY, EVENT_STREAM_READ_TIMEOUT);
} catch (Exception e) {
// we seem to have no Jersey, so let's hope for an injected builder by CXF
if (this.injectedClientBuilder != null) {
clientBuilder = injectedClientBuilder;
clientBuilder.property(CONNECT_TIMEOUT, EVENT_STREAM_CONNECT_TIMEOUT);
clientBuilder.property(READ_TIMEOUT, EVENT_STREAM_READ_TIMEOUT);
} else {
throw new IllegalStateException("No JAX RS Client Builder available.");
}
}
}
return clientBuilder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,20 @@ public class LaMetricTimeHandler extends ConfigStatusBridgeHandler {

private static final long CONNECTION_CHECK_INTERVAL = 60;

// TODO: Those constants are Jersey specific - once we move away from Jersey,
// this must be changed to https://stackoverflow.com/a/49736022 (assuming we have a JAX-RS 2.1 implementation).
public static final String READ_TIMEOUT = "jersey.config.client.readTimeout";
public static final String CONNECT_TIMEOUT = "jersey.config.client.connectTimeout";

private final Logger logger = LoggerFactory.getLogger(LaMetricTimeHandler.class);

private final StateDescriptionOptionsProvider stateDescriptionProvider;

private final ClientBuilder clientBuilder;

private LaMetricTime clock;

private ScheduledFuture<?> connectionJob;

public LaMetricTimeHandler(Bridge bridge, StateDescriptionOptionsProvider stateDescriptionProvider) {
public LaMetricTimeHandler(Bridge bridge, StateDescriptionOptionsProvider stateDescriptionProvider,
ClientBuilder clientBuilder) {
super(bridge);
this.clientBuilder = clientBuilder;
this.stateDescriptionProvider = stateDescriptionProvider;

if (stateDescriptionProvider == null) {
Expand All @@ -103,8 +102,6 @@ public void initialize() {
logger.debug("Creating LaMetric Time client");
Configuration clockConfig = new Configuration().withDeviceHost(bindingConfig.host)
.withDeviceApiKey(bindingConfig.apiKey).withLogging(logger.isDebugEnabled());
ClientBuilder clientBuilder = ClientBuilder.newBuilder().property(CONNECT_TIMEOUT, 10000).property(READ_TIMEOUT,
10000);
clock = LaMetricTime.create(clockConfig, clientBuilder);

connectionJob = scheduler.scheduleWithFixedDelay(() -> {
Expand Down

0 comments on commit b16b875

Please sign in to comment.