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