1 /* 2 * Copyright 2020 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import java.util.ArrayList; 20 import java.util.Arrays; 21 import java.util.Collections; 22 import java.util.List; 23 24 /** 25 * Provides a list of {@link ServerCredentials}, where any one may be used. The credentials are in 26 * preference order. 27 */ 28 public final class ChoiceServerCredentials extends ServerCredentials { 29 /** 30 * Constructs with the provided {@code creds} as options, with preferred credentials first. 31 * 32 * @throws IllegalArgumentException if no creds are provided 33 */ create(ServerCredentials... creds)34 public static ServerCredentials create(ServerCredentials... creds) { 35 if (creds.length == 0) { 36 throw new IllegalArgumentException("At least one credential is required"); 37 } 38 return new ChoiceServerCredentials(creds); 39 } 40 41 private final List<ServerCredentials> creds; 42 ChoiceServerCredentials(ServerCredentials... creds)43 private ChoiceServerCredentials(ServerCredentials... creds) { 44 for (ServerCredentials cred : creds) { 45 if (cred == null) { 46 throw new NullPointerException(); 47 } 48 } 49 this.creds = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(creds))); 50 } 51 52 /** Non-empty list of credentials, in preference order. */ getCredentialsList()53 public List<ServerCredentials> getCredentialsList() { 54 return creds; 55 } 56 } 57