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