001/*
002 * Copyright 2012-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2012-2022 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) 2012-2022 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.controls;
037
038
039
040import com.unboundid.asn1.ASN1Boolean;
041import com.unboundid.asn1.ASN1Element;
042import com.unboundid.asn1.ASN1Integer;
043import com.unboundid.asn1.ASN1OctetString;
044import com.unboundid.asn1.ASN1Sequence;
045import com.unboundid.ldap.sdk.Control;
046import com.unboundid.ldap.sdk.DecodeableControl;
047import com.unboundid.ldap.sdk.ExtendedResult;
048import com.unboundid.ldap.sdk.LDAPException;
049import com.unboundid.ldap.sdk.ResultCode;
050import com.unboundid.util.Debug;
051import com.unboundid.util.NotMutable;
052import com.unboundid.util.NotNull;
053import com.unboundid.util.Nullable;
054import com.unboundid.util.StaticUtils;
055import com.unboundid.util.ThreadSafety;
056import com.unboundid.util.ThreadSafetyLevel;
057
058import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
059
060
061
062/**
063 * This class provides a response control that may be used to provide the
064 * client with information about transaction-related information over the
065 * course of the associated operation.
066 * <BR>
067 * <BLOCKQUOTE>
068 *   <B>NOTE:</B>  This class, and other classes within the
069 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
070 *   supported for use against Ping Identity, UnboundID, and
071 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
072 *   for proprietary functionality or for external specifications that are not
073 *   considered stable or mature enough to be guaranteed to work in an
074 *   interoperable way with other types of LDAP servers.
075 * </BLOCKQUOTE>
076 * <BR>
077 * This control has an OID of 1.3.6.1.4.1.30221.2.5.39.  It should have a
078 * criticality of {@code false}, and a value with the following encoding:
079 * <PRE>
080 *   TransactionSettingsResponseValue ::= SEQUENCE {
081 *        numLockConflicts        [0] INTEGER
082 *        backendLockAcquired     [1] BOOLEAN,
083 *        ... }
084 * </PRE>
085 */
086@NotMutable()
087@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
088public final class TransactionSettingsResponseControl
089       extends Control
090       implements DecodeableControl
091{
092  /**
093   * The OID (1.3.6.1.4.1.30221.2.5.39) for the transaction settings response
094   * control.
095   */
096  @NotNull public static final String TRANSACTION_SETTINGS_RESPONSE_OID =
097       "1.3.6.1.4.1.30221.2.5.39";
098
099
100
101  /**
102   * The BER type for the value element used to hold the number of lock
103   * conflicts encountered during the course of processing.
104   */
105  private static final byte TYPE_NUM_LOCK_CONFLICTS = (byte) 0x80;
106
107
108
109  /**
110   * The BER type for the value element used to hold the number of lock
111   * conflicts encountered during the course of processing.
112   */
113  private static final byte TYPE_BACKEND_LOCK_ACQUIRED = (byte) 0x81;
114
115
116
117  /**
118   * The serial version UID for this serializable class.
119   */
120  private static final long serialVersionUID = 7290122856855738454L;
121
122
123
124  // Indicates whether the exclusive backend lock was acquired at any point
125  // during the course of processing the operation.
126  private final boolean backendLockAcquired;
127
128  // The number of lock conflicts encountered during the course of processing
129  // the operation.
130  private final int numLockConflicts;
131
132
133
134  /**
135   * Creates a new empty control instance that is intended to be used only for
136   * decoding controls via the {@code DecodeableControl} interface.
137   */
138  TransactionSettingsResponseControl()
139  {
140    backendLockAcquired = false;
141    numLockConflicts = -1;
142  }
143
144
145
146  /**
147   * Creates a new transaction settings response control with the provided
148   * information.
149   *
150   * @param  numLockConflicts     The number of lock conflicts encountered
151   *                              during the course of processing the operation.
152   * @param  backendLockAcquired  Indicates whether the exclusive backend lock
153   *                              was acquired at any point during the course of
154   *                              processing the operation.
155   */
156  public TransactionSettingsResponseControl(final int numLockConflicts,
157                                            final boolean backendLockAcquired)
158  {
159    super(TRANSACTION_SETTINGS_RESPONSE_OID, false,
160         encodeValue(numLockConflicts, backendLockAcquired));
161
162    this.numLockConflicts = numLockConflicts;
163    this.backendLockAcquired = backendLockAcquired;
164  }
165
166
167
168  /**
169   * Creates a new transaction settings response control with the provided
170   * information.
171   *
172   * @param  oid         The OID for the control.
173   * @param  isCritical  Indicates whether the control should be considered
174   *                     critical.
175   * @param  value       The value for the control.
176   *
177   * @throws LDAPException  If the provided information cannot be used to
178   *                         create a valid soft delete response control.
179   */
180  public TransactionSettingsResponseControl(@NotNull final String oid,
181              final boolean isCritical,
182              @Nullable final ASN1OctetString value)
183         throws LDAPException
184  {
185    super(oid, isCritical, value);
186
187    if (value == null)
188    {
189      throw new LDAPException(ResultCode.DECODING_ERROR,
190           ERR_TXN_SETTINGS_RESPONSE_NO_VALUE.get());
191    }
192
193    try
194    {
195      final ASN1Element[] elements =
196           ASN1Sequence.decodeAsSequence(value.getValue()).elements();
197      numLockConflicts = ASN1Integer.decodeAsInteger(elements[0]).intValue();
198      backendLockAcquired =
199           ASN1Boolean.decodeAsBoolean(elements[1]).booleanValue();
200    }
201    catch (final Exception e)
202    {
203      Debug.debugException(e);
204      throw new LDAPException(ResultCode.DECODING_ERROR,
205           ERR_TXN_SETTINGS_RESPONSE_ERROR_DECODING_VALUE.get(
206                StaticUtils.getExceptionMessage(e)));
207    }
208  }
209
210
211
212  /**
213   * Creates an encoded control value with the provided information.
214   *
215   * @param  numLockConflicts     The number of lock conflicts encountered
216   *                              during the course of processing the operation.
217   * @param  backendLockAcquired  Indicates whether the exclusive backend lock
218   *                              was acquired at any point during the course of
219   *                              processing the operation.
220   *
221   * @return  An encoded control value with the provided information.
222   */
223  @NotNull()
224  private static ASN1OctetString encodeValue(final int numLockConflicts,
225                                             final boolean backendLockAcquired)
226  {
227    final ASN1Sequence valueSequence = new ASN1Sequence(
228         new ASN1Integer(TYPE_NUM_LOCK_CONFLICTS, numLockConflicts),
229         new ASN1Boolean(TYPE_BACKEND_LOCK_ACQUIRED, backendLockAcquired));
230    return new ASN1OctetString(valueSequence.encode());
231  }
232
233
234
235  /**
236   * {@inheritDoc}
237   */
238  @Override()
239  @NotNull()
240  public TransactionSettingsResponseControl decodeControl(
241              @NotNull final String oid,
242              final boolean isCritical,
243              @Nullable final ASN1OctetString value)
244         throws LDAPException
245  {
246    return new TransactionSettingsResponseControl(oid, isCritical, value);
247  }
248
249
250
251  /**
252   * Retrieves the number of lock conflicts encountered during the course of
253   * processing the associated operation.
254   *
255   * @return  The number of lock conflicts encountered during the course of
256   *          processing the associated operation.
257   */
258  public int getNumLockConflicts()
259  {
260    return numLockConflicts;
261  }
262
263
264
265  /**
266   * Indicates whether the exclusive backend lock was acquired at any point
267   * during the course of processing the associated operation.
268   *
269   * @return  {@code true} if the backend lock was acquired, or {@code false} if
270   *          not.
271   */
272  public boolean backendLockAcquired()
273  {
274    return backendLockAcquired;
275  }
276
277
278
279  /**
280   * Extracts a transaction settings response control from the provided extended
281   * result.
282   *
283   * @param  extendedResult  The extended result from which to retrieve the
284   *                         transaction settings response control.
285   *
286   * @return  The transaction settings response control contained in the
287   *          provided extended result, or {@code null} if the result did not
288   *          contain a transaction settings response control.
289   *
290   * @throws  LDAPException  If a problem is encountered while attempting to
291   *                         decode the transaction settings response control
292   *                         contained in the provided result.
293   */
294  @Nullable()
295  public static TransactionSettingsResponseControl get(
296                     @NotNull final ExtendedResult extendedResult)
297         throws LDAPException
298  {
299    final Control c =
300         extendedResult.getResponseControl(TRANSACTION_SETTINGS_RESPONSE_OID);
301    if (c == null)
302    {
303      return null;
304    }
305
306    if (c instanceof TransactionSettingsResponseControl)
307    {
308      return (TransactionSettingsResponseControl) c;
309    }
310    else
311    {
312      return new TransactionSettingsResponseControl(c.getOID(), c.isCritical(),
313           c.getValue());
314    }
315  }
316
317
318
319  /**
320   * {@inheritDoc}
321   */
322  @Override()
323  @NotNull()
324  public String getControlName()
325  {
326    return INFO_CONTROL_NAME_TXN_SETTINGS_RESPONSE.get();
327  }
328
329
330
331  /**
332   * {@inheritDoc}
333   */
334  @Override()
335  public void toString(@NotNull final StringBuilder buffer)
336  {
337    buffer.append("TransactionSettingsResponseControl(numLockConflicts=");
338    buffer.append(numLockConflicts);
339    buffer.append(", backendLockAcquired=");
340    buffer.append(backendLockAcquired);
341    buffer.append(')');
342  }
343}