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