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.ProducerContext;
027
028 import java.io.IOException;
029 import java.util.Map;
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 CommandInvoker<C, P> command;
038
039 /** . */
040 private ProducerContext<P> context;
041
042 // /** . */
043 // private RenderPrintWriter writer;
044
045 InvokerPipeFilter(CommandInvoker<C, P> command) {
046 this.command = command;
047 }
048
049 public void invoke(InvocationContext<P> context) throws ScriptException, IOException {
050 open(context);
051 flush();
052 close();
053 }
054
055 public void setPiped(boolean piped) {
056 command.setPiped(piped);
057 }
058
059 public Class<P> getProducedType() {
060 return command.getProducedType();
061 }
062
063 public Class<C> getConsumedType() {
064 return command.getConsumedType();
065 }
066
067 public Map<String, Object> getSession() {
068 return context.getSession();
069 }
070
071 public Map<String, Object> getAttributes() {
072 return context.getAttributes();
073 }
074
075 public String getProperty(String propertyName) {
076 return context.getProperty(propertyName);
077 }
078
079 public String readLine(String msg, boolean echo) {
080 return context.readLine(msg, echo);
081 }
082
083 public int getWidth() {
084 return context.getWidth();
085 }
086
087 public int getHeight() {
088 return context.getHeight();
089 }
090
091 public void open(ProducerContext<P> context) {
092
093 //
094 this.context = context;
095
096 // Now open command
097 command.open(context);
098 }
099
100 public void provide(C element) throws IOException {
101 if (command.getConsumedType().isInstance(element)) {
102 command.provide(element);
103 }
104 }
105
106 public void flush() throws IOException {
107
108 // First flush the command
109 command.flush();
110
111 // Flush the next because the command may not call it
112 context.flush();
113 }
114
115 public void close() throws ScriptException {
116 command.close();
117 }
118 }