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