本文整理匯總了Java中org.ofbiz.base.util.GroovyUtil類的典型用法代碼示例。如果您正苦於以下問題:Java GroovyUtil類的具體用法?Java GroovyUtil怎麽用?Java GroovyUtil使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
GroovyUtil類屬於org.ofbiz.base.util包,在下文中一共展示了GroovyUtil類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testGetObject
import org.ofbiz.base.util.GroovyUtil; //導入依賴的package包/類
public void testGetObject() {
Long one = Long.valueOf(1);
byte[] oneBytes = UtilObject.getBytes(one);
assertNotNull("oneBytes", oneBytes);
assertEquals("one getObject", one, UtilObject.getObject(oneBytes));
boolean errorOn = Debug.isOn(Debug.ERROR);
try {
Debug.set(Debug.ERROR, false);
assertNull("parse empty array", UtilObject.getObject(new byte[0]));
// simulate a ClassNotFoundException
Object groovySerializable = GroovyUtil.eval("class foo implements java.io.Serializable { }; return new foo()", new HashMap<String, Object>());
byte[] groovySerializableBytes = UtilObject.getBytes(groovySerializable);
assertNotNull("groovySerializableBytes", groovySerializableBytes);
assertNull("groovyDeserializable", UtilObject.getObject(groovySerializableBytes));
byte[] injectorBytes = UtilObject.getBytes(new SerializationInjector(false, false));
assertNotNull("injectorBytes good", injectorBytes);
assertNotNull("injector good", UtilObject.getObject(injectorBytes));
injectorBytes = UtilObject.getBytes(new SerializationInjector(true, false));
assertNotNull("injectorBytes bad", injectorBytes);
assertNull("injector bad", UtilObject.getObject(injectorBytes));
} finally {
Debug.set(Debug.ERROR, errorOn);
}
}
示例2: run
import org.ofbiz.base.util.GroovyUtil; //導入依賴的package包/類
@Override
protected Object run(Map<String, Object> context) throws Exception {
return GroovyUtil.runScriptAtLocation(location, invokeName, context);
}
示例3: serviceInvoker
import org.ofbiz.base.util.GroovyUtil; //導入依賴的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);
}
}
示例4: serviceInvoker
import org.ofbiz.base.util.GroovyUtil; //導入依賴的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());
}
}