KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > scripting > interpreter > GroovyInterpreter


1 /*
2  The contents of this file are subject to the Mozilla Public License Version 1.1
3  (the "License"); you may not use this file except in compliance with the License.
4  You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
6  Software distributed under the License is distributed on an "AS IS" basis,
7  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8  for the specific language governing rights and limitations under the License.
9
10  The Original Code is "The Columba Project"
11
12  The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13  Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
15  All Rights Reserved.
16  */

17 package org.columba.core.scripting.interpreter;
18
19 import groovy.lang.GroovyClassLoader;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.codehaus.groovy.control.CompilationFailedException;
29 import org.columba.core.scripting.model.ColumbaScript;
30
31 /**
32     @author Celso Pinto (cpinto@yimports.com)
33  */

34 public class GroovyInterpreter
35     extends ScriptInterpreter
36 {
37
38     private final static String JavaDoc[] EXTENSIONS = new String JavaDoc[]{"groovy"};
39
40     public String JavaDoc getName()
41     {
42         return "Goovy Interpreter";
43     }
44
45     public String JavaDoc[] getSupportedExtensions()
46     {
47         return EXTENSIONS;
48     }
49
50     public void execute(ColumbaScript script, Map JavaDoc vars)
51     {
52         /*
53         * it's the script responsability to define the "metadata" by invoking
54         * .setName(), .setAuthor() and .setDescription()
55         */

56         logger.append("Loading groovy script: " + script.getPath());
57
58         GroovyClassLoader gcl = new GroovyClassLoader(getClass().getClassLoader());
59
60         File JavaDoc groovyFile = new File JavaDoc(script.getPath());
61
62         try
63         {
64             Class JavaDoc clazz = gcl.parseClass(groovyFile);
65
66             /*
67             if (!GroovyScriptInterface.class.isAssignableFrom(clazz))
68             {
69                 System.out.printf("Goovy script %s doesn't implement GroovyScriptInterface. " +
70                                     "Skipping...",script.getPath());
71                 return;
72             }
73             */

74
75             Method JavaDoc mainMethod = null;
76             boolean hasMain = true;
77             try
78             {
79                 mainMethod = clazz.getDeclaredMethod("main", java.util.Map JavaDoc.class);
80             }
81             catch (NoSuchMethodException JavaDoc e)
82             {
83                 hasMain = false;
84             }
85
86             if (!hasMain ||
87                 (mainMethod.getModifiers() & Modifier.PUBLIC) == 0 ||
88                 (mainMethod.getModifiers() & Modifier.STATIC) == 0)
89             {
90                 logger.append( "Failed to load script",
91                                 String.format("Groovy script %s doesn't declare a " +
92                                                     "'public static main(Map)' method. Skipping....",
93                                                 script.getPath()));
94                 return;
95             }
96
97             mainMethod.invoke(null, vars);
98
99         }
100         catch (CompilationFailedException e)
101         {
102             logger.append(String.format("Failed compilation of %s", script.getPath()), e);
103         }
104         catch (IOException JavaDoc e)
105         {
106             logger.append(String.format("I/O Exception in %s", script.getPath()), e);
107         }
108         catch (IllegalAccessException JavaDoc e)
109         {
110             logger.append(String.format("IllegalAccessException when calling main in %s",
111                                             script.getPath()),
112                                         e);
113         }
114         catch (InvocationTargetException JavaDoc e)
115         {
116             logger.append(String.format("InvocationTargetException when calling main in %s",
117                                             script.getPath()),
118                                         e);
119         }
120
121     }
122
123 }
124
Popular Tags