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.command; 021 022 import org.crsh.io.Consumer; 023 import org.crsh.text.Chunk; 024 import org.crsh.shell.ScreenContext; 025 import org.crsh.text.RenderPrintWriter; 026 027 import java.io.IOException; 028 import java.util.Map; 029 030 class InnerInvocationContext<P> implements InvocationContext<P> { 031 032 /** . */ 033 final InvocationContext<?> outter; 034 035 /** . */ 036 final Consumer<P> consumer; 037 038 /** . */ 039 private RenderPrintWriter writer; 040 041 /** . */ 042 private final boolean piped; 043 044 InnerInvocationContext( 045 InvocationContext<?> outter, 046 Consumer<P> consumer, 047 boolean piped) { 048 049 // 050 this.outter = outter; 051 this.consumer = consumer; 052 this.piped = piped; 053 } 054 055 public boolean isPiped() { 056 return piped; 057 } 058 059 public CommandInvoker<?, ?> resolve(String s) throws ScriptException, IOException { 060 return outter.resolve(s); 061 } 062 063 public int getWidth() { 064 return outter.getWidth(); 065 } 066 067 public int getHeight() { 068 return outter.getHeight(); 069 } 070 071 public boolean takeAlternateBuffer() throws IOException { 072 return outter.takeAlternateBuffer(); 073 } 074 075 public boolean releaseAlternateBuffer() throws IOException { 076 return outter.releaseAlternateBuffer(); 077 } 078 079 public String getProperty(String propertyName) { 080 return outter.getProperty(propertyName); 081 } 082 083 public String readLine(String msg, boolean echo) { 084 return outter.readLine(msg, echo); 085 } 086 087 public RenderPrintWriter getWriter() { 088 if (writer == null) { 089 writer = new RenderPrintWriter(new ScreenContext<Chunk>() { 090 public Class<Chunk> getConsumedType() { 091 return Chunk.class; 092 } 093 public int getWidth() { 094 return outter.getWidth(); 095 } 096 public int getHeight() { 097 return outter.getHeight(); 098 } 099 public void provide(Chunk element) throws IOException { 100 Class<P> consumedType = consumer.getConsumedType(); 101 if (consumedType.isInstance(element)) { 102 P p = consumedType.cast(element); 103 consumer.provide(p); 104 } 105 } 106 public void flush() throws IOException { 107 consumer.flush(); 108 } 109 }); 110 } 111 return writer; 112 } 113 114 public Class<P> getConsumedType() { 115 return consumer.getConsumedType(); 116 } 117 118 public void provide(P element) throws IOException { 119 consumer.provide(element); 120 } 121 122 public void flush() throws IOException { 123 consumer.flush(); 124 } 125 126 public void close() throws IOException { 127 // Nothing to do 128 } 129 130 public Map<String, Object> getSession() { 131 return outter.getSession(); 132 } 133 134 public Map<String, Object> getAttributes() { 135 return outter.getAttributes(); 136 } 137 }