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 org.crsh.cli.impl.completion.CompletionMatch;
022import org.crsh.plugin.CRaSHPlugin;
023import org.crsh.repl.EvalResponse;
024import org.crsh.repl.Repl;
025import org.crsh.repl.ReplSession;
026
027import java.util.logging.Logger;
028
029/**
030 * @author Julien Viet
031 */
032public class GroovyRepl extends CRaSHPlugin<Repl> implements Repl {
033
034  /** . */
035  static final Logger log = Logger.getLogger(GroovyRepl.class.getName());
036
037  /** . */
038  private static final Repl groovyRepl = getREPL();
039
040  public static Repl getREPL() {
041    try {
042      Class<Repl> groovyReplClass = (Class<Repl>)GroovyRepl.class.getClassLoader().loadClass("org.crsh.lang.groovy.GroovyReplImpl");
043      return groovyReplClass.newInstance();
044    }
045    catch (Exception e) {
046      log.info("Plugin is inactive");
047      return null;
048    }
049    catch (NoClassDefFoundError e) {
050      log.info("Plugin is inactive");
051      return null;
052    }
053  }
054
055  @Override
056  public Repl getImplementation() {
057    return this;
058  }
059
060  @Override
061  public boolean isActive() {
062    return groovyRepl != null;
063  }
064
065  @Override
066  public String getName() {
067    return "groovy";
068  }
069
070  @Override
071  public String getDescription() {
072    return "The Groovy REPL provides a Groovy interpreter able to interact with shell commands";
073  }
074
075  @Override
076  public EvalResponse eval(ReplSession session, String request) {
077    if (groovyRepl != null) {
078      return groovyRepl.eval(session, request);
079    } else {
080      throw new IllegalStateException("Groovy REPL is not available");
081    }
082  }
083
084  @Override
085  public CompletionMatch complete(ReplSession session, String prefix) {
086    if (groovyRepl != null) {
087      return groovyRepl.complete(session, prefix);
088    } else {
089      throw new IllegalStateException("Groovy REPL is not available");
090    }
091  }
092}