1 24 package org.ofbiz.minilang.method; 25 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 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 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 env = new HashMap (); 60 protected Map parameters; 61 protected Locale locale; 62 protected ClassLoader loader; 63 protected LocalDispatcher dispatcher; 64 protected GenericDelegator delegator; 65 protected Security security; 66 protected GenericValue userLogin; 67 68 protected HttpServletRequest request = null; 69 protected HttpServletResponse response = null; 70 71 protected Map results = null; 72 protected DispatchContext ctx; 73 74 public MethodContext(HttpServletRequest request, HttpServletResponse response, ClassLoader 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 e) { 90 this.loader = this.getClass().getClassLoader(); 91 } 92 } 93 } 94 95 public MethodContext(DispatchContext ctx, Map context, ClassLoader loader) { 96 this.methodType = MethodContext.SERVICE; 97 this.parameters = context; 98 this.loader = loader; 99 this.locale = (Locale ) context.get("locale"); 100 this.dispatcher = ctx.getDispatcher(); 101 this.delegator = ctx.getDelegator(); 102 this.security = ctx.getSecurity(); 103 this.results = new HashMap (); 104 this.userLogin = (GenericValue) context.get("userLogin"); 105 106 if (this.loader == null) { 107 try { 108 this.loader = Thread.currentThread().getContextClassLoader(); 109 } catch (SecurityException e) { 110 this.loader = this.getClass().getClassLoader(); 111 } 112 } 113 } 114 115 120 public MethodContext(Map context, ClassLoader loader, int methodType) { 121 this.methodType = methodType; 122 this.parameters = context; 123 this.loader = loader; 124 this.locale = (Locale ) 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 ) context.get("request"); 132 this.response = (HttpServletResponse ) context.get("response"); 133 if (this.locale == null) this.locale = UtilHttp.getLocale(request); 134 135 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 (); 146 } 147 148 if (this.loader == null) { 149 try { 150 this.loader = Thread.currentThread().getContextClassLoader(); 151 } catch (SecurityException e) { 152 this.loader = this.getClass().getClassLoader(); 153 } 154 } 155 } 156 157 public void setErrorReturn(String 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 getEnvMap() { 172 return this.env; 173 } 174 175 182 public Object getEnv(String key) { 183 String ekey = this.expandString(key); 184 FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey); 185 return this.getEnv(fma); 186 } 187 public Object getEnv(FlexibleMapAccessor fma) { 188 return fma.get(this.env); 189 } 190 191 203 public void putEnv(String key, Object value) { 204 String ekey = this.expandString(key); 205 FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey); 206 this.putEnv(fma, value); 207 } 208 public void putEnv(FlexibleMapAccessor fma, Object value) { 209 fma.put(this.env, value); 210 } 211 212 215 public void putAllEnv(Map values) { 216 Iterator viter = values.entrySet().iterator(); 217 while (viter.hasNext()) { 218 Map.Entry entry = (Map.Entry ) viter.next(); 219 this.putEnv((String ) entry.getKey(), entry.getValue()); 220 } 221 } 222 223 229 public Object removeEnv(String key) { 230 String ekey = this.expandString(key); 231 FlexibleMapAccessor fma = new FlexibleMapAccessor(ekey); 232 return this.removeEnv(fma); 233 } 234 public Object removeEnv(FlexibleMapAccessor fma) { 235 return fma.remove(this.env); 236 } 237 238 public Iterator getEnvEntryIterator() { 239 return this.env.entrySet().iterator(); 240 } 241 242 public Object getParameter(String key) { 243 return this.parameters.get(key); 244 } 245 246 public void putParameter(String key, Object value) { 247 this.parameters.put(key, value); 248 } 249 250 public Map getParameters() { 251 return this.parameters; 252 } 253 254 public ClassLoader getLoader() { 255 return this.loader; 256 } 257 258 public Locale 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 getRequest() { 275 return this.request; 276 } 277 278 public HttpServletResponse getResponse() { 279 return this.response; 280 } 281 282 public GenericValue getUserLogin() { 283 return this.userLogin; 284 } 285 286 public void setUserLogin(GenericValue userLogin, String userLoginEnvName) { 287 this.userLogin = userLogin; 288 this.putEnv(userLoginEnvName, userLogin); 289 } 290 291 public Object getResult(String key) { 292 return this.results.get(key); 293 } 294 295 public void putResult(String key, Object value) { 296 this.results.put(key, value); 297 } 298 299 public Map getResults() { 300 return this.results; 301 } 302 303 304 public String expandString(String original) { 305 return FlexibleStringExpander.expandString(original, this.env); 306 } 307 308 public String expandString(FlexibleStringExpander originalExdr) { 309 return originalExdr.expandString(this.env); 310 } 311 } 312 | Popular Tags |