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    package org.crsh.lang.groovy.shell;
020    
021    import groovy.lang.Binding;
022    import groovy.lang.Closure;
023    import groovy.lang.GroovyShell;
024    import org.codehaus.groovy.control.CompilerConfiguration;
025    import org.codehaus.groovy.runtime.InvokerHelper;
026    import org.crsh.command.NoSuchCommandException;
027    import org.crsh.command.ShellCommand;
028    import org.crsh.lang.CommandManager;
029    import org.crsh.lang.groovy.command.GroovyScript;
030    import org.crsh.lang.groovy.command.GroovyScriptCommand;
031    import org.crsh.plugin.PluginContext;
032    import org.crsh.plugin.ResourceKind;
033    
034    import java.util.HashMap;
035    import java.util.logging.Level;
036    import java.util.logging.Logger;
037    
038    /** @author Julien Viet */
039    public class GroovyCommandManager extends CommandManager {
040    
041      /** . */
042      static final Logger log = Logger.getLogger(GroovyCommandManager.class.getName());
043    
044      /** . */
045      final PluginContext context;
046    
047      /** . */
048      final AbstractClassManager<? extends ShellCommand> commandManager;
049    
050      /** . */
051      final AbstractClassManager<? extends GroovyScript> scriptManager;
052    
053      public GroovyCommandManager(PluginContext context) {
054        this.context = context;
055        this.commandManager = new ClassManager<ShellCommand>(context, ResourceKind.COMMAND, ShellCommand.class, GroovyScriptCommand.class);
056        this.scriptManager = new ClassManager<GroovyScript>(context, ResourceKind.LIFECYCLE, GroovyScript.class, GroovyScript.class);
057      }
058    
059      public String doCallBack(HashMap<String, Object> session, String name, String defaultValue) {
060        return eval(session, name, defaultValue);
061      }
062    
063      public void init(HashMap<String, Object> session) {
064        try {
065          GroovyScript login = getLifeCycle(session, "login");
066          if (login != null) {
067            login.setBinding(new Binding(session));
068            login.run();
069          }
070        }
071        catch (NoSuchCommandException e) {
072          e.printStackTrace();
073        }
074      }
075    
076      public void destroy(HashMap<String, Object> session) {
077        try {
078          GroovyScript logout = getLifeCycle(session, "logout");
079          if (logout != null) {
080            logout.setBinding(new Binding(session));
081            logout.run();
082          }
083        }
084        catch (NoSuchCommandException e) {
085          e.printStackTrace();
086        }
087      }
088    
089      /**
090       * Used for testing purposes.
091       *
092       * @return a groovy shell operating on the session attributes
093       */
094      public GroovyShell getGroovyShell(HashMap<String, Object> session) {
095        GroovyShell shell = (GroovyShell)session.get("shell");
096        if (shell == null) {
097          CompilerConfiguration config = new CompilerConfiguration();
098          config.setRecompileGroovySource(true);
099          config.setScriptBaseClass(GroovyScriptCommand.class.getName());
100          shell = new GroovyShell(context.getLoader(), new Binding(session), config);
101          session.put("shell", shell);
102        }
103        return shell;
104      }
105    
106      private String eval(HashMap<String, Object> session, String name, String def) {
107        try {
108          GroovyShell shell = getGroovyShell(session);
109          Object ret = shell.evaluate("return " + name + ";");
110          if (ret instanceof Closure) {
111            log.log(Level.FINEST, "Invoking " + name + " closure");
112            Closure c = (Closure)ret;
113            ret = c.call();
114          } else if (ret == null) {
115            log.log(Level.FINEST, "No " + name + " will use empty");
116            return def;
117          }
118          return String.valueOf(ret);
119        }
120        catch (Exception e) {
121          log.log(Level.SEVERE, "Could not get a " + name + " message, will use empty", e);
122          return def;
123        }
124      }
125    
126      public GroovyScript getLifeCycle(HashMap<String, Object> session, String name) throws NoSuchCommandException, NullPointerException {
127        Class<? extends GroovyScript> scriptClass = scriptManager.getClass(name);
128        if (scriptClass != null) {
129          GroovyScript script = (GroovyScript)InvokerHelper.createScript(scriptClass, new Binding(session));
130          script.setBinding(new Binding(session));
131          return script;
132        } else {
133          return null;
134        }
135      }
136    
137      public ShellCommand getCommand(String name) throws NoSuchCommandException, NullPointerException {
138        return commandManager.getInstance(name);
139      }
140    }