001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014 ForgeRock AS
026 */
027package org.opends.server.util.cli;
028
029import org.forgerock.i18n.LocalizableMessageBuilder;
030import org.opends.quicksetup.util.PlainTextProgressMessageFormatter;
031import org.opends.quicksetup.util.ProgressMessageFormatter;
032
033import com.forgerock.opendj.cli.ConsoleApplication;
034
035/**
036 * Class used to add points periodically to the end of the output.
037 */
038public class PointAdder implements Runnable
039{
040  private final ConsoleApplication app;
041  private Thread t;
042  private boolean stopPointAdder;
043  private boolean pointAdderStopped;
044  private long periodTime = DEFAULT_PERIOD_TIME;
045  private final boolean isError;
046  private final ProgressMessageFormatter formatter;
047
048
049  /**
050   * The default period time used to write points in the output.
051   */
052  public static final long DEFAULT_PERIOD_TIME = 3000;
053
054  /**
055   * Default constructor.
056   *
057   * @param app
058   *          The console application to be used. Creates a PointAdder that
059   *          writes to the standard output with the default period time.
060   */
061  public PointAdder(ConsoleApplication app)
062  {
063    this(app, DEFAULT_PERIOD_TIME, false,
064        new PlainTextProgressMessageFormatter());
065  }
066
067  /**
068   * Default constructor.
069   *
070   * @param app
071   *          The console application to be used.
072   * @param periodTime
073   *          The time between printing two points.
074   * @param isError
075   *          Whether the points must be printed in error stream or output
076   *          stream.
077   * @param formatter
078   *          The text formatter.
079   */
080  public PointAdder(ConsoleApplication app,
081      long periodTime, boolean isError,
082      ProgressMessageFormatter formatter)
083  {
084    this.app = app;
085    this.periodTime = periodTime;
086    this.isError = isError;
087    this.formatter = formatter;
088  }
089
090  /**
091   * Starts the PointAdder: points are added at the end of the logs
092   * periodically.
093   */
094  public void start()
095  {
096    LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
097    mb.append(formatter.getSpace());
098    for (int i=0; i< 5; i++)
099    {
100      mb.append(formatter.getFormattedPoint());
101    }
102    app.print(mb.toMessage());
103    t = new Thread(this);
104    t.start();
105  }
106
107  /**
108   * Stops the PointAdder: points are no longer added at the end of the logs
109   * periodically.
110   */
111  public synchronized void stop()
112  {
113    stopPointAdder = true;
114    while (!pointAdderStopped)
115    {
116      try
117      {
118        t.interrupt();
119        // To allow the thread to set the boolean.
120        Thread.sleep(100);
121      }
122      catch (Throwable t)
123      {
124      }
125    }
126  }
127
128  /** {@inheritDoc} */
129  public void run()
130  {
131    while (!stopPointAdder)
132    {
133      try
134      {
135        Thread.sleep(periodTime);
136        app.print(formatter.getFormattedPoint());
137      }
138      catch (Throwable t)
139      {
140      }
141    }
142    pointAdderStopped = true;
143  }
144}