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