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.pipeline;
021    
022    import org.crsh.command.CommandContext;
023    import org.crsh.shell.impl.command.spi.CommandInvoker;
024    import org.crsh.console.KeyHandler;
025    import org.crsh.text.Chunk;
026    
027    import java.io.IOException;
028    
029    public class PipeLine extends CommandInvoker<Void, Chunk> {
030    
031      /** . */
032      private final CommandInvoker[] invokers;
033    
034      /** . */
035      private CommandContext<?> current;
036    
037      public PipeLine(CommandInvoker[] invokers) {
038        this.invokers = invokers;
039        this.current = null;
040      }
041    
042      public Class<Void> getConsumedType() {
043        return Void.class;
044      }
045    
046      public Class<Chunk> getProducedType() {
047        return Chunk.class;
048      }
049    
050      public void open(CommandContext<? super Chunk> consumer) {
051        open(0, consumer);
052      }
053    
054      private CommandContext open(final int index, final CommandContext last) {
055        if (index < invokers.length) {
056    
057          //
058          final CommandInvoker invoker = invokers[index];
059          CommandContext next = open(index + 1, last);
060    
061          //
062          final Class produced = invoker.getProducedType();
063          final Class<?> consumed = next.getConsumedType();
064          boolean piped = index > 0;
065    
066          AbstractPipe filter;
067          if (consumed.equals(Chunk.class)) {
068            filter = new ToChunkPipe(produced, piped);
069          } else {
070            filter = new ConvertingPipe(produced, consumed, piped);
071          }
072          filter.open(next);
073          next = filter;
074    
075          //
076          PipeLineElement filterContext = new PipeLineElement(invoker);
077          filterContext.open(next);
078    
079          // Save current filter in field
080          // so if anything wrong happens it will be closed
081          current = filterContext;
082    
083          //
084          return filterContext;
085        } else {
086          current = last;
087          return last;
088        }
089      }
090    
091      @Override
092      public KeyHandler getKeyHandler() {
093        for (CommandInvoker<?, ?> invoker : invokers) {
094          KeyHandler handler = invoker.getKeyHandler();
095          if (handler != null) {
096            return handler;
097          }
098        }
099        return null;
100      }
101    
102      public void provide(Void element) throws IOException {
103        // Ignore
104      }
105    
106      public void flush() throws IOException {
107        current.flush();
108      }
109    
110      public void close() throws IOException {
111        current.close();
112      }
113    }