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.CommandInvoker;
023    import org.crsh.command.NoSuchCommandException;
024    import org.crsh.command.ScriptException;
025    import org.crsh.command.ShellCommand;
026    import org.crsh.shell.ErrorType;
027    import org.crsh.shell.ShellResponse;
028    import org.crsh.shell.ShellProcessContext;
029    import org.crsh.text.Chunk;
030    import org.crsh.util.Safe;
031    
032    import java.util.LinkedList;
033    import java.util.regex.Pattern;
034    
035    /**
036     * A factory for a pipeline.
037     */
038    public class PipeLineFactory {
039    
040      /** . */
041      final String line;
042    
043      /** . */
044      final String name;
045    
046      /** . */
047      final String rest;
048    
049      /** . */
050      final PipeLineFactory next;
051    
052      public String getLine() {
053        return line;
054      }
055    
056      PipeLineFactory(String line, PipeLineFactory next) {
057    
058        Pattern p = Pattern.compile("^\\s*(\\S+)");
059        java.util.regex.Matcher m = p.matcher(line);
060        String name = null;
061        String rest = null;
062        if (m.find()) {
063          name = m.group(1);
064          rest = line.substring(m.end());
065        }
066    
067        //
068        this.name = name;
069        this.rest = rest;
070        this.line = line;
071        this.next = next;
072      }
073    
074      public CommandInvoker<Void, Chunk> create(CRaSHSession session) throws NoSuchCommandException {
075    
076        //
077        LinkedList<CommandInvoker> pipes = new LinkedList<CommandInvoker>();
078        for (PipeLineFactory current = this;current != null;current = current.next) {
079          CommandInvoker commandInvoker = null;
080          if (current.name != null) {
081            ShellCommand command = session.crash.getCommand(current.name);
082            if (command != null) {
083              commandInvoker = command.resolveInvoker(current.rest);
084            }
085          }
086          if (commandInvoker == null) {
087            throw new NoSuchCommandException(current.name);
088          } else {
089            commandInvoker.setSession(session);
090          }
091          pipes.add(commandInvoker);
092        }
093    
094        //
095        return new PipeLine(pipes.toArray(new CommandInvoker[pipes.size()]));
096      }
097    
098      PipeLineFactory getLast() {
099        if (next != null) {
100          return next.getLast();
101        }
102        return this;
103      }
104    }