|
Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java Thu Apr 19 15:09:03 2012 @@ -25,14 +25,13 @@ import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.collections.FlexibleMapAccessor; /** - * Used to flexibly access Map values, supporting the "." (dot) syntax for - * accessing sub-map values and the "[]" (square bracket) syntax for accessing - * list elements. See individual Map operations for more information. + * Used to flexibly access Map values, supporting the "." (dot) syntax for accessing sub-map values and the "[]" (square bracket) syntax for accessing list elements. See individual Map operations for + * more information. */ public class ContextAccessor<T> { - protected String name; protected FlexibleMapAccessor<T> fma; + protected String name; public ContextAccessor(String name) { init(name); @@ -46,77 +45,73 @@ public class ContextAccessor<T> { } } - protected void init(String name) { - this.name = name; - this.fma = FlexibleMapAccessor.getInstance(name); + /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */ + @Override + public boolean equals(Object obj) { + if (obj instanceof ContextAccessor<?>) { + ContextAccessor<?> contextAccessor = (ContextAccessor<?>) obj; + if (this.name == null) { + return contextAccessor.name == null; + } + return this.name.equals(contextAccessor.name); + } else { + String str = (String) obj; + if (this.name == null) { + return str == null; + } + return this.name.equals(str); + } } - public boolean isEmpty() { - return this.fma.isEmpty(); + /** Based on name get from Map or from List in Map */ + public T get(Map<String, ? extends Object> theMap, MethodContext methodContext) { + return fma.get(theMap); } /** Based on name get from Map or from List in Map */ public T get(MethodContext methodContext) { - return UtilGenerics.<T>cast(methodContext.getEnv(fma)); + return UtilGenerics.<T> cast(methodContext.getEnv(fma)); } - /** Based on name put in Map or from List in Map; - * If the brackets for a list are empty the value will be appended to the list, - * otherwise the value will be set in the position of the number in the brackets. - * If a "+" (plus sign) is included inside the square brackets before the index - * number the value will inserted/added at that point instead of set at the point. - */ - public void put(MethodContext methodContext, T value) { - methodContext.putEnv(fma, value); + /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */ + @Override + public int hashCode() { + return this.name.hashCode(); } - /** Based on name remove from Map or from List in Map */ - public T remove(MethodContext methodContext) { - return UtilGenerics.<T>cast(methodContext.removeEnv(fma)); + protected void init(String name) { + this.name = name; + this.fma = FlexibleMapAccessor.getInstance(name); } - /** Based on name get from Map or from List in Map */ - public T get(Map<String, ? extends Object> theMap, MethodContext methodContext) { - return fma.get(theMap); + public boolean isEmpty() { + return this.fma.isEmpty(); } - /** Based on name put in Map or from List in Map; - * If the brackets for a list are empty the value will be appended to the list, - * otherwise the value will be set in the position of the number in the brackets. - * If a "+" (plus sign) is included inside the square brackets before the index - * number the value will inserted/added at that point instead of set at the point. + /** + * Based on name put in Map or from List in Map; If the brackets for a list are empty the value will be appended to the list, otherwise the value will be set in the position of the number in the + * brackets. If a "+" (plus sign) is included inside the square brackets before the index number the value will inserted/added at that point instead of set at the point. */ public void put(Map<String, Object> theMap, T value, MethodContext methodContext) { fma.put(theMap, value); } + /** + * Based on name put in Map or from List in Map; If the brackets for a list are empty the value will be appended to the list, otherwise the value will be set in the position of the number in the + * brackets. If a "+" (plus sign) is included inside the square brackets before the index number the value will inserted/added at that point instead of set at the point. + */ + public void put(MethodContext methodContext, T value) { + methodContext.putEnv(fma, value); + } + /** Based on name remove from Map or from List in Map */ public T remove(Map<String, ? extends Object> theMap, MethodContext methodContext) { return fma.remove(theMap); } - /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */ - @Override - public int hashCode() { - return this.name.hashCode(); - } - - /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */ - @Override - public boolean equals(Object obj) { - if (obj instanceof ContextAccessor<?>) { - ContextAccessor<?> contextAccessor = (ContextAccessor<?>) obj; - if (this.name == null) { - return contextAccessor.name == null; - } - return this.name.equals(contextAccessor.name); - } else { - String str = (String) obj; - if (this.name == null) { - return str == null; - } - return this.name.equals(str); - } + /** Based on name remove from Map or from List in Map */ + public T remove(MethodContext methodContext) { + return UtilGenerics.<T> cast(methodContext.removeEnv(fma)); } /** To be used for a string representation of the accessor, returns the original name. */ Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldObject.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldObject.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldObject.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldObject.java Thu Apr 19 15:09:03 2012 @@ -18,11 +18,14 @@ *******************************************************************************/ package org.ofbiz.minilang.method; -import java.util.*; +import java.util.Map; -import org.w3c.dom.*; -import org.ofbiz.base.util.*; -import org.ofbiz.minilang.*; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.ObjectType; +import org.ofbiz.base.util.UtilGenerics; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.minilang.SimpleMethod; +import org.w3c.dom.Element; /** * A type of MethodObject that represents an Object value in a certain location @@ -40,33 +43,15 @@ public class FieldObject<T> extends Meth // the schema for this element now just has the "field" attribute, though the old "field-name" and "map-name" pair is still supported fieldAcsr = new ContextAccessor<T>(element.getAttribute("field"), element.getAttribute("field-name")); mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map-name")); - type = element.getAttribute("type"); if (UtilValidate.isEmpty(type)) { type = "String"; } } - /** Get the name for the type of the object */ - @Override - public String getTypeName() { - return type; - } - - @Override - public Class<T> getTypeClass(ClassLoader loader) { - try { - return UtilGenerics.cast(ObjectType.loadClass(type, loader)); - } catch (ClassNotFoundException e) { - Debug.logError(e, "Could not find class for type: " + type, module); - return null; - } - } - @Override public T getObject(MethodContext methodContext) { T fieldVal = null; - if (!mapAcsr.isEmpty()) { Map<String, ? extends Object> fromMap = mapAcsr.get(methodContext); if (fromMap == null) { @@ -78,12 +63,27 @@ public class FieldObject<T> extends Meth // no map name, try the env fieldVal = fieldAcsr.get(methodContext); } - if (fieldVal == null) { - if (Debug.infoOn()) Debug.logInfo("Field value not found with name " + fieldAcsr + " in Map with name " + mapAcsr + ", not getting Object value, returning null.", module); + if (Debug.infoOn()) + Debug.logInfo("Field value not found with name " + fieldAcsr + " in Map with name " + mapAcsr + ", not getting Object value, returning null.", module); return null; } - return fieldVal; } + + @Override + public Class<T> getTypeClass(ClassLoader loader) { + try { + return UtilGenerics.cast(ObjectType.loadClass(type, loader)); + } catch (ClassNotFoundException e) { + Debug.logError(e, "Could not find class for type: " + type, module); + return null; + } + } + + /** Get the name for the type of the object */ + @Override + public String getTypeName() { + return type; + } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldString.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldString.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldString.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/FieldString.java Thu Apr 19 15:09:03 2012 @@ -18,11 +18,11 @@ *******************************************************************************/ package org.ofbiz.minilang.method; -import java.util.*; +import java.util.Map; -import org.w3c.dom.*; -import org.ofbiz.base.util.*; -import org.ofbiz.minilang.*; +import org.ofbiz.base.util.Debug; +import org.ofbiz.minilang.SimpleMethod; +import org.w3c.dom.Element; /** * A type of MethodString that represents a String constant value @@ -56,7 +56,8 @@ public class FieldString extends MethodS } if (fieldVal == null) { - if (Debug.infoOn()) Debug.logInfo("Field value not found with name " + fieldAcsr + " in Map with name " + mapAcsr + ", not getting string value", module); + if (Debug.infoOn()) + Debug.logInfo("Field value not found with name " + fieldAcsr + " in Map with name " + mapAcsr + ", not getting string value", module); return ""; } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java Thu Apr 19 15:09:03 2012 @@ -28,8 +28,8 @@ import javax.servlet.http.HttpServletRes import javolution.util.FastMap; -import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.collections.FlexibleMapAccessor; import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.Delegator; @@ -48,47 +48,21 @@ public class MethodContext implements It public static final int EVENT = 1; public static final int SERVICE = 2; - protected int methodType; - + protected Authorization authz; + protected DispatchContext ctx; + protected Delegator delegator; + protected LocalDispatcher dispatcher; protected Map<String, Object> env = FastMap.newInstance(); - protected Map<String, Object> parameters; - protected Locale locale; - protected TimeZone timeZone; protected ClassLoader loader; - protected LocalDispatcher dispatcher; - protected Delegator delegator; - protected Authorization authz; - protected Security security; - protected GenericValue userLogin; - + protected Locale locale; + protected int methodType; + protected Map<String, Object> parameters; protected HttpServletRequest request = null; protected HttpServletResponse response = null; - protected Map<String, Object> results = null; - protected DispatchContext ctx; - - public MethodContext(HttpServletRequest request, HttpServletResponse response, ClassLoader loader) { - this.methodType = MethodContext.EVENT; - this.parameters = UtilHttp.getParameterMap(request); - this.loader = loader; - this.request = request; - this.response = response; - this.locale = UtilHttp.getLocale(request); - this.timeZone = UtilHttp.getTimeZone(request); - this.dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); - this.delegator = (Delegator) request.getAttribute("delegator"); - this.authz = (Authorization) request.getAttribute("authz"); - this.security = (Security) request.getAttribute("security"); - this.userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); - - if (this.loader == null) { - try { - this.loader = Thread.currentThread().getContextClassLoader(); - } catch (SecurityException e) { - this.loader = this.getClass().getClassLoader(); - } - } - } + protected Security security; + protected TimeZone timeZone; + protected GenericValue userLogin; public MethodContext(DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) { this.methodType = MethodContext.SERVICE; @@ -102,7 +76,28 @@ public class MethodContext implements It this.security = ctx.getSecurity(); this.results = FastMap.newInstance(); this.userLogin = (GenericValue) context.get("userLogin"); + if (this.loader == null) { + try { + this.loader = Thread.currentThread().getContextClassLoader(); + } catch (SecurityException e) { + this.loader = this.getClass().getClassLoader(); + } + } + } + public MethodContext(HttpServletRequest request, HttpServletResponse response, ClassLoader loader) { + this.methodType = MethodContext.EVENT; + this.parameters = UtilHttp.getParameterMap(request); + this.loader = loader; + this.request = request; + this.response = response; + this.locale = UtilHttp.getLocale(request); + this.timeZone = UtilHttp.getTimeZone(request); + this.dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); + this.delegator = (Delegator) request.getAttribute("delegator"); + this.authz = (Authorization) request.getAttribute("authz"); + this.security = (Security) request.getAttribute("security"); + this.userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); if (this.loader == null) { try { this.loader = Thread.currentThread().getContextClassLoader(); @@ -113,9 +108,8 @@ public class MethodContext implements It } /** - * This is a very simple constructor which assumes the needed objects (dispatcher, - * delegator, authz, security, request, response, etc) are in the context. - * Will result in calling method as a service or event, as specified. + * This is a very simple constructor which assumes the needed objects (dispatcher, delegator, authz, security, request, response, etc) are in the context. Will result in calling method as a + * service or event, as specified. */ public MethodContext(Map<String, ? extends Object> context, ClassLoader loader, int methodType) { this.methodType = methodType; @@ -128,27 +122,31 @@ public class MethodContext implements It this.authz = (Authorization) context.get("authz"); this.security = (Security) context.get("security"); this.userLogin = (GenericValue) context.get("userLogin"); - if (methodType == MethodContext.EVENT) { this.request = (HttpServletRequest) context.get("request"); this.response = (HttpServletResponse) context.get("response"); - if (this.locale == null) this.locale = UtilHttp.getLocale(request); - if (this.timeZone == null) this.timeZone = UtilHttp.getTimeZone(request); - - //make sure the delegator and other objects are in place, getting from + if (this.locale == null) + this.locale = UtilHttp.getLocale(request); + if (this.timeZone == null) + this.timeZone = UtilHttp.getTimeZone(request); + // make sure the delegator and other objects are in place, getting from // request if necessary; assumes this came through the ControlServlet // or something similar if (this.request != null) { - if (this.dispatcher == null) this.dispatcher = (LocalDispatcher) this.request.getAttribute("dispatcher"); - if (this.delegator == null) this.delegator = (Delegator) this.request.getAttribute("delegator"); - if (this.authz == null) this.authz = (Authorization) this.request.getAttribute("authz"); - if (this.security == null) this.security = (Security) this.request.getAttribute("security"); - if (this.userLogin == null) this.userLogin = (GenericValue) this.request.getSession().getAttribute("userLogin"); + if (this.dispatcher == null) + this.dispatcher = (LocalDispatcher) this.request.getAttribute("dispatcher"); + if (this.delegator == null) + this.delegator = (Delegator) this.request.getAttribute("delegator"); + if (this.authz == null) + this.authz = (Authorization) this.request.getAttribute("authz"); + if (this.security == null) + this.security = (Security) this.request.getAttribute("security"); + if (this.userLogin == null) + this.userLogin = (GenericValue) this.request.getSession().getAttribute("userLogin"); } } else if (methodType == MethodContext.SERVICE) { this.results = FastMap.newInstance(); } - if (this.loader == null) { try { this.loader = Thread.currentThread().getContextClassLoader(); @@ -158,168 +156,171 @@ public class MethodContext implements It } } - public void setErrorReturn(String errMsg, SimpleMethod simpleMethod) { - if (getMethodType() == MethodContext.EVENT) { - putEnv(simpleMethod.getEventErrorMessageName(), errMsg); - putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); - } else if (getMethodType() == MethodContext.SERVICE) { - putEnv(simpleMethod.getServiceErrorMessageName(), errMsg); - putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); - } + public String expandString(FlexibleStringExpander originalExdr) { + return originalExdr.expandString(this.env); } - public int getMethodType() { - return this.methodType; + /** Expands environment variables delimited with ${} */ + public String expandString(String original) { + return FlexibleStringExpander.expandString(original, this.env); } - public Map<String, Object> getEnvMap() { - return this.env; + public Authorization getAuthz() { + return this.authz; } - /** Gets the named value from the environment. Supports the "." (dot) syntax to access Map members and the - * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other - * environment values using the "${}" notation. - * - * @param key The name of the environment value to get. Can contain "." and "[]" syntax elements as described above. - * @return The environment value if found, otherwise null. - */ - public <T> T getEnv(String key) { - String ekey = this.expandString(key); - FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey); - return this.getEnv(fma); + public Delegator getDelegator() { + return this.delegator; + } + + public LocalDispatcher getDispatcher() { + return this.dispatcher; } + public <T> T getEnv(FlexibleMapAccessor<T> fma) { return fma.get(this.env); } - /** Puts the named value in the environment. Supports the "." (dot) syntax to access Map members and the - * "[]" (bracket) syntax to access List entries. - * If the brackets for a list are empty the value will be appended to end of the list, - * otherwise the value will be set in the position of the number in the brackets. - * If a "+" (plus sign) is included inside the square brackets before the index - * number the value will inserted/added at that index instead of set at that index. - * This value is expanded, supporting the insertion of other - * environment values using the "${}" notation. - * - * @param key The name of the environment value to get. Can contain "." syntax elements as described above. - * @param value The value to set in the named environment location. + /** + * Gets the named value from the environment. Supports the "." (dot) syntax to access Map members and the "[]" (bracket) syntax to access List entries. This value is expanded, supporting the + * insertion of other environment values using the "${}" notation. + * + * @param key + * The name of the environment value to get. Can contain "." and "[]" syntax elements as described above. + * @return The environment value if found, otherwise null. */ - public <T> void putEnv(String key, T value) { + public <T> T getEnv(String key) { String ekey = this.expandString(key); FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey); - this.putEnv(fma, value); - } - public <T> void putEnv(FlexibleMapAccessor<T> fma, T value) { - fma.put(this.env, value); + return this.getEnv(fma); } - /** Calls putEnv for each entry in the Map, thus allowing for the additional flexibility in naming - * supported in that method. - */ - public void putAllEnv(Map<String, ? extends Object> values) { - for (Map.Entry<String, ? extends Object> entry: values.entrySet()) { - this.putEnv(entry.getKey(), entry.getValue()); - } + public Iterator<Map.Entry<String, Object>> getEnvEntryIterator() { + return this.env.entrySet().iterator(); } - /** Removes the named value from the environment. Supports the "." (dot) syntax to access Map members and the - * "[]" (bracket) syntax to access List entries. This value is expanded, supporting the insertion of other - * environment values using the "${}" notation. - * - * @param key The name of the environment value to get. Can contain "." syntax elements as described above. - */ - public <T> T removeEnv(String key) { - String ekey = this.expandString(key); - FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey); - return this.removeEnv(fma); + public Map<String, Object> getEnvMap() { + return this.env; } - public <T> T removeEnv(FlexibleMapAccessor<T> fma) { - return fma.remove(this.env); + + public ClassLoader getLoader() { + return this.loader; } - public Iterator<Map.Entry<String, Object>> iterator() { - return this.env.entrySet().iterator(); + public Locale getLocale() { + return this.locale; } - public Iterator<Map.Entry<String, Object>> getEnvEntryIterator() { - return this.env.entrySet().iterator(); + public int getMethodType() { + return this.methodType; } public Object getParameter(String key) { return this.parameters.get(key); } - public void putParameter(String key, Object value) { - this.parameters.put(key, value); - } - public Map<String, Object> getParameters() { return this.parameters; } - public ClassLoader getLoader() { - return this.loader; + public HttpServletRequest getRequest() { + return this.request; } - public Locale getLocale() { - return this.locale; + public HttpServletResponse getResponse() { + return this.response; } - public TimeZone getTimeZone() { - return this.timeZone; + public Object getResult(String key) { + return this.results.get(key); } - public LocalDispatcher getDispatcher() { - return this.dispatcher; + public Map<String, Object> getResults() { + return this.results; } - public Delegator getDelegator() { - return this.delegator; + public Security getSecurity() { + return this.security; } - public Authorization getAuthz() { - return this.authz; + public TimeZone getTimeZone() { + return this.timeZone; } - public Security getSecurity() { - return this.security; + public GenericValue getUserLogin() { + return this.userLogin; } - public HttpServletRequest getRequest() { - return this.request; + public Iterator<Map.Entry<String, Object>> iterator() { + return this.env.entrySet().iterator(); } - public HttpServletResponse getResponse() { - return this.response; + /** + * Calls putEnv for each entry in the Map, thus allowing for the additional flexibility in naming supported in that method. + */ + public void putAllEnv(Map<String, ? extends Object> values) { + for (Map.Entry<String, ? extends Object> entry : values.entrySet()) { + this.putEnv(entry.getKey(), entry.getValue()); + } } - public GenericValue getUserLogin() { - return this.userLogin; + public <T> void putEnv(FlexibleMapAccessor<T> fma, T value) { + fma.put(this.env, value); } - public void setUserLogin(GenericValue userLogin, String userLoginEnvName) { - this.userLogin = userLogin; - this.putEnv(userLoginEnvName, userLogin); + /** + * Puts the named value in the environment. Supports the "." (dot) syntax to access Map members and the "[]" (bracket) syntax to access List entries. If the brackets for a list are empty the value + * will be appended to end of the list, otherwise the value will be set in the position of the number in the brackets. If a "+" (plus sign) is included inside the square brackets before the index + * number the value will inserted/added at that index instead of set at that index. This value is expanded, supporting the insertion of other environment values using the "${}" notation. + * + * @param key + * The name of the environment value to get. Can contain "." syntax elements as described above. + * @param value + * The value to set in the named environment location. + */ + public <T> void putEnv(String key, T value) { + String ekey = this.expandString(key); + FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey); + this.putEnv(fma, value); } - public Object getResult(String key) { - return this.results.get(key); + public void putParameter(String key, Object value) { + this.parameters.put(key, value); } public void putResult(String key, Object value) { this.results.put(key, value); } - public Map<String, Object> getResults() { - return this.results; + public <T> T removeEnv(FlexibleMapAccessor<T> fma) { + return fma.remove(this.env); } - /** Expands environment variables delimited with ${} */ - public String expandString(String original) { - return FlexibleStringExpander.expandString(original, this.env); + /** + * Removes the named value from the environment. Supports the "." (dot) syntax to access Map members and the "[]" (bracket) syntax to access List entries. This value is expanded, supporting the + * insertion of other environment values using the "${}" notation. + * + * @param key + * The name of the environment value to get. Can contain "." syntax elements as described above. + */ + public <T> T removeEnv(String key) { + String ekey = this.expandString(key); + FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey); + return this.removeEnv(fma); } - public String expandString(FlexibleStringExpander originalExdr) { - return originalExdr.expandString(this.env); + public void setErrorReturn(String errMsg, SimpleMethod simpleMethod) { + if (getMethodType() == MethodContext.EVENT) { + putEnv(simpleMethod.getEventErrorMessageName(), errMsg); + putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); + } else if (getMethodType() == MethodContext.SERVICE) { + putEnv(simpleMethod.getServiceErrorMessageName(), errMsg); + putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); + } + } + + public void setUserLogin(GenericValue userLogin, String userLoginEnvName) { + this.userLogin = userLogin; + this.putEnv(userLoginEnvName, userLogin); } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodObject.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodObject.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodObject.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodObject.java Thu Apr 19 15:09:03 2012 @@ -18,8 +18,8 @@ *******************************************************************************/ package org.ofbiz.minilang.method; -import org.w3c.dom.*; -import org.ofbiz.minilang.*; +import org.ofbiz.minilang.SimpleMethod; +import org.w3c.dom.Element; /** * A single Object value to be used as a parameter or whatever @@ -32,12 +32,12 @@ public abstract class MethodObject<T> { this.simpleMethod = simpleMethod; } - /** Get the name for the type of the object */ - public abstract String getTypeName(); + /** Get the Object value */ + public abstract T getObject(MethodContext methodContext); /** Get the Class for the type of the object */ public abstract Class<T> getTypeClass(ClassLoader loader); - /** Get the Object value */ - public abstract T getObject(MethodContext methodContext); + /** Get the name for the type of the object */ + public abstract String getTypeName(); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java Thu Apr 19 15:09:03 2012 @@ -23,24 +23,13 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.w3c.dom.*; - -import org.ofbiz.minilang.*; +import org.ofbiz.minilang.SimpleMethod; +import org.w3c.dom.Element; /** * A single operation, does the specified operation on the given field */ public abstract class MethodOperation { - public interface Factory<M extends MethodOperation> { - M createMethodOperation(Element element, SimpleMethod simpleMethod); - String getName(); - } - - @Retention(RetentionPolicy.RUNTIME) - @Target(ElementType.TYPE) - public @interface DeprecatedOperation { - String value(); - } protected SimpleMethod simpleMethod; @@ -48,15 +37,29 @@ public abstract class MethodOperation { this.simpleMethod = simpleMethod; } + /** Execute the operation; if false is returned then no further operations will be executed */ + public abstract boolean exec(MethodContext methodContext); + + /** Create an expanded string representation of the operation, is for the current context */ + public abstract String expandedString(MethodContext methodContext); + public SimpleMethod getSimpleMethod() { return this.simpleMethod; } - /** Execute the operation; if false is returned then no further operations will be executed */ - public abstract boolean exec(MethodContext methodContext); - /** Create a raw string representation of the operation, would be similar to original XML */ public abstract String rawString(); - /** Create an expanded string representation of the operation, is for the current context */ - public abstract String expandedString(MethodContext methodContext); + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface DeprecatedOperation { + String value(); + } + + public interface Factory<M extends MethodOperation> { + + M createMethodOperation(Element element, SimpleMethod simpleMethod); + + String getName(); + } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodString.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodString.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodString.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodString.java Thu Apr 19 15:09:03 2012 @@ -18,8 +18,8 @@ *******************************************************************************/ package org.ofbiz.minilang.method; -import org.w3c.dom.*; -import org.ofbiz.minilang.*; +import org.ofbiz.minilang.SimpleMethod; +import org.w3c.dom.Element; /** * A single String value, generally meant to be combined with others Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringObject.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringObject.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringObject.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringObject.java Thu Apr 19 15:09:03 2012 @@ -18,18 +18,18 @@ *******************************************************************************/ package org.ofbiz.minilang.method; -import org.w3c.dom.*; - -import org.ofbiz.base.util.*; -import org.ofbiz.minilang.*; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.SimpleMethod; +import org.w3c.dom.Element; /** * A type of MethodObject that represents a String constant value to be used as an Object */ public class StringObject extends MethodObject<String> { - String value; String cdataValue; + String value; public StringObject(Element element, SimpleMethod simpleMethod) { super(element, simpleMethod); @@ -37,25 +37,12 @@ public class StringObject extends Method cdataValue = UtilXml.elementValue(element); } - /** Get the name for the type of the object */ - @Override - public String getTypeName() { - return "java.lang.String"; - } - - @Override - public Class<String> getTypeClass(ClassLoader loader) { - return java.lang.String.class; - } - @Override public String getObject(MethodContext methodContext) { String value = methodContext.expandString(this.value); String cdataValue = methodContext.expandString(this.cdataValue); - boolean valueExists = UtilValidate.isNotEmpty(value); boolean cdataValueExists = UtilValidate.isNotEmpty(cdataValue); - if (valueExists && cdataValueExists) { return value + cdataValue; } else { @@ -66,4 +53,15 @@ public class StringObject extends Method } } } + + @Override + public Class<String> getTypeClass(ClassLoader loader) { + return java.lang.String.class; + } + + /** Get the name for the type of the object */ + @Override + public String getTypeName() { + return "java.lang.String"; + } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringString.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringString.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringString.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/StringString.java Thu Apr 19 15:09:03 2012 @@ -18,17 +18,18 @@ *******************************************************************************/ package org.ofbiz.minilang.method; -import org.w3c.dom.*; -import org.ofbiz.base.util.*; -import org.ofbiz.minilang.*; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.SimpleMethod; +import org.w3c.dom.Element; /** * A type of MethodString that represents a String constant value */ public class StringString extends MethodString { - String value; String cdataValue; + String value; public StringString(Element element, SimpleMethod simpleMethod) { super(element, simpleMethod); @@ -40,10 +41,8 @@ public class StringString extends Method public String getString(MethodContext methodContext) { String value = methodContext.expandString(this.value); String cdataValue = methodContext.expandString(this.cdataValue); - boolean valueExists = UtilValidate.isNotEmpty(value); boolean cdataValueExists = UtilValidate.isNotEmpty(cdataValue); - if (valueExists && cdataValueExists) { return value + cdataValue; } else { Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java Thu Apr 19 15:09:03 2012 @@ -18,41 +18,34 @@ *******************************************************************************/ package org.ofbiz.minilang.method.callops; -import java.util.*; +import java.util.List; import javolution.util.FastList; -import org.w3c.dom.*; -import org.ofbiz.base.util.*; -import org.ofbiz.minilang.*; -import org.ofbiz.minilang.method.*; +import org.ofbiz.base.util.UtilProperties; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.SimpleMethod; +import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.method.MethodContext; +import org.ofbiz.minilang.method.MethodOperation; +import org.w3c.dom.Element; /** * Adds the fail-message or fail-property value to the error-list. */ public class AddError extends MethodOperation { - public static final class AddErrorFactory implements Factory<AddError> { - public AddError createMethodOperation(Element element, SimpleMethod simpleMethod) { - return new AddError(element, simpleMethod); - } - public String getName() { - return "add-error"; - } - } + ContextAccessor<List<Object>> errorListAcsr; + boolean isProperty = false; String message = null; String propertyResource = null; - boolean isProperty = false; - - ContextAccessor<List<Object>> errorListAcsr; public AddError(Element element, SimpleMethod simpleMethod) { super(element, simpleMethod); errorListAcsr = new ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), "error_list"); - Element failMessage = UtilXml.firstChildElement(element, "fail-message"); Element failProperty = UtilXml.firstChildElement(element, "fail-property"); - if (failMessage != null) { this.message = failMessage.getAttribute("message"); this.isProperty = false; @@ -63,50 +56,53 @@ public class AddError extends MethodOper } } - @Override - public boolean exec(MethodContext methodContext) { - - List<Object> messages = errorListAcsr.get(methodContext); - if (messages == null) { - messages = FastList.newInstance(); - errorListAcsr.put(methodContext, messages); - } - - this.addMessage(messages, methodContext.getLoader(), methodContext); - return true; - } - public void addMessage(List<Object> messages, ClassLoader loader, MethodContext methodContext) { String message = methodContext.expandString(this.message); String propertyResource = methodContext.expandString(this.propertyResource); - if (!isProperty && message != null) { messages.add(message); - // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] Adding message: " + message, module); } else if (isProperty && propertyResource != null && message != null) { - //String propMsg = UtilProperties.getPropertyValue(UtilURL.fromResource(propertyResource, loader), message); String propMsg = UtilProperties.getMessage(propertyResource, message, methodContext.getEnvMap(), methodContext.getLocale()); - if (UtilValidate.isEmpty(propMsg)) { messages.add("Simple Method error occurred, but no message was found, sorry."); } else { messages.add(methodContext.expandString(propMsg)); } - // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] Adding property message: " + propMsg, module); } else { messages.add("Simple Method error occurred, but no message was found, sorry."); - // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] ERROR: No message found", module); } } @Override - public String rawString() { - // TODO: something more than the empty tag - return "<add-error/>"; + public boolean exec(MethodContext methodContext) { + List<Object> messages = errorListAcsr.get(methodContext); + if (messages == null) { + messages = FastList.newInstance(); + errorListAcsr.put(methodContext, messages); + } + this.addMessage(messages, methodContext.getLoader(), methodContext); + return true; } + @Override public String expandedString(MethodContext methodContext) { // TODO: something more than a stub/dummy return this.rawString(); } + + @Override + public String rawString() { + // TODO: something more than the empty tag + return "<add-error/>"; + } + + public static final class AddErrorFactory implements Factory<AddError> { + public AddError createMethodOperation(Element element, SimpleMethod simpleMethod) { + return new AddError(element, simpleMethod); + } + + public String getName() { + return "add-error"; + } + } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java Thu Apr 19 15:09:03 2012 @@ -18,68 +18,64 @@ *******************************************************************************/ package org.ofbiz.minilang.method.callops; -import java.io.*; -import java.util.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.List; +import java.util.Map; import javolution.util.FastList; -import org.w3c.dom.*; -import org.ofbiz.base.util.*; -import org.ofbiz.minilang.*; -import org.ofbiz.minilang.method.*; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilGenerics; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.SimpleMethod; +import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.method.MethodContext; +import org.ofbiz.minilang.method.MethodOperation; +import org.w3c.dom.Element; -import bsh.*; +import bsh.EvalError; +import bsh.Interpreter; /** * Simple class to wrap messages that come either from a straight string or a properties file */ public class CallBsh extends MethodOperation { - public static final class CallBshFactory implements Factory<CallBsh> { - public CallBsh createMethodOperation(Element element, SimpleMethod simpleMethod) { - return new CallBsh(element, simpleMethod); - } - - public String getName() { - return "call-bsh"; - } - } public static final String module = CallBsh.class.getName(); - public static final int bufferLength = 4096; + ContextAccessor<List<Object>> errorListAcsr; String inline = null; String resource = null; - ContextAccessor<List<Object>> errorListAcsr; public CallBsh(Element element, SimpleMethod simpleMethod) { super(element, simpleMethod); inline = UtilXml.elementValue(element); resource = element.getAttribute("resource"); errorListAcsr = new ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), "error_list"); - - if (UtilValidate.isNotEmpty(inline)) {// pre-parse/compile inlined bsh, only accessed here + if (UtilValidate.isNotEmpty(inline)) { + // pre-parse/compile inlined bsh, only accessed here } } @Override public boolean exec(MethodContext methodContext) { List<Object> messages = errorListAcsr.get(methodContext); - if (messages == null) { messages = FastList.newInstance(); errorListAcsr.put(methodContext, messages); } - Interpreter bsh = new Interpreter(); bsh.setClassLoader(methodContext.getLoader()); - try { // setup environment - for (Map.Entry<String, Object> entry: methodContext) { + for (Map.Entry<String, Object> entry : methodContext) { bsh.set(entry.getKey(), entry.getValue()); } - // run external, from resource, first if resource specified if (UtilValidate.isNotEmpty(resource)) { String resource = methodContext.expandString(this.resource); @@ -92,19 +88,15 @@ public class CallBsh extends MethodOpera try { reader = new BufferedReader(new InputStreamReader(is)); StringBuilder outSb = new StringBuilder(); - String tempStr = null; - while ((tempStr = reader.readLine()) != null) { outSb.append(tempStr); outSb.append('\n'); } - Object resourceResult = bsh.eval(outSb.toString()); - // if map is returned, copy values into env if ((resourceResult != null) && (resourceResult instanceof Map<?, ?>)) { - methodContext.putAllEnv(UtilGenerics.<String, Object>checkMap(resourceResult)); + methodContext.putAllEnv(UtilGenerics.<String, Object> checkMap(resourceResult)); } } catch (IOException e) { messages.add("IO error loading bsh resource: " + e.getMessage()); @@ -119,33 +111,43 @@ public class CallBsh extends MethodOpera } } } - - if (Debug.verboseOn()) Debug.logVerbose("Running inline BSH script: " + inline, module); + if (Debug.verboseOn()) + Debug.logVerbose("Running inline BSH script: " + inline, module); // run inlined second to it can override the one from the property Object inlineResult = bsh.eval(inline); - if (Debug.verboseOn()) Debug.logVerbose("Result of inline BSH script: " + inlineResult, module); - + if (Debug.verboseOn()) + Debug.logVerbose("Result of inline BSH script: " + inlineResult, module); // if map is returned, copy values into env if ((inlineResult != null) && (inlineResult instanceof Map<?, ?>)) { - methodContext.putAllEnv(UtilGenerics.<String, Object>checkMap(inlineResult)); + methodContext.putAllEnv(UtilGenerics.<String, Object> checkMap(inlineResult)); } } catch (EvalError e) { Debug.logError(e, "BeanShell execution caused an error", module); messages.add("BeanShell execution caused an error: " + e.getMessage()); } - // always return true, error messages just go on the error list return true; } @Override + public String expandedString(MethodContext methodContext) { + // TODO: something more than a stub/dummy + return this.rawString(); + } + + @Override public String rawString() { // TODO: something more than the empty tag return "<call-bsh/>"; } - @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + + public static final class CallBshFactory implements Factory<CallBsh> { + public CallBsh createMethodOperation(Element element, SimpleMethod simpleMethod) { + return new CallBsh(element, simpleMethod); + } + + public String getName() { + return "call-bsh"; + } } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallClassMethod.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallClassMethod.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallClassMethod.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallClassMethod.java Thu Apr 19 15:09:03 2012 @@ -19,60 +19,56 @@ package org.ofbiz.minilang.method.callops; -import java.util.*; +import java.util.List; +import java.util.Map; import javolution.util.FastList; -import org.w3c.dom.*; -import org.ofbiz.base.util.*; - -import org.ofbiz.minilang.*; -import org.ofbiz.minilang.method.*; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.ObjectType; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.SimpleMethod; +import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.method.FieldObject; +import org.ofbiz.minilang.method.MethodContext; +import org.ofbiz.minilang.method.MethodObject; +import org.ofbiz.minilang.method.MethodOperation; +import org.ofbiz.minilang.method.StringObject; +import org.w3c.dom.Element; /** * Calls a Java class method using the given fields as parameters */ public class CallClassMethod extends MethodOperation { - public static final class CallClassMethodFactory implements Factory<CallClassMethod> { - public CallClassMethod createMethodOperation(Element element, SimpleMethod simpleMethod) { - return new CallClassMethod(element, simpleMethod); - } - - public String getName() { - return "call-class-method"; - } - } public static final String module = CallClassMethod.class.getName(); String className; String methodName; - ContextAccessor<Object> retFieldAcsr; - ContextAccessor<Map<String, Object>> retMapAcsr; - /** A list of MethodObject objects to use as the method call parameters */ List<MethodObject<?>> parameters; + ContextAccessor<Object> retFieldAcsr; + ContextAccessor<Map<String, Object>> retMapAcsr; public CallClassMethod(Element element, SimpleMethod simpleMethod) { super(element, simpleMethod); className = element.getAttribute("class-name"); methodName = element.getAttribute("method-name"); - // the schema for this element now just has the "ret-field" attribute, though the old "ret-field-name" and "ret-map-name" pair is still supported + // the schema for this element now just has the "ret-field" attribute, though the + // old "ret-field-name" and "ret-map-name" pair is still supported retFieldAcsr = new ContextAccessor<Object>(element.getAttribute("ret-field"), element.getAttribute("ret-field-name")); retMapAcsr = new ContextAccessor<Map<String, Object>>(element.getAttribute("ret-map-name")); - List<? extends Element> parameterElements = UtilXml.childElementList(element); if (parameterElements.size() > 0) { parameters = FastList.newInstance(); - - for (Element parameterElement: parameterElements) { + for (Element parameterElement : parameterElements) { MethodObject<?> methodObject = null; if ("string".equals(parameterElement.getNodeName())) { methodObject = new StringObject(parameterElement, simpleMethod); } else if ("field".equals(parameterElement.getNodeName())) { methodObject = new FieldObject<Object>(parameterElement, simpleMethod); } else { - //whoops, invalid tag here, print warning + // whoops, invalid tag here, print warning Debug.logWarning("Found an unsupported tag under the call-object-method tag: " + parameterElement.getNodeName() + "; ignoring", module); } if (methodObject != null) { @@ -86,7 +82,6 @@ public class CallClassMethod extends Met public boolean exec(MethodContext methodContext) { String className = methodContext.expandString(this.className); String methodName = methodContext.expandString(this.methodName); - Class<?> methodClass = null; try { methodClass = ObjectType.loadClass(className, methodContext.getLoader()); @@ -97,18 +92,28 @@ public class CallClassMethod extends Met methodContext.setErrorReturn(errMsg, simpleMethod); return false; } - return CallObjectMethod.callMethod(simpleMethod, methodContext, parameters, methodClass, null, methodName, retFieldAcsr, retMapAcsr); } @Override + public String expandedString(MethodContext methodContext) { + // TODO: something more than a stub/dummy + return this.rawString(); + } + + @Override public String rawString() { // TODO: something more than the empty tag return "<call-class-method/>"; } - @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + + public static final class CallClassMethodFactory implements Factory<CallClassMethod> { + public CallClassMethod createMethodOperation(Element element, SimpleMethod simpleMethod) { + return new CallClassMethod(element, simpleMethod); + } + + public String getName() { + return "call-class-method"; + } } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallObjectMethod.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallObjectMethod.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallObjectMethod.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallObjectMethod.java Thu Apr 19 15:09:03 2012 @@ -18,120 +18,41 @@ *******************************************************************************/ package org.ofbiz.minilang.method.callops; -import java.lang.reflect.*; -import java.util.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Map; import javolution.util.FastList; import javolution.util.FastMap; -import org.w3c.dom.*; -import org.ofbiz.base.util.*; - -import org.ofbiz.minilang.*; -import org.ofbiz.minilang.method.*; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.SimpleMethod; +import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.method.FieldObject; +import org.ofbiz.minilang.method.MethodContext; +import org.ofbiz.minilang.method.MethodObject; +import org.ofbiz.minilang.method.MethodOperation; +import org.ofbiz.minilang.method.StringObject; +import org.w3c.dom.Element; /** * Calls a Java object method using the given fields as parameters */ public class CallObjectMethod extends MethodOperation { - public static final class CallObjectMethodFactory implements Factory<CallObjectMethod> { - public CallObjectMethod createMethodOperation(Element element, SimpleMethod simpleMethod) { - return new CallObjectMethod(element, simpleMethod); - } - - public String getName() { - return "call-object-method"; - } - } public static final String module = CallClassMethod.class.getName(); - ContextAccessor<Object> objFieldAcsr; - ContextAccessor<Map<String, ? extends Object>> objMapAcsr; - String methodName; - ContextAccessor<Object> retFieldAcsr; - ContextAccessor<Map<String, Object>> retMapAcsr; - - /** A list of MethodObject objects to use as the method call parameters */ - List<MethodObject<?>> parameters; - - public CallObjectMethod(Element element, SimpleMethod simpleMethod) { - super(element, simpleMethod); - // the schema for this element now just has the "obj-field" attribute, though the old "obj-field-name" and "obj-map-name" pair is still supported - objFieldAcsr = new ContextAccessor<Object>(element.getAttribute("obj-field"), element.getAttribute("obj-field-name")); - objMapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("obj-map-name")); - - methodName = element.getAttribute("method-name"); - - // the schema for this element now just has the "ret-field" attribute, though the old "ret-field-name" and "ret-map-name" pair is still supported - retFieldAcsr = new ContextAccessor<Object>(element.getAttribute("ret-field"), element.getAttribute("ret-field-name")); - retMapAcsr = new ContextAccessor<Map<String, Object>>(element.getAttribute("ret-map-name")); - - List<? extends Element> parameterElements = UtilXml.childElementList(element); - if (parameterElements.size() > 0) { - parameters = FastList.newInstance(); - - for (Element parameterElement: parameterElements) { - MethodObject<?> methodObject = null; - if ("string".equals(parameterElement.getNodeName())) { - methodObject = new StringObject(parameterElement, simpleMethod); - } else if ("field".equals(parameterElement.getNodeName())) { - methodObject = new FieldObject<Object>(parameterElement, simpleMethod); - } else { - //whoops, invalid tag here, print warning - Debug.logWarning("Found an unsupported tag under the call-object-method tag: " + parameterElement.getNodeName() + "; ignoring", module); - } - if (methodObject != null) { - parameters.add(methodObject); - } - } - } - } - - @Override - public boolean exec(MethodContext methodContext) { - String methodName = methodContext.expandString(this.methodName); - - Object methodObject = null; - if (!objMapAcsr.isEmpty()) { - Map<String, ? extends Object> fromMap = objMapAcsr.get(methodContext); - if (fromMap == null) { - Debug.logWarning("Map not found with name " + objMapAcsr + ", which should contain the object to execute a method on; not executing method, rerturning error.", module); - - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Map not found with name " + objMapAcsr + ", which should contain the object to execute a method on]"; - methodContext.setErrorReturn(errMsg, simpleMethod); - return false; - } - methodObject = objFieldAcsr.get(fromMap, methodContext); - } else { - // no map name, try the env - methodObject = objFieldAcsr.get(methodContext); - } - - if (methodObject == null) { - if (Debug.infoOn()) Debug.logInfo("Object not found to execute method on with name " + objFieldAcsr + " in Map with name " + objMapAcsr + ", not executing method, rerturning error.", module); - - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Object not found to execute method on with name " + objFieldAcsr + " in Map with name " + objMapAcsr + "]"; - methodContext.setErrorReturn(errMsg, simpleMethod); - return false; - } - - Class<?> methodClass = methodObject.getClass(); - return CallObjectMethod.callMethod(simpleMethod, methodContext, parameters, methodClass, methodObject, methodName, retFieldAcsr, retMapAcsr); - } - public static boolean callMethod(SimpleMethod simpleMethod, MethodContext methodContext, List<MethodObject<?>> parameters, Class<?> methodClass, Object methodObject, String methodName, ContextAccessor<Object> retFieldAcsr, ContextAccessor<Map<String, Object>> retMapAcsr) { Object[] args = null; Class<?>[] parameterTypes = null; - if (parameters != null) { args = new Object[parameters.size()]; parameterTypes = new Class<?>[parameters.size()]; - int i = 0; - for (MethodObject<?> methodObjectDef: parameters) { + for (MethodObject<?> methodObjectDef : parameters) { args[i] = methodObjectDef.getObject(methodContext); - Class<?> typeClass = methodObjectDef.getTypeClass(methodContext.getLoader()); if (typeClass == null) { String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Parameter type not found with name " + methodObjectDef.getTypeName() + "]"; @@ -139,22 +60,18 @@ public class CallObjectMethod extends Me methodContext.setErrorReturn(errMsg, simpleMethod); return false; } - parameterTypes[i] = typeClass; i++; } } - try { Method method = methodClass.getMethod(methodName, parameterTypes); try { Object retValue = method.invoke(methodObject, args); - - //if retFieldAcsr is empty, ignore return value + // if retFieldAcsr is empty, ignore return value if (!retFieldAcsr.isEmpty()) { if (!retMapAcsr.isEmpty()) { Map<String, Object> retMap = retMapAcsr.get(methodContext); - if (retMap == null) { retMap = FastMap.newInstance(); retMapAcsr.put(methodContext, retMap); @@ -192,18 +109,95 @@ public class CallObjectMethod extends Me methodContext.setErrorReturn(errMsg, simpleMethod); return false; } - return true; } + String methodName; + ContextAccessor<Object> objFieldAcsr; + ContextAccessor<Map<String, ? extends Object>> objMapAcsr; + /** A list of MethodObject objects to use as the method call parameters */ + List<MethodObject<?>> parameters; + ContextAccessor<Object> retFieldAcsr; + ContextAccessor<Map<String, Object>> retMapAcsr; + + public CallObjectMethod(Element element, SimpleMethod simpleMethod) { + super(element, simpleMethod); + // the schema for this element now just has the "obj-field" attribute, though the + // old "obj-field-name" and "obj-map-name" pair is still supported + objFieldAcsr = new ContextAccessor<Object>(element.getAttribute("obj-field"), element.getAttribute("obj-field-name")); + objMapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("obj-map-name")); + methodName = element.getAttribute("method-name"); + // the schema for this element now just has the "ret-field" attribute, though the + // old "ret-field-name" and "ret-map-name" pair is still supported + retFieldAcsr = new ContextAccessor<Object>(element.getAttribute("ret-field"), element.getAttribute("ret-field-name")); + retMapAcsr = new ContextAccessor<Map<String, Object>>(element.getAttribute("ret-map-name")); + List<? extends Element> parameterElements = UtilXml.childElementList(element); + if (parameterElements.size() > 0) { + parameters = FastList.newInstance(); + for (Element parameterElement : parameterElements) { + MethodObject<?> methodObject = null; + if ("string".equals(parameterElement.getNodeName())) { + methodObject = new StringObject(parameterElement, simpleMethod); + } else if ("field".equals(parameterElement.getNodeName())) { + methodObject = new FieldObject<Object>(parameterElement, simpleMethod); + } else { + // whoops, invalid tag here, print warning + Debug.logWarning("Found an unsupported tag under the call-object-method tag: " + parameterElement.getNodeName() + "; ignoring", module); + } + if (methodObject != null) { + parameters.add(methodObject); + } + } + } + } + @Override - public String rawString() { - // TODO: something more than the empty tag - return "<call-object-method/>"; + public boolean exec(MethodContext methodContext) { + String methodName = methodContext.expandString(this.methodName); + Object methodObject = null; + if (!objMapAcsr.isEmpty()) { + Map<String, ? extends Object> fromMap = objMapAcsr.get(methodContext); + if (fromMap == null) { + Debug.logWarning("Map not found with name " + objMapAcsr + ", which should contain the object to execute a method on; not executing method, rerturning error.", module); + String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Map not found with name " + objMapAcsr + ", which should contain the object to execute a method on]"; + methodContext.setErrorReturn(errMsg, simpleMethod); + return false; + } + methodObject = objFieldAcsr.get(fromMap, methodContext); + } else { + // no map name, try the env + methodObject = objFieldAcsr.get(methodContext); + } + if (methodObject == null) { + if (Debug.infoOn()) + Debug.logInfo("Object not found to execute method on with name " + objFieldAcsr + " in Map with name " + objMapAcsr + ", not executing method, rerturning error.", module); + String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Object not found to execute method on with name " + objFieldAcsr + " in Map with name " + objMapAcsr + "]"; + methodContext.setErrorReturn(errMsg, simpleMethod); + return false; + } + Class<?> methodClass = methodObject.getClass(); + return CallObjectMethod.callMethod(simpleMethod, methodContext, parameters, methodClass, methodObject, methodName, retFieldAcsr, retMapAcsr); } + @Override public String expandedString(MethodContext methodContext) { // TODO: something more than a stub/dummy return this.rawString(); } + + @Override + public String rawString() { + // TODO: something more than the empty tag + return "<call-object-method/>"; + } + + public static final class CallObjectMethodFactory implements Factory<CallObjectMethod> { + public CallObjectMethod createMethodOperation(Element element, SimpleMethod simpleMethod) { + return new CallObjectMethod(element, simpleMethod); + } + + public String getName() { + return "call-object-method"; + } + } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java Thu Apr 19 15:09:03 2012 @@ -23,7 +23,7 @@ import java.util.Map; import javolution.util.FastList; -import org.ofbiz.base.util.*; +import org.ofbiz.base.util.ScriptUtil; import org.ofbiz.minilang.MiniLangException; import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.ContextAccessor; @@ -32,55 +32,58 @@ import org.ofbiz.minilang.method.MethodO import org.w3c.dom.Element; public class CallScript extends MethodOperation { - public static final class CallScriptFactory implements Factory<CallScript> { - public CallScript createMethodOperation(Element element, SimpleMethod simpleMethod) { - return new CallScript(element, simpleMethod); + + public static final String module = CallScript.class.getName(); + + private static String getScriptLocation(String combinedName) { + int pos = combinedName.lastIndexOf("#"); + if (pos == -1) { + return combinedName; } + return combinedName.substring(0, pos); + } - public String getName() { - return "script"; + private static String getScriptMethodName(String combinedName) { + int pos = combinedName.lastIndexOf("#"); + if (pos == -1) { + return null; } + return combinedName.substring(pos + 1); } - - public static final String module = CallScript.class.getName(); private ContextAccessor<List<Object>> errorListAcsr; private String location; private String method; public CallScript(Element element, SimpleMethod simpleMethod) { - super(element, simpleMethod); + super(element, simpleMethod); String scriptLocation = element.getAttribute("location"); this.location = getScriptLocation(scriptLocation); this.method = getScriptMethodName(scriptLocation); this.errorListAcsr = new ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), "error_list"); } - + @Override - public boolean exec(MethodContext methodContext) { + public boolean exec(MethodContext methodContext) { String location = methodContext.expandString(this.location); String method = methodContext.expandString(this.method); - List<Object> messages = errorListAcsr.get(methodContext); if (messages == null) { messages = FastList.newInstance(); errorListAcsr.put(methodContext, messages); } - - Map<String, Object> context = methodContext.getEnvMap(); + Map<String, Object> context = methodContext.getEnvMap(); if (location.endsWith(".xml")) { try { - SimpleMethod.runSimpleMethod(location, method, methodContext); + SimpleMethod.runSimpleMethod(location, method, methodContext); } catch (MiniLangException e) { messages.add("Error running simple method at location [" + location + "]: " + e.getMessage()); } } else { ScriptUtil.executeScript(this.location, this.method, context); } - // update the method environment methodContext.putAllEnv(context); - // always return true, error messages just go on the error list return true; } @@ -94,20 +97,14 @@ public class CallScript extends MethodOp public String rawString() { return "<script/>"; } - - private static String getScriptLocation(String combinedName) { - int pos = combinedName.lastIndexOf("#"); - if (pos == -1) { - return combinedName; + + public static final class CallScriptFactory implements Factory<CallScript> { + public CallScript createMethodOperation(Element element, SimpleMethod simpleMethod) { + return new CallScript(element, simpleMethod); } - return combinedName.substring(0, pos); - } - - private static String getScriptMethodName(String combinedName) { - int pos = combinedName.lastIndexOf("#"); - if (pos == -1) { - return null; + + public String getName() { + return "script"; } - return combinedName.substring(pos + 1); } } |
| Free forum by Nabble | Edit this page |
