001    /*
002     * Copyright (C) 2011 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     *
007     * published by the Free Software Foundation; either version 2.1 of
008     * the License, or (at your option) any later version.
009     *
010     * This software is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this software; if not, write to the Free
017     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
019     */
020    
021    package org.crsh.command.impl;
022    
023    import org.crsh.command.InvocationContext;
024    import org.crsh.shell.io.ShellPrinter;
025    import org.crsh.shell.io.ShellWriter;
026    import org.crsh.text.CharReader;
027    
028    import java.util.Collections;
029    import java.util.LinkedList;
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
035     * @version $Revision$
036     */
037    public abstract class BaseInvocationContext<C, P> extends BaseCommandContext implements InvocationContext<C, P> {
038    
039      /** . */
040      protected ShellPrinter writer;
041    
042      /** . */
043      protected CharReader reader;
044    
045      /** . */
046      protected List<P> producedItems;
047    
048      /** . */
049      protected Iterable<C> consumedItems;
050    
051      protected BaseInvocationContext(
052        Iterable<C> consumedItems,
053        Map<String, Object> session,
054        Map<String, Object> attributes) {
055        super(session, attributes);
056    
057        //
058        this.writer = null;
059        this.reader = null;
060        this.consumedItems = consumedItems;
061        this.producedItems = Collections.emptyList();
062      }
063    
064      public List<P> getProducedItems() {
065        return producedItems;
066      }
067    
068      public CharReader getReader() {
069        return reader;
070      }
071    
072      public boolean isPiped() {
073        return consumedItems != null;
074      }
075    
076      public Iterable<C> consume() {
077        if (consumedItems == null) {
078          throw new IllegalStateException("Cannot consume as no pipe operation is involved");
079        }
080        return consumedItems;
081      }
082    
083      public void produce(P product) {
084        if (producedItems.isEmpty()) {
085          producedItems = new LinkedList<P>();
086        }
087        producedItems.add(product);
088      }
089    
090      public ShellPrinter getWriter() {
091        if (writer == null) {
092          reader = new CharReader();
093          writer = new ShellPrinter(new ShellWriter(reader, "\r\n"), this);
094        }
095        return writer;
096      }
097    }