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.CommandInvoker;
024 import org.crsh.text.Chunk;
025
026 import java.io.IOException;
027
028 class PipeLine implements CommandInvoker<Void, Chunk> {
029
030 /** . */
031 private final CommandInvoker[] invokers;
032
033 /** . */
034 private Pipe.Invoker current;
035
036 PipeLine(CommandInvoker[] invokers) {
037 this.invokers = invokers;
038 this.current = null;
039 }
040
041 public Class<Void> getConsumedType() {
042 throw new UnsupportedOperationException();
043 }
044
045 public Class<Chunk> getProducedType() {
046 throw new UnsupportedOperationException();
047 }
048
049 public void setPiped(boolean piped) {
050 throw new UnsupportedOperationException("This should not be called");
051 }
052
053 public void open(CommandContext<Chunk> consumer) {
054 open(0, consumer);
055 }
056
057 private CommandContext open(final int index, final CommandContext last) {
058 if (index < invokers.length) {
059
060 //
061 final CommandInvoker invoker = invokers[index];
062 CommandContext next = open(index + 1, last);
063
064 //
065 final Class produced = invoker.getProducedType();
066 final Class<?> consumed = next.getConsumedType();
067
068 if (!consumed.isAssignableFrom(produced)) {
069 if (produced.equals(Void.class) || consumed.equals(Void.class)) {
070 // We need to check (i.e test) what happens for chunk (i.e the writer)
071 Pipe.Sink filter = new Pipe.Sink(consumed);
072 filter.open(next);
073 next = filter;
074 } else if (consumed.equals(Chunk.class)) {
075 Pipe.Chunkizer filter = new Pipe.Chunkizer();
076 filter.open((CommandContext<Chunk>)next);
077 next = filter;
078 } else {
079 Pipe.Sink filter = new Pipe.Sink(consumed);
080 filter.open(next);
081 next = filter;
082 }
083 }
084
085 //
086 Pipe.Invoker filterContext = new Pipe.Invoker(invoker);
087 filterContext.setPiped(index > 0);
088 filterContext.open(next);
089
090 // Save current filter in field
091 // so if anything wrong happens it will be closed
092 current = filterContext;
093
094 //
095 return filterContext;
096 } else {
097 return last;
098 }
099 }
100
101 public void provide(Void element) throws IOException {
102 throw new UnsupportedOperationException("This is not yet implemented");
103 }
104
105 public void flush() throws IOException {
106 current.flush();
107 }
108
109 public void close() {
110 current.close();
111 }
112 }