KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > callops > CallServiceAsynch


1 /*
2  * $Id: CallServiceAsynch.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.callops;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.minilang.SimpleMethod;
33 import org.ofbiz.minilang.method.ContextAccessor;
34 import org.ofbiz.minilang.method.MethodContext;
35 import org.ofbiz.minilang.method.MethodOperation;
36 import org.ofbiz.service.GenericServiceException;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * Calls a service using the given parameters
41  *
42  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 5462 $
45  * @since 2.0
46  */

47 public class CallServiceAsynch extends MethodOperation {
48     
49     public static final String JavaDoc module = CallServiceAsynch.class.getName();
50     
51     String JavaDoc serviceName;
52     ContextAccessor inMapAcsr;
53     String JavaDoc includeUserLoginStr;
54
55     public CallServiceAsynch(Element JavaDoc element, SimpleMethod simpleMethod) {
56         super(element, simpleMethod);
57         serviceName = element.getAttribute("service-name");
58         inMapAcsr = new ContextAccessor(element.getAttribute("in-map-name"));
59         includeUserLoginStr = element.getAttribute("include-user-login");
60     }
61
62     public boolean exec(MethodContext methodContext) {
63         String JavaDoc serviceName = methodContext.expandString(this.serviceName);
64         boolean includeUserLogin = !"false".equals(methodContext.expandString(includeUserLoginStr));
65         
66         Map JavaDoc inMap = null;
67         if (inMapAcsr.isEmpty()) {
68             inMap = new HashMap JavaDoc();
69         } else {
70             inMap = (Map JavaDoc) inMapAcsr.get(methodContext);
71             if (inMap == null) {
72                 inMap = new HashMap JavaDoc();
73                 inMapAcsr.put(methodContext, inMap);
74             }
75         }
76
77         // add UserLogin to context if expected
78
if (includeUserLogin) {
79             GenericValue userLogin = methodContext.getUserLogin();
80
81             if (userLogin != null)
82                 inMap.put("userLogin", userLogin);
83         }
84         
85         // always add Locale to context unless null
86
Locale JavaDoc locale = methodContext.getLocale();
87         if (locale != null) {
88             inMap.put("locale", locale);
89         }
90         
91         // invoke the service
92
try {
93             methodContext.getDispatcher().runAsync(serviceName, inMap);
94         } catch (GenericServiceException e) {
95             Debug.logError(e, module);
96             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem invoking the " + serviceName + " service: " + e.getMessage() + "]";
97
98             if (methodContext.getMethodType() == MethodContext.EVENT) {
99                 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
100                 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
101             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
102                 methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
103                 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
104             }
105             return false;
106         }
107
108         return true;
109     }
110
111     public String JavaDoc rawString() {
112         // TODO: something more than the empty tag
113
return "<call-service-asynch/>";
114     }
115     public String JavaDoc expandedString(MethodContext methodContext) {
116         // TODO: something more than a stub/dummy
117
return this.rawString();
118     }
119 }
120
Popular Tags