01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.service.mail;
19:
20: import java.util.Map;
21:
22: import org.ofbiz.service.GenericServiceException;
23: import org.ofbiz.service.LocalDispatcher;
24: import org.ofbiz.service.ServiceUtil;
25: import org.ofbiz.base.util.UtilMisc;
26: import org.ofbiz.base.util.Debug;
27: import org.ofbiz.base.util.UtilValidate;
28: import org.ofbiz.entity.GenericValue;
29:
30: import org.w3c.dom.Element;
31:
32: public class ServiceMcaAction implements java.io.Serializable {
33:
34: public static final String module = ServiceMcaAction.class
35: .getName();
36:
37: protected String serviceName = null;
38: protected String serviceMode = null;
39: protected String runAsUser = null;
40: protected boolean persist = false;
41:
42: protected ServiceMcaAction() {
43: }
44:
45: protected ServiceMcaAction(Element action) {
46: this .serviceName = action.getAttribute("service");
47: this .serviceMode = action.getAttribute("mode");
48: this .runAsUser = action.getAttribute("run-as-user");
49: // support the old, inconsistent attribute name
50: if (UtilValidate.isEmail(this .runAsUser))
51: this .runAsUser = action.getAttribute("runAsUser");
52: this .persist = "true".equals(action.getAttribute("persist"));
53: }
54:
55: public boolean runAction(LocalDispatcher dispatcher,
56: MimeMessageWrapper messageWrapper, GenericValue userLogin)
57: throws GenericServiceException {
58: Map serviceContext = UtilMisc.toMap("messageWrapper",
59: messageWrapper, "userLogin", userLogin);
60: serviceContext.put("userLogin", ServiceUtil.getUserLogin(
61: dispatcher.getDispatchContext(), serviceContext,
62: runAsUser));
63:
64: if (serviceMode.equals("sync")) {
65: Map result = dispatcher
66: .runSync(serviceName, serviceContext);
67: if (ServiceUtil.isError(result)) {
68: Debug.logError(ServiceUtil.getErrorMessage(result),
69: module);
70: return false;
71: } else {
72: return true;
73: }
74: } else if (serviceMode.equals("async")) {
75: dispatcher.runAsync(serviceName, serviceContext, persist);
76: return true;
77: } else {
78: Debug.logError("Invalid service mode [" + serviceMode
79: + "] unable to invoke MCA action (" + serviceName
80: + ").", module);
81: return false;
82: }
83: }
84: }
|