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.command.CommandContext;
023    import org.crsh.shell.impl.command.spi.CommandCreationException;
024    import org.crsh.command.InvocationContext;
025    import org.crsh.command.ScriptException;
026    import org.crsh.lang.script.Token;
027    import org.crsh.shell.ScreenContext;
028    import org.crsh.lang.script.PipeLineFactory;
029    import org.crsh.shell.impl.command.spi.CommandInvoker;
030    import org.crsh.text.Chunk;
031    import org.crsh.text.RenderPrintWriter;
032    import org.crsh.util.Utils;
033    
034    import java.io.IOException;
035    import java.util.Map;
036    
037    public final class InvocationContextImpl<P> implements InvocationContext<P> {
038    
039      /** . */
040      private static final int WRITTEN = 0;
041    
042      /** . */
043      private static final int FLUSHED = 1;
044    
045      /** . */
046      private static final int CLOSED = 2;
047    
048      /** . */
049      private final CommandContext<P> commandContext;
050    
051      /** . */
052      private RenderPrintWriter writer;
053    
054      /** . */
055      int status;
056    
057      public InvocationContextImpl(CommandContext<P> commandContext) {
058        this.commandContext = commandContext;
059        this.status = FLUSHED;
060      }
061    
062      public boolean isPiped() {
063        return commandContext.isPiped();
064      }
065    
066      public RenderPrintWriter getWriter() {
067        if (writer == null) {
068          writer = new RenderPrintWriter(new ScreenContext() {
069            public int getWidth() {
070              return InvocationContextImpl.this.getWidth();
071            }
072            public int getHeight() {
073              return InvocationContextImpl.this.getHeight();
074            }
075            public void write(Chunk chunk) throws IOException {
076              InvocationContextImpl.this.write(chunk);
077            }
078            public void flush() throws IOException {
079              InvocationContextImpl.this.flush();
080            }
081          });
082        }
083        return writer;
084      }
085    
086      public boolean takeAlternateBuffer() throws IOException {
087        return commandContext.takeAlternateBuffer();
088      }
089    
090      public boolean releaseAlternateBuffer() throws IOException {
091        return commandContext.releaseAlternateBuffer();
092      }
093    
094      public CommandInvoker<?, ?> resolve(String s) throws ScriptException, IOException {
095        CRaSHSession session = (CRaSHSession)getSession();
096        Token token2 = Token.parse(s);
097        PipeLineFactory factory = token2.createFactory();
098        try {
099          return factory.create(session);
100        }
101        catch (CommandCreationException e) {
102          throw new ScriptException(e);
103        }
104      }
105    
106      public Class<P> getConsumedType() {
107        return commandContext.getConsumedType();
108      }
109    
110      public String getProperty(String propertyName) {
111        return commandContext.getProperty(propertyName);
112      }
113    
114      public String readLine(String msg, boolean echo) throws IOException, InterruptedException {
115        return commandContext.readLine(msg, echo);
116      }
117    
118      public int getWidth() {
119        return commandContext.getWidth();
120      }
121    
122      public int getHeight() {
123        return commandContext.getHeight();
124      }
125    
126      public void write(Chunk chunk) throws IOException {
127        if (status != CLOSED) {
128          status = WRITTEN;
129          commandContext.write(chunk);
130        }
131      }
132    
133      public void provide(P element) throws IOException {
134        if (status != CLOSED) {
135          status = WRITTEN;
136          commandContext.provide(element);
137        }
138      }
139    
140      public void flush() throws IOException {
141        if (status == WRITTEN) {
142          status = FLUSHED;
143          commandContext.flush();
144        }
145      }
146    
147      public void close() throws IOException {
148        if (status != CLOSED) {
149          Utils.flush(this);
150          status = CLOSED;
151          Utils.close(commandContext);
152        }
153      }
154    
155      public Map<String, Object> getSession() {
156        return commandContext.getSession();
157      }
158    
159      public Map<String, Object> getAttributes() {
160        return commandContext.getAttributes();
161      }
162    
163      public InvocationContextImpl<P> leftShift(Object o) throws IOException {
164        Class<P> consumedType = getConsumedType();
165        if (consumedType.isInstance(o)) {
166          P p = consumedType.cast(o);
167          provide(p);
168        }
169        return this;
170      }
171    }