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