001/* 002 * Copyright 2009-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2009-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2015-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.sdk.unboundidds.extensions; 037 038 039 040import java.io.Serializable; 041 042import com.unboundid.asn1.ASN1Element; 043import com.unboundid.asn1.ASN1Integer; 044import com.unboundid.asn1.ASN1OctetString; 045import com.unboundid.asn1.ASN1Sequence; 046import com.unboundid.ldap.sdk.LDAPException; 047import com.unboundid.ldap.sdk.ResultCode; 048import com.unboundid.util.Debug; 049import com.unboundid.util.NotMutable; 050import com.unboundid.util.StaticUtils; 051import com.unboundid.util.ThreadSafety; 052import com.unboundid.util.ThreadSafetyLevel; 053import com.unboundid.util.Validator; 054 055import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 056 057 058 059/** 060 * This class provides a data structure for holding information about the 061 * configuration of backend sets as used by the stream proxy values extended 062 * request. 063 * <BR> 064 * <BLOCKQUOTE> 065 * <B>NOTE:</B> This class, and other classes within the 066 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 067 * supported for use against Ping Identity, UnboundID, and 068 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 069 * for proprietary functionality or for external specifications that are not 070 * considered stable or mature enough to be guaranteed to work in an 071 * interoperable way with other types of LDAP servers. 072 * </BLOCKQUOTE> 073 */ 074@NotMutable() 075@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 076public final class StreamProxyValuesBackendSet 077 implements Serializable 078{ 079 /** 080 * The serial version UID for this serializable class. 081 */ 082 private static final long serialVersionUID = -5437145469462592611L; 083 084 085 086 // The backend set ID for this backend set. 087 private final ASN1OctetString backendSetID; 088 089 // The ports of the directory servers in this backend set. 090 private final int[] ports; 091 092 // The addresses of the directory servers in this backend set. 093 private final String[] hosts; 094 095 096 097 /** 098 * Creates a new backend set with the provided information. 099 * 100 * @param backendSetID The backend set ID for this backend set. It must not 101 * be {@code null}. 102 * @param hosts The addresses of the servers for this backend set. 103 * It must not be {@code null} or empty, and it must 104 * have the same number of elements as the {@code ports} 105 * array. 106 * @param ports The ports of the servers for this backend set. It 107 * must not be {@code null} or empty, and it must have 108 * the same number of elements as the {@code hosts} 109 * array. 110 */ 111 public StreamProxyValuesBackendSet(final ASN1OctetString backendSetID, 112 final String[] hosts, final int[] ports) 113 { 114 Validator.ensureNotNull(backendSetID, hosts, ports); 115 Validator.ensureTrue(hosts.length > 0); 116 Validator.ensureTrue(hosts.length == ports.length); 117 118 this.backendSetID = backendSetID; 119 this.hosts = hosts; 120 this.ports = ports; 121 } 122 123 124 125 /** 126 * Retrieves the backend set ID for this backend set. 127 * 128 * @return The backend set ID for this backend set. 129 */ 130 public ASN1OctetString getBackendSetID() 131 { 132 return backendSetID; 133 } 134 135 136 137 /** 138 * Retrieves the addresses of the servers for this backend set. 139 * 140 * @return The addresses of the servers for this backend set. 141 */ 142 public String[] getHosts() 143 { 144 return hosts; 145 } 146 147 148 149 /** 150 * Retrieves the ports of the servers for this backend set. 151 * 152 * @return The ports of the servers for this backend set. 153 */ 154 public int[] getPorts() 155 { 156 return ports; 157 } 158 159 160 161 /** 162 * Encodes this backend set object in a form suitable for inclusion in the 163 * value of the stream proxy values extended request. 164 * 165 * @return The encoded representation of this backend set. 166 */ 167 public ASN1Element encode() 168 { 169 final ASN1Element[] hostPortElements = new ASN1Element[hosts.length]; 170 for (int i=0; i < hosts.length; i++) 171 { 172 hostPortElements[i] = new ASN1Sequence( 173 new ASN1OctetString(hosts[i]), 174 new ASN1Integer(ports[i])); 175 } 176 177 return new ASN1Sequence( 178 backendSetID, 179 new ASN1Sequence(hostPortElements)); 180 } 181 182 183 184 /** 185 * Decodes the provided ASN.1 element as a backend set. 186 * 187 * @param element The element to be decoded as a backend set. 188 * 189 * @return The decoded backend set. 190 * 191 * @throws LDAPException If the provided ASN.1 element cannot be decoded as 192 * a backend set. 193 */ 194 public static StreamProxyValuesBackendSet decode(final ASN1Element element) 195 throws LDAPException 196 { 197 try 198 { 199 final ASN1Element[] elements = 200 ASN1Sequence.decodeAsSequence(element).elements(); 201 final ASN1OctetString backendSetID = 202 ASN1OctetString.decodeAsOctetString(elements[0]); 203 204 final ASN1Element[] hostPortElements = 205 ASN1Sequence.decodeAsSequence(elements[1]).elements(); 206 final String[] hosts = new String[hostPortElements.length]; 207 final int[] ports = new int[hostPortElements.length]; 208 for (int i=0; i < hostPortElements.length; i++) 209 { 210 final ASN1Element[] hpElements = 211 ASN1Sequence.decodeAsSequence(hostPortElements[i]).elements(); 212 hosts[i] = 213 ASN1OctetString.decodeAsOctetString(hpElements[0]).stringValue(); 214 ports[i] = ASN1Integer.decodeAsInteger(hpElements[1]).intValue(); 215 } 216 217 return new StreamProxyValuesBackendSet(backendSetID, hosts, ports); 218 } 219 catch (final Exception e) 220 { 221 Debug.debugException(e); 222 throw new LDAPException(ResultCode.DECODING_ERROR, 223 ERR_STREAM_PROXY_VALUES_BACKEND_SET_CANNOT_DECODE.get( 224 StaticUtils.getExceptionMessage(e)), e); 225 } 226 } 227 228 229 230 /** 231 * Retrieves a string representation of this stream proxy values backend set. 232 * 233 * @return A string representation of this stream proxy values backend set. 234 */ 235 @Override() 236 public String toString() 237 { 238 final StringBuilder buffer = new StringBuilder(); 239 toString(buffer); 240 return buffer.toString(); 241 } 242 243 244 245 /** 246 * Appends a string representation of this stream proxy values backend set to 247 * the provided buffer. 248 * 249 * @param buffer The buffer to which the stream representation should be 250 * appended. 251 */ 252 public void toString(final StringBuilder buffer) 253 { 254 buffer.append("StreamProxyValuesBackendSet(id="); 255 backendSetID.toString(buffer); 256 buffer.append(", servers={"); 257 258 for (int i=0; i < hosts.length; i++) 259 { 260 if (i > 0) 261 { 262 buffer.append(", "); 263 } 264 buffer.append(hosts[i]); 265 buffer.append(':'); 266 buffer.append(ports[i]); 267 } 268 buffer.append("})"); 269 } 270}