Remove Jersey ClientBuilder compatibility (#8561)

Fixes #7531

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2020-09-24 19:40:12 +02:00 committed by GitHub
parent cc042dabff
commit f0ee7ffb2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 94 deletions

View File

@ -14,16 +14,16 @@ package org.openhab.binding.lametrictime.internal;
import static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.*; import static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.*;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.lametrictime.internal.discovery.LaMetricTimeAppDiscoveryService; import org.openhab.binding.lametrictime.internal.discovery.LaMetricTimeAppDiscoveryService;
import org.openhab.binding.lametrictime.internal.handler.ClockAppHandler; import org.openhab.binding.lametrictime.internal.handler.ClockAppHandler;
import org.openhab.binding.lametrictime.internal.handler.CountdownAppHandler; import org.openhab.binding.lametrictime.internal.handler.CountdownAppHandler;
@ -40,9 +40,9 @@ import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.framework.ServiceRegistration; import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -53,33 +53,31 @@ import org.slf4j.LoggerFactory;
* @author Gregory Moyer - Initial contribution * @author Gregory Moyer - Initial contribution
*/ */
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.lametrictime") @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lametrictime")
@NonNullByDefault
public class LaMetricTimeHandlerFactory extends BaseThingHandlerFactory { public class LaMetricTimeHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections.unmodifiableSet( private static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Set.of(THING_TYPE_DEVICE, THING_TYPE_CLOCK_APP,
Stream.of(THING_TYPE_DEVICE, THING_TYPE_CLOCK_APP, THING_TYPE_COUNTDOWN_APP, THING_TYPE_RADIO_APP, THING_TYPE_COUNTDOWN_APP, THING_TYPE_RADIO_APP, THING_TYPE_STOPWATCH_APP, THING_TYPE_WEATHER_APP);
THING_TYPE_STOPWATCH_APP, THING_TYPE_WEATHER_APP).collect(Collectors.toSet()));
// TODO: Those constants are Jersey specific - once we move away from Jersey, private static final int EVENT_STREAM_CONNECT_TIMEOUT = 10;
// this can be removed and the client builder creation simplified. private static final int EVENT_STREAM_READ_TIMEOUT = 10;
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 Logger logger = LoggerFactory.getLogger(LaMetricTimeHandlerFactory.class);
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>(); private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
@Reference(cardinality = ReferenceCardinality.OPTIONAL) private final ClientBuilder clientBuilder;
private ClientBuilder injectedClientBuilder;
private ClientBuilder clientBuilder; private final StateDescriptionOptionsProvider stateDescriptionProvider;
private StateDescriptionOptionsProvider stateDescriptionProvider; @Activate
public LaMetricTimeHandlerFactory(@Reference ClientBuilder clientBuilder,
@Reference StateDescriptionOptionsProvider stateDescriptionProvider) {
this.clientBuilder = clientBuilder //
.connectTimeout(EVENT_STREAM_CONNECT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(EVENT_STREAM_READ_TIMEOUT, TimeUnit.SECONDS);
this.stateDescriptionProvider = stateDescriptionProvider;
}
@Override @Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) { public boolean supportsThingType(ThingTypeUID thingTypeUID) {
@ -87,14 +85,14 @@ public class LaMetricTimeHandlerFactory extends BaseThingHandlerFactory {
} }
@Override @Override
protected ThingHandler createHandler(Thing thing) { protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID(); ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (THING_TYPE_DEVICE.equals(thingTypeUID)) { if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
logger.debug("Creating handler for LaMetric Time device {}", thing); logger.debug("Creating handler for LaMetric Time device {}", thing);
LaMetricTimeHandler deviceHandler = new LaMetricTimeHandler((Bridge) thing, stateDescriptionProvider, LaMetricTimeHandler deviceHandler = new LaMetricTimeHandler((Bridge) thing, stateDescriptionProvider,
getClientBuilder()); clientBuilder);
registerAppDiscoveryService(deviceHandler); registerAppDiscoveryService(deviceHandler);
return deviceHandler; return deviceHandler;
@ -152,33 +150,4 @@ public class LaMetricTimeHandlerFactory extends BaseThingHandlerFactory {
serviceReg.unregister(); serviceReg.unregister();
} }
} }
@Reference
protected void setDynamicStateDescriptionProvider(StateDescriptionOptionsProvider provider) {
this.stateDescriptionProvider = provider;
}
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;
}
} }

View File

@ -15,11 +15,12 @@ package org.openhab.binding.tesla.internal;
import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*; import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler; import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler;
import org.openhab.binding.tesla.internal.handler.TeslaVehicleHandler; import org.openhab.binding.tesla.internal.handler.TeslaVehicleHandler;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
@ -28,9 +29,9 @@ import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory; import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
/** /**
* The {@link TeslaHandlerFactory} is responsible for creating things and thing * The {@link TeslaHandlerFactory} is responsible for creating things and thing
@ -41,27 +42,23 @@ import org.osgi.service.component.annotations.ReferenceCardinality;
* @author Kai Kreuzer - Introduced account handler * @author Kai Kreuzer - Introduced account handler
*/ */
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla") @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
@NonNullByDefault
public class TeslaHandlerFactory extends BaseThingHandlerFactory { public class TeslaHandlerFactory extends BaseThingHandlerFactory {
// TODO: Those constants are Jersey specific - once we move away from Jersey, private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3;
// this can be removed and the client builder creation simplified. private static final int EVENT_STREAM_READ_TIMEOUT = 200;
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 Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS,
public static final String CONNECT_TIMEOUT = "http.connection.timeout"; THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY);
private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3000; private final ClientBuilder clientBuilder;
private static final int EVENT_STREAM_READ_TIMEOUT = 200000;
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream @Activate
.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS, THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY) public TeslaHandlerFactory(@Reference ClientBuilder clientBuilder) {
.collect(Collectors.toSet()); this.clientBuilder = clientBuilder //
.connectTimeout(EVENT_STREAM_CONNECT_TIMEOUT, TimeUnit.SECONDS)
@Reference(cardinality = ReferenceCardinality.OPTIONAL) .readTimeout(EVENT_STREAM_READ_TIMEOUT, TimeUnit.SECONDS);
private ClientBuilder injectedClientBuilder; }
private ClientBuilder clientBuilder;
@Override @Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) { public boolean supportsThingType(ThingTypeUID thingTypeUID) {
@ -69,33 +66,13 @@ public class TeslaHandlerFactory extends BaseThingHandlerFactory {
} }
@Override @Override
protected ThingHandler createHandler(Thing thing) { protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID(); ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) { if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
return new TeslaAccountHandler((Bridge) thing, getClientBuilder().build()); return new TeslaAccountHandler((Bridge) thing, clientBuilder.build());
} else { } else {
return new TeslaVehicleHandler(thing, getClientBuilder()); return new TeslaVehicleHandler(thing, clientBuilder);
} }
} }
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;
}
} }