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-2015 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  /** {@inheritDoc} */
047  @Override
048  public void println(String msg)
049  {
050    notifyListenersNewLine(msg);
051    logger.info(LocalizableMessage.raw(msg));
052  }
053
054  /** {@inheritDoc} */
055  @Override
056  public void write(byte[] b, int off, int len)
057  {
058    if (b == null)
059    {
060      throw new NullPointerException("b is null");
061    }
062
063    if (off + len > b.length)
064    {
065      throw new IndexOutOfBoundsException(
066          "len + off are bigger than the length of the byte array");
067    }
068    println(new String(b, off, len));
069  }
070
071  /**
072   * Adds a print stream listener.
073   * @param listener the listener.
074   */
075  public void addListener(PrintStreamListener listener)
076  {
077    listeners.add(listener);
078  }
079
080  /**
081   * Removes a print stream listener.
082   * @param listener the listener.
083   */
084  public void removeListener(PrintStreamListener listener)
085  {
086    listeners.remove(listener);
087  }
088
089  private void notifyListenersNewLine(String msg)
090  {
091    if (notifyListeners)
092    {
093      for (PrintStreamListener listener : listeners)
094      {
095        listener.newLine(msg);
096      }
097    }
098  }
099
100  /**
101   * Sets whether the listeners must be notified or not.
102   * @param notifyListeners whether the listeners must be notified or not.
103   */
104  public void setNotifyListeners(boolean notifyListeners)
105  {
106    this.notifyListeners = notifyListeners;
107  }
108}