KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > requester > ServiceRequester


1 /*
2  * $Id: ServiceRequester.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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  */

25 package org.ofbiz.shark.requester;
26
27 import java.util.HashMap 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.service.DispatchContext;
33 import org.ofbiz.service.GenericServiceException;
34 import org.ofbiz.service.LocalDispatcher;
35 import org.ofbiz.service.ModelService;
36 import org.ofbiz.shark.container.SharkContainer;
37
38 import org.enhydra.shark.api.SharkTransaction;
39 import org.enhydra.shark.api.client.wfbase.BaseException;
40 import org.enhydra.shark.api.client.wfmodel.InvalidPerformer;
41 import org.enhydra.shark.api.client.wfmodel.SourceNotAvailable;
42 import org.enhydra.shark.api.client.wfmodel.WfEventAudit;
43
44 /**
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 3.1
49  */

50 public class ServiceRequester extends AbstractRequester {
51
52     public static final String JavaDoc module = ServiceRequester.class.getName();
53     public static final int ASYNC = 0;
54     public static final int SYNC = 1;
55
56     protected Map JavaDoc initialContext = new HashMap JavaDoc();
57     protected String JavaDoc serviceName = null;
58     protected String JavaDoc eventType = null;
59     protected int serviceMode = 1;
60
61     // new requester
62
public ServiceRequester(GenericValue userLogin, String JavaDoc eventType) {
63         super(userLogin);
64         this.setEventType(eventType);
65     }
66
67     public ServiceRequester(GenericValue userLogin) {
68         super(userLogin);
69     }
70
71     // -------------------
72
// WfRequester methods
73
// -------------------
74

75     public void receive_event(WfEventAudit event) throws BaseException, InvalidPerformer {
76         if (this.getEventType() == null || this.getEventType().equals(event.event_type())) {
77             try {
78                 this.run(event);
79             } catch (GenericServiceException e) {
80                 Debug.logError(e, module);
81                 throw new BaseException(e);
82             }
83         }
84     }
85
86     public void receive_event(SharkTransaction trans, WfEventAudit event) throws BaseException, InvalidPerformer {
87         receive_event(event);
88     }
89
90     // -------------
91
// local methods
92
// -------------
93

94
95     public void setEventType(String JavaDoc eventType) {
96         this.eventType = eventType;
97     }
98
99     public String JavaDoc getEventType() {
100         return this.eventType;
101     }
102
103     public void setService(String JavaDoc serviceName, int serviceMode) {
104         this.serviceName = serviceName;
105         this.serviceMode = serviceMode;
106     }
107
108     public void setService(String JavaDoc serviceName) {
109         this.setService(serviceName, ServiceRequester.ASYNC);
110     }
111
112     public String JavaDoc getServiceName() {
113         return this.serviceName;
114     }
115
116     public int getServiceMode() {
117         return this.serviceMode;
118     }
119
120     public void setInitalContextValues(Map JavaDoc initialContext) {
121         this.initialContext = new HashMap JavaDoc(initialContext);
122     }
123
124     private void run(WfEventAudit event) throws GenericServiceException {
125         // get the dispatcher
126
LocalDispatcher dispatcher = SharkContainer.getDispatcher();
127         if (dispatcher == null) {
128             throw new GenericServiceException("Cannot run service with null dispatcher");
129         }
130
131         // get the service context
132
Map JavaDoc serviceContext = makeServiceContext(event, dispatcher);
133
134         // invoke the service
135
String JavaDoc serviceName = this.getServiceName();
136         if (serviceName != null) {
137             int mode = this.getServiceMode();
138             if (mode == ServiceRequester.SYNC) {
139                 dispatcher.runSyncIgnore(serviceName, serviceContext);
140             } else {
141                 dispatcher.runAsync(serviceName, serviceContext);
142             }
143         } else {
144             Debug.logWarning("ServiceRequester -> receive_event() called with no service defined!", module);
145         }
146     }
147
148     private Map JavaDoc makeServiceContext(WfEventAudit event, LocalDispatcher dispatcher) throws GenericServiceException {
149         DispatchContext dctx = dispatcher.getDispatchContext();
150         try {
151             return dctx.getModelService(this.getServiceName()).makeValid(getWRD(event, initialContext), ModelService.IN_PARAM);
152         } catch (BaseException e) {
153             throw new GenericServiceException(e);
154         }
155     }
156 }
157
Popular Tags