mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Yannick Lecaillez
05.38.2016 eeed545264e26d4f8d417047adc77432a30e968a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2009 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.opends.server.tools;
 
import java.io.*;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.server.core.DirectoryServer;
import org.opends.server.loggers.JDKLogging;
import org.opends.server.types.NullOutputStream;
 
import com.forgerock.opendj.cli.*;
 
import static org.opends.messages.CoreMessages.*;
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.util.StaticUtils.*;
import static com.forgerock.opendj.cli.Utils.filterExitCode;
import static com.forgerock.opendj.cli.CommonArguments.*;
 
/**
 * This program provides a simple tool that will wait for a specified file to be
 * deleted before exiting.  It can be used in the process of confirming that the
 * server has completed its startup or shutdown process.
 */
public class WaitForFileDelete extends ConsoleApplication
{
  /**
   * The fully-qualified name of this class.
   */
  private static final String CLASS_NAME =
       "org.opends.server.tools.WaitForFileDelete";
 
 
 
  /**
   * The exit code value that will be used if the target file is deleted
   * successfully.
   */
  public static final int EXIT_CODE_SUCCESS = 0;
 
 
 
  /**
   * The exit code value that will be used if an internal error occurs within
   * this program.
   */
  public static final int EXIT_CODE_INTERNAL_ERROR = 1;
 
 
 
  /**
   * The exit code value that will be used if a timeout occurs while waiting for
   * the file to be removed.
   */
  public static final int EXIT_CODE_TIMEOUT = 2;
 
 
 
  /**
   * Constructor for the WaitForFileDelete object.
   *
   * @param out the print stream to use for standard output.
   * @param err the print stream to use for standard error.
   * @param in the input stream to use for standard input.
   */
  public WaitForFileDelete(PrintStream out, PrintStream err, InputStream in)
  {
    super(out, err);
  }
 
  /**
   * Processes the command-line arguments and initiates the process of waiting
   * for the file to be removed.
   *
   * @param  args  The command-line arguments provided to this program.
   */
  public static void main(String[] args)
  {
    int retCode = mainCLI(args, true, System.out, System.err, System.in);
 
    System.exit(retCode);
  }
 
  /**
   * Processes the command-line arguments and initiates the process of waiting
   * for the file to be removed.
   *
   * @param  args              The command-line arguments provided to this
   *                           program.
   * @param initializeServer   Indicates whether to initialize the server.
   * @param  outStream         The output stream to use for standard output, or
   *                           <CODE>null</CODE> if standard output is not
   *                           needed.
   * @param  errStream         The output stream to use for standard error, or
   *                           <CODE>null</CODE> if standard error is not
   *                           needed.
   * @param  inStream          The input stream to use for standard input.
   * @return The error code.
   */
 
  public static int mainCLI(String[] args, boolean initializeServer,
      OutputStream outStream, OutputStream errStream, InputStream inStream)
  {
    int exitCode;
    PrintStream out = NullOutputStream.wrapOrNullStream(outStream);
    PrintStream err = NullOutputStream.wrapOrNullStream(errStream);
    JDKLogging.disableLogging();
    try
    {
      WaitForFileDelete wffd = new WaitForFileDelete(out, err, System.in);
      exitCode = wffd.mainWait(args);
      if (exitCode != EXIT_CODE_SUCCESS)
      {
        exitCode = filterExitCode(exitCode);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      exitCode = EXIT_CODE_INTERNAL_ERROR;
    }
    return exitCode;
  }
 
 
 
  /**
   * Processes the command-line arguments and then waits for the specified file
   * to be removed.
   *
   * @param  args  The command-line arguments provided to this program.
   * @param  out         The output stream to use for standard output, or
   *                           <CODE>null</CODE> if standard output is not
   *                           needed.
   * @param  err         The output stream to use for standard error, or
   *                           <CODE>null</CODE> if standard error is not
   *                           needed.
   * @param  inStream          The input stream to use for standard input.
   *
   * @return  An integer value of zero if the file was deleted successfully, or
   *          some other value if a problem occurred.
   */
  private int mainWait(String[] args)
  {
    // Create all of the command-line arguments for this program.
    final BooleanArgument showUsage;
    final IntegerArgument timeout;
    final StringArgument logFilePath;
    final StringArgument targetFilePath;
    final StringArgument outputFilePath;
    final BooleanArgument quietMode;
 
    LocalizableMessage toolDescription = INFO_WAIT4DEL_TOOL_DESCRIPTION.get();
    ArgumentParser argParser = new ArgumentParser(CLASS_NAME, toolDescription,
                                                  false);
 
    try
    {
      targetFilePath =
              StringArgument.builder("targetFile")
                      .shortIdentifier('f')
                      .description(INFO_WAIT4DEL_DESCRIPTION_TARGET_FILE.get())
                      .required()
                      .valuePlaceholder(INFO_PATH_PLACEHOLDER.get())
                      .buildAndAddToParser(argParser);
      logFilePath =
              StringArgument.builder("logFile")
                      .shortIdentifier('l')
                      .description(INFO_WAIT4DEL_DESCRIPTION_LOG_FILE.get())
                      .valuePlaceholder(INFO_PATH_PLACEHOLDER.get())
                      .buildAndAddToParser(argParser);
      outputFilePath =
              StringArgument.builder("outputFile")
                      .shortIdentifier('o')
                      .description(INFO_WAIT4DEL_DESCRIPTION_OUTPUT_FILE.get())
                      .valuePlaceholder(INFO_PATH_PLACEHOLDER.get())
                      .buildAndAddToParser(argParser);
      timeout =
              IntegerArgument.builder("timeout")
                      .shortIdentifier('t')
                      .description(INFO_WAIT4DEL_DESCRIPTION_TIMEOUT.get())
                      .required()
                      .lowerBound(0)
                      .defaultValue(DirectoryServer.DEFAULT_TIMEOUT)
                      .valuePlaceholder(INFO_SECONDS_PLACEHOLDER.get())
                      .buildAndAddToParser(argParser);
 
 
      // Not used in this class, but required by the start-ds script (see issue #3814)
      BooleanArgument.builder("useLastKnownGoodConfig")
              .shortIdentifier('L')
              .description(INFO_DSCORE_DESCRIPTION_LASTKNOWNGOODCFG.get())
              .buildAndAddToParser(argParser);
 
      // Not used in this class, but required by the start-ds script (see issue #3814)
      quietMode = quietArgument();
      argParser.addArgument(quietMode);
 
      showUsage = showUsageArgument();
      argParser.addArgument(showUsage);
      argParser.setUsageArgument(showUsage);
    }
    catch (ArgumentException ae)
    {
      LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
      println(message);
      return EXIT_CODE_INTERNAL_ERROR;
    }
 
 
    // Parse the command-line arguments provided to the program.
    try
    {
      argParser.parseArguments(args);
    }
    catch (ArgumentException ae)
    {
      argParser.displayMessageAndUsageReference(getErrStream(), ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
      return EXIT_CODE_INTERNAL_ERROR;
    }
 
 
    // If we should just display usage or version information,
    // then print it and exit.
    if (argParser.usageOrVersionDisplayed())
    {
      return EXIT_CODE_SUCCESS;
    }
 
 
    // Get the file to watch.  If it doesn't exist now, then exit immediately.
    File targetFile = new File(targetFilePath.getValue());
    if (! targetFile.exists())
    {
      return EXIT_CODE_SUCCESS;
    }
 
 
    // If a log file was specified, then open it.
    long logFileOffset = 0L;
    RandomAccessFile logFile = null;
    if (logFilePath.isPresent())
    {
      try
      {
        File f = new File(logFilePath.getValue());
        if (f.exists())
        {
          logFile = new RandomAccessFile(f, "r");
          logFileOffset = logFile.length();
          logFile.seek(logFileOffset);
        }
      }
      catch (Exception e)
      {
        println(WARN_WAIT4DEL_CANNOT_OPEN_LOG_FILE.get(logFilePath.getValue(), e));
        logFile = null;
      }
    }
 
 
    // If an output file was specified and we could open the log file, open it
    // and append data to it.
    RandomAccessFile outputFile = null;
    long outputFileOffset = 0L;
    if (logFile != null && outputFilePath.isPresent())
    {
      try
      {
        File f = new File(outputFilePath.getValue());
        if (f.exists())
        {
          outputFile = new RandomAccessFile(f, "rw");
          outputFileOffset = outputFile.length();
          outputFile.seek(outputFileOffset);
        }
      }
      catch (Exception e)
      {
        println(WARN_WAIT4DEL_CANNOT_OPEN_OUTPUT_FILE.get(outputFilePath.getValue(), e));
        outputFile = null;
      }
    }
    // Figure out when to stop waiting.
    long stopWaitingTime;
    try
    {
      long timeoutMillis = 1000L * Integer.parseInt(timeout.getValue());
      if (timeoutMillis > 0)
      {
        stopWaitingTime = System.currentTimeMillis() + timeoutMillis;
      }
      else
      {
        stopWaitingTime = Long.MAX_VALUE;
      }
    }
    catch (Exception e)
    {
      // This shouldn't happen, but if it does then ignore it.
      stopWaitingTime = System.currentTimeMillis() + 60000;
    }
 
 
    // Operate in a loop, printing out any applicable log messages and waiting
    // for the target file to be removed.
    byte[] logBuffer = new byte[8192];
    while (System.currentTimeMillis() < stopWaitingTime)
    {
      if (logFile != null)
      {
        try
        {
          while (logFile.length() > logFileOffset)
          {
            int bytesRead = logFile.read(logBuffer);
            if (bytesRead > 0)
            {
              if (outputFile == null)
              {
                getOutputStream().write(logBuffer, 0, bytesRead);
                getOutputStream().flush();
              }
              else
              {
                // Write on the file.
                // TODO
                outputFile.write(logBuffer, 0, bytesRead);
 
              }
              logFileOffset += bytesRead;
            }
          }
        }
        catch (Exception e)
        {
          // We'll just ignore this.
        }
      }
 
 
      if (! targetFile.exists())
      {
        break;
      }
      else
      {
        try
        {
          Thread.sleep(10);
        } catch (InterruptedException ie) {}
      }
    }
 
    close(outputFile);
 
    if (targetFile.exists())
    {
      println(ERR_TIMEOUT_DURING_STARTUP.get(
          Integer.parseInt(timeout.getValue()),
          timeout.getLongIdentifier()));
      return EXIT_CODE_TIMEOUT;
    }
    else
    {
      return EXIT_CODE_SUCCESS;
    }
  }
 
  /** {@inheritDoc} */
  @Override
  public boolean isAdvancedMode()
  {
    return false;
  }
 
  /** {@inheritDoc} */
  @Override
  public boolean isInteractive()
  {
    return false;
  }
 
  /** {@inheritDoc} */
  @Override
  public boolean isMenuDrivenMode()
  {
    return false;
  }
 
  /** {@inheritDoc} */
  @Override
  public boolean isQuiet()
  {
    return false;
  }
 
  /** {@inheritDoc} */
  @Override
  public boolean isScriptFriendly()
  {
    return false;
  }
 
  /** {@inheritDoc} */
  @Override
  public boolean isVerbose()
  {
    return false;
  }
}