001    /*
002     * Copyright 2010-2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2010-2016 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.extensions;
022    
023    
024    
025    import com.unboundid.ldap.sdk.Control;
026    import com.unboundid.ldap.sdk.ExtendedRequest;
027    import com.unboundid.ldap.sdk.ExtendedResult;
028    import com.unboundid.ldap.sdk.LDAPConnection;
029    import com.unboundid.ldap.sdk.LDAPException;
030    import com.unboundid.ldap.sdk.ResultCode;
031    import com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl;
032    import com.unboundid.util.NotMutable;
033    import com.unboundid.util.ThreadSafety;
034    import com.unboundid.util.ThreadSafetyLevel;
035    
036    import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
037    
038    
039    
040    /**
041     * This class provides an implementation of the start transaction extended
042     * request as defined in
043     * <A HREF="http://www.ietf.org/rfc/rfc5805.txt">RFC 5805</A>.  It may be used
044     * to begin a transaction that allows multiple write operations to be processed
045     * as a single atomic unit.  The {@link StartTransactionExtendedResult} that is
046     * returned will include a transaction ID.  For each operation that is performed
047     * as part of the transaction, this transaction ID should be included in the
048     * corresponding request through the
049     * {@link TransactionSpecificationRequestControl}.  Finally, after all requests
050     * for the transaction have been submitted to the server, the
051     * {@link EndTransactionExtendedRequest} should be used to commit that
052     * transaction, or it may also be used to abort the transaction if it is decided
053     * that it is no longer needed.
054     * <BR><BR>
055     * <H2>Example</H2>
056     * The following example demonstrates the process for using LDAP  transactions.
057     * It will modify two different entries as a single atomic unit.
058     * <PRE>
059     * // Use the start transaction extended operation to begin a transaction.
060     * StartTransactionExtendedResult startTxnResult;
061     * try
062     * {
063     *   startTxnResult = (StartTransactionExtendedResult)
064     *        connection.processExtendedOperation(
065     *             new StartTransactionExtendedRequest());
066     *   // This doesn't necessarily mean that the operation was successful, since
067     *   // some kinds of extended operations return non-success results under
068     *   // normal conditions.
069     * }
070     * catch (LDAPException le)
071     * {
072     *   // For an extended operation, this generally means that a problem was
073     *   // encountered while trying to send the request or read the result.
074     *   startTxnResult = new StartTransactionExtendedResult(
075     *        new ExtendedResult(le));
076     * }
077     * LDAPTestUtils.assertResultCodeEquals(startTxnResult, ResultCode.SUCCESS);
078     * ASN1OctetString txnID = startTxnResult.getTransactionID();
079     *
080     *
081     * // At this point, we have a transaction available for use.  If any problem
082     * // arises, we want to ensure that the transaction is aborted, so create a
083     * // try block to process the operations and a finally block to commit or
084     * // abort the transaction.
085     * boolean commit = false;
086     * try
087     * {
088     *   // Create and process a modify operation to update a first entry as part
089     *   // of the transaction.  Make sure to include the transaction specification
090     *   // control in the request to indicate that it should be part of the
091     *   // transaction.
092     *   ModifyRequest firstModifyRequest = new ModifyRequest(
093     *        "cn=first,dc=example,dc=com",
094     *        new Modification(ModificationType.REPLACE, "description", "first"));
095     *   firstModifyRequest.addControl(
096     *        new TransactionSpecificationRequestControl(txnID));
097     *   LDAPResult firstModifyResult;
098     *   try
099     *   {
100     *     firstModifyResult = connection.modify(firstModifyRequest);
101     *   }
102     *   catch (LDAPException le)
103     *   {
104     *     firstModifyResult = le.toLDAPResult();
105     *   }
106     *   LDAPTestUtils.assertResultCodeEquals(firstModifyResult,
107     *        ResultCode.SUCCESS);
108     *
109     *   // Perform a second modify operation as part of the transaction.
110     *   ModifyRequest secondModifyRequest = new ModifyRequest(
111     *        "cn=second,dc=example,dc=com",
112     *        new Modification(ModificationType.REPLACE, "description", "second"));
113     *   secondModifyRequest.addControl(
114     *        new TransactionSpecificationRequestControl(txnID));
115     *   LDAPResult secondModifyResult;
116     *   try
117     *   {
118     *     secondModifyResult = connection.modify(secondModifyRequest);
119     *   }
120     *   catch (LDAPException le)
121     *   {
122     *     secondModifyResult = le.toLDAPResult();
123     *   }
124     *   LDAPTestUtils.assertResultCodeEquals(secondModifyResult,
125     *        ResultCode.SUCCESS);
126     *
127     *   // If we've gotten here, then all writes have been processed successfully
128     *   // and we can indicate that the transaction should be committed rather
129     *   // than aborted.
130     *   commit = true;
131     * }
132     * finally
133     * {
134     *   // Commit or abort the transaction.
135     *   EndTransactionExtendedResult endTxnResult;
136     *   try
137     *   {
138     *     endTxnResult = (EndTransactionExtendedResult)
139     *          connection.processExtendedOperation(
140     *               new EndTransactionExtendedRequest(txnID, commit));
141     *   }
142     *   catch (LDAPException le)
143     *   {
144     *     endTxnResult = new EndTransactionExtendedResult(new ExtendedResult(le));
145     *   }
146     *   LDAPTestUtils.assertResultCodeEquals(endTxnResult, ResultCode.SUCCESS);
147     * }
148     * </PRE>
149     */
150    @NotMutable()
151    @ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
152    public final class StartTransactionExtendedRequest
153           extends ExtendedRequest
154    {
155      /**
156       * The OID (1.3.6.1.1.21.1) for the start transaction extended request.
157       */
158      public static final String START_TRANSACTION_REQUEST_OID = "1.3.6.1.1.21.1";
159    
160    
161      /**
162       * The serial version UID for this serializable class.
163       */
164      private static final long serialVersionUID = 7382735226826929629L;
165    
166    
167    
168      // This is an ugly hack to prevent checkstyle from complaining about imports
169      // for classes that are needed by javadoc @link elements but aren't otherwise
170      // used in the class.  It appears that checkstyle does not recognize the use
171      // of these classes in javadoc @link elements so we must ensure that they are
172      // referenced elsewhere in the class to prevent checkstyle from complaining.
173      static
174      {
175        final TransactionSpecificationRequestControl c = null;
176      }
177    
178    
179    
180      /**
181       * Creates a new start transaction extended request.
182       */
183      public StartTransactionExtendedRequest()
184      {
185        super(START_TRANSACTION_REQUEST_OID);
186      }
187    
188    
189    
190      /**
191       * Creates a new start transaction extended request.
192       *
193       * @param  controls  The set of controls to include in the request.
194       */
195      public StartTransactionExtendedRequest(final Control[] controls)
196      {
197        super(START_TRANSACTION_REQUEST_OID, controls);
198      }
199    
200    
201    
202      /**
203       * Creates a new start transaction extended request from the provided generic
204       * extended request.
205       *
206       * @param  extendedRequest  The generic extended request to use to create this
207       *                          start transaction extended request.
208       *
209       * @throws  LDAPException  If a problem occurs while decoding the request.
210       */
211      public StartTransactionExtendedRequest(final ExtendedRequest extendedRequest)
212             throws LDAPException
213      {
214        super(extendedRequest);
215    
216        if (extendedRequest.hasValue())
217        {
218          throw new LDAPException(ResultCode.DECODING_ERROR,
219               ERR_START_TXN_REQUEST_HAS_VALUE.get());
220        }
221      }
222    
223    
224    
225      /**
226       * {@inheritDoc}
227       */
228      @Override()
229      public StartTransactionExtendedResult process(
230                  final LDAPConnection connection, final int depth)
231             throws LDAPException
232      {
233        final ExtendedResult extendedResponse = super.process(connection, depth);
234        return new StartTransactionExtendedResult(extendedResponse);
235      }
236    
237    
238    
239      /**
240       * {@inheritDoc}
241       */
242      @Override()
243      public StartTransactionExtendedRequest duplicate()
244      {
245        return duplicate(getControls());
246      }
247    
248    
249    
250      /**
251       * {@inheritDoc}
252       */
253      @Override()
254      public StartTransactionExtendedRequest duplicate(final Control[] controls)
255      {
256        final StartTransactionExtendedRequest r =
257             new StartTransactionExtendedRequest(controls);
258        r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
259        return r;
260      }
261    
262    
263    
264      /**
265       * {@inheritDoc}
266       */
267      @Override()
268      public String getExtendedRequestName()
269      {
270        return INFO_EXTENDED_REQUEST_NAME_START_TXN.get();
271      }
272    
273    
274    
275      /**
276       * {@inheritDoc}
277       */
278      @Override()
279      public void toString(final StringBuilder buffer)
280      {
281        buffer.append("StartTransactionExtendedRequest(");
282    
283        final Control[] controls = getControls();
284        if (controls.length > 0)
285        {
286          buffer.append("controls={");
287          for (int i=0; i < controls.length; i++)
288          {
289            if (i > 0)
290            {
291              buffer.append(", ");
292            }
293    
294            buffer.append(controls[i]);
295          }
296          buffer.append('}');
297        }
298    
299        buffer.append(')');
300      }
301    }