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