001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Locale;
023: import java.util.Map;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.ofbiz.base.util.UtilHttp;
029: import org.ofbiz.base.util.collections.FlexibleMapAccessor;
030: import org.ofbiz.base.util.string.FlexibleStringExpander;
031: import org.ofbiz.entity.GenericDelegator;
032: import org.ofbiz.entity.GenericValue;
033: import org.ofbiz.minilang.SimpleMethod;
034: import org.ofbiz.security.Security;
035: import org.ofbiz.service.DispatchContext;
036: import org.ofbiz.service.LocalDispatcher;
037:
038: /**
039: * A single operation, does the specified operation on the given field
040: */
041: public class MethodContext {
042:
043: public static final int EVENT = 1;
044: public static final int SERVICE = 2;
045:
046: protected int methodType;
047:
048: protected Map env = new HashMap();
049: protected Map parameters;
050: protected Locale locale;
051: protected ClassLoader loader;
052: protected LocalDispatcher dispatcher;
053: protected GenericDelegator delegator;
054: protected Security security;
055: protected GenericValue userLogin;
056:
057: protected HttpServletRequest request = null;
058: protected HttpServletResponse response = null;
059:
060: protected Map results = null;
061: protected DispatchContext ctx;
062:
063: public MethodContext(HttpServletRequest request,
064: HttpServletResponse response, ClassLoader loader) {
065: this .methodType = MethodContext.EVENT;
066: this .parameters = UtilHttp.getParameterMap(request);
067: this .loader = loader;
068: this .request = request;
069: this .response = response;
070: this .locale = UtilHttp.getLocale(request);
071: this .dispatcher = (LocalDispatcher) request
072: .getAttribute("dispatcher");
073: this .delegator = (GenericDelegator) request
074: .getAttribute("delegator");
075: this .security = (Security) request.getAttribute("security");
076: this .userLogin = (GenericValue) request.getSession()
077: .getAttribute("userLogin");
078:
079: if (this .loader == null) {
080: try {
081: this .loader = Thread.currentThread()
082: .getContextClassLoader();
083: } catch (SecurityException e) {
084: this .loader = this .getClass().getClassLoader();
085: }
086: }
087: }
088:
089: public MethodContext(DispatchContext ctx, Map context,
090: ClassLoader loader) {
091: this .methodType = MethodContext.SERVICE;
092: this .parameters = context;
093: this .loader = loader;
094: this .locale = (Locale) context.get("locale");
095: this .dispatcher = ctx.getDispatcher();
096: this .delegator = ctx.getDelegator();
097: this .security = ctx.getSecurity();
098: this .results = new HashMap();
099: this .userLogin = (GenericValue) context.get("userLogin");
100:
101: if (this .loader == null) {
102: try {
103: this .loader = Thread.currentThread()
104: .getContextClassLoader();
105: } catch (SecurityException e) {
106: this .loader = this .getClass().getClassLoader();
107: }
108: }
109: }
110:
111: /**
112: * This is a very simple constructor which assumes the needed objects (dispatcher,
113: * delegator, security, request, response, etc) are in the context.
114: * Will result in calling method as a service or event, as specified.
115: */
116: public MethodContext(Map context, ClassLoader loader, int methodType) {
117: this .methodType = methodType;
118: this .parameters = context;
119: this .loader = loader;
120: this .locale = (Locale) context.get("locale");
121: this .dispatcher = (LocalDispatcher) context.get("dispatcher");
122: this .delegator = (GenericDelegator) context.get("delegator");
123: this .security = (Security) context.get("security");
124: this .userLogin = (GenericValue) context.get("userLogin");
125:
126: if (methodType == MethodContext.EVENT) {
127: this .request = (HttpServletRequest) context.get("request");
128: this .response = (HttpServletResponse) context
129: .get("response");
130: if (this .locale == null)
131: this .locale = UtilHttp.getLocale(request);
132:
133: //make sure the delegator and other objects are in place, getting from
134: // request if necessary; assumes this came through the ControlServlet
135: // or something similar
136: if (this .request != null) {
137: if (this .dispatcher == null)
138: this .dispatcher = (LocalDispatcher) this .request
139: .getAttribute("dispatcher");
140: if (this .delegator == null)
141: this .delegator = (GenericDelegator) this .request
142: .getAttribute("delegator");
143: if (this .security == null)
144: this .security = (Security) this .request
145: .getAttribute("security");
146: if (this .userLogin == null)
147: this .userLogin = (GenericValue) this .request
148: .getSession().getAttribute("userLogin");
149: }
150: } else if (methodType == MethodContext.SERVICE) {
151: this .results = new HashMap();
152: }
153:
154: if (this .loader == null) {
155: try {
156: this .loader = Thread.currentThread()
157: .getContextClassLoader();
158: } catch (SecurityException e) {
159: this .loader = this .getClass().getClassLoader();
160: }
161: }
162: }
163:
164: public void setErrorReturn(String errMsg, SimpleMethod simpleMethod) {
165: if (getMethodType() == MethodContext.EVENT) {
166: putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
167: putEnv(simpleMethod.getEventResponseCodeName(),
168: simpleMethod.getDefaultErrorCode());
169: } else if (getMethodType() == MethodContext.SERVICE) {
170: putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
171: putEnv(simpleMethod.getServiceResponseMessageName(),
172: simpleMethod.getDefaultErrorCode());
173: }
174: }
175:
176: public int getMethodType() {
177: return this .methodType;
178: }
179:
180: public Map getEnvMap() {
181: return this .env;
182: }
183:
184: /** Gets the named value from the environment. Supports the "." (dot) syntax to access Map members and the
185: * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other
186: * environment values using the "${}" notation.
187: *
188: * @param key The name of the environment value to get. Can contain "." and "[]" syntax elements as described above.
189: * @return The environment value if found, otherwise null.
190: */
191: public Object getEnv(String key) {
192: String ekey = this .expandString(key);
193: FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
194: return this .getEnv(fma);
195: }
196:
197: public Object getEnv(FlexibleMapAccessor fma) {
198: return fma.get(this .env);
199: }
200:
201: /** Puts the named value in the environment. Supports the "." (dot) syntax to access Map members and the
202: * "[]" (bracket) syntax to access List entries.
203: * If the brackets for a list are empty the value will be appended to end of the list,
204: * otherwise the value will be set in the position of the number in the brackets.
205: * If a "+" (plus sign) is included inside the square brackets before the index
206: * number the value will inserted/added at that index instead of set at that index.
207: * This value is expanded, supporting the insertion of other
208: * environment values using the "${}" notation.
209: *
210: * @param key The name of the environment value to get. Can contain "." syntax elements as described above.
211: * @param value The value to set in the named environment location.
212: */
213: public void putEnv(String key, Object value) {
214: String ekey = this .expandString(key);
215: FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
216: this .putEnv(fma, value);
217: }
218:
219: public void putEnv(FlexibleMapAccessor fma, Object value) {
220: fma.put(this .env, value);
221: }
222:
223: /** Calls putEnv for each entry in the Map, thus allowing for the additional flexibility in naming
224: * supported in that method.
225: */
226: public void putAllEnv(Map values) {
227: Iterator viter = values.entrySet().iterator();
228: while (viter.hasNext()) {
229: Map.Entry entry = (Map.Entry) viter.next();
230: this .putEnv((String) entry.getKey(), entry.getValue());
231: }
232: }
233:
234: /** Removes the named value from the environment. Supports the "." (dot) syntax to access Map members and the
235: * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other
236: * environment values using the "${}" notation.
237: *
238: * @param key The name of the environment value to get. Can contain "." syntax elements as described above.
239: */
240: public Object removeEnv(String key) {
241: String ekey = this .expandString(key);
242: FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey);
243: return this .removeEnv(fma);
244: }
245:
246: public Object removeEnv(FlexibleMapAccessor fma) {
247: return fma.remove(this .env);
248: }
249:
250: public Iterator getEnvEntryIterator() {
251: return this .env.entrySet().iterator();
252: }
253:
254: public Object getParameter(String key) {
255: return this .parameters.get(key);
256: }
257:
258: public void putParameter(String key, Object value) {
259: this .parameters.put(key, value);
260: }
261:
262: public Map getParameters() {
263: return this .parameters;
264: }
265:
266: public ClassLoader getLoader() {
267: return this .loader;
268: }
269:
270: public Locale getLocale() {
271: return this .locale;
272: }
273:
274: public LocalDispatcher getDispatcher() {
275: return this .dispatcher;
276: }
277:
278: public GenericDelegator getDelegator() {
279: return this .delegator;
280: }
281:
282: public Security getSecurity() {
283: return this .security;
284: }
285:
286: public HttpServletRequest getRequest() {
287: return this .request;
288: }
289:
290: public HttpServletResponse getResponse() {
291: return this .response;
292: }
293:
294: public GenericValue getUserLogin() {
295: return this .userLogin;
296: }
297:
298: public void setUserLogin(GenericValue userLogin,
299: String userLoginEnvName) {
300: this .userLogin = userLogin;
301: this .putEnv(userLoginEnvName, userLogin);
302: }
303:
304: public Object getResult(String key) {
305: return this .results.get(key);
306: }
307:
308: public void putResult(String key, Object value) {
309: this .results.put(key, value);
310: }
311:
312: public Map getResults() {
313: return this .results;
314: }
315:
316: /** Expands environment variables delimited with ${} */
317: public String expandString(String original) {
318: return FlexibleStringExpander.expandString(original, this .env);
319: }
320:
321: public String expandString(FlexibleStringExpander originalExdr) {
322: return originalExdr.expandString(this.env);
323: }
324: }
|