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          } else {
088            commandInvoker.setSession(session);
089          }
090          pipes.add(new InvokerPipeFilter(commandInvoker));
091        }
092    
093        //
094        return new PipeLine(session, pipes.toArray(new Filter[pipes.size()]));
095    
096      }
097    
098      PipeLineFactory getLast() {
099        if (next != null) {
100          return next.getLast();
101        }
102        return this;
103      }
104    
105      //
106      CRaSHProcess create(final CRaSHSession session, String request) {
107        return new CRaSHProcess(session, request) {
108          @Override
109          ShellResponse doInvoke(final ShellProcessContext context) throws InterruptedException {
110    
111            //
112            PipeLine proxy;
113            try {
114              proxy = create(crash);
115            }
116            catch (NoSuchCommandException e) {
117              return ShellResponse.unknownCommand(e.getCommandName());
118            }
119    
120            //
121            try {
122              proxy.invoke(new ProcessInvocationContext(session, context));
123            }
124            catch (ScriptException e) {
125              // Should we handle InterruptedException here ?
126              return build(e);
127            } catch (Throwable t) {
128              return build(t);
129            }
130            return ShellResponse.ok();
131          }
132        };
133      }
134    
135      private ShellResponse.Error build(Throwable throwable) {
136        ErrorType errorType;
137        if (throwable instanceof ScriptException) {
138          errorType = ErrorType.EVALUATION;
139          Throwable cause = throwable.getCause();
140          if (cause != null) {
141            throwable = cause;
142          }
143        } else {
144          errorType = ErrorType.INTERNAL;
145        }
146        String result;
147        String msg = throwable.getMessage();
148        if (throwable instanceof ScriptException) {
149          if (msg == null) {
150            result = name + ": failed";
151          } else {
152            result = name + ": " + msg;
153          }
154          return ShellResponse.error(errorType, result, throwable);
155        } else {
156          if (msg == null) {
157            msg = throwable.getClass().getSimpleName();
158          }
159          if (throwable instanceof RuntimeException) {
160            result = name + ": exception: " + msg;
161          } else if (throwable instanceof Exception) {
162            result = name + ": exception: " + msg;
163          } else if (throwable instanceof java.lang.Error) {
164            result = name + ": error: " + msg;
165          } else {
166            result = name + ": unexpected throwable: " + msg;
167          }
168          return ShellResponse.error(errorType, result, throwable);
169        }
170      }
171    }