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.Pipe;
023 import org.crsh.command.BaseCommandContext;
024 import org.crsh.command.InvocationContext;
025 import org.crsh.shell.ShellProcessContext;
026 import org.crsh.text.Chunk;
027 import org.crsh.text.RenderingContext;
028 import org.crsh.text.RenderPrintWriter;
029
030 import java.io.IOException;
031 import java.util.Map;
032
033 public class CRaSHInvocationContext<P> extends BaseCommandContext implements InvocationContext<P> {
034
035 /** . */
036 private final ShellProcessContext processContext;
037
038 /** . */
039 private RenderPrintWriter writer;
040
041 /** . */
042 protected Pipe<Object> producer;
043
044 protected CRaSHInvocationContext(
045 ShellProcessContext processContext,
046 Map<String, Object> session,
047 Map<String, Object> attributes,
048 Pipe<Object> producer) {
049 super(session, attributes);
050
051 //
052 this.processContext = processContext;
053 this.producer = producer;
054 }
055
056 public int getWidth() {
057 return processContext.getWidth();
058 }
059
060 public int getHeight() {
061 return processContext.getHeight();
062 }
063
064 public String getProperty(String propertyName) {
065 return processContext.getProperty(propertyName);
066 }
067
068 public String readLine(String msg, boolean echo) {
069 return processContext.readLine(msg, echo);
070 }
071
072 public void provide(P element) throws IOException {
073 producer.provide(element);
074 }
075
076 public void flush() throws IOException {
077 producer.flush();
078 }
079
080 public RenderPrintWriter getWriter() {
081 if (writer == null) {
082 writer = new RenderPrintWriter(new RenderingContext() {
083 public int getWidth() {
084 return processContext.getWidth();
085 }
086 public void provide(Chunk element) throws IOException {
087 producer.provide(element);
088 }
089 public void flush() throws IOException {
090 producer.flush();
091 }
092 });
093 }
094 return writer;
095 }
096 }