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