]> git.basschouten.com Git - openhab-addons.git/commitdiff
[googletts] Dispose oAuth2 service (#14937)
authorlolodomo <lg.hc@free.fr>
Fri, 5 May 2023 21:06:52 +0000 (23:06 +0200)
committerGitHub <noreply@github.com>
Fri, 5 May 2023 21:06:52 +0000 (23:06 +0200)
* [googletts] Dispose oAuth2 service

Related to #14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleCloudAPI.java
bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java

index 8acb1cbaf6351e6f372f61de0c39fcbffd37c746..692d47170148941a6dd00fa1ffca5c20ff4af57c 100644 (file)
@@ -113,11 +113,6 @@ class GoogleCloudAPI {
      */
     private @Nullable GoogleTTSConfig config;
 
-    /**
-     * Status flag
-     */
-    private boolean initialized;
-
     private final Gson gson = new GsonBuilder().create();
     private final ConfigurationAdmin configAdmin;
     private final OAuthFactory oAuthFactory;
@@ -143,26 +138,27 @@ class GoogleCloudAPI {
     void setConfig(GoogleTTSConfig config) {
         this.config = config;
 
+        if (oAuthService != null) {
+            oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
+            oAuthService = null;
+        }
+
         String clientId = config.clientId;
         String clientSecret = config.clientSecret;
         if (clientId != null && !clientId.isEmpty() && clientSecret != null && !clientSecret.isEmpty()) {
+            final OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(GoogleTTSService.SERVICE_PID,
+                    GCP_TOKEN_URI, GCP_AUTH_URI, clientId, clientSecret, GCP_SCOPE, false);
+            this.oAuthService = oAuthService;
             try {
-                final OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(
-                        GoogleTTSService.SERVICE_PID, GCP_TOKEN_URI, GCP_AUTH_URI, clientId, clientSecret, GCP_SCOPE,
-                        false);
-                this.oAuthService = oAuthService;
                 getAccessToken();
-                initialized = true;
                 initVoices();
             } catch (AuthenticationException | CommunicationException e) {
                 logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
-                oAuthService = null;
-                initialized = false;
+                oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
+                this.oAuthService = null;
                 voices.clear();
             }
         } else {
-            oAuthService = null;
-            initialized = false;
             voices.clear();
         }
 
@@ -176,6 +172,14 @@ class GoogleCloudAPI {
         }
     }
 
+    public void dispose() {
+        if (oAuthService != null) {
+            oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
+            oAuthService = null;
+        }
+        voices.clear();
+    }
+
     /**
      * Fetches the OAuth2 tokens from Google Cloud Platform if the auth-code is set in the configuration. If successful
      * the auth-code will be removed from the configuration.
@@ -367,8 +371,10 @@ class GoogleCloudAPI {
             return audio;
         } catch (AuthenticationException | CommunicationException e) {
             logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
-            oAuthService = null;
-            initialized = false;
+            if (oAuthService != null) {
+                oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
+                oAuthService = null;
+            }
             voices.clear();
         } catch (FileNotFoundException e) {
             logger.warn("Could not write file {} to cache: {}", audioFileInCache, e.getMessage());
@@ -490,6 +496,6 @@ class GoogleCloudAPI {
     }
 
     boolean isInitialized() {
-        return initialized;
+        return oAuthService != null;
     }
 }
index 3e95e75bc5e4aa848d4d5a53f58422562355a328..66f682dafbd934c965ef0f3ef535f23272297f4d 100644 (file)
@@ -41,6 +41,7 @@ import org.osgi.framework.Constants;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Modified;
 import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
@@ -139,6 +140,13 @@ public class GoogleTTSService implements TTSService {
         updateConfig(config);
     }
 
+    @Deactivate
+    protected void dispose() {
+        apiImpl.dispose();
+        audioFormats.clear();
+        allVoices.clear();
+    }
+
     /**
      * Initializing audio formats. Google supports 3 formats:
      * LINEAR16