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