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.ScreenContext;
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 final SessionContext sessionContext;
040
041 /** . */
042 private RenderPrintWriter writer;
043
044 InvocationContextImpl(ProducerContext<P> producerContext, SessionContext sessionContext) {
045 this.producerContext = producerContext;
046 this.sessionContext = sessionContext;
047 }
048
049 public RenderPrintWriter getWriter() {
050 if (writer == null) {
051 writer = new RenderPrintWriter(new ScreenContext<Chunk>() {
052 public int getWidth() {
053 return producerContext.getWidth();
054 }
055 public int getHeight() {
056 return producerContext.getHeight();
057 }
058 public void provide(Chunk element) throws IOException {
059 Class<P> consumedType = producerContext.getConsumedType();
060 if (consumedType.isInstance(element)) {
061 P p = consumedType.cast(element);
062 producerContext.provide(p);
063 }
064 }
065 public void flush() throws IOException {
066 producerContext.flush();
067 }
068 });
069 }
070 return writer;
071 }
072
073 public boolean takeAlternateBuffer() {
074 return producerContext.takeAlternateBuffer();
075 }
076
077 public boolean releaseAlternateBuffer() {
078 return producerContext.releaseAlternateBuffer();
079 }
080
081 public CommandInvoker<?, ?> resolve(String s) throws ScriptException, IOException {
082 // A bit nasty : will improve that later
083 CRaSHSession session = (CRaSHSession)getSession();
084 PipeLineParser parser= new PipeLineParser(s);
085 PipeLineFactory factory = parser.parse();
086 try {
087 return factory.create(session);
088 }
089 catch (NoSuchCommandException e) {
090 throw new ScriptException(e);
091 }
092 }
093
094 public Class<P> getConsumedType() {
095 return producerContext.getConsumedType();
096 }
097
098 public String getProperty(String propertyName) {
099 return producerContext.getProperty(propertyName);
100 }
101
102 public String readLine(String msg, boolean echo) {
103 return producerContext.readLine(msg, echo);
104 }
105
106 public int getWidth() {
107 return producerContext.getWidth();
108 }
109
110 public int getHeight() {
111 return producerContext.getHeight();
112 }
113
114 public void provide(P element) throws IOException {
115 producerContext.provide(element);
116 }
117
118 public void flush() throws IOException {
119 producerContext.flush();
120 }
121
122 public Map<String, Object> getSession() {
123 return sessionContext.getSession();
124 }
125
126 public Map<String, Object> getAttributes() {
127 return sessionContext.getAttributes();
128 }
129 }