001 /**
002 * Copyright 2007 Mike Kroutikov.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the Lesser GNU General Public License as
006 * published by the Free Software Foundation; either version 3 of
007 * the License, or (at your option) any later version.
008 *
009 * This program 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
012 * Lesser GNU General Public License for more details.
013 *
014 * You should have received a copy of the Lesser GNU General Public License
015 * along with this program. If not, see <http://www.gnu.org/licenses/>.
016 */
017
018 package org.otfeed.script;
019
020 import groovy.lang.Binding;
021 import groovy.lang.GroovyShell;
022
023 import java.io.FileInputStream;
024
025 /**
026 * Scripting engine: reads Groovy script file, and executes it.
027 * When packaged with org.otfeed driver, all Java API calls of the
028 * driver are available to Groovy script.
029 * </p>
030 * Groovy script receives one prameter, named "args", which is a list
031 * of command-line parameters (excluding the script file name).
032 */
033 public class Main {
034
035 private Main() { }
036
037 public static void main(String ... av) throws Exception {
038
039 if(av.length == 0) {
040 System.out.println("Please, supply script file name as first parameter");
041 return;
042 }
043
044 String [] args = new String[av.length - 1];
045 for(int i = 1; i < av.length; i++) {
046 args[i - 1] = av[i];
047 }
048
049 Binding binding = new Binding();
050 binding.setVariable("args", args);
051
052 GroovyShell shell = new GroovyShell(binding);
053 shell.evaluate(new FileInputStream(av[0]));
054 }
055 }