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.command.pipeline;
021
022 import org.crsh.command.CommandContext;
023 import org.crsh.command.ScriptException;
024 import org.crsh.io.Filter;
025 import org.crsh.text.Chunk;
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 PipeLineElement<C, P> implements Filter<C, P, CommandContext<P>>, CommandContext<C> {
032
033 /** . */
034 protected CommandContext<P> context;
035
036 /** . */
037 final Filter<C, P, CommandContext<P>> command;
038
039 /** . */
040 final Class<C> consumedType;
041
042
043 PipeLineElement(Filter<C, P, CommandContext<P>> command) {
044 this.command = command;
045 this.consumedType = command.getConsumedType();
046 }
047
048 public final boolean takeAlternateBuffer() throws IOException {
049 return context.takeAlternateBuffer();
050 }
051
052 public final boolean releaseAlternateBuffer() throws IOException {
053 return context.releaseAlternateBuffer();
054 }
055
056 public final String getProperty(String propertyName) {
057 return context.getProperty(propertyName);
058 }
059
060 public final String readLine(String msg, boolean echo) {
061 return context.readLine(msg, echo);
062 }
063
064 public final int getWidth() {
065 return context.getWidth();
066 }
067
068 public final int getHeight() {
069 return context.getHeight();
070 }
071
072 public Map<String, Object> getSession() {
073 return context.getSession();
074 }
075
076 public Map<String, Object> getAttributes() {
077 return context.getAttributes();
078 }
079
080 public boolean isPiped() {
081 return context.isPiped();
082 }
083
084 public Class<P> getProducedType() {
085 return command.getProducedType();
086 }
087
088 public Class<C> getConsumedType() {
089 return command.getConsumedType();
090 }
091
092 public void open(CommandContext<P> consumer) {
093 this.context = consumer;
094 this.command.open(consumer);
095 }
096
097 public void write(Chunk chunk) throws IOException {
098 if (consumedType.isInstance(chunk)) {
099 provide(consumedType.cast(chunk));
100 } else {
101 context.write(chunk);
102 }
103 }
104
105 public void provide(C element) throws IOException {
106 command.provide(element);
107 }
108
109 public void flush() throws IOException {
110 command.flush();
111 }
112
113 public void close() throws ScriptException, IOException {
114 command.close();
115 }
116 }