Author: jacopoc
Date: Thu Mar 8 16:36:18 2012 New Revision: 1298461 URL: http://svn.apache.org/viewvc?rev=1298461&view=rev Log: Enhanced Groovy service engine to inject a (configurable) script base class that provides a basic DSL language to the Groovy services; the DSL is still a work in progress but it is already enough to implement several services in Groovy following a programming style very close to Minilang. Added: ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java (with props) ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy (with props) Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java ofbiz/trunk/framework/service/config/serviceengine.xml ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java?rev=1298461&r1=1298460&r2=1298461&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java Thu Mar 8 16:36:18 2012 @@ -30,6 +30,7 @@ import groovy.lang.GroovyShell; import javolution.util.FastMap; import org.codehaus.groovy.control.CompilationFailedException; +import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.runtime.InvokerHelper; import org.ofbiz.base.location.FlexibleLocation; import org.ofbiz.base.util.cache.UtilCache; @@ -101,6 +102,9 @@ public class GroovyUtil { } public static Class<?> getScriptClassFromLocation(String location) throws GeneralException { + return getScriptClassFromLocation(location, null); + } + public static Class<?> getScriptClassFromLocation(String location, GroovyClassLoader groovyClassLoader) throws GeneralException { try { Class<?> scriptClass = parsedScripts.get(location); if (scriptClass == null) { @@ -108,7 +112,11 @@ public class GroovyUtil { if (scriptUrl == null) { throw new GeneralException("Script not found at location [" + location + "]"); } - scriptClass = parseClass(scriptUrl.openStream(), location); + if (groovyClassLoader != null) { + scriptClass = parseClass(scriptUrl.openStream(), location, groovyClassLoader); + } else { + scriptClass = parseClass(scriptUrl.openStream(), location); + } if (Debug.verboseOn()) { Debug.logVerbose("Caching Groovy script at: " + location, module); } @@ -127,6 +135,9 @@ public class GroovyUtil { public static Class<?> parseClass(InputStream in, String location) throws IOException { return new GroovyClassLoader().parseClass(UtilIO.readString(in), location); } + public static Class<?> parseClass(InputStream in, String location, GroovyClassLoader groovyClassLoader) throws IOException { + return groovyClassLoader.parseClass(UtilIO.readString(in), location); + } public static Class<?> parseClass(String text) { return new GroovyClassLoader().parseClass(text); Modified: ofbiz/trunk/framework/service/config/serviceengine.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/config/serviceengine.xml?rev=1298461&r1=1298460&r2=1298461&view=diff ============================================================================== --- ofbiz/trunk/framework/service/config/serviceengine.xml (original) +++ ofbiz/trunk/framework/service/config/serviceengine.xml Thu Mar 8 16:36:18 2012 @@ -48,7 +48,9 @@ under the License. <engine name="script" class="org.ofbiz.service.engine.ScriptEngine"/> <!-- Engines that can be replaced by the generic script engine --> <engine name="bsh" class="org.ofbiz.service.engine.BeanShellEngine"/> - <engine name="groovy" class="org.ofbiz.service.engine.GroovyEngine"/> + <engine name="groovy" class="org.ofbiz.service.engine.GroovyEngine"> + <parameter name="scriptBaseClass" value="org.ofbiz.service.engine.GroovyBaseScript"/> + </engine> <engine name="jacl" class="org.ofbiz.service.engine.BSFEngine"/> <engine name="javascript" class="org.ofbiz.service.engine.ScriptEngine"/> <engine name="jpython" class="org.ofbiz.service.engine.BSFEngine"/> Added: ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java?rev=1298461&view=auto ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java (added) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java Thu Mar 8 16:36:18 2012 @@ -0,0 +1,38 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.service; + +public class ExecutionServiceException extends org.ofbiz.base.util.GeneralException { + + public ExecutionServiceException() { + super(); + } + + public ExecutionServiceException(String str) { + super(str); + } + + public ExecutionServiceException(String str, Throwable nested) { + super(str, nested); + } + + public ExecutionServiceException(Throwable nested) { + super(nested); + } +} Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/ExecutionServiceException.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy?rev=1298461&view=auto ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy (added) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy Thu Mar 8 16:36:18 2012 @@ -0,0 +1,89 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.service.engine + +import org.ofbiz.base.util.Debug; +import org.ofbiz.service.ServiceUtil +import org.ofbiz.service.GenericServiceException +import org.ofbiz.service.ExecutionServiceException + +abstract class GroovyBaseScript extends Script { + public static final String module = GroovyBaseScript.class.getName(); + + Map runService(String serviceName, Map inputMap) throws ExecutionServiceException { + if (!inputMap.userLogin) { + inputMap.userLogin = this.binding.getVariable('parameters').userLogin; + } + Map result = binding.getVariable('dispatcher').runSync(serviceName, inputMap); + if (ServiceUtil.isError(result)) { + throw new ExecutionServiceException(ServiceUtil.getErrorMessage(result)) + } + return result; + } + + Map makeValue(String entityName) throws ExecutionServiceException { + return result = binding.getVariable('delegator').makeValue(entityName); + } + + Map findOne(String entityName, Map inputMap) { + Map genericValue = binding.getVariable('delegator').findOne(entityName, inputMap, true); + // TODO: get the list of pk fields from the map and use them only + return genericValue; + } + + List findList(String entityName, Map inputMap) { + List genericValues = binding.getVariable('delegator').findByAnd(entityName, inputMap); + // TODO: get the list of entity fields from the map and use them only + return genericValues; + } + + Map success(String message) { + // TODO: implement some clever i18n mechanism based on the userLogin and locale in the binding + if (message) { + return ServiceUtil.returnSuccess(message); + } else { + return ServiceUtil.returnSuccess(); + } + } + Map failure(String message) { + // TODO: implement some clever i18n mechanism based on the userLogin and locale in the binding + if (message) { + return ServiceUtil.returnFailure(message); + } else { + return ServiceUtil.returnFailure(); + } + } + Map error(String message) { + // TODO: implement some clever i18n mechanism based on the userLogin and locale in the binding + if (message) { + return ServiceUtil.returnError(message); + } else { + return ServiceUtil.returnError(); + } + } + def logInfo(String message) { + Debug.logInfo(message, module); + } + def logWarning(String message) { + Debug.logWarning(message, module); + } + def logError(String message) { + Debug.logError(message, module); + } +} Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java?rev=1298461&r1=1298460&r2=1298461&view=diff ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java Thu Mar 8 16:36:18 2012 @@ -19,31 +19,46 @@ package org.ofbiz.service.engine; import static org.ofbiz.base.util.UtilGenerics.cast; + +import groovy.lang.GroovyClassLoader; import groovy.lang.Script; import java.util.Map; import javolution.util.FastMap; +import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.runtime.InvokerHelper; +import org.ofbiz.base.config.GenericConfigException; +import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.GroovyUtil; import org.ofbiz.base.util.UtilValidate; -import org.ofbiz.service.DispatchContext; -import org.ofbiz.service.GenericServiceException; -import org.ofbiz.service.ModelService; -import org.ofbiz.service.ServiceDispatcher; -import org.ofbiz.service.ServiceUtil; +import org.ofbiz.service.*; +import org.ofbiz.service.config.ServiceConfigUtil; /** * Groovy Script Service Engine */ public final class GroovyEngine extends GenericAsyncEngine { + public static final String module = GroovyEngine.class.getName(); protected static final Object[] EMPTY_ARGS = {}; + GroovyClassLoader groovyClassLoader; + public GroovyEngine(ServiceDispatcher dispatcher) { super(dispatcher); + try { + String scriptBaseClass = ServiceConfigUtil.getEngineParameter("groovy", "scriptBaseClass"); + if (scriptBaseClass != null) { + CompilerConfiguration conf = new CompilerConfiguration(); + conf.setScriptBaseClass(scriptBaseClass); + groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader(), conf); + } + } catch (GenericConfigException gce) { + Debug.logWarning(gce, "Error retrieving the configuration for the groovy service engine: ", module); + } } /** @@ -75,7 +90,7 @@ public final class GroovyEngine extends context.put("dispatcher", dctx.getDispatcher()); context.put("delegator", dispatcher.getDelegator()); try { - Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService)), GroovyUtil.getBinding(context)); + Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService), groovyClassLoader), GroovyUtil.getBinding(context)); Object resultObj = null; if (UtilValidate.isEmpty(modelService.invoke)) { resultObj = script.run(); @@ -87,6 +102,8 @@ public final class GroovyEngine extends } else if (context.get("result") != null && context.get("result") instanceof Map<?, ?>) { return cast(context.get("result")); } + } catch (ExecutionServiceException ese) { + return ServiceUtil.returnError(ese.getMessage()); } catch (GeneralException e) { throw new GenericServiceException(e); } |
Free forum by Nabble | Edit this page |