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 */
019
020 package org.crsh.shell.impl.command;
021
022 import org.crsh.command.CommandContext;
023 import org.crsh.command.ScriptException;
024 import org.crsh.shell.InteractionContext;
025 import org.crsh.io.Filter;
026
027 import java.io.IOException;
028 import java.util.Map;
029
030 /** @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> */
031 class Pipe<C, P> implements Filter<C, P, CommandContext<P>>, CommandContext<C> {
032
033 /** . */
034 protected CommandContext<P> context;
035
036 /** . */
037 final Filter<C, P, InteractionContext<P>> command;
038
039 Pipe(Filter<C, P, InteractionContext<P>> command) {
040 this.command = command;
041 }
042
043 public final boolean takeAlternateBuffer() throws IOException {
044 return context.takeAlternateBuffer();
045 }
046
047 public final boolean releaseAlternateBuffer() throws IOException {
048 return context.releaseAlternateBuffer();
049 }
050
051 public final String getProperty(String propertyName) {
052 return context.getProperty(propertyName);
053 }
054
055 public final String readLine(String msg, boolean echo) {
056 return context.readLine(msg, echo);
057 }
058
059 public final int getWidth() {
060 return context.getWidth();
061 }
062
063 public final int getHeight() {
064 return context.getHeight();
065 }
066
067 public Map<String, Object> getSession() {
068 return context.getSession();
069 }
070
071 public Map<String, Object> getAttributes() {
072 return context.getAttributes();
073 }
074
075 public boolean isPiped() {
076 return context.isPiped();
077 }
078
079 public Class<P> getProducedType() {
080 return command.getProducedType();
081 }
082
083 public Class<C> getConsumedType() {
084 return command.getConsumedType();
085 }
086
087 public void open(CommandContext<P> consumer) {
088 this.context = consumer;
089 this.command.open(consumer);
090 }
091
092 public void provide(C element) throws IOException {
093 if (command.getConsumedType().isInstance(element)) {
094 command.provide(element);
095 }
096 }
097
098 public void flush() throws IOException {
099 command.flush();
100 }
101
102 public void close() throws ScriptException, IOException {
103 command.close();
104 }
105 }