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