001/*
002 * Copyright 2009-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2018 Ping Identity Corporation
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 */
021package com.unboundid.ldap.sdk.persist;
022
023
024
025import com.unboundid.ldap.sdk.Control;
026import com.unboundid.ldap.sdk.LDAPException;
027import com.unboundid.ldap.sdk.ResultCode;
028import com.unboundid.ldap.sdk.Version;
029import com.unboundid.util.NotMutable;
030import com.unboundid.util.StaticUtils;
031import com.unboundid.util.ThreadSafety;
032import com.unboundid.util.ThreadSafetyLevel;
033
034
035
036/**
037 * This class defines an exception that may be thrown if a problem occurs while
038 * attempting to perform processing related to persisting Java objects in an
039 * LDAP directory server.
040 */
041@NotMutable()
042@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
043public final class LDAPPersistException
044       extends LDAPException
045{
046  /**
047   * The serial version UID for this serializable class.
048   */
049  private static final long serialVersionUID = 8625904586803506713L;
050
051
052
053  // The object that was in the process of being decoded, if available.  If it
054  // is non-null, then it will likely only be partially initialized.
055  private final Object partiallyDecodedObject;
056
057
058
059  /**
060   * Creates a new LDAP persist exception that wraps the provided LDAP
061   * exception.
062   *
063   * @param  e  The LDAP exception to wrap with this LDAP persist exception.
064   */
065  public LDAPPersistException(final LDAPException e)
066  {
067    super(e);
068
069    partiallyDecodedObject = null;
070  }
071
072
073
074  /**
075   * Creates a new LDAP persist exception with the provided message.
076   *
077   * @param  message  The message for this exception.
078   */
079  public LDAPPersistException(final String message)
080  {
081    super(ResultCode.LOCAL_ERROR, message);
082
083    partiallyDecodedObject = null;
084  }
085
086
087
088  /**
089   * Creates a new LDAP persist exception with the provided message and cause.
090   *
091   * @param  message  The message for this exception.
092   * @param  cause    The underlying cause for this exception.
093   */
094  public LDAPPersistException(final String message, final Throwable cause)
095  {
096    super(ResultCode.LOCAL_ERROR, message, cause);
097
098    partiallyDecodedObject = null;
099  }
100
101
102
103  /**
104   * Creates a new LDAP persist exception with the provided message and cause.
105   *
106   * @param  message                 The message for this exception.
107   * @param  partiallyDecodedObject  The object that was in the process of being
108   *                                 decoded when this exception was thrown.  It
109   *                                 may be {@code null} if the exception was
110   *                                 thrown outside of the context of decoding
111   *                                 an object.  If an object is available, then
112   *                                 it will likely be only partially
113   *                                 initialized.
114   * @param  cause                   The underlying cause for this exception.
115   */
116  public LDAPPersistException(final String message,
117                              final Object partiallyDecodedObject,
118                              final Throwable cause)
119  {
120    super(ResultCode.LOCAL_ERROR, message, cause);
121
122    this.partiallyDecodedObject = partiallyDecodedObject;
123  }
124
125
126
127  /**
128   * Retrieves the partially-decoded object in the process of being initialized
129   * when this exception was thrown.
130   *
131   * @return  The partially-decoded object in the process of being initialized
132   *          when this exception was thrown, or {@code null} if none is
133   *          available or the exception was not thrown while decoding an
134   *          object.
135   */
136  public Object getPartiallyDecodedObject()
137  {
138    return partiallyDecodedObject;
139  }
140
141
142
143  /**
144   * {@inheritDoc}
145   */
146  @Override()
147  public void toString(final StringBuilder buffer)
148  {
149    super.toString(buffer);
150  }
151
152
153
154  /**
155   * {@inheritDoc}
156   */
157  @Override()
158  public void toString(final StringBuilder buffer, final boolean includeCause,
159                       final boolean includeStackTrace)
160  {
161    buffer.append("LDAPException(resultCode=");
162    buffer.append(getResultCode());
163
164    final String errorMessage = getMessage();
165    final String diagnosticMessage = getDiagnosticMessage();
166    if ((errorMessage != null) && (! errorMessage.equals(diagnosticMessage)))
167    {
168      buffer.append(", errorMessage='");
169      buffer.append(errorMessage);
170      buffer.append('\'');
171    }
172
173    if (partiallyDecodedObject != null)
174    {
175      buffer.append(", partiallyDecodedObject=");
176      buffer.append(partiallyDecodedObject);
177    }
178
179    if (diagnosticMessage != null)
180    {
181      buffer.append(", diagnosticMessage='");
182      buffer.append(diagnosticMessage);
183      buffer.append('\'');
184    }
185
186    final String matchedDN = getMatchedDN();
187    if (matchedDN != null)
188    {
189      buffer.append(", matchedDN='");
190      buffer.append(matchedDN);
191      buffer.append('\'');
192    }
193
194    final String[] referralURLs = getReferralURLs();
195    if (referralURLs.length > 0)
196    {
197      buffer.append(", referralURLs={");
198
199      for (int i=0; i < referralURLs.length; i++)
200      {
201        if (i > 0)
202        {
203          buffer.append(", ");
204        }
205
206        buffer.append('\'');
207        buffer.append(referralURLs[i]);
208        buffer.append('\'');
209      }
210
211      buffer.append('}');
212    }
213
214    final Control[] responseControls = getResponseControls();
215    if (responseControls.length > 0)
216    {
217      buffer.append(", responseControls={");
218
219      for (int i=0; i < responseControls.length; i++)
220      {
221        if (i > 0)
222        {
223          buffer.append(", ");
224        }
225
226        buffer.append(responseControls[i]);
227      }
228
229      buffer.append('}');
230    }
231
232    if (includeStackTrace)
233    {
234      buffer.append(", trace='");
235      StaticUtils.getStackTrace(getStackTrace(), buffer);
236      buffer.append('\'');
237    }
238
239    if (includeCause || includeStackTrace)
240    {
241      final Throwable cause = getCause();
242      if (cause != null)
243      {
244        buffer.append(", cause=");
245        buffer.append(StaticUtils.getExceptionMessage(cause, true,
246             includeStackTrace));
247      }
248    }
249
250    final String ldapSDKVersionString = ", ldapSDKVersion=" +
251         Version.NUMERIC_VERSION_STRING + ", revision=" + Version.REVISION_ID;
252    if (buffer.indexOf(ldapSDKVersionString) < 0)
253    {
254      buffer.append(ldapSDKVersionString);
255    }
256
257    buffer.append("')");
258  }
259}