From 7d6cbeb049c2afb27d4273a4bbac8985aceefd99 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Mon, 30 Sep 2019 16:12:42 +0200 Subject: [PATCH] openvidu-server: new class KmsProperties refactoring --- .../kurento/kms/FixedOneKmsManager.java | 11 +++--- .../io/openvidu/server/kurento/kms/Kms.java | 7 ++-- .../server/kurento/kms/KmsManager.java | 11 +++++- .../server/kurento/kms/KmsProperties.java | 37 +++++++++++++++++++ .../server/recording/RecordingInfoUtils.java | 2 +- .../recording/service/RecordingManager.java | 2 +- .../server/utils/CommandExecutor.java | 4 +- .../io/openvidu/server/utils/JsonUtils.java | 37 +++++++++++++++---- 8 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsProperties.java diff --git a/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/FixedOneKmsManager.java b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/FixedOneKmsManager.java index cc6c89b1..9aaf7dce 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/FixedOneKmsManager.java +++ b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/FixedOneKmsManager.java @@ -26,15 +26,16 @@ import org.kurento.commons.exception.KurentoException; public class FixedOneKmsManager extends KmsManager { @Override - public List initializeKurentoClients(List kmsUris, boolean disconnectUponFailure) throws Exception { - final String kmsUri = kmsUris.get(0); + public List initializeKurentoClients(List kmsProperties, boolean disconnectUponFailure) + throws Exception { + KmsProperties firstProps = kmsProperties.get(0); KurentoClient kClient = null; - Kms kms = new Kms(kmsUri, loadManager); + Kms kms = new Kms(firstProps, loadManager); this.addKms(kms); try { - kClient = KurentoClient.create(kmsUri, this.generateKurentoConnectionListener(kms.getId())); + kClient = KurentoClient.create(firstProps.getUri(), this.generateKurentoConnectionListener(kms.getId())); } catch (KurentoException e) { - log.error("KMS in {} is not reachable by OpenVidu Server", kmsUri); + log.error("KMS in {} is not reachable by OpenVidu Server", firstProps.getUri()); if (kClient != null) { kClient.destroy(); } diff --git a/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/Kms.java b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/Kms.java index e7b6ede9..862570f2 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/Kms.java +++ b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/Kms.java @@ -25,7 +25,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; -import org.apache.commons.lang3.RandomStringUtils; import org.kurento.client.KurentoClient; import org.kurento.client.ModuleInfo; import org.kurento.client.ServerInfo; @@ -64,9 +63,9 @@ public class Kms { private Map kurentoSessions = new ConcurrentHashMap<>(); - public Kms(String uri, LoadManager loadManager) { - this.uri = uri; - this.id = "KMS-" + RandomStringUtils.randomAlphanumeric(6).toUpperCase(); + public Kms(KmsProperties props, LoadManager loadManager) { + this.id = props.getId(); + this.uri = props.getUri(); String parsedUri = uri.replaceAll("^ws://", "http://").replaceAll("^wss://", "https://"); URL url = null; diff --git a/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsManager.java b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsManager.java index 8140a6ce..dd0b3563 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsManager.java +++ b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsManager.java @@ -28,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import org.apache.commons.lang3.RandomStringUtils; import org.kurento.client.KurentoConnectionListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -184,13 +185,19 @@ public abstract class KmsManager { }; } - public abstract List initializeKurentoClients(List kmsUris, boolean disconnectUponFailure) + public abstract List initializeKurentoClients(List kmsProperties, boolean disconnectUponFailure) throws Exception; @PostConstruct private void postConstruct() { try { - this.initializeKurentoClients(this.openviduConfig.getKmsUris(), true); + List kmsProps = new ArrayList<>(); + String kmsId; + for (String kmsUri : this.openviduConfig.getKmsUris()) { + kmsId = "KMS-" + RandomStringUtils.randomAlphanumeric(6).toUpperCase(); + kmsProps.add(new KmsProperties(kmsId, kmsUri)); + } + this.initializeKurentoClients(kmsProps, true); } catch (Exception e) { // Some KMS wasn't reachable log.error("Shutting down OpenVidu Server"); diff --git a/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsProperties.java b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsProperties.java new file mode 100644 index 00000000..7230de74 --- /dev/null +++ b/openvidu-server/src/main/java/io/openvidu/server/kurento/kms/KmsProperties.java @@ -0,0 +1,37 @@ +/* + * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package io.openvidu.server.kurento.kms; + +public class KmsProperties { + + private String id; + private String uri; + + public KmsProperties(String id, String uri) { + this.id = id; + this.uri = uri; + } + + public String getId() { + return id; + } + + public String getUri() { + return uri; + } +} \ No newline at end of file diff --git a/openvidu-server/src/main/java/io/openvidu/server/recording/RecordingInfoUtils.java b/openvidu-server/src/main/java/io/openvidu/server/recording/RecordingInfoUtils.java index 98128381..db438391 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/recording/RecordingInfoUtils.java +++ b/openvidu-server/src/main/java/io/openvidu/server/recording/RecordingInfoUtils.java @@ -44,7 +44,7 @@ public class RecordingInfoUtils { this.infoFilePath = infoFilePath; try { - this.json = new JsonUtils().fromFileToJson(infoFilePath); + this.json = new JsonUtils().fromFileToJsonObject(infoFilePath); } catch (JsonIOException | JsonSyntaxException e) { // Recording metadata from ffprobe is not a JSON: video file is corrupted throw new OpenViduException(Code.RECORDING_FILE_EMPTY_ERROR, "The recording file is corrupted"); diff --git a/openvidu-server/src/main/java/io/openvidu/server/recording/service/RecordingManager.java b/openvidu-server/src/main/java/io/openvidu/server/recording/service/RecordingManager.java index 50eca50a..a53c8c2a 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/recording/service/RecordingManager.java +++ b/openvidu-server/src/main/java/io/openvidu/server/recording/service/RecordingManager.java @@ -408,7 +408,7 @@ public class RecordingManager { if (file.isFile() && file.getName().startsWith(RecordingManager.RECORDING_ENTITY_FILE)) { JsonObject json; try { - json = jsonUtils.fromFileToJson(file.getAbsolutePath()); + json = jsonUtils.fromFileToJsonObject(file.getAbsolutePath()); } catch (JsonIOException | JsonSyntaxException | IOException e) { log.error("Error reading recording entity file {}: {}", file.getAbsolutePath(), (e.getMessage())); return null; diff --git a/openvidu-server/src/main/java/io/openvidu/server/utils/CommandExecutor.java b/openvidu-server/src/main/java/io/openvidu/server/utils/CommandExecutor.java index b191d64f..e486235c 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/utils/CommandExecutor.java +++ b/openvidu-server/src/main/java/io/openvidu/server/utils/CommandExecutor.java @@ -36,11 +36,11 @@ public class CommandExecutor { return commonExecCommand(processBuilder); } - public static String execCommandRedirectStandardOutputAndError(File standardOutputFile, File errorOutputFile, + public static void execCommandRedirectStandardOutputAndError(File standardOutputFile, File errorOutputFile, String... command) throws IOException, InterruptedException { ProcessBuilder processBuilder = new ProcessBuilder(command).redirectOutput(standardOutputFile) .redirectError(errorOutputFile); - return commonExecCommand(processBuilder); + commonExecCommand(processBuilder); } private static String commonExecCommand(ProcessBuilder processBuilder) throws IOException, InterruptedException { diff --git a/openvidu-server/src/main/java/io/openvidu/server/utils/JsonUtils.java b/openvidu-server/src/main/java/io/openvidu/server/utils/JsonUtils.java index 1ce87af2..d51c0160 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/utils/JsonUtils.java +++ b/openvidu-server/src/main/java/io/openvidu/server/utils/JsonUtils.java @@ -17,17 +17,18 @@ package io.openvidu.server.utils; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Map.Entry; import org.kurento.jsonrpc.Props; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; -import com.google.gson.JsonIOException; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; public class JsonUtils { @@ -43,14 +44,36 @@ public class JsonUtils { return props; } - public JsonObject fromFileToJson(String filePath) throws JsonIOException, JsonSyntaxException, IOException { - JsonObject json; + public JsonObject fromFileToJsonObject(String filePath) + throws IOException, FileNotFoundException, JsonParseException, IllegalStateException { + return this.fromFileToJsonElement(filePath).getAsJsonObject(); + } + + public JsonArray fromFileToJsonArray(String filePath) + throws IOException, FileNotFoundException, JsonParseException, IllegalStateException { + return this.fromFileToJsonElement(filePath).getAsJsonArray(); + } + + public JsonElement fromFileToJsonElement(String filePath) + throws IOException, FileNotFoundException, JsonParseException, IllegalStateException { + JsonElement json = null; JsonParser parser = new JsonParser(); - FileReader reader = new FileReader(filePath); + FileReader reader = null; try { - json = parser.parse(new FileReader(filePath)).getAsJsonObject(); + reader = new FileReader(filePath); + } catch (FileNotFoundException e) { + throw e; + } + try { + json = parser.parse(reader); + } catch (JsonParseException | IllegalStateException exception) { + throw exception; } finally { - reader.close(); + try { + reader.close(); + } catch (IOException e) { + throw e; + } } return json; }