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.io.Filter;
027    import org.crsh.shell.ErrorType;
028    import org.crsh.shell.ShellResponse;
029    import org.crsh.shell.ShellProcessContext;
030    
031    import java.util.LinkedList;
032    import java.util.regex.Pattern;
033    
034    /**
035     * A factory for a pipeline.
036     */
037    public class PipeLineFactory {
038    
039      /** . */
040      final String line;
041    
042      /** . */
043      final String name;
044    
045      /** . */
046      final String rest;
047    
048      /** . */
049      final PipeLineFactory next;
050    
051      public String getLine() {
052        return line;
053      }
054    
055      PipeLineFactory(String line, PipeLineFactory next) {
056    
057        Pattern p = Pattern.compile("^\\s*(\\S+)");
058        java.util.regex.Matcher m = p.matcher(line);
059        String name = null;
060        String rest = null;
061        if (m.find()) {
062          name = m.group(1);
063          rest = line.substring(m.end());
064        }
065    
066        //
067        this.name = name;
068        this.rest = rest;
069        this.line = line;
070        this.next = next;
071      }
072    
073      public PipeLine create(CRaSHSession session) throws NoSuchCommandException {
074    
075        //
076        LinkedList<InvokerPipeFilter> pipes = new LinkedList<InvokerPipeFilter>();
077        for (PipeLineFactory current = this;current != null;current = current.next) {
078          CommandInvoker commandInvoker = null;
079          if (current.name != null) {
080            ShellCommand command = session.crash.getCommand(current.name);
081            if (command != null) {
082              commandInvoker = command.resolveInvoker(current.rest);
083            }
084          }
085          if (commandInvoker == null) {
086            throw new NoSuchCommandException(current.name);
087          }
088          pipes.add(new InvokerPipeFilter(commandInvoker));
089        }
090    
091        //
092        return new PipeLine(pipes.toArray(new Filter[pipes.size()]));
093    
094      }
095    
096      PipeLineFactory getLast() {
097        if (next != null) {
098          return next.getLast();
099        }
100        return this;
101      }
102    
103      //
104      CRaSHProcess create(final CRaSHSession session, String request) {
105        return new CRaSHProcess(session, request) {
106          @Override
107          ShellResponse doInvoke(final ShellProcessContext context) throws InterruptedException {
108    
109            //
110            PipeLine proxy;
111            try {
112              proxy = create(crash);
113            }
114            catch (NoSuchCommandException e) {
115              return ShellResponse.unknownCommand(e.getCommandName());
116            }
117    
118            //
119            try {
120              proxy.invoke(new ProcessInvocationContext(session, context));
121            }
122            catch (ScriptException e) {
123              // Should we handle InterruptedException here ?
124              return build(e);
125            } catch (Throwable t) {
126              return build(t);
127            }
128            return ShellResponse.ok();
129          }
130        };
131      }
132    
133      private ShellResponse.Error build(Throwable throwable) {
134        ErrorType errorType;
135        if (throwable instanceof ScriptException) {
136          errorType = ErrorType.EVALUATION;
137          Throwable cause = throwable.getCause();
138          if (cause != null) {
139            throwable = cause;
140          }
141        } else {
142          errorType = ErrorType.INTERNAL;
143        }
144        String result;
145        String msg = throwable.getMessage();
146        if (throwable instanceof ScriptException) {
147          if (msg == null) {
148            result = name + ": failed";
149          } else {
150            result = name + ": " + msg;
151          }
152          return ShellResponse.error(errorType, result, throwable);
153        } else {
154          if (msg == null) {
155            msg = throwable.getClass().getSimpleName();
156          }
157          if (throwable instanceof RuntimeException) {
158            result = name + ": exception: " + msg;
159          } else if (throwable instanceof Exception) {
160            result = name + ": exception: " + msg;
161          } else if (throwable instanceof java.lang.Error) {
162            result = name + ": error: " + msg;
163          } else {
164            result = name + ": unexpected throwable: " + msg;
165          }
166          return ShellResponse.error(errorType, result, throwable);
167        }
168      }
169    }