本文整理汇总了Java中groovy.util.GroovyScriptEngine类的典型用法代码示例。如果您正苦于以下问题:Java GroovyScriptEngine类的具体用法?Java GroovyScriptEngine怎么用?Java GroovyScriptEngine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GroovyScriptEngine类属于groovy.util包,在下文中一共展示了GroovyScriptEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getGroovyScriptEngine
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
/**
* Lazy getter of the script engine. use invalidateGroovyScriptEngine() method to regenerate it
* @return new or existing script engine
* @throws IOException
*/
public GroovyScriptEngine getGroovyScriptEngine() throws IOException {
File groovyFile = this.getGroovyFile();
if (groovyFile == null) {
return null;
}
if (this.gse == null) {
if (!groovyFile.isFile() || !groovyFile.canRead()) {
throw new IOException("Groovy script file " + groovyFile + " is not a real file or not readable");
}
String[] roots = new String[] {
groovyFile.getAbsolutePath()
};
this.gse = new GroovyScriptEngine(roots);
this.invalidateBinding();
}
return this.gse;
}
示例2: resetGroovyScriptEngine
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
@Button(list = "groovy", key = "reset.groovy.script.engine", order = 1, type = Button.TYPE_PRIMARY , icon = Button.ICON_RELOAD)
public Resolution resetGroovyScriptEngine() {
logger.info("Resetting Groovy script engine");
ServletContext servletContext = context.getServletContext();
GroovyScriptEngine groovyScriptEngine =
ScriptingUtil.createScriptEngine(groovyClasspath, getClass().getClassLoader());
ClassLoader classLoader = groovyScriptEngine.getGroovyClassLoader();
servletContext.setAttribute(BaseModule.CLASS_LOADER, classLoader);
servletContext.setAttribute(BaseModule.GROOVY_SCRIPT_ENGINE, groovyScriptEngine);
SessionMessages.addInfoMessage(ElementsThreadLocals.getText("script.engine.successfully.reset"));
logger.info("Clearing OGNL caches potentially holding Groovy objects");
OgnlUtils.clearCache();
logger.info("Groovy script engine reset.");
return new ForwardResolution("/m/admin/groovy.jsp");
}
示例3: testGroovyScriptEngineVsGroovyShell
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException {
// @todo refactor this path
File currentDir = new File("./src/test/groovy/bugs");
String file = "bug1567_script.groovy";
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
String[] test = null;
Object result = shell.run(new File(currentDir, file), test);
String[] roots = new String[]{currentDir.getAbsolutePath()};
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
binding = new Binding();
// a MME was ensued here stating no 't.start()' was available
// in the script
gse.run(file, binding);
}
示例4: GroovyMod
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
public GroovyMod(File directory, String name) {
File file = directory;
Throwable ex;
try {
Binding binding = new Binding();
URL groovyURL = GroovyMod.class.getResource("");
GroovyScriptEngine scriptEngine = new GroovyScriptEngine(new URL[]{groovyURL, file.toURI().toURL()});
CompilerConfiguration config = new CompilerConfiguration();
scriptEngine.setConfig(config);
script = new GroovyRunner(file, name, scriptEngine.getGroovyClassLoader());
/* binding.setVariable("dir", file);
binding.setVariable("name", name);
binding.setVariable("cl", scriptEngine.getGroovyClassLoader());
script = (GroovyModInterface) scriptEngine.run("GroovyRunner.groovy", binding);*/
ex = null;
} catch (Exception | AssertionError e) { // MalformedURLException | ResourceException | groovy.util.ScriptException |
ex = e;
}
this.exception = ex;
}
示例5: main
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
public static void main(String[] args) throws Exception
{
Binding binding = new Binding();
binding.setVariable("language", "Groovy");
GroovyShell shell = new GroovyShell(binding);
GroovyScriptEngine engine = new GroovyScriptEngine(new URL[]{GroovyTest.class.getClassLoader().getResource("/")});
Script script = shell.parse(GroovyTest.class.getClassLoader().getResource("random.groovy").toURI());
// System.out.println(script);
// script.invokeMethod("new SuRenRandom()", null);
// script.evaluate("new SuRenRandom()");
// engine.run("random.groovy", binding);
InputStream stream = GroovyTest.class.getClassLoader().getResource("random.groovy").openStream();
StringBuffer buf = new StringBuffer();
byte[] bf = new byte[1024];
int len = -1;
while((len = stream.read(bf)) != -1)
{
buf.append(new String(bf, 0, len));
}
buf.append("\n");
for(int i = 0; i < 30; i++)
{
System.out.println(shell.evaluate(buf.toString() + "new SuRenRandom().randomPhoneNum()"));
System.out.println(shell.evaluate(buf.toString() + "new SuRenRandom().randomEmail()"));
System.out.println(shell.evaluate(buf.toString() + "new SuRenRandom().randomZipCode()"));
}
}
示例6: getEngine
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
/**
* Strategy: build new instance on first call.
*
* @param managerService LEP manager service
* @return GroovyScriptEngine instance
*/
@Override
public GroovyScriptEngine getEngine(LepManagerService managerService) {
GroovyScriptEngine engine = this.groovyScriptEngine;
if (engine == null) {
synchronized (this) {
engine = this.groovyScriptEngine;
if (engine == null) {
this.groovyScriptEngine = engine = buildGroovyScriptEngine(managerService);
}
}
}
return engine;
}
示例7: buildGroovyScriptEngine
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
private GroovyScriptEngine buildGroovyScriptEngine(LepManagerService managerService) {
final ClassLoader parentClassLoader = getParentClassLoader();
final ResourceConnector rc = buildResourceConnector(managerService);
GroovyScriptEngine engine = (parentClassLoader == null)
? new GroovyScriptEngine(rc)
: new GroovyScriptEngine(rc, parentClassLoader);
initGroovyScriptEngine(engine, managerService);
return engine;
}
示例8: reloadScript
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
public Script reloadScript(String scriptName) {
try {
invalidateCache();
GroovyScriptEngine groovy = new GroovyScriptEngine(createClasspath(getEngineOperations().getEngine()).toArray(new String[0]),
shell.getClassLoader());
Script script = groovy.createScript(scriptName, binding);
script.run();
return script;
} catch (IOException | ResourceException | ScriptException e) {
throw SpongeUtils.wrapException(e);
}
}
示例9: getGroovyRenderer
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
private PrimitiveRenderer getGroovyRenderer() throws IOException, ResourceException, ScriptException {
if (this.groovyRenderer == null) {
GroovyScriptEngine gse = this.getGroovyScriptEngine();
Binding binding = this.getBinding();
gse.run(this.groovyFile.getName(), binding);
this.groovyRenderer = (PrimitiveRenderer) binding.getVariable("renderer");
}
return this.groovyRenderer;
}
示例10: clearCache
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
@Override
public void clearCache() {
getGroovyClassLoader().clearCache();
javaClassLoader.clearCache();
getPool().clear();
GroovyScriptEngine gse = getGroovyScriptEngine();
try {
Field scriptCacheField = gse.getClass().getDeclaredField("scriptCache");
scriptCacheField.setAccessible(true);
Map scriptCacheMap = (Map) scriptCacheField.get(gse);
scriptCacheMap.clear();
} catch (NoSuchFieldException | IllegalAccessException e) {
//ignore the exception
}
}
示例11: getGroovyScriptEngine
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
private GroovyScriptEngine getGroovyScriptEngine(boolean cleanBefore) {
try {
saveScriptsToFileSystem(cleanBefore);
String sourceFolder = HYBRIDBPM_DIRECTORY + System.getProperty("file.separator") + GROOVY_SOURCE_DIRECTORY + System.getProperty("file.separator");
// generate structure
String[] roots = new String[]{sourceFolder};
GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine(roots, Thread.currentThread().getContextClassLoader());
return groovyScriptEngine;
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
return null;
}
示例12: init
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
// Set up the scripting engine
String pathPrefix = getScriptRootPath(config);
try
{
this.gse = new GroovyScriptEngine(new String[]{pathPrefix});
}
catch(IOException ex) { throw new RuntimeException(ex); }
//gse.getConfig().setMinimumRecompilationInterval(0);
//System.out.println("MinimumRecompilationInterval " + gse.getConfig().getMinimumRecompilationInterval());
getServletContext().log("Groovy servlet initialized on " + gse + ".");
String initScript = getInitScript(config);
File initFile = new File(pathPrefix + initScript);
if (!initFile.exists())
throw new ServletException(initFile.getAbsolutePath() + " does not exist");
final Binding binding = new Binding();
binding.setVariable("itsNatServlet", itsNatServlet);
binding.setVariable("servlet", this);
binding.setVariable("config", config);
binding.setVariable("context", getServletContext());
binding.setVariable("application", getServletContext());
execGroovyScript(initScript,binding);
}
示例13: SecurityGroovyRealm
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
public SecurityGroovyRealm(GroovyScriptEngine groovyScriptEngine, String scriptUrl, ServletContext servletContext)
throws ScriptException, ResourceException, InstantiationException, IllegalAccessException {
this.groovyScriptEngine = groovyScriptEngine;
this.scriptUrl = scriptUrl;
this.servletContext = servletContext;
doEnsureDelegate();
}
示例14: getGroovyClass
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
public static Class<?> getGroovyClass(File scriptFile) throws IOException, ScriptException, ResourceException {
if(!scriptFile.exists()) {
return null;
}
GroovyScriptEngine scriptEngine =
(GroovyScriptEngine) ElementsThreadLocals.getServletContext().getAttribute(
BaseModule.GROOVY_SCRIPT_ENGINE);
return scriptEngine.loadScriptByName(scriptFile.toURI().toString());
}
示例15: executeScriptByPath
import groovy.util.GroovyScriptEngine; //导入依赖的package包/类
@Override
public void executeScriptByPath(String scriptPath, Map<Object, Object> context) {
try {
String scriptName = PathUtil.fileName(scriptPath);
String scriptDir = PathUtil.parentFolder(scriptPath);
GroovyScriptEngine gse = new GroovyScriptEngine(scriptDir);
Binding binding = new Binding(context);
gse.run(scriptName, binding);
} catch (Exception e) {
throw new CloudRuntimeException(e);
}
}