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 */
019package org.crsh.lang.groovy;
020
021import groovy.lang.GroovyShell;
022import org.crsh.cli.impl.Delimiter;
023import org.crsh.cli.impl.completion.CompletionMatch;
024import org.crsh.cli.spi.Completion;
025import org.crsh.command.CommandContext;
026import org.crsh.command.CommandInvoker;
027import org.crsh.command.InvocationContextImpl;
028import org.crsh.lang.groovy.closure.PipeLineInvoker;
029import org.crsh.plugin.CRaSHPlugin;
030import org.crsh.repl.EvalResponse;
031import org.crsh.repl.REPL;
032import org.crsh.repl.REPLSession;
033
034import java.io.IOException;
035
036/**
037 * Groovy REPL implementation.
038 *
039 * @author Julien Viet
040 */
041public class GroovyREPL extends CRaSHPlugin<REPL> implements  REPL {
042
043  @Override
044  public REPL getImplementation() {
045    return this;
046  }
047
048  public String getName() {
049    return "groovy";
050  }
051
052  public EvalResponse eval(final REPLSession session, final String request) {
053    CommandInvoker<Void, Object> invoker = new CommandInvoker<Void, Object>() {
054      public void provide(Void element) throws IOException {
055        throw new UnsupportedOperationException("Should not be invoked");
056      }
057      public Class<Void> getConsumedType() {
058        return Void.class;
059      }
060      public void flush() throws IOException {
061      }
062      public Class<Object> getProducedType() {
063        return Object.class;
064      }
065      CommandContext<Object> foo;
066      public void open(CommandContext<? super Object> consumer) {
067        this.foo = (CommandContext<Object>)consumer;
068        GroovyShell shell = GroovyCommandManager.getGroovyShell(session.getContext(), session);
069        ShellBinding binding = (ShellBinding)shell.getContext();
070        binding.setCurrent(foo);
071        Object o;
072        try {
073          o = shell.evaluate(request);
074        }
075        finally {
076          binding.setCurrent(null);
077        }
078        if (o instanceof PipeLineInvoker) {
079          PipeLineInvoker eval = (PipeLineInvoker)o;
080          try {
081            eval.invoke(new InvocationContextImpl<Object>(foo));
082          }
083          catch (Exception e) {
084            throw new UnsupportedOperationException("handle me gracefully", e);
085          }
086        } else {
087          try {
088            if (o != null) {
089              consumer.provide(o);
090            }
091          }
092          catch (IOException e) {
093            throw new UnsupportedOperationException("handle me gracefully", e);
094          }
095        }
096      }
097      public void close() throws IOException {
098        foo.flush();
099        foo.close();
100      }
101    };
102    return new EvalResponse.Invoke(invoker);
103  }
104
105  public CompletionMatch complete(REPLSession session, String prefix) {
106    return new CompletionMatch(Delimiter.EMPTY, Completion.create());
107  }
108}