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.shell.ScreenContext; 024 import org.crsh.shell.ShellProcessContext; 025 import org.crsh.text.Chunk; 026 import org.crsh.text.ChunkAdapter; 027 import org.crsh.text.ChunkBuffer; 028 029 import java.io.Closeable; 030 import java.io.IOException; 031 import java.util.Map; 032 033 class CRaSHProcessContext implements CommandContext<Chunk>, Closeable { 034 035 /** . */ 036 private final CRaSHSession session; 037 038 /** . */ 039 private final ShellProcessContext processContext; 040 041 /** . */ 042 private final ChunkAdapter adapter; 043 044 /** . */ 045 private boolean useAlternateBuffer; 046 047 CRaSHProcessContext(CRaSHSession session, final ShellProcessContext processContext) { 048 049 // We use this chunk buffer to buffer stuff 050 // but also because it optimises the chunks 051 // which provides better perormances on the client 052 final ChunkBuffer buffer = new ChunkBuffer(processContext); 053 054 // 055 final ChunkAdapter adapter = new ChunkAdapter(new ScreenContext<Chunk>() { 056 public int getWidth() { 057 return processContext.getWidth(); 058 } 059 060 public int getHeight() { 061 return processContext.getHeight(); 062 } 063 064 public Class<Chunk> getConsumedType() { 065 return Chunk.class; 066 } 067 068 public void provide(Chunk element) throws IOException { 069 buffer.provide(element); 070 } 071 072 public void flush() throws IOException { 073 buffer.flush(); 074 } 075 }); 076 077 // 078 this.session = session; 079 this.processContext = processContext; 080 this.adapter = adapter; 081 this.useAlternateBuffer = false; 082 } 083 084 public boolean isPiped() { 085 throw new UnsupportedOperationException(); 086 } 087 088 public boolean takeAlternateBuffer() throws IOException { 089 return useAlternateBuffer = processContext.takeAlternateBuffer(); 090 } 091 092 public boolean releaseAlternateBuffer() throws IOException { 093 return useAlternateBuffer = processContext.releaseAlternateBuffer(); 094 } 095 096 public String getProperty(String propertyName) { 097 return processContext.getProperty(propertyName); 098 } 099 100 public String readLine(String msg, boolean echo) { 101 return processContext.readLine(msg, echo); 102 } 103 104 public int getWidth() { 105 return adapter.getWidth(); 106 } 107 108 public int getHeight() { 109 return adapter.getHeight(); 110 } 111 112 public Class<Chunk> getConsumedType() { 113 return Chunk.class; 114 } 115 116 public void provide(Chunk element) throws IOException { 117 adapter.provide(element); 118 } 119 120 public void flush() throws IOException { 121 adapter.flush(); 122 } 123 124 public Map<String, Object> getSession() { 125 return session; 126 } 127 128 public Map<String, Object> getAttributes() { 129 return session.crash.getContext().getAttributes(); 130 } 131 132 public void close() throws IOException { 133 if (useAlternateBuffer) { 134 releaseAlternateBuffer(); 135 } 136 } 137 }