001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.shell.impl.command.pipeline;
020
021import org.crsh.command.CommandContext;
022import org.crsh.io.Consumer;
023import org.crsh.io.Producer;
024
025import java.io.IOException;
026import java.util.Map;
027
028/** @author Julien Viet */
029public abstract class AbstractPipe<C, P, CONS extends CommandContext<? super P>> implements
030    Consumer<C>, Producer<P, CONS>,
031    CommandContext<C> {
032
033  /** . */
034  protected final boolean piped;
035
036  /** . */
037  protected CONS consumer;
038
039  public AbstractPipe(boolean piped) {
040    this.piped = piped;
041    this.consumer = null;
042  }
043
044  public boolean isPiped() {
045    return piped;
046  }
047
048  public boolean takeAlternateBuffer() throws IOException {
049    return consumer.takeAlternateBuffer();
050  }
051
052  public boolean releaseAlternateBuffer() throws IOException {
053    return consumer.releaseAlternateBuffer();
054  }
055
056  public String getProperty(String propertyName) {
057    return consumer.getProperty(propertyName);
058  }
059
060  public String readLine(String msg, boolean echo) throws IOException, InterruptedException {
061    return consumer.readLine(msg, echo);
062  }
063
064  public Map<String, Object> getSession() {
065    return consumer.getSession();
066  }
067
068  public Map<String, Object> getAttributes() {
069    return consumer.getAttributes();
070  }
071
072  public int getWidth() {
073    return consumer.getWidth();
074  }
075
076  public int getHeight() {
077    return consumer.getHeight();
078  }
079
080  public void open(CONS consumer) {
081    this.consumer = consumer;
082  }
083
084  public void flush() throws IOException {
085    consumer.flush();
086  }
087
088  public void close() throws IOException {
089    consumer.close();
090  }
091}