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