From 5f8101a44c004332ed9f79bbedfeef7faef650e1 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 3 May 2018 13:14:54 -0500 Subject: [PATCH] Make RepositoriesMetaData contents unmodifiable (#30361) This commit makes the RepositoriesMetaData backing list no longer modifiable. Ref #30333 --- .../elasticsearch/cluster/metadata/RepositoriesMetaData.java | 5 +++-- .../snapshots/RepositoriesMetaDataSerializationTests.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/RepositoriesMetaData.java b/server/src/main/java/org/elasticsearch/cluster/metadata/RepositoriesMetaData.java index 7a0b9285896..c813ba76e82 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/RepositoriesMetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/RepositoriesMetaData.java @@ -33,6 +33,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.EnumSet; import java.util.List; @@ -51,7 +52,7 @@ public class RepositoriesMetaData extends AbstractNamedDiffable implemen * @param repositories list of repositories */ public RepositoriesMetaData(List repositories) { - this.repositories = repositories; + this.repositories = Collections.unmodifiableList(repositories); } /** @@ -107,7 +108,7 @@ public class RepositoriesMetaData extends AbstractNamedDiffable implemen for (int i = 0; i < repository.length; i++) { repository[i] = new RepositoryMetaData(in); } - this.repositories = Arrays.asList(repository); + this.repositories = Collections.unmodifiableList(Arrays.asList(repository)); } public static NamedDiff readDiffFrom(StreamInput in) throws IOException { diff --git a/server/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java b/server/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java index 7627dafa5a9..17ae1def235 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java +++ b/server/src/test/java/org/elasticsearch/snapshots/RepositoriesMetaDataSerializationTests.java @@ -112,7 +112,7 @@ public class RepositoriesMetaDataSerializationTests extends AbstractDiffableSeri assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); RepositoriesMetaData repositoriesMetaData = RepositoriesMetaData.fromXContent(parser); assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); - List repos = repositoriesMetaData.repositories(); + List repos = new ArrayList<>(repositoriesMetaData.repositories()); repos.sort(Comparator.comparing(RepositoryMetaData::name)); return new RepositoriesMetaData(repos); }