001: /*
002: The contents of this file are subject to the Mozilla Public License Version 1.1
003: (the "License"); you may not use this file except in compliance with the License.
004: You may obtain a copy of the License at http://www.mozilla.org/MPL/
005:
006: Software distributed under the License is distributed on an "AS IS" basis,
007: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: for the specific language governing rights and limitations under the License.
009:
010: The Original Code is "The Columba Project"
011:
012: The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014:
015: All Rights Reserved.
016: */
017: package org.columba.core.scripting.interpreter;
018:
019: import groovy.lang.GroovyClassLoader;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.lang.reflect.InvocationTargetException;
024: import java.lang.reflect.Method;
025: import java.lang.reflect.Modifier;
026: import java.util.Map;
027:
028: import org.codehaus.groovy.control.CompilationFailedException;
029: import org.columba.core.scripting.model.ColumbaScript;
030:
031: /**
032: @author Celso Pinto (cpinto@yimports.com)
033: */
034: public class GroovyInterpreter extends ScriptInterpreter {
035:
036: private final static String[] EXTENSIONS = new String[] { "groovy" };
037:
038: public String getName() {
039: return "Goovy Interpreter";
040: }
041:
042: public String[] getSupportedExtensions() {
043: return EXTENSIONS;
044: }
045:
046: public void execute(ColumbaScript script, Map vars) {
047: /*
048: * it's the script responsability to define the "metadata" by invoking
049: * .setName(), .setAuthor() and .setDescription()
050: */
051: logger.append("Loading groovy script: " + script.getPath());
052:
053: GroovyClassLoader gcl = new GroovyClassLoader(getClass()
054: .getClassLoader());
055:
056: File groovyFile = new File(script.getPath());
057:
058: try {
059: Class clazz = gcl.parseClass(groovyFile);
060:
061: /*
062: if (!GroovyScriptInterface.class.isAssignableFrom(clazz))
063: {
064: System.out.printf("Goovy script %s doesn't implement GroovyScriptInterface. " +
065: "Skipping...",script.getPath());
066: return;
067: }
068: */
069:
070: Method mainMethod = null;
071: boolean hasMain = true;
072: try {
073: mainMethod = clazz.getDeclaredMethod("main",
074: java.util.Map.class);
075: } catch (NoSuchMethodException e) {
076: hasMain = false;
077: }
078:
079: if (!hasMain
080: || (mainMethod.getModifiers() & Modifier.PUBLIC) == 0
081: || (mainMethod.getModifiers() & Modifier.STATIC) == 0) {
082: logger
083: .append(
084: "Failed to load script",
085: String
086: .format(
087: "Groovy script %s doesn't declare a "
088: + "'public static main(Map)' method. Skipping....",
089: script.getPath()));
090: return;
091: }
092:
093: mainMethod.invoke(null, vars);
094:
095: } catch (CompilationFailedException e) {
096: logger.append(String.format("Failed compilation of %s",
097: script.getPath()), e);
098: } catch (IOException e) {
099: logger.append(String.format("I/O Exception in %s", script
100: .getPath()), e);
101: } catch (IllegalAccessException e) {
102: logger.append(String.format(
103: "IllegalAccessException when calling main in %s",
104: script.getPath()), e);
105: } catch (InvocationTargetException e) {
106: logger
107: .append(
108: String
109: .format(
110: "InvocationTargetException when calling main in %s",
111: script.getPath()), e);
112: }
113:
114: }
115:
116: }
|