[googletts] Dispose oAuth2 service (#14937)

* [googletts] Dispose oAuth2 service

Related to #14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-05-05 23:06:52 +02:00 committed by GitHub
parent 831a7af2c6
commit d18a524c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 17 deletions

View File

@ -113,11 +113,6 @@ class GoogleCloudAPI {
*/ */
private @Nullable GoogleTTSConfig config; private @Nullable GoogleTTSConfig config;
/**
* Status flag
*/
private boolean initialized;
private final Gson gson = new GsonBuilder().create(); private final Gson gson = new GsonBuilder().create();
private final ConfigurationAdmin configAdmin; private final ConfigurationAdmin configAdmin;
private final OAuthFactory oAuthFactory; private final OAuthFactory oAuthFactory;
@ -143,26 +138,27 @@ class GoogleCloudAPI {
void setConfig(GoogleTTSConfig config) { void setConfig(GoogleTTSConfig config) {
this.config = config; this.config = config;
if (oAuthService != null) {
oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
oAuthService = null;
}
String clientId = config.clientId; String clientId = config.clientId;
String clientSecret = config.clientSecret; String clientSecret = config.clientSecret;
if (clientId != null && !clientId.isEmpty() && clientSecret != null && !clientSecret.isEmpty()) { 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 { try {
final OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(
GoogleTTSService.SERVICE_PID, GCP_TOKEN_URI, GCP_AUTH_URI, clientId, clientSecret, GCP_SCOPE,
false);
this.oAuthService = oAuthService;
getAccessToken(); getAccessToken();
initialized = true;
initVoices(); initVoices();
} catch (AuthenticationException | CommunicationException e) { } catch (AuthenticationException | CommunicationException e) {
logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage()); logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
oAuthService = null; oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
initialized = false; this.oAuthService = null;
voices.clear(); voices.clear();
} }
} else { } else {
oAuthService = null;
initialized = false;
voices.clear(); 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 * 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. * the auth-code will be removed from the configuration.
@ -367,8 +371,10 @@ class GoogleCloudAPI {
return audio; return audio;
} catch (AuthenticationException | CommunicationException e) { } catch (AuthenticationException | CommunicationException e) {
logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage()); logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
oAuthService = null; if (oAuthService != null) {
initialized = false; oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
oAuthService = null;
}
voices.clear(); voices.clear();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.warn("Could not write file {} to cache: {}", audioFileInCache, e.getMessage()); logger.warn("Could not write file {} to cache: {}", audioFileInCache, e.getMessage());
@ -490,6 +496,6 @@ class GoogleCloudAPI {
} }
boolean isInitialized() { boolean isInitialized() {
return initialized; return oAuthService != null;
} }
} }

View File

@ -41,6 +41,7 @@ import org.osgi.framework.Constants;
import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate; 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.Deactivate;
import org.osgi.service.component.annotations.Modified; import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -139,6 +140,13 @@ public class GoogleTTSService implements TTSService {
updateConfig(config); updateConfig(config);
} }
@Deactivate
protected void dispose() {
apiImpl.dispose();
audioFormats.clear();
allVoices.clear();
}
/** /**
* Initializing audio formats. Google supports 3 formats: * Initializing audio formats. Google supports 3 formats:
* LINEAR16 * LINEAR16