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 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.util;
029
030import java.io.ByteArrayOutputStream;
031import java.io.PrintStream;
032import java.util.ArrayList;
033
034import org.forgerock.i18n.LocalizableMessage;
035import org.forgerock.i18n.slf4j.LocalizedLogger;
036
037import org.opends.guitools.controlpanel.event.PrintStreamListener;
038
039/**
040 * This class is used to notify the ProgressUpdateListeners of events
041 * that are written to the standard streams.
042 */
043public class ApplicationPrintStream extends PrintStream
044{
045  private ArrayList<PrintStreamListener> listeners = new ArrayList<>();
046  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
047
048  private boolean notifyListeners = true;
049
050  /** Default constructor. */
051  public ApplicationPrintStream()
052  {
053    super(new ByteArrayOutputStream(), true);
054  }
055
056  /** {@inheritDoc} */
057  @Override
058  public void println(String msg)
059  {
060    notifyListenersNewLine(msg);
061    logger.info(LocalizableMessage.raw(msg));
062  }
063
064  /** {@inheritDoc} */
065  @Override
066  public void write(byte[] b, int off, int len)
067  {
068    if (b == null)
069    {
070      throw new NullPointerException("b is null");
071    }
072
073    if (off + len > b.length)
074    {
075      throw new IndexOutOfBoundsException(
076          "len + off are bigger than the length of the byte array");
077    }
078    println(new String(b, off, len));
079  }
080
081  /**
082   * Adds a print stream listener.
083   * @param listener the listener.
084   */
085  public void addListener(PrintStreamListener listener)
086  {
087    listeners.add(listener);
088  }
089
090  /**
091   * Removes a print stream listener.
092   * @param listener the listener.
093   */
094  public void removeListener(PrintStreamListener listener)
095  {
096    listeners.remove(listener);
097  }
098
099  private void notifyListenersNewLine(String msg)
100  {
101    if (notifyListeners)
102    {
103      for (PrintStreamListener listener : listeners)
104      {
105        listener.newLine(msg);
106      }
107    }
108  }
109
110  /**
111   * Sets whether the listeners must be notified or not.
112   * @param notifyListeners whether the listeners must be notified or not.
113   */
114  public void setNotifyListeners(boolean notifyListeners)
115  {
116    this.notifyListeners = notifyListeners;
117  }
118}