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