001    /*
002     * Copyright 2013-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.controls;
022    
023    
024    
025    import java.io.Serializable;
026    import java.util.ArrayList;
027    
028    import com.unboundid.asn1.ASN1Element;
029    import com.unboundid.asn1.ASN1Enumerated;
030    import com.unboundid.asn1.ASN1Integer;
031    import com.unboundid.asn1.ASN1Sequence;
032    import com.unboundid.ldap.sdk.LDAPException;
033    import com.unboundid.ldap.sdk.ResultCode;
034    import com.unboundid.util.Debug;
035    import com.unboundid.util.NotMutable;
036    import com.unboundid.util.StaticUtils;
037    import com.unboundid.util.ThreadSafety;
038    import com.unboundid.util.ThreadSafetyLevel;
039    
040    import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
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 defines a data structure that provides information about the
052     * result of assured replication processing, either on a replication server (if
053     * that is all that is needed to satisfy the desired level of assurance) or
054     * on a directory server (if required by the desired level of assurance).
055     */
056    @NotMutable()
057    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
058    public final class AssuredReplicationServerResult
059           implements Serializable
060    {
061      /**
062       * The BER type for the result code element.
063       */
064      private static final byte TYPE_RESULT_CODE = (byte) 0x80;
065    
066    
067      /**
068       * The BER type for the server ID element.
069       */
070      private static final byte TYPE_SERVER_ID = (byte) 0x81;
071    
072    
073      /**
074       * The BER type for the replica ID element.
075       */
076      private static final byte TYPE_REPLICA_ID = (byte) 0x82;
077    
078    
079    
080      /**
081       * The serial version UID for this serializable class.
082       */
083      private static final long serialVersionUID = 3015162215769386343L;
084    
085    
086    
087      // The result code for this server result.
088      private final AssuredReplicationServerResultCode resultCode;
089    
090      // The replica ID of the associated directory server.
091      private final Short replicaID;
092    
093      // The server ID of the associated replication server.
094      private final Short replicationServerID;
095    
096    
097    
098      /**
099       * Creates a new assured replication server result with the provided
100       * information.
101       *
102       * @param  resultCode           The result code that indicates the state of
103       *                              assurance processing for the associated
104       *                              replication server and/or directory server.
105       *                              It must not be {@code null}.
106       * @param  replicationServerID  The server ID of the replication server from
107       *                              which this server result was obtained.  It may
108       *                              be {@code null} if no replication server ID is
109       *                              available for this result.
110       * @param  replicaID            The replica ID of the directory server with
111       *                              which this result is associated.  It may be
112       *                              {@code null} if no replica ID is available
113       *                              for this result.
114       */
115      public AssuredReplicationServerResult(
116           final AssuredReplicationServerResultCode resultCode,
117           final Short replicationServerID,
118           final Short replicaID)
119      {
120        this.resultCode = resultCode;
121        this.replicationServerID = replicationServerID;
122        this.replicaID = replicaID;
123      }
124    
125    
126    
127      /**
128       * Retrieves the result code that indicates the state of assurance processing
129       * for this server result.
130       *
131       * @return  The result code for this server result.
132       */
133      public AssuredReplicationServerResultCode getResultCode()
134      {
135        return resultCode;
136      }
137    
138    
139    
140      /**
141       * Retrieves the server ID for the replication server from which this server
142       * result was obtained, if available.
143       *
144       * @return  The server ID for the replication server from which this server
145       *          result was obtained, or {@code null} if no replication server ID
146       *          is available.
147       */
148      public Short getReplicationServerID()
149      {
150        return replicationServerID;
151      }
152    
153    
154    
155      /**
156       * Retrieves the replica ID for the directory server with which this server
157       * result is associated, if applicable.
158       *
159       * @return  The replica ID for the directory server with which this server
160       *          result is associated, or {@code null} if there is no associated
161       *          directory server.
162       */
163      public Short getReplicaID()
164      {
165        return replicaID;
166      }
167    
168    
169    
170      /**
171       * Encodes this assured replication server result to an ASN.1 element suitable
172       * for use in a {@link AssuredReplicationResponseControl}.
173       *
174       * @return  The encoded representation of this assured replication server
175       *          result.
176       */
177      ASN1Element encode()
178      {
179        final ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(3);
180    
181        elements.add(new ASN1Enumerated(TYPE_RESULT_CODE, resultCode.intValue()));
182    
183        if (replicationServerID != null)
184        {
185          elements.add(new ASN1Integer(TYPE_SERVER_ID, replicationServerID));
186        }
187    
188        if (replicaID != null)
189        {
190          elements.add(new ASN1Integer(TYPE_REPLICA_ID, replicaID));
191        }
192    
193        return new ASN1Sequence(elements);
194      }
195    
196    
197    
198      /**
199       * Decodes the provided ASN.1 element as an assured replication server
200       * result.
201       *
202       * @param  element  The ASN.1 element to be decoded.  It must not be
203       *                  {@code null}.
204       *
205       * @return  The decoded assured replication server result.
206       *
207       * @throws  LDAPException  If a problem is encountered while attempting to
208       *                         decode the provided ASN.1 element as an assured
209       *                         replication server result.
210       */
211      static AssuredReplicationServerResult decode(final ASN1Element element)
212             throws LDAPException
213      {
214        AssuredReplicationServerResultCode resultCode = null;
215        Short serverID  = null;
216        Short replicaID = null;
217    
218        try
219        {
220          for (final ASN1Element e :
221               ASN1Sequence.decodeAsSequence(element).elements())
222          {
223            switch (e.getType())
224            {
225              case TYPE_RESULT_CODE:
226                final int rcValue = ASN1Enumerated.decodeAsEnumerated(e).intValue();
227                resultCode = AssuredReplicationServerResultCode.valueOf(rcValue);
228                if (resultCode == null)
229                {
230                  throw new LDAPException(ResultCode.DECODING_ERROR,
231                       ERR_ASSURED_REPLICATION_SERVER_RESULT_INVALID_RESULT_CODE.
232                            get(rcValue));
233                }
234                break;
235    
236              case TYPE_SERVER_ID:
237                serverID = (short) ASN1Integer.decodeAsInteger(e).intValue();
238                break;
239    
240              case TYPE_REPLICA_ID:
241                replicaID = (short) ASN1Integer.decodeAsInteger(e).intValue();
242                break;
243    
244              default:
245                throw new LDAPException(ResultCode.DECODING_ERROR,
246                     ERR_ASSURED_REPLICATION_SERVER_RESULT_UNEXPECTED_ELEMENT_TYPE.
247                          get(StaticUtils.toHex(e.getType())));
248            }
249          }
250        }
251        catch (final LDAPException le)
252        {
253          Debug.debugException(le);
254          throw le;
255        }
256        catch (final Exception e)
257        {
258          Debug.debugException(e);
259          throw new LDAPException(ResultCode.DECODING_ERROR,
260               ERR_ASSURED_REPLICATION_SERVER_RESULT_CANNOT_DECODE.get(
261                    StaticUtils.getExceptionMessage(e)),
262               e);
263        }
264    
265        if (resultCode == null)
266        {
267          throw new LDAPException(ResultCode.DECODING_ERROR,
268               ERR_ASSURED_REPLICATION_SERVER_RESULT_NO_RESULT_CODE.get());
269        }
270    
271        return new AssuredReplicationServerResult(resultCode, serverID, replicaID);
272      }
273    
274    
275    
276      /**
277       * Retrieves a string representation of this assured replication server
278       * result.
279       *
280       * @return  A string representation of this assured replication server
281       *          result.
282       */
283      @Override()
284      public String toString()
285      {
286        final StringBuilder buffer = new StringBuilder();
287        toString(buffer);
288        return buffer.toString();
289      }
290    
291    
292    
293      /**
294       * Appends a string representation of this assured replication server result
295       * to the provided buffer.
296       *
297       * @param  buffer  The buffer to which the information should be appended.
298       */
299      public void toString(final StringBuilder buffer)
300      {
301        buffer.append("AssuredReplicationServerResult(resultCode=");
302        buffer.append(resultCode.name());
303    
304        if (replicationServerID != null)
305        {
306          buffer.append(", replicationServerID=");
307          buffer.append(replicationServerID);
308        }
309    
310        if (replicaID != null)
311        {
312          buffer.append(", replicaID=");
313          buffer.append(replicaID);
314        }
315    
316        buffer.append(')');
317      }
318    }