本文整理汇总了Java中org.codehaus.groovy.runtime.InvokerHelper.createScript方法的典型用法代码示例。如果您正苦于以下问题:Java InvokerHelper.createScript方法的具体用法?Java InvokerHelper.createScript怎么用?Java InvokerHelper.createScript使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.groovy.runtime.InvokerHelper
的用法示例。
在下文中一共展示了InvokerHelper.createScript方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadScript
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
/**
* Loads a precompiled script with the supplied ID. The script class file must
* be stored in the pre-compiled scripts folder (see {@link Configuration#getFolderCompiledScripts()}.
*
* The method will return null if such a folder does not exist or if it does not contain
* the desired script.
* @param id
* @return
*/
public static Script loadScript(final String id) {
String fileName = id+".class";
Script cached = cache.get(fileName);
if (cached != null) {
return cached;
}
try {
URLClassLoader cl = getURLClassLoader();
if (cl != null) {
Script script = InvokerHelper.createScript(cl.loadClass(fileName), new Binding());
cache.put(fileName, script);
return script;
}
} catch (ClassNotFoundException | RuntimeException | MalformedURLException e) {
// do nothing and just build the class from the text
}
return null;
}
示例2: eval
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
/**
* Evaluate an expression.
*/
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
try {
Class scriptClass = evalScripts.get(script);
if (scriptClass == null) {
scriptClass = loader.parseClass(script.toString(), source);
evalScripts.put(script, scriptClass);
} else {
LOG.fine("eval() - Using cached script...");
}
//can't cache the script because the context may be different.
//but don't bother loading parsing the class again
Script s = InvokerHelper.createScript(scriptClass, context);
return s.run();
} catch (Exception e) {
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
}
}
示例3: run
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
public void run()
{
final long id = Thread.currentThread().getId();
// run the script numIter times
for (int i = 0; i < numIter; i++)
{
Builder builder = new Builder();
Binding binding = new Binding();
binding.setVariable("builder", builder);
script = InvokerHelper.createScript(scriptClass, binding);
script.run();
}
latch.countDown();
}
示例4: executeScript
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
protected void executeScript(Class scriptClass, Permission missingPermission) {
try {
Script script = InvokerHelper.createScript(scriptClass, new Binding());
script.run();
//InvokerHelper.runScript(scriptClass, null);
} catch (AccessControlException ace) {
if (missingPermission != null && missingPermission.implies(ace.getPermission())) {
return;
} else {
fail(ace.toString());
}
}
if (missingPermission != null) {
fail("Should catch an AccessControlException");
}
}
示例5: runScriptAtLocation
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context) throws GeneralException {
Script script = InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context));
Object result = null;
if (UtilValidate.isEmpty(methodName)) {
result = script.run();
} else {
result = script.invokeMethod(methodName, new Object[] { context });
}
return result;
}
示例6: computeQuotaKey
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
protected String computeQuotaKey(Map<String, Object> bindingVariables) throws Exception {
Binding binding = new Binding(bindingVariables);
Script script = InvokerHelper.createScript(scriptClass, binding);
Object result = script.run();
if(result!=null) {
return result.toString();
} else {
return null;
}
}
示例7: make
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
public Writable make(final Map map) {
return new Writable() {
/**
* Write the template document with the set binding applied to the writer.
*
* @see groovy.lang.Writable#writeTo(java.io.Writer)
*/
public Writer writeTo(Writer writer) {
Binding binding;
if (map == null)
binding = new Binding();
else
binding = new Binding(map);
Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
PrintWriter pw = new PrintWriter(writer);
scriptObject.setProperty("out", pw);
scriptObject.run();
pw.flush();
return writer;
}
/**
* Convert the template and binding into a result String.
*
* @see java.lang.Object#toString()
*/
public String toString() {
Writer sw = new StringBuilderWriter();
writeTo(sw);
return sw.toString();
}
};
}
示例8: writeTo
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
public Writer writeTo(Writer out) {
Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
PrintWriter pw = new PrintWriter(out);
scriptObject.setProperty("out", pw);
scriptObject.run();
pw.flush();
return out;
}
示例9: build
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
public Object build(Class viewClass) {
if (Script.class.isAssignableFrom(viewClass)) {
Script script = InvokerHelper.createScript(viewClass, this);
return build(script);
} else {
throw new RuntimeException("Only scripts can be executed via build(Class)");
}
}
示例10: assertScript
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
protected void assertScript(final String text, final String scriptName) throws Exception {
log.info("About to execute script");
log.info(text);
GroovyCodeSource gcs = AccessController.doPrivileged(
new PrivilegedAction<GroovyCodeSource>() {
@Override
public GroovyCodeSource run() {
return new GroovyCodeSource(text, scriptName, "/groovy/testSupport");
}
}
);
Class groovyClass = loader.parseClass(gcs);
Script script = InvokerHelper.createScript(groovyClass, new Binding());
script.run();
}
示例11: runScriptAtLocation
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context, GroovyClassLoader groovyClassLoader) throws GeneralException {
Script script = InvokerHelper.createScript(getScriptClassFromLocation(location, groovyClassLoader), getBinding(context));
Object result = null;
if (UtilValidate.isEmpty(methodName)) {
result = script.run();
} else {
result = script.invokeMethod(methodName, new Object[] { context });
}
return result;
}
示例12: serviceInvoker
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
private Map<String, Object> serviceInvoker(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
if (UtilValidate.isEmpty(modelService.location)) {
throw new GenericServiceException("Cannot run Groovy service with empty location");
}
Map<String, Object> params = new HashMap<String, Object>();
params.putAll(context);
Map<String, Object> gContext = new HashMap<String, Object>();
gContext.putAll(context);
gContext.put(ScriptUtil.PARAMETERS_KEY, params);
DispatchContext dctx = dispatcher.getLocalContext(localName);
gContext.put("dctx", dctx);
gContext.put("dispatcher", dctx.getDispatcher());
gContext.put("delegator", dispatcher.getDelegator());
try {
ScriptContext scriptContext = ScriptUtil.createScriptContext(gContext, protectedKeys);
ScriptHelper scriptHelper = (ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY);
if (scriptHelper != null) {
gContext.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
}
Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService)), GroovyUtil.getBinding(gContext));
Object resultObj = null;
if (UtilValidate.isEmpty(modelService.invoke)) {
resultObj = script.run();
} else {
resultObj = script.invokeMethod(modelService.invoke, EMPTY_ARGS);
}
if (resultObj == null) {
resultObj = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
}
if (resultObj != null && resultObj instanceof Map<?, ?>) {
return cast(resultObj);
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.putAll(modelService.makeValid(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE), "OUT"));
return result;
} catch (GeneralException ge) {
throw new GenericServiceException(ge);
} catch (Exception e) {
// detailMessage can be null. If it is null, the exception won't be properly returned and logged, and that will
// make spotting problems very difficult. Disabling this for now in favor of returning a proper exception.
// return ServiceUtil.returnError(e.getMessage());
throw new GenericServiceException("Error running Groovy method [" + modelService.invoke + "] in Groovy file [" + modelService.location + "]: ", e);
}
}
示例13: createScript
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
private static Script createScript(final String scriptId, String scriptText, String targetDirectory, String... imports) {
if (scriptText == null || scriptText.isEmpty()) {
return null;
}
if (targetDirectory == null) {
Script cached = cache.get(scriptText);
if (cached != null) {
return cached;
}
}
StringBuilder scriptStringBuffer = StringUtil.getFSB();
for (String oneimport : imports) {
scriptStringBuffer.append("import ");
scriptStringBuffer.append(oneimport);
scriptStringBuffer.append(";\n");
}
scriptStringBuffer.append("\n");
scriptStringBuffer.append(scriptText);
final String finalScriptText = scriptStringBuffer.toString();
StringUtil.freeFSB(scriptStringBuffer);
GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>() {
public GroovyCodeSource run() {
return new GroovyCodeSource(finalScriptText, scriptId+".groovy", GroovyShell.DEFAULT_CODE_BASE);
}
});
GroovyClassLoader loader = null;
try {
loader = getLoader(targetDirectory);
Class<?> scriptClass = loader.parseClass(gcs, false);
Script script = InvokerHelper.createScript(scriptClass, new Binding());
cache.put(scriptText, script);
return script;
} finally {
if (loader != null) {
StreamUtils.closeQuietly(loader);
}
}
}
示例14: assertScriptFile
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
protected void assertScriptFile(String fileName) throws Exception {
log.info("About to execute script: " + fileName);
Class groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName)));
Script script = InvokerHelper.createScript(groovyClass, new Binding());
script.run();
}
示例15: serviceInvoker
import org.codehaus.groovy.runtime.InvokerHelper; //导入方法依赖的package包/类
private Map<String, Object> serviceInvoker(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
if (UtilValidate.isEmpty(modelService.location)) {
throw new GenericServiceException("Cannot run Groovy service with empty location");
}
Map<String, Object> params = FastMap.newInstance();
params.putAll(context);
Map<String, Object> gContext = FastMap.newInstance();
gContext.putAll(context);
gContext.put(ScriptUtil.PARAMETERS_KEY, params);
DispatchContext dctx = dispatcher.getLocalContext(localName);
gContext.put("dctx", dctx);
gContext.put("dispatcher", dctx.getDispatcher());
gContext.put("delegator", dispatcher.getDelegator());
try {
ScriptContext scriptContext = ScriptUtil.createScriptContext(gContext, protectedKeys);
ScriptHelper scriptHelper = (ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY);
if (scriptHelper != null) {
gContext.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
}
Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService), groovyClassLoader), GroovyUtil.getBinding(gContext));
Object resultObj = null;
if (UtilValidate.isEmpty(modelService.invoke)) {
resultObj = script.run();
} else {
resultObj = script.invokeMethod(modelService.invoke, EMPTY_ARGS);
}
if (resultObj == null) {
resultObj = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
}
if (resultObj != null && resultObj instanceof Map<?, ?>) {
return cast(resultObj);
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.putAll(modelService.makeValid(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE), "OUT"));
return result;
} catch (GeneralException ge) {
throw new GenericServiceException(ge);
} catch (Exception e) {
return ServiceUtil.returnError(e.getMessage());
}
}