svn commit: r657701 - in /ofbiz/trunk/framework: base/src/base/org/ofbiz/base/util/ common/script/org/ofbiz/common/ common/servicedef/ common/src/org/ofbiz/common/ service/config/ service/src/org/ofbiz/service/engine/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r657701 - in /ofbiz/trunk/framework: base/src/base/org/ofbiz/base/util/ common/script/org/ofbiz/common/ common/servicedef/ common/src/org/ofbiz/common/ service/config/ service/src/org/ofbiz/service/engine/

jonesde
Author: jonesde
Date: Sun May 18 17:22:46 2008
New Revision: 657701

URL: http://svn.apache.org/viewvc?rev=657701&view=rev
Log:
Added support for groovy services, and an example of the same; cleaned up the way that bsh services work and updated the example and such to match, now returns a context as one would expect

Added:
    ofbiz/trunk/framework/common/script/org/ofbiz/common/BshServiceTest.bsh
      - copied, changed from r657609, ofbiz/trunk/framework/common/src/org/ofbiz/common/BshServiceTest.bsh
    ofbiz/trunk/framework/common/script/org/ofbiz/common/GroovyServiceTest.groovy
    ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java   (with props)
Removed:
    ofbiz/trunk/framework/common/src/org/ofbiz/common/BshServiceTest.bsh
Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java
    ofbiz/trunk/framework/common/servicedef/secas_test.xml
    ofbiz/trunk/framework/common/servicedef/services_test.xml
    ofbiz/trunk/framework/service/config/serviceengine.xml
    ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BeanShellEngine.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java?rev=657701&r1=657700&r2=657701&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java Sun May 18 17:22:46 2008
@@ -53,6 +53,10 @@
             if (script == null) {
 
                 URL scriptUrl = FlexibleLocation.resolveLocation(location);
+                if (scriptUrl == null) {
+                    throw new GeneralException("Script not found at location [" + location + "]");
+                }
+                
                 GroovyShell shell = getShell(context);
                 script = shell.parse(scriptUrl.openStream(), scriptUrl.getFile());
                 if (Debug.verboseOn()) {

Copied: ofbiz/trunk/framework/common/script/org/ofbiz/common/BshServiceTest.bsh (from r657609, ofbiz/trunk/framework/common/src/org/ofbiz/common/BshServiceTest.bsh)
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/script/org/ofbiz/common/BshServiceTest.bsh?p2=ofbiz/trunk/framework/common/script/org/ofbiz/common/BshServiceTest.bsh&p1=ofbiz/trunk/framework/common/src/org/ofbiz/common/BshServiceTest.bsh&r1=657609&r2=657701&rev=657701&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/BshServiceTest.bsh (original)
+++ ofbiz/trunk/framework/common/script/org/ofbiz/common/BshServiceTest.bsh Sun May 18 17:22:46 2008
@@ -17,9 +17,19 @@
  * under the License.
  */
 
-System.out.println("-=-=-=- TEST BSH SERVICE -=-=-=-");
-if ( context.containsKey("message") ) {
-  String message = (String) context.get("message");
-  System.out.println("----- Message is: " + message + " -----");
+import org.ofbiz.service.ServiceUtil;
+import org.ofbiz.base.util.Debug;
+
+Debug.logInfo("-=-=-=- TEST BSH SERVICE -=-=-=-", "");
+results = ServiceUtil.returnSuccess();
+if (context.containsKey("message")) {
+    message = context.get("message");
+    results.put("result", message);
+    results.put("successMessage", "Got message [" + message + "] and finished fine!");
+    Debug.logInfo("----- Message is: " + message + " -----", "");
+} else {
+    results.put("successMessage", "Got no message but finished fine anyway");
+    results.put("result", "[no message received]");
 }
-context.put("result","Script finished");
+
+return results;

Added: ofbiz/trunk/framework/common/script/org/ofbiz/common/GroovyServiceTest.groovy
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/script/org/ofbiz/common/GroovyServiceTest.groovy?rev=657701&view=auto
==============================================================================
--- ofbiz/trunk/framework/common/script/org/ofbiz/common/GroovyServiceTest.groovy (added)
+++ ofbiz/trunk/framework/common/script/org/ofbiz/common/GroovyServiceTest.groovy Sun May 18 17:22:46 2008
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+import org.ofbiz.service.ServiceUtil;
+import org.ofbiz.base.util.Debug;
+
+Debug.logInfo("-=-=-=- TEST BSH SERVICE -=-=-=-", "");
+result = ServiceUtil.returnSuccess();
+if (context.containsKey("message")) {
+    message = context.get("message");
+    result.put("successMessage", "Got message [" + message + "] and finished fine");
+    result.put("result", message);
+    Debug.logInfo("----- Message is: " + message + " -----", "");
+} else {
+    result.put("successMessage", "Got no message but finished fine anyway");
+    result.put("result", "[no message received]");
+}
+
+return result;

Modified: ofbiz/trunk/framework/common/servicedef/secas_test.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/servicedef/secas_test.xml?rev=657701&r1=657700&r2=657701&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/servicedef/secas_test.xml (original)
+++ ofbiz/trunk/framework/common/servicedef/secas_test.xml Sun May 18 17:22:46 2008
@@ -28,7 +28,7 @@
         <condition field-name="message" operator="equals" value="12345"/>
         <action service="testBsh" mode="sync"/>
     </eca>
-    <eca service="testBsh" event="commit">
+    <eca service="testScv" event="commit">
         <condition-service service-name="conditionReturnTrue"/>
         <action service="blockingTestScv" mode="sync"/>
     </eca>

Modified: ofbiz/trunk/framework/common/servicedef/services_test.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/servicedef/services_test.xml?rev=657701&r1=657700&r2=657701&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/servicedef/services_test.xml (original)
+++ ofbiz/trunk/framework/common/servicedef/services_test.xml Sun May 18 17:22:46 2008
@@ -90,21 +90,24 @@
         <attribute name="result" type="String" mode="OUT"/>
     </service>
 
-    <service name="testBsh" engine="bsh"
-            location="org/ofbiz/common/BshServiceTest.bsh" invoke="">
+    <service name="testBsh" engine="bsh" location="org/ofbiz/common/BshServiceTest.bsh" invoke="">
         <description>Test BeanShell Script Service</description>
         <attribute name="message" type="String" mode="IN" optional="true"/>
         <attribute name="result" type="String" mode="OUT"/>
     </service>
 
-    <service name="testJMSQueue" engine="jms"
-            location="serviceMessenger" invoke="testScv">
+    <service name="testGroovy" engine="groovy" location="org/ofbiz/common/GroovyServiceTest.groovy" invoke="">
+        <description>Test Groovy Script Service</description>
+        <attribute name="message" type="String" mode="IN" optional="true"/>
+        <attribute name="result" type="String" mode="OUT"/>
+    </service>
+    
+    <service name="testJMSQueue" engine="jms" location="serviceMessenger" invoke="testScv">
         <description>Test JMS Queue service</description>
         <attribute name="message" type="String" mode="IN"/>
     </service>
 
-    <service name="testJMSTopic" engine="jms"
-            location="serviceMessenger" invoke="testScv">
+    <service name="testJMSTopic" engine="jms" location="serviceMessenger" invoke="testScv">
         <description>Test JMS Topic service</description>
         <attribute name="message" type="String" mode="IN"/>
     </service>

Modified: ofbiz/trunk/framework/service/config/serviceengine.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/config/serviceengine.xml?rev=657701&r1=657700&r2=657701&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/config/serviceengine.xml (original)
+++ ofbiz/trunk/framework/service/config/serviceengine.xml Sun May 18 17:22:46 2008
@@ -41,6 +41,7 @@
 
         <!-- Service Engine Configuration -->
         <engine name="bsh" class="org.ofbiz.service.engine.BeanShellEngine"/>
+        <engine name="groovy" class="org.ofbiz.service.engine.GroovyEngine"/>
         <engine name="group" class="org.ofbiz.service.group.ServiceGroupEngine"/>
         <engine name="http" class="org.ofbiz.service.engine.HttpEngine"/>
         <engine name="interface" class="org.ofbiz.service.engine.InterfaceEngine"/>

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BeanShellEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BeanShellEngine.java?rev=657701&r1=657700&r2=657701&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BeanShellEngine.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BeanShellEngine.java Sun May 18 17:22:46 2008
@@ -21,15 +21,20 @@
 import java.net.URL;
 import java.util.Map;
 
+import org.ofbiz.base.util.BshUtil;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.GeneralException;
 import org.ofbiz.base.util.HttpClient;
 import org.ofbiz.base.util.HttpClientException;
 import org.ofbiz.base.util.UtilGenerics;
 import org.ofbiz.base.util.UtilURL;
+import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.cache.UtilCache;
 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 bsh.EvalError;
 import bsh.Interpreter;
@@ -61,70 +66,26 @@
 
     // Invoke the BeanShell Script.
     private Map<String, Object> serviceInvoker(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
-        if (modelService.location == null || modelService.invoke == null)
-            throw new GenericServiceException("Cannot locate service to invoke");
-
-        // get the DispatchContext from the localName and ServiceDispatcher
-        DispatchContext dctx = dispatcher.getLocalContext(localName);
-        
-        // get the classloader to use
-        ClassLoader cl = null;
-
-        if (dctx == null) {
-            cl = this.getClass().getClassLoader();
-        } else {
-            cl = dctx.getClassLoader();
+        if (UtilValidate.isEmpty(modelService.location)) {
+            throw new GenericServiceException("Cannot run Groovy service with empty location");
         }
 
         String location = this.getLocation(modelService);
-
-        // source the script into a string
-        String script = scriptCache.get(localName + "_" + location);
-
-        if (script == null) {
-            synchronized (this) {
-                script = scriptCache.get(localName + "_" + location);
-                if (script == null) {
-                    URL scriptUrl = UtilURL.fromResource(location, cl);
-
-                    if (scriptUrl != null) {
-                        try {
-                            HttpClient http = new HttpClient(scriptUrl);
-                            script = http.get();
-                        } catch (HttpClientException e) {
-                            throw new GenericServiceException("Cannot read script from resource", e);
-                        }
-                    } else {
-                        throw new GenericServiceException("Cannot read script, resource [" + location + "] not found");
-                    }
-                    if (script == null || script.length() < 2) {
-                        throw new GenericServiceException("Null or empty script");
-                    }
-                    scriptCache.put(localName + "_" + location, script);
-                }
-            }
-        }
-
-        Interpreter bsh = new Interpreter();
-
-        Map<String, Object> result = null;
-
+        
         try {
-            bsh.set("dctx", dctx); // set the dispatch context
-            bsh.set("context", context); // set the parameter context used for both IN and OUT
-            bsh.eval(script);
-            Object bshResult = bsh.get("result");
-
-            if ((bshResult != null) && (bshResult instanceof Map)) {
-                Map<String, Object> bshMapResult = UtilGenerics.checkMap(bshResult);
-                context.putAll(bshMapResult);
+            Object resultObj = BshUtil.runBshAtLocation(location, context);
+            
+            if (resultObj != null && resultObj instanceof Map) {
+                Debug.logInfo("Got result Map from script return: " + resultObj, module);
+                return (Map<String, Object>) resultObj;
+            } else if (context.get("result") != null && context.get("result") instanceof Map) {
+                Debug.logInfo("Got result Map from context: " + resultObj, module);
+                return (Map<String, Object>) context.get("result");
             }
-            result = modelService.makeValid(context, ModelService.OUT_PARAM);
-        } catch (EvalError e) {
-            throw new GenericServiceException("BeanShell script threw an exception", e);
+        } catch (GeneralException e) {
+            throw new GenericServiceException(e);
         }
-        return result;
-    }
 
+        return ServiceUtil.returnSuccess();
+    }
 }
-

Added: 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=657701&view=auto
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java (added)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java Sun May 18 17:22:46 2008
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * 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 java.util.Map;
+
+import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.GroovyUtil;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.service.GenericServiceException;
+import org.ofbiz.service.ModelService;
+import org.ofbiz.service.ServiceDispatcher;
+import org.ofbiz.service.ServiceUtil;
+
+/**
+ * Groovy Script Service Engine
+ */
+public final class GroovyEngine extends GenericAsyncEngine {
+
+    public GroovyEngine(ServiceDispatcher dispatcher) {
+        super(dispatcher);
+    }
+    
+    /**
+     * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
+     */
+    public void runSyncIgnore(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
+        runSync(localName, modelService, context);
+    }
+  
+    /**
+     * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
+     */
+    public Map<String, Object> runSync(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
+        return serviceInvoker(localName, modelService, context);
+    }
+
+    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");
+        }
+
+        String location = this.getLocation(modelService);
+        
+        try {
+            Object resultObj = GroovyUtil.runScriptAtLocation(location, context);
+            
+            if (resultObj != null && resultObj instanceof Map) {
+                return (Map<String, Object>) resultObj;
+            } else if (context.get("result") != null && context.get("result") instanceof Map) {
+                return (Map<String, Object>) context.get("result");
+            }
+        } catch (GeneralException e) {
+            throw new GenericServiceException(e);
+        }
+
+        return ServiceUtil.returnSuccess();
+    }
+}

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain