KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > service > SharkServiceEngine


1 /*
2  * $Id: SharkServiceEngine.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.service;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.transaction.Transaction JavaDoc;
33
34 import org.ofbiz.base.util.StringUtil;
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.entity.GenericValue;
37 import org.ofbiz.entity.transaction.TransactionUtil;
38 import org.ofbiz.entity.transaction.GenericTransactionException;
39 import org.ofbiz.service.GenericRequester;
40 import org.ofbiz.service.GenericResultWaiter;
41 import org.ofbiz.service.GenericServiceException;
42 import org.ofbiz.service.ModelService;
43 import org.ofbiz.service.ServiceDispatcher;
44 import org.ofbiz.service.engine.AbstractEngine;
45 import org.ofbiz.shark.container.SharkContainer;
46 import org.ofbiz.shark.requester.SimpleRequester;
47
48 import org.enhydra.shark.api.client.wfbase.BaseException;
49 import org.enhydra.shark.api.client.wfmodel.*;
50 import org.enhydra.shark.api.client.wfservice.AdminInterface;
51 import org.enhydra.shark.api.client.wfservice.ConnectFailed;
52 import org.enhydra.shark.api.client.wfservice.ExecutionAdministration;
53 import org.enhydra.shark.api.client.wfservice.NotConnected;
54
55 /**
56  * Shark Service Engine
57  *
58  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
59  * @version $Rev: 5462 $
60  * @since 3.1
61  */

62 public class SharkServiceEngine extends AbstractEngine {
63
64     public static final String JavaDoc module = SharkServiceEngine.class.getName();
65
66     public SharkServiceEngine(ServiceDispatcher dispatcher) {
67         super(dispatcher);
68     }
69
70     /**
71      * Run the service synchronously and return the result.
72      *
73      * @param localName Name of the LocalDispatcher.
74      * @param modelService Service model object.
75      * @param context Map of name, value pairs composing the context.
76      * @return Map of name, value pairs composing the result.
77      * @throws org.ofbiz.service.GenericServiceException
78      *
79      */

80     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
81         GenericResultWaiter waiter = new GenericResultWaiter();
82         this.runAsync(localName, modelService, context, waiter, false);
83         return waiter.waitForResult();
84     }
85
86     /**
87      * Run the service synchronously and IGNORE the result.
88      *
89      * @param localName Name of the LocalDispatcher.
90      * @param modelService Service model object.
91      * @param context Map of name, value pairs composing the context.
92      * @throws org.ofbiz.service.GenericServiceException
93      *
94      */

95     public void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
96         this.runSync(localName, modelService, context);
97     }
98
99     /**
100      * Run the service asynchronously, passing an instance of GenericRequester that will receive the result.
101      *
102      * @param localName Name of the LocalDispatcher.
103      * @param modelService Service model object.
104      * @param context Map of name, value pairs composing the context.
105      * @param requester Object implementing GenericRequester interface which will receive the result.
106      * @param persist True for store/run; False for run.
107      * @throws org.ofbiz.service.GenericServiceException
108      *
109      */

110     public void runAsync(String JavaDoc localName, ModelService modelService, Map JavaDoc context, GenericRequester requester, boolean persist) throws GenericServiceException {
111         this.runWf(modelService, context, requester);
112     }
113
114     /**
115      * Run the service asynchronously and IGNORE the result.
116      *
117      * @param localName Name of the LocalDispatcher.
118      * @param modelService Service model object.
119      * @param context Map of name, value pairs composing the context.
120      * @param persist True for store/run; False for run.
121      * @throws org.ofbiz.service.GenericServiceException
122      *
123      */

124     public void runAsync(String JavaDoc localName, ModelService modelService, Map JavaDoc context, boolean persist) throws GenericServiceException {
125         this.runAsync(localName, modelService, context, new GenericResultWaiter(), persist);
126     }
127
128     private GenericRequester runWf(ModelService model, Map JavaDoc context, GenericRequester waiter) throws GenericServiceException {
129         GenericValue userLogin = (GenericValue) context.get("userLogin");
130         if (userLogin == null) {
131             userLogin = SharkContainer.getAdminUser();
132         }
133
134         AdminInterface admin = SharkContainer.getAdminInterface();
135         ExecutionAdministration exec = admin.getExecutionAdministration();
136
137         boolean beganTrans = false;
138         boolean hasError = false;
139         Transaction JavaDoc trans = null;
140
141         try {
142             beganTrans = TransactionUtil.begin();
143             if (!beganTrans) {
144                 trans = TransactionUtil.suspend();
145                 beganTrans = TransactionUtil.begin();
146             }
147
148             try {
149                 // connect to admin API
150
try {
151                     exec.connect(userLogin.getString("userLoginId"), userLogin.getString("currentPassword"), null, null);
152                 } catch (BaseException e) {
153                     throw new GenericServiceException(e);
154                 } catch (ConnectFailed e) {
155                     throw new GenericServiceException(e);
156                 }
157
158                 try {
159                     // create the requester
160
WfRequester req = new SimpleRequester(userLogin, model, waiter);
161                     WfProcessMgr mgr = null;
162                     String JavaDoc location = this.getLocation(model);
163                     String JavaDoc version = null;
164
165                     // locate packageId::version
166
if (location.indexOf("::") != -1) {
167                         List JavaDoc splitList = StringUtil.split(location, "::");
168                         location = (String JavaDoc) splitList.get(0);
169                         version = (String JavaDoc) splitList.get(1);
170                     }
171
172                     // obtain the process manager
173
try {
174                         if (version == null) {
175                             mgr = exec.getProcessMgr(location, model.invoke);
176                         } else {
177                             mgr = exec.getProcessMgr(location, version, model.invoke);
178                         }
179                     } catch (BaseException e) {
180                         throw new GenericServiceException(e);
181                     } catch (NotConnected e) {
182                         throw new GenericServiceException(e);
183                     }
184
185                     // make sure the manager exists
186
if (mgr == null) {
187                         throw new GenericServiceException("Unable to obtain Process Manager for : " + this.getLocation(model) + " / " + model.invoke);
188                     }
189
190                     // create the process instance
191
WfProcess proc = null;
192                     try {
193                         proc = mgr.create_process(req);
194                     } catch (BaseException e) {
195                         throw new GenericServiceException(e);
196                     } catch (NotEnabled e) {
197                         throw new GenericServiceException(e);
198                     } catch (InvalidRequester e) {
199                         throw new GenericServiceException(e);
200                     } catch (RequesterRequired e) {
201                         throw new GenericServiceException(e);
202                     }
203
204                     Map JavaDoc contextSig = null;
205                     try {
206                         contextSig = mgr.context_signature();
207                     } catch (BaseException e) {
208                         throw new GenericServiceException(e);
209                     }
210
211                     if (contextSig != null) {
212                         Iterator JavaDoc sigKeys = contextSig.keySet().iterator();
213                         Map JavaDoc formalParams = new HashMap JavaDoc();
214                         while (sigKeys.hasNext()) {
215                             String JavaDoc key = (String JavaDoc) sigKeys.next();
216                             formalParams.put(key, context.get(key));
217                         }
218
219                         // set the initial WRD
220
try {
221                             proc.set_process_context(formalParams);
222                         } catch (BaseException e) {
223                             throw new GenericServiceException(e);
224                         } catch (InvalidData e) {
225                             throw new GenericServiceException(e);
226                         } catch (UpdateNotAllowed e) {
227                             throw new GenericServiceException(e);
228                         }
229                     }
230
231                     // start the process
232
try {
233                         proc.start();
234                     } catch (BaseException e) {
235                         throw new GenericServiceException(e);
236                     } catch (CannotStart e) {
237                         throw new GenericServiceException(e);
238                     } catch (AlreadyRunning e) {
239                         throw new GenericServiceException(e);
240                     }
241                 } catch (GenericServiceException e) {
242                     throw e;
243                 } finally {
244                     // disconnect from admin API
245
try {
246                         exec.disconnect();
247                     } catch (NotConnected e) {
248                         throw new GenericServiceException(e);
249                     } catch (BaseException e) {
250                         throw new GenericServiceException(e);
251                     }
252                 }
253             } catch (GenericServiceException e) {
254                 hasError = true;
255                 throw e;
256             } finally {
257                 if (hasError) {
258                     try {
259                         TransactionUtil.rollback(beganTrans, "Transaction rollback from Shark", null);
260                     } catch (GenericTransactionException e) {
261                         Debug.logError(e, "Could not rollback transaction", module);
262                     }
263                 } else {
264                     try {
265                         TransactionUtil.commit(beganTrans);
266                     } catch (GenericTransactionException e) {
267                         Debug.logError(e, "Could not commit transaction", module);
268                         throw new GenericServiceException("Commit transaction failed");
269                     }
270                 }
271             }
272         } catch (GenericTransactionException e) {
273             throw new GenericServiceException(e);
274         } finally {
275             if (trans != null) {
276                 try {
277                     TransactionUtil.resume(trans);
278                 } catch (GenericTransactionException e) {
279                     throw new GenericServiceException(e);
280                 }
281             }
282         }
283
284         return waiter;
285     }
286 }
287
Popular Tags