Applied patch from Andrew Sykes to run services synchronously in webtools (OFBIZ-677)

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@507567 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Si Chen
2007-02-14 15:12:32 +00:00
parent 8dea3ca24d
commit e2facb2fac
11 changed files with 308 additions and 6 deletions
@@ -39,8 +39,9 @@ coreEvents.invalid_format_time=Invalid format for SERVICE_TIME
coreEvents.service_time_already_passed=SERVICE_TIME has already passed
coreEvents.invalid_format_interval=Invalid format for SERVICE_INTERVAL
coreEvents.invalid_format_count=Invalid format for SERVICE_COUNT
coreEvents.invalid_format_frequency=Invalid format for SERIVCE_FREQUENCY
coreEvents.invalid_format_frequency=Invalid format for SERVICE_FREQUENCY
coreEvents.service_dispatcher_exception=Service dispatcher threw an exception
coreEvents.no_fields_in_session=No previous fields found in session
coreEvents.service_scheduled=Service has been scheduled
coreEvents.must_specify_service_name=You must specify a 'serviceName', and optionally a 'mode' (sync or async, defaults to sync)
coreEvents.service_eventhandler_exception=ServiceEventHandler threw an exception
@@ -23,13 +23,20 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javolution.util.FastMap;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilHttp;
@@ -37,6 +44,7 @@ import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.webapp.control.RequestHandler;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.security.Security;
import org.ofbiz.service.DispatchContext;
@@ -397,20 +405,107 @@ public class CoreEvents {
request.setAttribute("_ERROR_MESSAGE_", errorBuf.toString());
return "error";
}
Map syncServiceResult = null;
// schedule service
try {
dispatcher.schedule(jobName, poolName, serviceName, serviceContext, startTime, frequency, interval, count, endTime, maxRetry);
if(null!=request.getParameter("_RUN_SYNC_") && request.getParameter("_RUN_SYNC_").equals("Y")){
syncServiceResult = dispatcher.runSync(serviceName, serviceContext);
}else{
dispatcher.schedule(jobName, poolName, serviceName, serviceContext, startTime, frequency, interval, count, endTime, maxRetry);
}
} catch (GenericServiceException e) {
String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_dispatcher_exception", locale);
request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + e.getMessage());
return "error";
}
String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_scheduled", locale);
request.setAttribute("_EVENT_MESSAGE_", errMsg);
if(null!=syncServiceResult){
request.getSession().setAttribute("_RUN_SYNC_RESULT_", syncServiceResult);
return "sync_success";
}
return "success";
}
public static String saveServiceResultsToSession(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
Locale locale = UtilHttp.getLocale(request);
Map syncServiceResult = (Map)session.getAttribute("_RUN_SYNC_RESULT_");
if(null==syncServiceResult){
String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.no_fields_in_session", locale);
request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);
return "error";
}
if(null!=request.getParameter("_CLEAR_PREVIOUS_PARAMS_") && request.getParameter("_CLEAR_PREVIOUS_PARAMS_").equalsIgnoreCase("on"))
session.removeAttribute("_SAVED_SYNC_RESULT_");
Map serviceFieldsToSave = request.getParameterMap();
Map savedFields = FastMap.newInstance();
Set keys = serviceFieldsToSave.keySet();
Iterator keysItr = keys.iterator();
while(keysItr.hasNext()){
String key = (String)keysItr.next();
if(null!=serviceFieldsToSave.get(key) && request.getParameter(key).equalsIgnoreCase("on") && !key.equals("_CLEAR_PREVIOUS_PARAMS_")){
String[] servicePath = key.split("\\|\\|");
String partialKey = servicePath[servicePath.length-1];
savedFields.put(partialKey, getObjectFromServicePath(key ,syncServiceResult));
}
}
if(null!=session.getAttribute("_SAVED_SYNC_RESULT_")){
Map savedSyncResult = (Map)session.getAttribute("_SAVED_SYNC_RESULT_");
savedSyncResult.putAll(savedFields);
savedFields = savedSyncResult;
}
session.setAttribute("_SAVED_SYNC_RESULT_", savedFields);
return "success";
}
//Tries to return a map, if Object is one of HashMap, GenericEntity, List
public static Object getObjectFromServicePath(String servicePath, Map serviceResult){
String[] sp = servicePath.split("\\|\\|");
Object servicePathObject = null;
Map servicePathMap = null;
for(int i=0;i<sp.length;i++){
String servicePathEntry = sp[i];
if(null==servicePathMap){
servicePathObject = serviceResult.get(servicePathEntry);
}else{
servicePathObject = servicePathMap.get(servicePathEntry);
}
servicePathMap = null;
if(servicePathObject instanceof HashMap){
servicePathMap = (HashMap)servicePathObject;
}else if(servicePathObject instanceof GenericEntity){
GenericEntity servicePathEntity = (GenericEntity)servicePathObject;
Set servicePathEntitySet = servicePathEntity.keySet();
Iterator spesItr = servicePathEntitySet.iterator();
servicePathMap = FastMap.newInstance();
while(spesItr.hasNext()){
String spesKey = (String)spesItr.next();
servicePathMap.put(spesKey, servicePathEntity.get(spesKey));
}
}else if(servicePathObject instanceof Collection){
Collection servicePathColl = (Collection)servicePathObject;
Iterator splItr = servicePathColl.iterator();
int count=0;
servicePathMap = FastMap.newInstance();
while(splItr.hasNext()){
servicePathMap.put("_"+count+"_", splItr.next());
count++;
}
}
}
if(null==servicePathMap){
return servicePathObject;
}else{
return servicePathMap;
}
}
public static ServiceEventHandler seh = new ServiceEventHandler();
@@ -37,6 +37,7 @@ PageTitleLogConfiguration=Debugging Levels
PageTitleLogView=Log View
PageTitleJobList=Job List
PageTitleScheduleJob=Schedule Job
PageTitleRunService=Run Service
PageTitleServiceList=Service List
PageTitleThreadList=Thread List
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map;
import java.sql.Timestamp;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.service.ServiceDispatcher;
@@ -34,6 +35,11 @@ import org.ofbiz.service.RunningService;
import org.ofbiz.service.engine.GenericEngine;
import org.ofbiz.service.config.ServiceConfigUtil;
Map savedSyncResult = null;
if( null!=session.getAttribute("_SAVED_SYNC_RESULT_") ){
savedSyncResult = (Map)session.getAttribute("_SAVED_SYNC_RESULT_");
}
String serviceName = request.getParameter("SERVICE_NAME");
context.put("POOL_NAME", ServiceConfigUtil.getSendPool());
@@ -63,7 +69,13 @@ if (UtilValidate.isNotEmpty(serviceName)) {
if (par.internal) {
continue;
}
serviceParameters.add(UtilMisc.toMap("name", par.name, "type", par.type, "optional", (par.optional? "Y": "N")));
Map serviceParam = null;
if(null!=savedSyncResult && null!=savedSyncResult.get(par.name)){
serviceParam = UtilMisc.toMap("name", par.name, "type", par.type, "optional", (par.optional? "Y": "N"), "value", savedSyncResult.get(par.name).toString());
}else{
serviceParam = UtilMisc.toMap("name", par.name, "type", par.type, "optional", (par.optional? "Y": "N"));
}
serviceParameters.add(serviceParam);
}
}
}
@@ -0,0 +1,65 @@
/*
* 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 java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.sql.Timestamp;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.Debug;
import org.ofbiz.webapp.event.CoreEvents;
import javolution.util.FastList;
import javolution.util.FastMap;
if(null!=session.getAttribute("_RUN_SYNC_RESULT_")){
List serviceResultList = FastList.newInstance();
Map serviceResult = (Map)session.getAttribute("_RUN_SYNC_RESULT_");
if(request.getParameter("servicePath")!=null && !request.getParameter("servicePath").equals("")){
String servicePath = request.getParameter("servicePath");
newServiceResult = CoreEvents.getObjectFromServicePath(servicePath, serviceResult);
if(null!=newServiceResult && newServiceResult instanceof Map)
serviceResult = newServiceResult;
context.put("servicePath", servicePath);
}
Set serviceResultSet = serviceResult.keySet();
Iterator serviceResultItr = serviceResultSet.iterator();
while(serviceResultItr.hasNext()){
String key = serviceResultItr.next();
Map valueMap = UtilMisc.toMap("key", key, "value", (null==serviceResult.get(key))?"null":serviceResult.get(key).toString());
if(serviceResult.get(key) instanceof GenericEntity ||
serviceResult.get(key) instanceof HashMap ||
serviceResult.get(key) instanceof Collection){
valueMap.put("hasChild", "Y");
}else{
valueMap.put("hasChild", "N");
}
serviceResultList.add(valueMap);
}
context.put("serviceResultList", serviceResultList);
}
@@ -326,16 +326,35 @@ under the License.
<security https="true" auth="true"/>
<response name="success" type="view" value="scheduleJob"/>
</request-map>
<request-map uri="runService">
<security https="true" auth="true"/>
<response name="success" type="view" value="runService"/>
</request-map>
<request-map uri="setServiceParameters">
<security https="true" auth="true"/>
<response name="success" type="view" value="setServiceParameters"/>
</request-map>
<request-map uri="setSyncServiceParameters">
<security https="true" auth="true"/>
<response name="success" type="view" value="setSyncServiceParameters"/>
</request-map>
<request-map uri="scheduleService">
<security https="true" auth="true"/>
<event type="java" path="org.ofbiz.webapp.event.CoreEvents" invoke="scheduleService"/>
<response name="success" type="view" value="jobList"/>
<response name="sync_success" type="view" value="serviceResult"/>
<response name="error" type="view" value="scheduleJob"/>
</request-map>
<request-map uri="serviceResult">
<security https="true" auth="true"/>
<response name="success" type="view" value="serviceResult"/>
</request-map>
<request-map uri="saveServiceResultsToSession">
<security https="true" auth="true"/>
<event type="java" path="org.ofbiz.webapp.event.CoreEvents" invoke="saveServiceResultsToSession"/>
<response name="success" type="view" value="runService"/>
<response name="error" type="view" value="error"/>
</request-map>
<!-- Available services requests -->
<request-map uri="availableServices">
@@ -540,9 +559,12 @@ under the License.
<view-map name="serviceList" type="screen" page="component://webtools/widget/ServiceScreens.xml#ServiceList"/>
<view-map name="jobList" type="screen" page="component://webtools/widget/ServiceScreens.xml#JobList"/>
<view-map name="serviceResult" type="screen" page="component://webtools/widget/ServiceScreens.xml#ServiceResult"/>
<view-map name="threadList" type="screen" page="component://webtools/widget/ServiceScreens.xml#ThreadList"/>
<view-map name="scheduleJob" type="screen" page="component://webtools/widget/ServiceScreens.xml#ScheduleJob"/>
<view-map name="runService" type="screen" page="component://webtools/widget/ServiceScreens.xml#RunService"/>
<view-map name="setServiceParameters" type="screen" page="component://webtools/widget/ServiceScreens.xml#setServiceParameters"/>
<view-map name="setSyncServiceParameters" type="screen" page="component://webtools/widget/ServiceScreens.xml#setSyncServiceParameters"/>
<view-map name="availableServices" type="screen" page="component://webtools/widget/AvailableServicesScreens.xml#AvailableServicesList"/>
<view-map name="serviceEcaDetail" type="screen" page="component://webtools/widget/AvailableServicesScreens.xml#ServiceEcaDetail"/>
@@ -89,6 +89,7 @@ under the License.
<ul>
<li><a href="<@ofbizUrl>/availableServices</@ofbizUrl>" class="linktext">${uiLabelMap.WebtoolsServiceReference}</a>
<li><a href="<@ofbizUrl>/scheduleJob</@ofbizUrl>" class="linktext">${uiLabelMap.PageTitleScheduleJob}</a>
<li><a href="<@ofbizUrl>/runService</@ofbizUrl>" class="linktext">${uiLabelMap.PageTitleRunService}</a>
<li><a href="<@ofbizUrl>/jobList</@ofbizUrl>" class="linktext">${uiLabelMap.PageTitleJobList}</a>
<li><a href="<@ofbizUrl>/threadList</@ofbizUrl>" class="linktext">${uiLabelMap.PageTitleThreadList}</a>
<li><a href="<@ofbizUrl>/serviceList</@ofbizUrl>" class="linktext">${uiLabelMap.WebtoolsServiceLog}</a>
@@ -44,4 +44,11 @@ under the License.
<field name="SERVICE_MAXRETRY" title="${uiLabelMap.WebtoolsMaxRetry}" tooltip="${uiLabelMap.WebtoolsMessage10}"><text/></field>
<field name="submitButton" title="${uiLabelMap.CommonSubmit}" widget-style="smallSubmit"><submit button-type="button"/></field>
</form>
<form name="runService" type="single" target="setSyncServiceParameters" title="" default-title-style="tableheadtext" default-widget-style="inputBox" default-tooltip-style="tabletext">
<field name="SERVICE_NAME" title="${uiLabelMap.WebtoolsService}"><text/></field>
<field name="POOL_NAME" title="${uiLabelMap.WebtoolsPool}"><text/></field>
<field name="_RUN_SYNC_"><hidden value="Y"/></field>
<field name="submitButton" title="${uiLabelMap.CommonSubmit}" widget-style="smallSubmit"><submit button-type="button"/></field>
</form>
</forms>
@@ -0,0 +1,45 @@
<#--
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.
-->
<form method="post" action="<@ofbizUrl>saveServiceResultsToSession</@ofbizUrl>"
<table cellpadding="2" cellspacing="0" border="1" width="100%">
<tr>
<td><div class="tableheadtext">parameter</div></td>
<td><div class="tableheadtext">value</div></td>
<td><div class="tableheadtext">save value?</div></td>
</tr>
<#list serviceResultList as srl>
<tr>
<#if srl.hasChild=="Y">
<td><div class="tabletext"><a href="<@ofbizUrl>/serviceResult?servicePath=</@ofbizUrl><#if parameters.servicePath?exists>${parameters.servicePath}||</#if>${srl.key?if_exists}">${srl.key?if_exists}</a></div></td>
<#else>
<td><div class="tabletext">${srl.key?if_exists}</div></td>
</#if>
<td><div class="tabletext">${srl.value?if_exists}</div></td>
<td><div class="tabletext"><input type="checkbox" name="<#if parameters.servicePath?exists>${parameters.servicePath}||</#if>${srl.key?if_exists}" /></div></td>
</tr>
</#list>
<tr>
<td><div class="tabletext">&nbsp</div></td>
<td><div class="tabletext">Clear previous params? <input type="checkbox" name="_CLEAR_PREVIOUS_PARAMS_" /></div></td>
<td><div class="tabletext"><input type="submit" value="submit" /></div></td>
</tr>
</table>
@@ -28,7 +28,7 @@ under the License.
<tr>
<td align="right"><div class="tabletext">${serviceParameter.name} (${serviceParameter.type})</div></td>
<td>
<input type="text" class="inputBox" size="20" name="${serviceParameter.name}"/>
<input type="text" class="inputBox" size="20" name="${serviceParameter.name}" value="${serviceParameter.value?if_exists}" />
<span class="tabletext"><#if serviceParameter.optional == "N">(required)<#else>(optional)</#if></span>
</td>
</tr>
@@ -129,6 +129,21 @@ under the License.
</widgets>
</section>
</screen>
<screen name="RunService">
<section>
<actions>
<set field="titleProperty" value="PageTitleRunService"/>
<script location="component://webtools/webapp/webtools/WEB-INF/actions/service/scheduleJob.bsh"/>
</actions>
<widgets>
<decorator-screen name="CommonWebtoolsDecorator" location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<include-form name="runService" location="component://webtools/webapp/webtools/service/ServiceForms.xml"/>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
<screen name="setServiceParameters">
<section>
<actions>
@@ -149,5 +164,43 @@ under the License.
</widgets>
</section>
</screen>
<screen name="setSyncServiceParameters">
<section>
<actions>
<set field="titleProperty" value="PageTitleScheduleJob"/>
<set field="headerItem" value="services"/>
<set field="tabButtonItem" value="ScheduleJob"/>
<script location="component://webtools/webapp/webtools/WEB-INF/actions/service/scheduleJob.bsh"/>
</actions>
<widgets>
<decorator-screen name="CommonWebtoolsDecorator" location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<platform-specific>
<html><html-template location="component://webtools/webapp/webtools/service/setServiceParameter.ftl"/></html>
</platform-specific>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
<screen name="ServiceResult">
<section>
<actions>
<set field="titleProperty" value="PageTitleScheduleJob"/>
<set field="headerItem" value="services"/>
<set field="tabButtonItem" value="ScheduleJob"/>
<script location="component://webtools/webapp/webtools/WEB-INF/actions/service/serviceResult.bsh"/>
</actions>
<widgets>
<decorator-screen name="CommonWebtoolsDecorator" location="${parameters.mainDecoratorLocation}">
<decorator-section name="body">
<platform-specific>
<html><html-template location="component://webtools/webapp/webtools/service/serviceResult.ftl"/></html>
</platform-specific>
</decorator-section>
</decorator-screen>
</widgets>
</section>
</screen>
</screens>