001: /*
002: * $Id: CallServiceAsynch.java,v 1.1 2003/08/17 06:06:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.callops;
025:
026: import java.util.HashMap;
027: import java.util.Locale;
028: import java.util.Map;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.entity.GenericValue;
032: import org.ofbiz.minilang.SimpleMethod;
033: import org.ofbiz.minilang.method.ContextAccessor;
034: import org.ofbiz.minilang.method.MethodContext;
035: import org.ofbiz.minilang.method.MethodOperation;
036: import org.ofbiz.service.GenericServiceException;
037: import org.w3c.dom.Element;
038:
039: /**
040: * Calls a service using the given parameters
041: *
042: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
043: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
044: * @version $Revision: 1.1 $
045: * @since 2.0
046: */
047: public class CallServiceAsynch extends MethodOperation {
048:
049: public static final String module = CallServiceAsynch.class
050: .getName();
051:
052: String serviceName;
053: ContextAccessor inMapAcsr;
054: String includeUserLoginStr;
055:
056: public CallServiceAsynch(Element element, SimpleMethod simpleMethod) {
057: super (element, simpleMethod);
058: serviceName = element.getAttribute("service-name");
059: inMapAcsr = new ContextAccessor(element
060: .getAttribute("in-map-name"));
061: includeUserLoginStr = element
062: .getAttribute("include-user-login");
063: }
064:
065: public boolean exec(MethodContext methodContext) {
066: String serviceName = methodContext
067: .expandString(this .serviceName);
068: boolean includeUserLogin = !"false".equals(methodContext
069: .expandString(includeUserLoginStr));
070:
071: Map inMap = null;
072: if (inMapAcsr.isEmpty()) {
073: inMap = new HashMap();
074: } else {
075: inMap = (Map) inMapAcsr.get(methodContext);
076: if (inMap == null) {
077: inMap = new HashMap();
078: inMapAcsr.put(methodContext, inMap);
079: }
080: }
081:
082: // add UserLogin to context if expected
083: if (includeUserLogin) {
084: GenericValue userLogin = methodContext.getUserLogin();
085:
086: if (userLogin != null)
087: inMap.put("userLogin", userLogin);
088: }
089:
090: // always add Locale to context unless null
091: Locale locale = methodContext.getLocale();
092: if (locale != null) {
093: inMap.put("locale", locale);
094: }
095:
096: // invoke the service
097: try {
098: methodContext.getDispatcher().runAsync(serviceName, inMap);
099: } catch (GenericServiceException e) {
100: Debug.logError(e, module);
101: String errMsg = "ERROR: Could not complete the "
102: + simpleMethod.getShortDescription()
103: + " process [problem invoking the " + serviceName
104: + " service: " + e.getMessage() + "]";
105:
106: if (methodContext.getMethodType() == MethodContext.EVENT) {
107: methodContext.putEnv(simpleMethod
108: .getEventErrorMessageName(), errMsg);
109: methodContext.putEnv(simpleMethod
110: .getEventResponseCodeName(), simpleMethod
111: .getDefaultErrorCode());
112: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
113: methodContext.putEnv(simpleMethod
114: .getServiceErrorMessageName(), errMsg);
115: methodContext.putEnv(simpleMethod
116: .getServiceResponseMessageName(), simpleMethod
117: .getDefaultErrorCode());
118: }
119: return false;
120: }
121:
122: return true;
123: }
124: }
|