⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 groovyscriptengine.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: GroovyScriptEngine.java,v 1.14 2006/02/26 22:11:48 glaforge Exp $version Jan 9, 2004 12:19:58 PM $user Exp $ *  * Copyright 2003 (C) Sam Pullara. All Rights Reserved. *  * Redistribution and use of this software and associated documentation * ("Software"), with or without modification, are permitted provided that the * following conditions are met: 1. Redistributions of source code must retain * copyright statements and notices. Redistributions must also contain a copy * of this document. 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. 3. * The name "groovy" must not be used to endorse or promote products derived * from this Software without prior written permission of The Codehaus. For * written permission, please contact info@codehaus.org. 4. Products derived * from this Software may not be called "groovy" nor may "groovy" appear in * their names without prior written permission of The Codehaus. "groovy" is a * registered trademark of The Codehaus. 5. Due credit should be given to The * Codehaus - http://groovy.codehaus.org/ *  * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *   */package groovy.util;import groovy.lang.Binding;import groovy.lang.GroovyClassLoader;import groovy.lang.Script;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.security.AccessController;import java.security.PrivilegedAction;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.codehaus.groovy.control.CompilationFailedException;import org.codehaus.groovy.runtime.InvokerHelper;/** * Specific script engine able to reload modified scripts as well as dealing properly with dependent scripts. * * @author sam * @author Marc Palmer * @author Guillaume Laforge */public class GroovyScriptEngine implements ResourceConnector {    /**     * Simple testing harness for the GSE. Enter script roots as arguments and     * then input script names to run them.     *     * @param urls     * @throws Exception     */    public static void main(String[] urls) throws Exception {        URL[] roots = new URL[urls.length];        for (int i = 0; i < roots.length; i++) {            roots[i] = new File(urls[i]).toURL();        }        GroovyScriptEngine gse = new GroovyScriptEngine(roots);        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String line;        while (true) {            System.out.print("groovy> ");            if ((line = br.readLine()) == null || line.equals("quit"))                break;            try {                System.out.println(gse.run(line, new Binding()));            } catch (Exception e) {                e.printStackTrace();            }        }    }    private URL[] roots;    private Map scriptCache = Collections.synchronizedMap(new HashMap());    private ResourceConnector rc;    private ClassLoader parentClassLoader = getClass().getClassLoader();    private static class ScriptCacheEntry {        private Class scriptClass;        private long lastModified;        private Map dependencies = new HashMap();    }    /**     * Get a resource connection as a <code>URLConnection</code> to retrieve a script     * from the <code>ResourceConnector</code>     *     * @param resourceName name of the resource to be retrieved     * @return a URLConnection to the resource     * @throws ResourceException     */    public URLConnection getResourceConnection(String resourceName) throws ResourceException {        // Get the URLConnection        URLConnection groovyScriptConn = null;        ResourceException se = null;        for (int i = 0; i < roots.length; i++) {            URL scriptURL = null;            try {                scriptURL = new URL(roots[i], resourceName);                groovyScriptConn = scriptURL.openConnection();                // Make sure we can open it, if we can't it doesn't exist.                // Could be very slow if there are any non-file:// URLs in there                groovyScriptConn.getInputStream();                break; // Now this is a bit unusual            } catch (MalformedURLException e) {                String message = "Malformed URL: " + roots[i] + ", " + resourceName;                if (se == null) {                    se = new ResourceException(message);                } else {                    se = new ResourceException(message, se);                }            } catch (IOException e1) {                String message = "Cannot open URL: " + scriptURL;                if (se == null) {                    se = new ResourceException(message);                } else {                    se = new ResourceException(message, se);                }            }        }        // If we didn't find anything, report on all the exceptions that occurred.        if (groovyScriptConn == null) {            throw se;        }        return groovyScriptConn;    }    /**     * The groovy script engine will run groovy scripts and reload them and     * their dependencies when they are modified. This is useful for embedding     * groovy in other containers like games and application servers.     *     * @param roots This an array of URLs where Groovy scripts will be stored. They should     * be layed out using their package structure like Java classes      */    public GroovyScriptEngine(URL[] roots) {        this.roots = roots;        this.rc = this;    }    public GroovyScriptEngine(URL[] roots, ClassLoader parentClassLoader) {        this(roots);        this.parentClassLoader = parentClassLoader;    }    public GroovyScriptEngine(String[] urls) throws IOException {        roots = new URL[urls.length];        for (int i = 0; i < roots.length; i++) {            roots[i] = new File(urls[i]).toURL();        }        this.rc = this;    }    public GroovyScriptEngine(String[] urls, ClassLoader parentClassLoader) throws IOException {        this(urls);        this.parentClassLoader = parentClassLoader;    }    public GroovyScriptEngine(String url) throws IOException {        roots = new URL[1];        roots[0] = new File(url).toURL();        this.rc = this;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -