KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > MethodContext


1 /*
2  * $Id: MethodContext.java 5462 2005-08-05 18:35:48Z jonesde $
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 package org.ofbiz.minilang.method;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.ofbiz.base.util.UtilHttp;
35 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
36 import org.ofbiz.base.util.string.FlexibleStringExpander;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.minilang.SimpleMethod;
40 import org.ofbiz.security.Security;
41 import org.ofbiz.service.DispatchContext;
42 import org.ofbiz.service.LocalDispatcher;
43
44 /**
45  * A single operation, does the specified operation on the given field
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
49  * @version $Rev: 5462 $
50  * @since 2.0
51  */

52 public class MethodContext {
53     
54     public static final int EVENT = 1;
55     public static final int SERVICE = 2;
56
57     protected int methodType;
58
59     protected Map JavaDoc env = new HashMap JavaDoc();
60     protected Map JavaDoc parameters;
61     protected Locale JavaDoc locale;
62     protected ClassLoader JavaDoc loader;
63     protected LocalDispatcher dispatcher;
64     protected GenericDelegator delegator;
65     protected Security security;
66     protected GenericValue userLogin;
67
68     protected HttpServletRequest JavaDoc request = null;
69     protected HttpServletResponse JavaDoc response = null;
70
71     protected Map JavaDoc results = null;
72     protected DispatchContext ctx;
73
74     public MethodContext(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ClassLoader JavaDoc loader) {
75         this.methodType = MethodContext.EVENT;
76         this.parameters = UtilHttp.getParameterMap(request);
77         this.loader = loader;
78         this.request = request;
79         this.response = response;
80         this.locale = UtilHttp.getLocale(request);
81         this.dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
82         this.delegator = (GenericDelegator) request.getAttribute("delegator");
83         this.security = (Security) request.getAttribute("security");
84         this.userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
85
86         if (this.loader == null) {
87             try {
88                 this.loader = Thread.currentThread().getContextClassLoader();
89             } catch (SecurityException JavaDoc e) {
90                 this.loader = this.getClass().getClassLoader();
91             }
92         }
93     }
94
95     public MethodContext(DispatchContext ctx, Map JavaDoc context, ClassLoader JavaDoc loader) {
96         this.methodType = MethodContext.SERVICE;
97         this.parameters = context;
98         this.loader = loader;
99         this.locale = (Locale JavaDoc) context.get("locale");
100         this.dispatcher = ctx.getDispatcher();
101         this.delegator = ctx.getDelegator();
102         this.security = ctx.getSecurity();
103         this.results = new HashMap JavaDoc();
104         this.userLogin = (GenericValue) context.get("userLogin");
105
106         if (this.loader == null) {
107             try {
108                 this.loader = Thread.currentThread().getContextClassLoader();
109             } catch (SecurityException JavaDoc e) {
110                 this.loader = this.getClass().getClassLoader();
111             }
112         }
113     }
114
115     /**
116      * This is a very simple constructor which assumes the needed objects (dispatcher,
117      * delegator, security, request, response, etc) are in the context.
118      * Will result in calling method as a service or event, as specified.
119      */

120     public MethodContext(Map JavaDoc context, ClassLoader JavaDoc loader, int methodType) {
121         this.methodType = methodType;
122         this.parameters = context;
123         this.loader = loader;
124         this.locale = (Locale JavaDoc) context.get("locale");
125         this.dispatcher = (LocalDispatcher) context.get("dispatcher");
126         this.delegator = (GenericDelegator) context.get("delegator");
127         this.security = (Security) context.get("security");
128         this.userLogin = (GenericValue) context.get("userLogin");
129
130         if (methodType == MethodContext.EVENT) {
131             this.request = (HttpServletRequest JavaDoc) context.get("request");
132             this.response = (HttpServletResponse JavaDoc) context.get("response");
133             if (this.locale == null) this.locale = UtilHttp.getLocale(request);
134             
135             //make sure the delegator and other objects are in place, getting from
136
// request if necessary; assumes this came through the ControlServlet
137
// or something similar
138
if (this.request != null) {
139                 if (this.dispatcher == null) this.dispatcher = (LocalDispatcher) this.request.getAttribute("dispatcher");
140                 if (this.delegator == null) this.delegator = (GenericDelegator) this.request.getAttribute("delegator");
141                 if (this.security == null) this.security = (Security) this.request.getAttribute("security");
142                 if (this.userLogin == null) this.userLogin = (GenericValue) this.request.getSession().getAttribute("userLogin");
143             }
144         } else if (methodType == MethodContext.SERVICE) {
145             this.results = new HashMap JavaDoc();
146         }
147         
148         if (this.loader == null) {
149             try {
150                 this.loader = Thread.currentThread().getContextClassLoader();
151             } catch (SecurityException JavaDoc e) {
152                 this.loader = this.getClass().getClassLoader();
153             }
154         }
155     }
156     
157     public void setErrorReturn(String JavaDoc errMsg, SimpleMethod simpleMethod) {
158         if (getMethodType() == MethodContext.EVENT) {
159             putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
160             putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
161         } else if (getMethodType() == MethodContext.SERVICE) {
162             putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
163             putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
164         }
165     }
166
167     public int getMethodType() {
168         return this.methodType;
169     }
170
171     public Map JavaDoc getEnvMap() {
172         return this.env;
173     }
174     
175     /** Gets the named value from the environment. Supports the "." (dot) syntax to access Map members and the
176      * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other
177      * environment values using the "${}" notation.
178      *
179      * @param key The name of the environment value to get. Can contain "." and "[]" syntax elements as described above.
180      * @return The environment value if found, otherwise null.
181      */

182     public Object JavaDoc getEnv(String JavaDoc key) {
183         String JavaDoc ekey = this.expandString(key);
184         FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
185         return this.getEnv(fma);
186     }
187     public Object JavaDoc getEnv(FlexibleMapAccessor fma) {
188         return fma.get(this.env);
189     }
190
191     /** Puts the named value in the environment. Supports the "." (dot) syntax to access Map members and the
192      * "[]" (bracket) syntax to access List entries.
193      * If the brackets for a list are empty the value will be appended to end of the list,
194      * otherwise the value will be set in the position of the number in the brackets.
195      * If a "+" (plus sign) is included inside the square brackets before the index
196      * number the value will inserted/added at that index instead of set at that index.
197      * This value is expanded, supporting the insertion of other
198      * environment values using the "${}" notation.
199      *
200      * @param key The name of the environment value to get. Can contain "." syntax elements as described above.
201      * @param value The value to set in the named environment location.
202      */

203     public void putEnv(String JavaDoc key, Object JavaDoc value) {
204         String JavaDoc ekey = this.expandString(key);
205         FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
206         this.putEnv(fma, value);
207     }
208     public void putEnv(FlexibleMapAccessor fma, Object JavaDoc value) {
209         fma.put(this.env, value);
210     }
211
212     /** Calls putEnv for each entry in the Map, thus allowing for the additional flexibility in naming
213      * supported in that method.
214      */

215     public void putAllEnv(Map JavaDoc values) {
216         Iterator JavaDoc viter = values.entrySet().iterator();
217         while (viter.hasNext()) {
218             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) viter.next();
219             this.putEnv((String JavaDoc) entry.getKey(), entry.getValue());
220         }
221     }
222
223     /** Removes the named value from the environment. Supports the "." (dot) syntax to access Map members and the
224      * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other
225      * environment values using the "${}" notation.
226      *
227      * @param key The name of the environment value to get. Can contain "." syntax elements as described above.
228      */

229     public Object JavaDoc removeEnv(String JavaDoc key) {
230         String JavaDoc ekey = this.expandString(key);
231         FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
232         return this.removeEnv(fma);
233     }
234     public Object JavaDoc removeEnv(FlexibleMapAccessor fma) {
235         return fma.remove(this.env);
236     }
237
238     public Iterator JavaDoc getEnvEntryIterator() {
239         return this.env.entrySet().iterator();
240     }
241
242     public Object JavaDoc getParameter(String JavaDoc key) {
243         return this.parameters.get(key);
244     }
245
246     public void putParameter(String JavaDoc key, Object JavaDoc value) {
247         this.parameters.put(key, value);
248     }
249
250     public Map JavaDoc getParameters() {
251         return this.parameters;
252     }
253
254     public ClassLoader JavaDoc getLoader() {
255         return this.loader;
256     }
257     
258     public Locale JavaDoc getLocale() {
259         return this.locale;
260     }
261
262     public LocalDispatcher getDispatcher() {
263         return this.dispatcher;
264     }
265
266     public GenericDelegator getDelegator() {
267         return this.delegator;
268     }
269
270     public Security getSecurity() {
271         return this.security;
272     }
273
274     public HttpServletRequest JavaDoc getRequest() {
275         return this.request;
276     }
277
278     public HttpServletResponse JavaDoc getResponse() {
279         return this.response;
280     }
281
282     public GenericValue getUserLogin() {
283         return this.userLogin;
284     }
285
286     public void setUserLogin(GenericValue userLogin, String JavaDoc userLoginEnvName) {
287         this.userLogin = userLogin;
288         this.putEnv(userLoginEnvName, userLogin);
289     }
290
291     public Object JavaDoc getResult(String JavaDoc key) {
292         return this.results.get(key);
293     }
294
295     public void putResult(String JavaDoc key, Object JavaDoc value) {
296         this.results.put(key, value);
297     }
298
299     public Map JavaDoc getResults() {
300         return this.results;
301     }
302     
303     /** Expands environment variables delimited with ${} */
304     public String JavaDoc expandString(String JavaDoc original) {
305         return FlexibleStringExpander.expandString(original, this.env);
306     }
307
308     public String JavaDoc expandString(FlexibleStringExpander originalExdr) {
309         return originalExdr.expandString(this.env);
310     }
311 }
312
Popular Tags