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.util.LineFeedWriter;
026    
027    import java.io.StringWriter;
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 StringWriter buffer;
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> attributes) {
054        super(attributes);
055        this.writer = null;
056        this.buffer = null;
057        this.consumedItems = consumedItems;
058        this.producedItems = Collections.emptyList();
059      }
060    
061      @Override
062      protected Map<String, Object> attributes(Map<String, Object> attributes) {
063        return new AttributesMap(this, attributes);
064      }
065    
066      public List<P> getProducedItems() {
067        return producedItems;
068      }
069    
070      public StringWriter getBuffer() {
071        return buffer;
072      }
073    
074      public boolean isPiped() {
075        return consumedItems != null;
076      }
077    
078      public Iterable<C> consume() {
079        if (consumedItems == null) {
080          throw new IllegalStateException("Cannot consume as no pipe operation is involved");
081        }
082        return consumedItems;
083      }
084    
085      public void produce(P product) {
086        if (producedItems.isEmpty()) {
087          producedItems = new LinkedList<P>();
088        }
089        producedItems.add(product);
090      }
091    
092      public ShellPrinter getWriter() {
093        if (writer == null) {
094          buffer = new StringWriter();
095          writer = new ShellPrinter(new LineFeedWriter(buffer, "\r\n"));
096        }
097        return writer;
098      }
099    }