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.shell.impl.command;
021    
022    import org.crsh.io.IOContext;
023    import org.crsh.io.ProducerContext;
024    import org.crsh.shell.ShellProcessContext;
025    import org.crsh.text.Chunk;
026    import org.crsh.text.ChunkAdapter;
027    import org.crsh.text.ChunkBuffer;
028    
029    import java.io.IOException;
030    import java.util.Map;
031    
032    class ProcessInvocationContext implements ProducerContext<Object> {
033    
034      /** . */
035      private final CRaSHSession session;
036    
037      /** . */
038      private final ShellProcessContext processContext;
039    
040      /** . */
041      private final ChunkAdapter adapter;
042    
043      ProcessInvocationContext(CRaSHSession session, final ShellProcessContext processContext) {
044    
045        // We use this chunk buffer to buffer stuff
046        // but also because it optimises the chunks
047        // which provides better perormances on the client
048        final ChunkBuffer buffer = new ChunkBuffer(processContext);
049    
050        //
051        final ChunkAdapter adapter = new ChunkAdapter(new IOContext<Chunk>() {
052          public int getWidth() {
053            return processContext.getWidth();
054          }
055    
056          public int getHeight() {
057            return processContext.getHeight();
058          }
059    
060          public void provide(Chunk element) throws IOException {
061            buffer.provide(element);
062          }
063    
064          public void flush() throws IOException {
065            buffer.flush();
066          }
067        });
068    
069        //
070        this.session = session;
071        this.processContext = processContext;
072        this.adapter = adapter;
073      }
074    
075      public String getProperty(String propertyName) {
076        return processContext.getProperty(propertyName);
077      }
078    
079      public String readLine(String msg, boolean echo) {
080        return processContext.readLine(msg, echo);
081      }
082    
083      public int getWidth() {
084        return adapter.getWidth();
085      }
086    
087      public int getHeight() {
088        return adapter.getHeight();
089      }
090    
091      public Class<Object> getConsumedType() {
092        return Object.class;
093      }
094    
095      public void provide(Object element) throws IOException {
096        adapter.provide(element);
097      }
098    
099      public void flush() throws IOException {
100        adapter.flush();
101      }
102    
103      public Map<String, Object> getSession() {
104        return session;
105      }
106    
107      public Map<String, Object> getAttributes() {
108        return session.crash.getContext().getAttributes();
109      }
110    }