001: /*
002: * $Id: MethodContext.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Locale;
029: import java.util.Map;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.ofbiz.base.util.FlexibleMapAccessor;
035: import org.ofbiz.base.util.FlexibleStringExpander;
036: import org.ofbiz.base.util.UtilHttp;
037: import org.ofbiz.entity.GenericDelegator;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.minilang.SimpleMethod;
040: import org.ofbiz.security.Security;
041: import org.ofbiz.service.DispatchContext;
042: import org.ofbiz.service.LocalDispatcher;
043:
044: /**
045: * A single operation, does the specified operation on the given field
046: *
047: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
048: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
049: * @version $Revision: 1.1 $
050: * @since 2.0
051: */
052: public class MethodContext {
053:
054: public static final int EVENT = 1;
055: public static final int SERVICE = 2;
056:
057: protected int methodType;
058:
059: protected Map env = new HashMap();
060: protected Map parameters;
061: protected Locale locale;
062: protected ClassLoader loader;
063: protected LocalDispatcher dispatcher;
064: protected GenericDelegator delegator;
065: protected Security security;
066: protected GenericValue userLogin;
067:
068: protected HttpServletRequest request = null;
069: protected HttpServletResponse response = null;
070:
071: protected Map results = null;
072: protected DispatchContext ctx;
073:
074: public MethodContext(HttpServletRequest request,
075: HttpServletResponse response, ClassLoader loader) {
076: this .methodType = MethodContext.EVENT;
077: this .parameters = UtilHttp.getParameterMap(request);
078: this .loader = loader;
079: this .request = request;
080: this .response = response;
081: this .locale = UtilHttp.getLocale(request);
082: this .dispatcher = (LocalDispatcher) request
083: .getAttribute("dispatcher");
084: this .delegator = (GenericDelegator) request
085: .getAttribute("delegator");
086: this .security = (Security) request.getAttribute("security");
087: this .userLogin = (GenericValue) request.getSession()
088: .getAttribute("userLogin");
089:
090: if (this .loader == null) {
091: try {
092: this .loader = Thread.currentThread()
093: .getContextClassLoader();
094: } catch (SecurityException e) {
095: this .loader = this .getClass().getClassLoader();
096: }
097: }
098: }
099:
100: public MethodContext(DispatchContext ctx, Map context,
101: ClassLoader loader) {
102: this .methodType = MethodContext.SERVICE;
103: this .parameters = context;
104: this .loader = loader;
105: this .locale = (Locale) context.get("locale");
106: this .dispatcher = ctx.getDispatcher();
107: this .delegator = ctx.getDelegator();
108: this .security = ctx.getSecurity();
109: this .results = new HashMap();
110: this .userLogin = (GenericValue) context.get("userLogin");
111:
112: if (this .loader == null) {
113: try {
114: this .loader = Thread.currentThread()
115: .getContextClassLoader();
116: } catch (SecurityException e) {
117: this .loader = this .getClass().getClassLoader();
118: }
119: }
120: }
121:
122: /**
123: * This is a very simple constructor which assumes the needed objects (dispatcher,
124: * delegator, security, request, response, etc) are in the context.
125: * Will result in calling method as a service or event, as specified.
126: */
127: public MethodContext(Map context, ClassLoader loader, int methodType) {
128: this .methodType = methodType;
129: this .parameters = context;
130: this .loader = loader;
131: this .locale = (Locale) context.get("locale");
132: this .dispatcher = (LocalDispatcher) context.get("dispatcher");
133: this .delegator = (GenericDelegator) context.get("delegator");
134: this .security = (Security) context.get("security");
135: this .userLogin = (GenericValue) context.get("userLogin");
136:
137: if (methodType == MethodContext.EVENT) {
138: this .request = (HttpServletRequest) context.get("request");
139: this .response = (HttpServletResponse) context
140: .get("response");
141: if (this .locale == null)
142: this .locale = UtilHttp.getLocale(request);
143:
144: //make sure the delegator and other objects are in place, getting from
145: // request if necessary; assumes this came through the ControlServlet
146: // or something similar
147: if (this .request != null) {
148: if (this .dispatcher == null)
149: this .dispatcher = (LocalDispatcher) this .request
150: .getAttribute("dispatcher");
151: if (this .delegator == null)
152: this .delegator = (GenericDelegator) this .request
153: .getAttribute("delegator");
154: if (this .security == null)
155: this .security = (Security) this .request
156: .getAttribute("security");
157: if (this .userLogin == null)
158: this .userLogin = (GenericValue) this .request
159: .getSession().getAttribute("userLogin");
160: }
161: } else if (methodType == MethodContext.SERVICE) {
162: this .results = new HashMap();
163: }
164:
165: if (this .loader == null) {
166: try {
167: this .loader = Thread.currentThread()
168: .getContextClassLoader();
169: } catch (SecurityException e) {
170: this .loader = this .getClass().getClassLoader();
171: }
172: }
173: }
174:
175: public void setErrorReturn(String errMsg, SimpleMethod simpleMethod) {
176: if (getMethodType() == MethodContext.EVENT) {
177: putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
178: putEnv(simpleMethod.getEventResponseCodeName(),
179: simpleMethod.getDefaultErrorCode());
180: } else if (getMethodType() == MethodContext.SERVICE) {
181: putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
182: putEnv(simpleMethod.getServiceResponseMessageName(),
183: simpleMethod.getDefaultErrorCode());
184: }
185: }
186:
187: public int getMethodType() {
188: return this .methodType;
189: }
190:
191: public Map getEnvMap() {
192: return this .env;
193: }
194:
195: /** Gets the named value from the environment. Supports the "." (dot) syntax to access Map members and the
196: * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other
197: * environment values using the "${}" notation.
198: *
199: * @param key The name of the environment value to get. Can contain "." and "[]" syntax elements as described above.
200: * @return The environment value if found, otherwise null.
201: */
202: public Object getEnv(String key) {
203: String ekey = this .expandString(key);
204: FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
205: return this .getEnv(fma);
206: }
207:
208: public Object getEnv(FlexibleMapAccessor fma) {
209: return fma.get(this .env);
210: }
211:
212: /** Puts the named value in the environment. Supports the "." (dot) syntax to access Map members and the
213: * "[]" (bracket) syntax to access List entries.
214: * If the brackets for a list are empty the value will be appended to end of the list,
215: * otherwise the value will be set in the position of the number in the brackets.
216: * If a "+" (plus sign) is included inside the square brackets before the index
217: * number the value will inserted/added at that index instead of set at that index.
218: * This value is expanded, supporting the insertion of other
219: * environment values using the "${}" notation.
220: *
221: * @param key The name of the environment value to get. Can contain "." syntax elements as described above.
222: * @param value The value to set in the named environment location.
223: */
224: public void putEnv(String key, Object value) {
225: String ekey = this .expandString(key);
226: FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
227: this .putEnv(fma, value);
228: }
229:
230: public void putEnv(FlexibleMapAccessor fma, Object value) {
231: fma.put(this .env, value);
232: }
233:
234: /** Calls putEnv for each entry in the Map, thus allowing for the additional flexibility in naming
235: * supported in that method.
236: */
237: public void putAllEnv(Map values) {
238: Iterator viter = values.entrySet().iterator();
239: while (viter.hasNext()) {
240: Map.Entry entry = (Map.Entry) viter.next();
241: this .putEnv((String) entry.getKey(), entry.getValue());
242: }
243: }
244:
245: /** Removes the named value from the environment. Supports the "." (dot) syntax to access Map members and the
246: * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other
247: * environment values using the "${}" notation.
248: *
249: * @param key The name of the environment value to get. Can contain "." syntax elements as described above.
250: */
251: public Object removeEnv(String key) {
252: String ekey = this .expandString(key);
253: FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
254: return this .removeEnv(fma);
255: }
256:
257: public Object removeEnv(FlexibleMapAccessor fma) {
258: return fma.remove(this .env);
259: }
260:
261: public Iterator getEnvEntryIterator() {
262: return this .env.entrySet().iterator();
263: }
264:
265: public Object getParameter(String key) {
266: return this .parameters.get(key);
267: }
268:
269: public void putParameter(String key, Object value) {
270: this .parameters.put(key, value);
271: }
272:
273: public Map getParameters() {
274: return this .parameters;
275: }
276:
277: public ClassLoader getLoader() {
278: return this .loader;
279: }
280:
281: public Locale getLocale() {
282: return this .locale;
283: }
284:
285: public LocalDispatcher getDispatcher() {
286: return this .dispatcher;
287: }
288:
289: public GenericDelegator getDelegator() {
290: return this .delegator;
291: }
292:
293: public Security getSecurity() {
294: return this .security;
295: }
296:
297: public HttpServletRequest getRequest() {
298: return this .request;
299: }
300:
301: public HttpServletResponse getResponse() {
302: return this .response;
303: }
304:
305: public GenericValue getUserLogin() {
306: return this .userLogin;
307: }
308:
309: public void setUserLogin(GenericValue userLogin,
310: String userLoginEnvName) {
311: this .userLogin = userLogin;
312: this .putEnv(userLoginEnvName, userLogin);
313: }
314:
315: public Object getResult(String key) {
316: return this .results.get(key);
317: }
318:
319: public void putResult(String key, Object value) {
320: this .results.put(key, value);
321: }
322:
323: public Map getResults() {
324: return this .results;
325: }
326:
327: /** Expands environment variables delimited with ${} */
328: public String expandString(String original) {
329: return FlexibleStringExpander.expandString(original, this.env);
330: }
331: }
|