Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[googletts] Dispose oAuth2 service #14937

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -143,26 +138,27 @@ class GoogleCloudAPI {
void setConfig(GoogleTTSConfig config) {
this.config = config;

if (oAuthService != null) {
oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
oAuthService = null;
lolodomo marked this conversation as resolved.
Show resolved Hide resolved
}

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();
}

Expand All @@ -176,6 +172,14 @@ void setConfig(GoogleTTSConfig config) {
}
}

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.
Expand Down Expand Up @@ -367,8 +371,10 @@ public byte[] synthesizeSpeech(String text, GoogleTTSVoice voice, String codec)
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());
Expand Down Expand Up @@ -490,6 +496,6 @@ private String getUniqueFilenameForText(String text, String voiceName) {
}

boolean isInitialized() {
return initialized;
return oAuthService != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -139,6 +140,13 @@ protected void activate(Map<String, Object> config) {
updateConfig(config);
}

@Deactivate
protected void dispose() {
apiImpl.dispose();
audioFormats.clear();
allVoices.clear();
}

/**
* Initializing audio formats. Google supports 3 formats:
* LINEAR16
Expand Down