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.CommandInvoker;
023    import org.crsh.command.InvocationContext;
024    import org.crsh.command.ScriptException;
025    import org.crsh.io.Filter;
026    import org.crsh.io.ProducerConsumer;
027    import org.crsh.io.ProducerContext;
028    
029    import java.io.IOException;
030    
031    /**
032     * A pipe filter that invokes a command through a {@link CommandInvoker}.
033     */
034    class InvokerPipeFilter<C, P> implements Filter<C, P> {
035    
036      /** . */
037      final ProducerConsumer<C, P> command;
038    
039      /** . */
040      private ProducerContext<P> context;
041    
042      InvokerPipeFilter(ProducerConsumer<C, P> command) {
043        this.command = command;
044      }
045    
046      public void invoke(InvocationContext<P> context) throws ScriptException, IOException {
047        open(context);
048        flush();
049        close();
050      }
051    
052      public void setPiped(boolean piped) {
053        command.setPiped(piped);
054      }
055    
056      public Class<P> getProducedType() {
057        return command.getProducedType();
058      }
059    
060      public Class<C> getConsumedType() {
061        return command.getConsumedType();
062      }
063    
064      public boolean takeAlternateBuffer() {
065        return context.takeAlternateBuffer();
066      }
067    
068      public boolean releaseAlternateBuffer() {
069        return context.releaseAlternateBuffer();
070      }
071    
072      public String getProperty(String propertyName) {
073        return context.getProperty(propertyName);
074      }
075    
076      public String readLine(String msg, boolean echo) {
077        return context.readLine(msg, echo);
078      }
079    
080      public int getWidth() {
081        return context.getWidth();
082      }
083    
084      public int getHeight() {
085        return context.getHeight();
086      }
087    
088      public void open(ProducerContext<P> context) {
089    
090        //
091        this.context = context;
092    
093        // Now open command
094        command.open(context);
095      }
096    
097      public void provide(C element) throws IOException {
098        if (command.getConsumedType().isInstance(element)) {
099          command.provide(element);
100        }
101      }
102    
103      public void flush() throws IOException {
104        command.flush();
105      }
106    
107      public void close() throws ScriptException {
108        command.close();
109      }
110    }