source: branches/MetisMQI/src/main/java/weka/core/scripting/Groovy.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 6.2 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * Groovy.java
19 * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.core.scripting;
23
24import weka.core.RevisionHandler;
25import weka.core.RevisionUtils;
26
27import java.io.File;
28import java.io.Serializable;
29import java.lang.reflect.Constructor;
30import java.lang.reflect.Method;
31
32/**
33 * A helper class for <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>.
34 * <p/>
35 * In order to use Groovy, the jar containing all the classes must be present
36 * in the CLASSPATH. This jar is normally found in the <i>embeddable</i>
37 * sub-directory of the Groovy installation.
38 * <p/>
39 * Tested with Groovy 1.5.7.
40 *
41 * @author  fracpete (fracpete at waikato dot ac dot nz)
42 * @version $Revision: 5953 $
43 */
44public class Groovy
45  implements Serializable, RevisionHandler {
46
47  /** for serialization. */
48  private static final long serialVersionUID = -2628766602043134673L;
49
50  /** the classname of the Groovy classloader. */
51  public final static String CLASS_GROOVYCLASSLOADER = "groovy.lang.GroovyClassLoader";
52 
53  /** whether the Groovy classes are in the Classpath. */
54  protected static boolean m_Present = false;
55  static {
56    try {
57      Class.forName(CLASS_GROOVYCLASSLOADER);
58      m_Present = true;
59    }
60    catch (Exception e) {
61      m_Present = false;
62    }
63  }
64 
65  /** the classloader. */
66  protected Object m_ClassLoader;
67 
68  /**
69   * default constructor, tries to instantiate a Groovy classloader.
70   */
71  public Groovy() {
72    m_ClassLoader = newClassLoader();
73  }
74 
75  /**
76   * returns the currently used Groovy classloader.
77   *
78   * @return            the classloader, can be null
79   */
80  public Object getClassLoader() {
81    return m_ClassLoader;
82  }
83 
84  /**
85   * executes the specified method on the current interpreter and returns the
86   * result, if any.
87   *
88   * @param methodName          the name of the method
89   * @param paramClasses        the classes of the parameters
90   * @param paramValues         the values of the parameters
91   * @return                    the return value of the method, if any (in that case null)
92   */
93  public Object invoke(String methodName, Class[] paramClasses, Object[] paramValues) {
94    Object      result;
95   
96    result = null;
97    if (getClassLoader() != null)
98      result = invoke(getClassLoader(), methodName, paramClasses, paramValues);
99   
100    return result;
101  }
102 
103  /**
104   * returns whether the Groovy classes are present or not, i.e. whether the
105   * classes are in the classpath or not
106   *
107   * @return                    whether the Groovy classes are available
108   */
109  public static boolean isPresent() {
110    return m_Present;
111  }
112
113  /**
114   * initializes and returns a Groovy Interpreter.
115   *
116   * @return                    the interpreter or null if Groovy classes not present
117   */
118  public static Object newClassLoader() {
119    Object      result;
120    Class<?>    cls;
121    Constructor constr;
122   
123    result = null;
124   
125    if (isPresent()) {
126      try {
127        cls    = Class.forName(CLASS_GROOVYCLASSLOADER);
128        constr = cls.getConstructor(new Class[]{ClassLoader.class});
129        result = constr.newInstance(Groovy.class.getClassLoader());
130      }
131      catch (Exception e) {
132        e.printStackTrace();
133        result = null;
134      }
135    }
136
137    return result;
138  }
139
140  /**
141   * loads the module and returns a new instance of it as instance of the
142   * provided Java class template.
143   *
144   * @param file                the Groovy module file
145   * @param template            the template for the returned Java object
146   * @return                    the Groovy object
147   */
148  public static Object newInstance(File file, Class template) {
149    Object      result;
150    Object      interpreter;
151    Class       cls;
152
153    result = null;
154
155    if (!isPresent())
156      return result;
157   
158    interpreter = newClassLoader();
159    if (interpreter == null)
160      return result;
161
162    try {
163      cls    = (Class) invoke(interpreter, "parseClass", new Class[]{File.class}, new Object[]{file});
164      result = cls.newInstance();
165    }
166    catch (Exception e) {
167      e.printStackTrace();
168    }
169
170    return result;
171  }
172 
173  /**
174   * executes the specified method and returns the result, if any.
175   *
176   * @param o                   the object the method should be called from,
177   *                            e.g., a Groovy Interpreter
178   * @param methodName          the name of the method
179   * @param paramClasses        the classes of the parameters
180   * @param paramValues         the values of the parameters
181   * @return                    the return value of the method, if any (in that case null)
182   */
183  public static Object invoke(Object o, String methodName, Class[] paramClasses, Object[] paramValues) {
184    Method      m;
185    Object      result;
186   
187    result = null;
188   
189    try {
190      m      = o.getClass().getMethod(methodName, paramClasses);
191      result = m.invoke(o, paramValues);
192    }
193    catch (Exception e) {
194      e.printStackTrace();
195      result = null;
196    }
197   
198    return result;
199  }
200 
201  /**
202   * Returns the revision string.
203   *
204   * @return            the revision
205   */
206  public String getRevision() {
207    return RevisionUtils.extract("$Revision: 5953 $");
208  }
209 
210  /**
211   * If no arguments are given, it just prints the presence of the Groovy
212   * classes, otherwise it expects a Groovy filename to execute.
213   *
214   * @param args                commandline arguments
215   */
216  public static void main(String[] args) {
217    if (args.length == 0) {
218      System.out.println("Groovy present: " + isPresent());
219    }
220    else {
221      Groovy groovy = new Groovy();
222      if (groovy.getClassLoader() == null) {
223        System.err.println("Cannot instantiate Groovy ClassLoader!");
224      }
225      else {
226        Object groovyObject = Groovy.newInstance(new File(args[0]), Object.class);
227        Groovy.invoke(groovyObject, "run", new Class[]{}, new Object[]{});
228      }
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.