diff --git a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/command/TeslaCommandExtension.java b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/command/TeslaCommandExtension.java index 43dd85f300cb7..54b8a197ecc34 100644 --- a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/command/TeslaCommandExtension.java +++ b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/command/TeslaCommandExtension.java @@ -26,6 +26,8 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.config.discovery.DiscoveryResult; import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder; import org.eclipse.smarthome.core.thing.ThingUID; @@ -38,8 +40,10 @@ import org.openhab.binding.tesla.internal.protocol.TokenRequest; import org.openhab.binding.tesla.internal.protocol.TokenRequestPassword; import org.openhab.binding.tesla.internal.protocol.TokenResponse; +import org.osgi.service.component.annotations.Activate; 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; @@ -51,6 +55,7 @@ * @author Nicolai Grødum - Initial contribution * @author Kai Kreuzer - refactored to use Tesla account thing */ +@NonNullByDefault @Component(service = ConsoleCommandExtension.class, immediate = true) public class TeslaCommandExtension extends AbstractConsoleCommandExtension { @@ -58,23 +63,17 @@ public class TeslaCommandExtension extends AbstractConsoleCommandExtension { private final Logger logger = LoggerFactory.getLogger(TeslaCommandExtension.class); - private final Client teslaClient = ClientBuilder.newClient(); - private final WebTarget teslaTarget = teslaClient.target(URI_OWNERS); - private final WebTarget tokenTarget = teslaTarget.path(URI_ACCESS_TOKEN); + @Reference(cardinality = ReferenceCardinality.OPTIONAL) + private @Nullable ClientBuilder injectedClientBuilder; - private TeslaAccountDiscoveryService teslaAccountDiscoveryService; + private @Nullable WebTarget tokenTarget; - public TeslaCommandExtension() { - super("tesla", "Interact with the Tesla integration."); - } - - @Reference - protected void setTeslaAccountDiscoveryService(TeslaAccountDiscoveryService ds) { - this.teslaAccountDiscoveryService = ds; - } + private final TeslaAccountDiscoveryService teslaAccountDiscoveryService; - protected void unsetTeslaAccountDiscoveryService(TeslaAccountDiscoveryService ds) { - this.teslaAccountDiscoveryService = null; + @Activate + public TeslaCommandExtension(@Reference TeslaAccountDiscoveryService teslaAccountDiscoveryService) { + super("tesla", "Interact with the Tesla integration."); + this.teslaAccountDiscoveryService = teslaAccountDiscoveryService; } @Override @@ -127,7 +126,8 @@ private void login(Console console, String username, String password) { TokenRequest token = new TokenRequestPassword(username, password); String payLoad = gson.toJson(token); - Response response = tokenTarget.request().post(Entity.entity(payLoad, MediaType.APPLICATION_JSON_TYPE)); + Response response = getTokenTarget().request() + .post(Entity.entity(payLoad, MediaType.APPLICATION_JSON_TYPE)); if (response != null) { if (response.getStatus() == 200 && response.hasEntity()) { @@ -152,4 +152,28 @@ private void login(Console console, String username, String password) { logger.error("Could not get refresh token.", e); } } + + private synchronized WebTarget getTokenTarget() { + WebTarget target = this.tokenTarget; + if (target != null) { + return target; + } else { + Client client; + try { + client = ClientBuilder.newBuilder().build(); + } catch (Exception e) { + // we seem to have no Jersey, so let's hope for an injected builder by CXF + if (this.injectedClientBuilder != null) { + client = injectedClientBuilder.build(); + } else { + throw new IllegalStateException("No JAX RS Client Builder available."); + } + } + WebTarget teslaTarget = client.target(URI_OWNERS); + target = teslaTarget.path(URI_ACCESS_TOKEN); + this.tokenTarget = target; + return target; + } + } + }