001/*
002 * Copyright 2017-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-2024 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) 2017-2024 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.tools;
037
038
039
040import java.io.File;
041import java.io.PrintStream;
042import java.util.Collections;
043import java.util.Iterator;
044import java.util.Set;
045
046import com.unboundid.util.CryptoHelper;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.Nullable;
050import com.unboundid.util.NullOutputStream;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053
054
055
056/**
057 * This class represents a data structure that contains information that should
058 * be used when logging launch and completion messages for a tool invocation.
059 * <BR>
060 * <BLOCKQUOTE>
061 *   <B>NOTE:</B>  This class, and other classes within the
062 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
063 *   supported for use against Ping Identity, UnboundID, and
064 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
065 *   for proprietary functionality or for external specifications that are not
066 *   considered stable or mature enough to be guaranteed to work in an
067 *   interoperable way with other types of LDAP servers.
068 * </BLOCKQUOTE>
069 */
070@NotMutable()
071@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
072public final class ToolInvocationLogDetails
073{
074  // Indicates whether to log launch and completion messages for the associated
075  // tool.
076  private final boolean logInvocation;
077
078  // A print stream that may be used to report information about any problems
079  // encountered while attempting to perform invocation logging.
080  @NotNull private final PrintStream toolErrorStream;
081
082  // The set of log files in which invocation logging should be performed.
083  @NotNull private final Set<File> logFiles;
084
085  // The name of the command used to invoke the tool.
086  @NotNull private final String commandName;
087
088  // An identifier that will appear in launch and completion messages for the
089  // tool so that those messages can be correlated for the same invocation of
090  // the tool.
091  @NotNull private final String invocationID;
092
093
094
095  /**
096   * Creates a new tool invocation log details object with the provided
097   * information.
098   *
099   * @param  logInvocation    Indicates whether to perform launch and completion
100   *                          logging for the associated tool.
101   * @param  commandName      The name (without any path information) for the
102   *                          provided tool.  It must not be {@code null}.
103   * @param  invocationID     A unique identifier that will be used to correlate
104   *                          launch and completion messages for the same
105   *                          invocation of the tool.  If this is {@code null},
106   *                          then an identifier will be generated.
107   * @param  logFiles         The set of log files in which launch and
108   *                          completion messages should be recorded.  It may be
109   *                          {@code null} or empty if no invocation logging
110   *                          should be performed for this tool.  It must not be
111   *                          {@code null} or empty if invocation logging should
112   *                          be performed.
113   * @param  toolErrorStream  A print stream that may be used to report
114   *                          information about any problems encountered while
115   *                          attempting to perform invocation logging.  It
116   *                          must not be {@code null}.
117   */
118  private ToolInvocationLogDetails(final boolean logInvocation,
119                                   @NotNull final String commandName,
120                                   @Nullable final String invocationID,
121                                   @Nullable final Set<File> logFiles,
122                                   @NotNull final PrintStream toolErrorStream)
123  {
124    this.logInvocation = logInvocation;
125    this.commandName = commandName;
126    this.toolErrorStream = toolErrorStream;
127
128    if (invocationID == null)
129    {
130      this.invocationID = CryptoHelper.getRandomUUID().toString();
131    }
132    else
133    {
134      this.invocationID = invocationID;
135    }
136
137    if (logFiles == null)
138    {
139      this.logFiles = Collections.emptySet();
140    }
141    else
142    {
143      this.logFiles = Collections.unmodifiableSet(logFiles);
144    }
145  }
146
147
148
149  /**
150   * Creates a new {@code ToolInvocationLogDetails} instance that indicates that
151   * no logging should be performed for the specified tool.
152   *
153   * @param  commandName  The name of the command (without any path information)
154   *                      for the associated tool.  It must not be {@code null}.
155   *
156   * @return  The {@code ToolInvocationLogDetails} object that was created.
157   */
158  @NotNull()
159  static ToolInvocationLogDetails createDoNotLogDetails(
160                                       @NotNull final String commandName)
161  {
162    return new ToolInvocationLogDetails(false, commandName, "",
163         Collections.<File>emptySet(), NullOutputStream.getPrintStream());
164  }
165
166
167
168  /**
169   * Creates a new {@code ToolInvocationLogDetails} instance that indicates that
170   * launch and completion messages should be logged for the specified tool.
171   *
172   * @param  commandName      The name (without any path information) for the
173   *                          provided tool.  It must not be {@code null}.
174   * @param  invocationID     A unique identifier that will be used to correlate
175   *                          launch and completion messages for the same
176   *                          invocation of the tool.  If this is {@code null},
177   *                          then an identifier will be generated.
178   * @param  logFiles         The set of log files in which launch and
179   *                          completion messages should be recorded.  It may be
180   *                          {@code null} or empty if no invocation logging
181   *                          should be performed for this tool.  It must not be
182   *                          {@code null} or empty if invocation logging should
183   *                          be performed.
184   * @param  toolErrorStream  A print stream that should be used to report
185   *                          information about any problems encountered while
186   *                          attempting to perform invocation logging.  It
187   *                          must not be {@code null}.
188   *
189   * @return  The {@code ToolInvocationLogDetails} object that was created.
190   */
191  @NotNull()
192  static ToolInvocationLogDetails createLogDetails(
193              @NotNull final String commandName,
194              @Nullable final String invocationID,
195              @NotNull final Set<File> logFiles,
196              @NotNull final PrintStream toolErrorStream)
197  {
198    return new ToolInvocationLogDetails(true, commandName, invocationID,
199         logFiles, toolErrorStream);
200  }
201
202
203
204  /**
205   * Retrieves the name of the command (without any path information) for the
206   * associated tool.
207   *
208   * @return  The name of the command for the associated tool.
209   */
210  @NotNull()
211  public String getCommandName()
212  {
213    return commandName;
214  }
215
216
217
218  /**
219   * Indicates whether launch and completion messages should be logged for the
220   * tool.
221   *
222   * @return  {@code true} if the messages should be logged, or {@code false} if
223   *          not.
224   */
225  public boolean logInvocation()
226  {
227    return logInvocation;
228  }
229
230
231
232  /**
233   * Retrieves the unique identifier to use to correlate the launch and
234   * completion messages for the tool invocation, if available.
235   *
236   * @return  The unique identifier to use to correlate the launch and
237   *          completion messages for the tool invocation, or an empty string if
238   *          no invocation logging should be performed for the tool.
239   */
240  @NotNull()
241  public String getInvocationID()
242  {
243    return invocationID;
244  }
245
246
247
248  /**
249   * Retrieves an unmodifiable set of the files in which launch and completion
250   * log messages should be recorded for the tool invocation.
251   *
252   * @return  An unmodifiable set of the files in which launch and completion
253   *          log messages should be recorded for the tool invocation.  It may
254   *          be empty if no invocation logging should be performed.
255   */
256  @NotNull()
257  public Set<File> getLogFiles()
258  {
259    return logFiles;
260  }
261
262
263
264  /**
265   * Retrieves a print stream that may be used to report information about any
266   * problems encountered while attempting to perform invocation logging.
267   *
268   * @return  A print stream that may be used to report information about any
269   *          problems encountered while attempting to perform invocation
270   *          logging.
271   */
272  @NotNull()
273  public PrintStream getToolErrorStream()
274  {
275    return toolErrorStream;
276  }
277
278
279  /**
280   * Retrieves a string representation of this tool invocation log details
281   * object.
282   *
283   * @return  A string representation of this tool invocation log details
284   *          object.
285   */
286  @Override()
287  @NotNull()
288  public String toString()
289  {
290    final StringBuilder buffer = new StringBuilder();
291    toString(buffer);
292    return buffer.toString();
293  }
294
295
296
297  /**
298   * Appends a string representation of this tool invocation log details
299   * object to the provided buffer.
300   *
301   * @param  buffer  The buffer to which the string representation should be
302   *                 appended.
303   */
304  public void toString(@NotNull final StringBuilder buffer)
305  {
306    buffer.append("ToolInvocationLogDetails(commandName='");
307    buffer.append(commandName);
308    buffer.append("', logInvocation=");
309    buffer.append(logInvocation);
310
311    if (logInvocation)
312    {
313      buffer.append(", invocationID='");
314      buffer.append(invocationID);
315      buffer.append("', logFiles={");
316
317      final Iterator<File> fileIterator = logFiles.iterator();
318      while (fileIterator.hasNext())
319      {
320        buffer.append('\'');
321        buffer.append(fileIterator.next().getAbsolutePath());
322        buffer.append('\'');
323
324        if (fileIterator.hasNext())
325        {
326          buffer.append(", ");
327        }
328      }
329
330      buffer.append('}');
331    }
332
333    buffer.append(')');
334  }
335}