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