001    /*
002     * Copyright (C) 2003-2009 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.shell.impl;
020    
021    import groovy.lang.Binding;
022    import groovy.lang.Closure;
023    import groovy.lang.MissingPropertyException;
024    import groovy.lang.Script;
025    import org.crsh.command.CommandContext;
026    import org.crsh.command.DescriptionMode;
027    import org.crsh.command.InvocationContext;
028    import org.crsh.command.ScriptException;
029    import org.crsh.command.CommandInvoker;
030    import org.crsh.command.ShellCommand;
031    import org.crsh.shell.io.ShellPrinter;
032    import org.crsh.util.Strings;
033    
034    import java.util.Collections;
035    import java.util.List;
036    import java.util.Map;
037    
038    /**
039     * This class provides the base class for Groovy scripts. It should not be used directly as it is rather used
040     * for configuring a Groovy {@link org.codehaus.groovy.control.CompilerConfiguration#setScriptBaseClass(String)} class.
041     *
042     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
043     * @version $Revision$
044     */
045    public abstract class GroovyScriptCommand extends Script implements ShellCommand, CommandInvoker<Void, Void> {
046    
047      /** . */
048      private String[] args;
049    
050      public final Class<Void> getProducedType() {
051        return Void.class;
052      }
053    
054      public final Class<Void> getConsumedType() {
055        return Void.class;
056      }
057    
058      @Override
059      public final Object getProperty(String property) {
060        try {
061          return super.getProperty(property);
062        }
063        catch (MissingPropertyException e) {
064          return null;
065        }
066      }
067    
068      public final Map<String, String> complete(CommandContext context, String line) {
069        return Collections.emptyMap();
070      }
071    
072      public String describe(String line, DescriptionMode mode) {
073        return null;
074      }
075    
076      public final void invoke(InvocationContext<Void, Void> context) throws ScriptException {
077    
078        // Set up current binding
079        Binding binding = new Binding(context.getAttributes());
080    
081        // Set the args on the script
082        binding.setProperty("args", args);
083    
084        //
085        setBinding(binding);
086    
087        //
088        Object res = run();
089    
090        // Evaluate the closure
091        if (res instanceof Closure) {
092          Closure closure = (Closure)res;
093          res = closure.call(args);
094        }
095    
096        //
097        if (res != null) {
098          context.getWriter().print(res);
099        }
100      }
101    
102      public final CommandInvoker<?, ?> createInvoker(String line) {
103        List<String> chunks = Strings.chunks(line);
104        this.args = chunks.toArray(new String[chunks.size()]);
105        return this;
106      }
107    }