001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.shark.requester;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.enhydra.shark.api.SharkTransaction;
024: import org.enhydra.shark.api.client.wfbase.BaseException;
025: import org.enhydra.shark.api.client.wfmodel.InvalidPerformer;
026: import org.enhydra.shark.api.client.wfmodel.WfEventAudit;
027:
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.entity.GenericValue;
030: import org.ofbiz.service.DispatchContext;
031: import org.ofbiz.service.GenericServiceException;
032: import org.ofbiz.service.LocalDispatcher;
033: import org.ofbiz.service.ModelService;
034: import org.ofbiz.shark.container.SharkContainer;
035:
036: public class ServiceRequester extends AbstractRequester {
037:
038: /**
039: *
040: */
041: private static final long serialVersionUID = 1L;
042: public static final String module = ServiceRequester.class
043: .getName();
044: public static final int ASYNC = 0;
045: public static final int SYNC = 1;
046:
047: protected Map initialContext = new HashMap();
048: protected String serviceName = null;
049: protected String eventType = null;
050: protected int serviceMode = 1;
051:
052: // new requester
053: public ServiceRequester(GenericValue userLogin, String eventType) {
054: super (userLogin);
055: this .setEventType(eventType);
056: }
057:
058: public ServiceRequester(GenericValue userLogin) {
059: super (userLogin);
060: }
061:
062: // -------------------
063: // WfRequester methods
064: // -------------------
065:
066: public void receive_event(WfEventAudit event) throws BaseException,
067: InvalidPerformer {
068: Debug
069: .logInfo(
070: "Call : ServiceRequester.receive_event(WfEventAudit event)",
071: module);
072: if (this .getEventType() == null
073: || this .getEventType().equals(event.event_type())) {
074: try {
075: this .run(event);
076: } catch (GenericServiceException e) {
077: Debug.logError(e, module);
078: throw new BaseException(e);
079: }
080: }
081: }
082:
083: public void receive_event(SharkTransaction trans, WfEventAudit event)
084: throws BaseException, InvalidPerformer {
085: Debug
086: .logInfo(
087: "Call : ServiceRequester.receive_event (SharkTransaction trans, WfEventAudit event)",
088: module);
089: receive_event(event);
090: }
091:
092: // -------------
093: // local methods
094: // -------------
095:
096: public void setEventType(String eventType) {
097: this .eventType = eventType;
098: }
099:
100: public String getEventType() {
101: return this .eventType;
102: }
103:
104: public void setService(String serviceName, int serviceMode) {
105: this .serviceName = serviceName;
106: this .serviceMode = serviceMode;
107: }
108:
109: public void setService(String serviceName) {
110: this .setService(serviceName, ServiceRequester.ASYNC);
111: }
112:
113: public String getServiceName() {
114: return this .serviceName;
115: }
116:
117: public int getServiceMode() {
118: return this .serviceMode;
119: }
120:
121: public void setInitalContextValues(Map initialContext) {
122: this .initialContext = new HashMap(initialContext);
123: }
124:
125: private void run(WfEventAudit event) throws GenericServiceException {
126: // get the dispatcher
127: LocalDispatcher dispatcher = SharkContainer.getDispatcher();
128: if (dispatcher == null) {
129: throw new GenericServiceException(
130: "Cannot run service with null dispatcher");
131: }
132:
133: // get the service context
134: Map serviceContext = makeServiceContext(event, dispatcher);
135:
136: // invoke the service
137: String serviceName = this .getServiceName();
138: if (serviceName != null) {
139: int mode = this .getServiceMode();
140: if (mode == ServiceRequester.SYNC) {
141: dispatcher.runSyncIgnore(serviceName, serviceContext);
142: } else {
143: dispatcher.runAsync(serviceName, serviceContext);
144: }
145: } else {
146: Debug
147: .logWarning(
148: "ServiceRequester -> receive_event() called with no service defined!",
149: module);
150: }
151: }
152:
153: private Map makeServiceContext(WfEventAudit event,
154: LocalDispatcher dispatcher) throws GenericServiceException {
155: DispatchContext dctx = dispatcher.getDispatchContext();
156: try {
157: return dctx.getModelService(this .getServiceName())
158: .makeValid(getWRD(event, initialContext),
159: ModelService.IN_PARAM);
160: } catch (BaseException e) {
161: throw new GenericServiceException(e);
162: }
163: }
164: }
|