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