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.quicksetup.util;
029
030import java.io.BufferedReader;
031
032import org.forgerock.i18n.LocalizableMessage;
033import org.forgerock.i18n.slf4j.LocalizedLogger;
034
035/**
036   * This class is used to read an input stream and process ouput.
037 */
038public abstract class OutputReader {
039
040  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
041
042  /**
043   * Called whenever new input is read from the reader.
044   * @param line String representing new input
045   */
046  public abstract void processLine(String line);
047
048  /**
049   * The protected constructor.
050   *
051   * @param reader  the BufferedReader of the stop process.
052   */
053  public OutputReader(final BufferedReader reader) {
054    Thread t = new Thread(new Runnable() {
055      public void run() {
056        try {
057          String line;
058          while (null != (line = reader.readLine())) {
059            processLine(line);
060          }
061        } catch (Throwable t) {
062          logger.info(LocalizableMessage.raw("error reading output"), t);
063        }
064      }
065    });
066    t.start();
067  }
068}