public static Map<String, Object> testRollbackListener( DispatchContext dctx, Map<String, ?> context) { Locale locale = (Locale) context.get("locale"); ServiceXaWrapper xar = new ServiceXaWrapper(dctx); xar.setRollbackService("testScv", context); try { xar.enlist(); } catch (XAException e) { Debug.logError(e, module); } return ServiceUtil.returnError( UtilProperties.getMessage(resource, "CommonTestRollingBack", locale)); }
public boolean runAction( String selfService, DispatchContext dctx, Map<String, Object> context, Map<String, Object> result) throws GenericServiceException { if (serviceName.equals(selfService)) { throw new GenericServiceException("Cannot invoke self on ECA."); } // pull out context parameters needed for this service. Map<String, Object> actionContext = dctx.getModelService(serviceName).makeValid(context, ModelService.IN_PARAM); // set the userLogin object in the context actionContext.put("userLogin", ServiceUtil.getUserLogin(dctx, actionContext, runAsUser)); Map<String, Object> actionResult = null; LocalDispatcher dispatcher = dctx.getDispatcher(); // if SECAs have been turned off, then just return true which has same effect as if secas ran // successfully if (dispatcher.isEcasDisabled()) { Debug.logWarning( "SECAs have been disabled on purpose and will not be run for [" + serviceName + "]", module); return true; } if (eventName.startsWith("global-")) { // XA resource ECA ServiceXaWrapper xaw = new ServiceXaWrapper(dctx); if (eventName.equals("global-rollback")) { xaw.setRollbackService( serviceName, context, "async".equals(serviceMode), persist); // using the actual context so we get updates } else if (eventName.equals("global-commit")) { xaw.setCommitService( serviceName, context, "async".equals(serviceMode), persist); // using the actual context so we get updates } else if (eventName.equals("global-commit-post-run")) { xaw.setCommitService( serviceName, context, "async".equals(serviceMode), persist); // using the actual context so we get updates } try { xaw.enlist(); } catch (XAException e) { throw new GenericServiceException("Unable to enlist ServiceXaWrapper with transaction", e); } } else { // standard ECA if (this.serviceMode.equals("sync")) { if (newTransaction) { actionResult = dispatcher.runSync(this.serviceName, actionContext, -1, true); } else { actionResult = dispatcher.runSync(this.serviceName, actionContext); } } else if (this.serviceMode.equals("async")) { dispatcher.runAsync(serviceName, actionContext, persist); } } // put the results in to the defined map if (UtilValidate.isNotEmpty(resultMapName)) { Map<String, Object> resultMap = UtilGenerics.checkMap(context.get(resultMapName)); if (resultMap == null) { resultMap = FastMap.newInstance(); } resultMap.putAll( dctx.getModelService(this.serviceName) .makeValid(actionResult, ModelService.OUT_PARAM, false, null)); context.put(resultMapName, resultMap); } // use the result to update the context fields. if (resultToContext) { context.putAll( dctx.getModelService(this.serviceName) .makeValid(actionResult, ModelService.OUT_PARAM, false, null)); } // use the result to update the result fields if (resultToResult) { Map<String, Object> normalizedActionResult = dctx.getModelService(selfService) .makeValid(actionResult, ModelService.OUT_PARAM, false, null); // don't copy over the error messages, use the combining code to do that later normalizedActionResult.remove(ModelService.ERROR_MESSAGE); normalizedActionResult.remove(ModelService.ERROR_MESSAGE_LIST); normalizedActionResult.remove(ModelService.ERROR_MESSAGE_MAP); normalizedActionResult.remove("failMessage"); result.putAll(normalizedActionResult); } // if we aren't ignoring errors check it here... boolean success = true; // don't do this if resultToResult, will already be copied over if (actionResult != null && !resultToResult) { if (!ignoreFailure) { if (ModelService.RESPOND_FAIL.equals(actionResult.get(ModelService.RESPONSE_MESSAGE))) { if (result != null) { result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_FAIL); } success = false; } } if (!ignoreError) { if (ModelService.RESPOND_ERROR.equals(actionResult.get(ModelService.RESPONSE_MESSAGE))) { if (result != null) { result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); } success = false; } } } // copy/combine error messages on error/failure (!success) or on resultToResult to combine any // error info coming out, regardless of success status if (!success || resultToResult) { String errorMessage = (String) actionResult.get(ModelService.ERROR_MESSAGE); String failMessage = (String) actionResult.get("failMessage"); List<? extends Object> errorMessageList = UtilGenerics.checkList(actionResult.get(ModelService.ERROR_MESSAGE_LIST)); Map<String, ? extends Object> errorMessageMap = UtilGenerics.checkMap(actionResult.get(ModelService.ERROR_MESSAGE_MAP)); // do something with the errorMessage if (UtilValidate.isNotEmpty(errorMessage)) { if (UtilValidate.isEmpty(result.get(ModelService.ERROR_MESSAGE))) { result.put(ModelService.ERROR_MESSAGE, errorMessage); } else { List<Object> origErrorMessageList = UtilGenerics.checkList(result.get(ModelService.ERROR_MESSAGE_LIST)); if (origErrorMessageList == null) { origErrorMessageList = new LinkedList<Object>(); result.put(ModelService.ERROR_MESSAGE_LIST, origErrorMessageList); } origErrorMessageList.add(0, errorMessage); } } // do something with the errorMessageList if (UtilValidate.isNotEmpty(errorMessageList)) { List<Object> origErrorMessageList = UtilGenerics.checkList(result.get(ModelService.ERROR_MESSAGE_LIST)); if (origErrorMessageList == null) { result.put(ModelService.ERROR_MESSAGE_LIST, errorMessageList); } else { origErrorMessageList.addAll(errorMessageList); } } // do something with the errorMessageMap if (UtilValidate.isNotEmpty(errorMessageMap)) { Map<String, Object> origErrorMessageMap = UtilGenerics.checkMap(result.get(ModelService.ERROR_MESSAGE_MAP)); if (origErrorMessageMap == null) { result.put(ModelService.ERROR_MESSAGE_MAP, errorMessageMap); } else { origErrorMessageMap.putAll(errorMessageMap); } } // do something with the fail message if (UtilValidate.isNotEmpty(failMessage)) { String origFailMessage = (String) result.get("failMessage"); if (UtilValidate.isEmpty(origFailMessage)) { result.put("failMessage", failMessage); } else { result.put("failMessage", origFailMessage + ", " + failMessage); } } } return success; }