KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > GenericDispatcher


1 /*
2  * $Id: GenericDispatcher.java 7284 2006-04-12 18:39:42Z jaz $
3  *
4  * Copyright (c) 2001, 2002 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.service;
26
27 import java.util.Map JavaDoc;
28
29 import org.ofbiz.entity.GenericDelegator;
30 import org.ofbiz.base.util.Debug;
31
32 /**
33  * Generic Services Local Dispatcher
34  *
35  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
36  * @version $Rev: 7284 $
37  * @since 2.0
38  */

39 public class GenericDispatcher extends GenericAbstractDispatcher {
40
41     public static final String JavaDoc module = GenericDispatcher.class.getName();
42
43     public GenericDispatcher() {}
44
45     public GenericDispatcher(String JavaDoc name, GenericDelegator delegator) {
46         this(name, delegator, null);
47     }
48
49     public GenericDispatcher(String JavaDoc name, GenericDelegator delegator, ClassLoader JavaDoc loader) {
50         if (loader == null) {
51             try {
52                 loader = Thread.currentThread().getContextClassLoader();
53             } catch (SecurityException JavaDoc e) {
54                 loader = this.getClass().getClassLoader();
55             }
56         }
57         DispatchContext dc = new DispatchContext(name, null, loader, null);
58         init(name, delegator, dc);
59     }
60
61     public GenericDispatcher(DispatchContext ctx, GenericDelegator delegator) {
62         init(ctx.getName(), delegator, ctx);
63     }
64
65     public GenericDispatcher(DispatchContext ctx, ServiceDispatcher dispatcher) {
66         this.dispatcher = dispatcher;
67         this.ctx = ctx;
68         this.name = ctx.getName();
69
70         ctx.setDispatcher(this);
71         ctx.loadReaders();
72         dispatcher.register(name, ctx);
73     }
74
75     protected void init(String JavaDoc name, GenericDelegator delegator, DispatchContext ctx) {
76         if (name == null || name.length() == 0)
77             throw new IllegalArgumentException JavaDoc("The name of a LocalDispatcher cannot be a null or empty String");
78
79         this.name = name;
80         this.ctx = ctx;
81         this.dispatcher = ServiceDispatcher.getInstance(name, ctx, delegator);
82
83         ctx.setDispatcher(this);
84         ctx.loadReaders();
85         if (Debug.infoOn()) Debug.logInfo("[LocalDispatcher] : Created Dispatcher for: " + name, module);
86     }
87
88     public static LocalDispatcher getLocalDispatcher(String JavaDoc name, GenericDelegator delegator) throws GenericServiceException {
89         ServiceDispatcher sd = ServiceDispatcher.getInstance(name, delegator);
90         LocalDispatcher thisDispatcher = null;
91         if (sd != null) {
92             thisDispatcher = sd.getLocalDispatcher(name);
93         }
94         if (thisDispatcher == null) {
95             thisDispatcher = new GenericDispatcher(name, delegator);
96         }
97
98         if (thisDispatcher != null) {
99             return thisDispatcher;
100         } else {
101             throw new GenericServiceException("Unable to load dispatcher for name : " + name + " with delegator : " + delegator.getDelegatorName());
102         }
103     }
104
105     // special method to obtain a new 'unique' reference
106
public static LocalDispatcher newInstance(String JavaDoc name, GenericDelegator delegator, boolean enableJM, boolean enableJMS, boolean enableSvcs) throws GenericServiceException {
107         ServiceDispatcher sd = new ServiceDispatcher(delegator, enableJM, enableJMS, enableSvcs);
108         ClassLoader JavaDoc loader = null;
109         try {
110             loader = Thread.currentThread().getContextClassLoader();
111         } catch (SecurityException JavaDoc e) {
112             loader = GenericDispatcher.class.getClassLoader();
113         }
114         DispatchContext dc = new DispatchContext(name, null, loader, null);
115         return new GenericDispatcher(dc, sd);
116     }
117
118     /**
119      * @see org.ofbiz.service.LocalDispatcher#runSync(java.lang.String, java.util.Map)
120      */

121     public Map JavaDoc runSync(String JavaDoc serviceName, Map JavaDoc context) throws ServiceValidationException, GenericServiceException {
122         ModelService service = ctx.getModelService(serviceName);
123         return dispatcher.runSync(this.name, service, context);
124     }
125
126     /**
127      * @see org.ofbiz.service.LocalDispatcher#runSync(java.lang.String, java.util.Map, int, boolean)
128      */

129     public Map JavaDoc runSync(String JavaDoc serviceName, Map JavaDoc context, int transactionTimeout, boolean requireNewTransaction) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
130         ModelService service = ctx.getModelService(serviceName);
131         // clone the model service for updates
132
ModelService cloned = new ModelService(service);
133         cloned.requireNewTransaction = requireNewTransaction;
134         cloned.transactionTimeout = transactionTimeout;
135         return dispatcher.runSync(this.name, cloned, context);
136     }
137
138     /**
139      * @see org.ofbiz.service.LocalDispatcher#runSyncIgnore(java.lang.String, java.util.Map)
140      */

141     public void runSyncIgnore(String JavaDoc serviceName, Map JavaDoc context) throws GenericServiceException {
142         ModelService service = ctx.getModelService(serviceName);
143         dispatcher.runSyncIgnore(this.name, service, context);
144     }
145
146     /**
147      * @see org.ofbiz.service.LocalDispatcher#runSyncIgnore(java.lang.String, java.util.Map)
148      */

149     public void runSyncIgnore(String JavaDoc serviceName, Map JavaDoc context, int transactionTimeout, boolean requireNewTransaction) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
150         ModelService service = ctx.getModelService(serviceName);
151         // clone the model service for updates
152
ModelService cloned = new ModelService(service);
153         cloned.requireNewTransaction = requireNewTransaction;
154         cloned.transactionTimeout = transactionTimeout;
155         dispatcher.runSyncIgnore(this.name, cloned, context);
156     }
157
158     /**
159      * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester, boolean, int, boolean)
160      */

161     public void runAsync(String JavaDoc serviceName, Map JavaDoc context, GenericRequester requester, boolean persist, int transactionTimeout, boolean requireNewTransaction) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
162         ModelService service = ctx.getModelService(serviceName);
163         // clone the model service for updates
164
ModelService cloned = new ModelService(service);
165         cloned.requireNewTransaction = requireNewTransaction;
166         cloned.transactionTimeout = transactionTimeout;
167         dispatcher.runAsync(this.name, cloned, context, requester, persist);
168     }
169
170     /**
171      * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester, boolean)
172      */

173     public void runAsync(String JavaDoc serviceName, Map JavaDoc context, GenericRequester requester, boolean persist) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
174         ModelService service = ctx.getModelService(serviceName);
175         dispatcher.runAsync(this.name, service, context, requester, persist);
176     }
177    
178     /**
179      * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester)
180      */

181     public void runAsync(String JavaDoc serviceName, Map JavaDoc context, GenericRequester requester) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
182         runAsync(serviceName, context, requester, true);
183     }
184     
185     /**
186      * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, boolean)
187      */

188     public void runAsync(String JavaDoc serviceName, Map JavaDoc context, boolean persist) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
189         ModelService service = ctx.getModelService(serviceName);
190         dispatcher.runAsync(this.name, service, context, persist);
191     }
192    
193     /**
194      * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map)
195      */

196     public void runAsync(String JavaDoc serviceName, Map JavaDoc context) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
197         runAsync(serviceName, context, true);
198     }
199   
200     /**
201      * @see org.ofbiz.service.LocalDispatcher#runAsyncWait(java.lang.String, java.util.Map, boolean)
202      */

203     public GenericResultWaiter runAsyncWait(String JavaDoc serviceName, Map JavaDoc context, boolean persist) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
204         GenericResultWaiter waiter = new GenericResultWaiter();
205         this.runAsync(serviceName, context, waiter, persist);
206         return waiter;
207     }
208  
209     /**
210      * @see org.ofbiz.service.LocalDispatcher#runAsyncWait(java.lang.String, java.util.Map)
211      */

212     public GenericResultWaiter runAsyncWait(String JavaDoc serviceName, Map JavaDoc context) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
213         return runAsyncWait(serviceName, context, true);
214     }
215 }
216
217
Popular Tags