svn commit: r1337137 - in /ofbiz/trunk/framework/minilang: dtd/ src/META-INF/services/ src/org/ofbiz/minilang/ src/org/ofbiz/minilang/method/ src/org/ofbiz/minilang/method/otherops/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1337137 - in /ofbiz/trunk/framework/minilang: dtd/ src/META-INF/services/ src/org/ofbiz/minilang/ src/org/ofbiz/minilang/method/ src/org/ofbiz/minilang/method/otherops/

adrianc
Author: adrianc
Date: Fri May 11 13:02:35 2012
New Revision: 1337137

URL: http://svn.apache.org/viewvc?rev=1337137&view=rev
Log:
Added new element - <trace> - to Mini-language. Also updated the <simple-method> element to output trace messages.

Added:
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Trace.java   (with props)
Modified:
    ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
    ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodContext.java

Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd?rev=1337137&r1=1337136&r2=1337137&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original)
+++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Fri May 11 13:02:35 2012
@@ -137,6 +137,30 @@ under the License.
         </xs:attribute>
     </xs:attributeGroup>
 
+    <xs:attribute name="level">
+        <xs:annotation>
+            <xs:documentation>
+                The logging/debug level to use.
+                Must be one of the following: verbose | timing | info | important | warning | error | fatal | always.
+                Defaults to "info".
+                &lt;br/&gt;&lt;br/&gt;
+                Required. Attribute type: constant.
+                </xs:documentation>
+        </xs:annotation>
+        <xs:simpleType>
+            <xs:restriction base="xs:token">
+                <xs:enumeration value="info" />
+                <xs:enumeration value="verbose" />
+                <xs:enumeration value="timing" />
+                <xs:enumeration value="important" />
+                <xs:enumeration value="warning" />
+                <xs:enumeration value="error" />
+                <xs:enumeration value="fatal" />
+                <xs:enumeration value="always" />
+            </xs:restriction>
+        </xs:simpleType>
+    </xs:attribute>
+
   <!-- ELEMENTS start here -->
     <xs:element name="field">
         <xs:annotation>
@@ -3550,12 +3574,14 @@ under the License.
             <xs:attribute type="xs:string" name="error-code">
                 <xs:annotation>
                     <xs:documentation>
-                        The error code to return to the calling process. Defaults to "error".
+                        The error code to return to the calling process.
+                        Defaults to value of the containing simple-method "default-error-code" attribute.
                         &lt;br/&gt;&lt;br/&gt;
                         Optional. Attribute type: constant+expr
                     </xs:documentation>
                 </xs:annotation>
             </xs:attribute>
+            <xs:attribute ref="error-list-name"/>
         </xs:complexType>
     </xs:element>
     <xs:element name="add-error" substitutionGroup="ControlOperations">
@@ -4242,29 +4268,7 @@ under the License.
             </xs:documentation>
         </xs:annotation>
         <xs:complexType>
-            <xs:attribute name="level" use="required">
-                <xs:annotation>
-                    <xs:documentation>
-                        The logging/debug level to use.
-                        Must be one of the following: verbose | timing | info | important | warning | error | fatal | always.
-                        Defaults to "info".
-                        &lt;br/&gt;&lt;br/&gt;
-                        Required. Attribute type: constant.
-                    </xs:documentation>
-                </xs:annotation>
-                <xs:simpleType>
-                    <xs:restriction base="xs:token">
-                        <xs:enumeration value="info" />
-                        <xs:enumeration value="verbose" />
-                        <xs:enumeration value="timing" />
-                        <xs:enumeration value="important" />
-                        <xs:enumeration value="warning" />
-                        <xs:enumeration value="error" />
-                        <xs:enumeration value="fatal" />
-                        <xs:enumeration value="always" />
-                    </xs:restriction>
-                </xs:simpleType>
-            </xs:attribute>
+            <xs:attribute ref="level" use="required"/>
             <xs:attribute type="xs:string" name="message" use="required">
                 <xs:annotation>
                     <xs:documentation>
@@ -4276,6 +4280,17 @@ under the License.
             </xs:attribute>
         </xs:complexType>
     </xs:element>
+    <xs:element name="trace" substitutionGroup="OtherOperations">
+        <xs:annotation>
+            <xs:documentation>
+                Logs detailed information on the internal state of its sub-elements, using the specified log level.
+            </xs:documentation>
+        </xs:annotation>
+        <xs:complexType>
+            <xs:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations" />
+            <xs:attribute ref="level" use="required"/>
+        </xs:complexType>
+    </xs:element>
     <xs:element name="now" substitutionGroup="OtherOperations">
         <xs:annotation>
             <xs:documentation>

Modified: ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation%24Factory?rev=1337137&r1=1337136&r2=1337137&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory (original)
+++ ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory Fri May 11 13:02:35 2012
@@ -103,4 +103,5 @@ org.ofbiz.minilang.method.ifops.IfValida
 org.ofbiz.minilang.method.otherops.Calculate$CalculateFactory
 org.ofbiz.minilang.method.otherops.Log$LogFactory
 org.ofbiz.minilang.method.otherops.PropertyToField$PropertyToFieldFactory
+org.ofbiz.minilang.method.otherops.Trace$TraceFactory
 org.ofbiz.minilang.method.serviceops.FieldToResult$FieldToResultFactory

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java?rev=1337137&r1=1337136&r2=1337137&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java Fri May 11 13:02:35 2012
@@ -18,6 +18,8 @@
  *******************************************************************************/
 package org.ofbiz.minilang;
 
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.minilang.method.MethodContext;
 import org.w3c.dom.Element;
 
 /**
@@ -25,6 +27,8 @@ import org.w3c.dom.Element;
  */
 public class MiniLangElement {
 
+    public static final String module = MiniLangElement.class.getName();
+
     private final Object lineNumber;
     protected final SimpleMethod simpleMethod;
     private final String tagName;
@@ -47,6 +51,19 @@ public class MiniLangElement {
         return this.tagName;
     }
 
+    public void outputTraceMessage(MethodContext methodContext, String... messages) {
+        String lineSep = System.getProperty("line.separator");
+        StringBuilder buf = new StringBuilder(getSimpleMethod().getFileName());
+        buf.append(", Line ").append(getLineNumber()).append(" <").append(getTagName()).append("> element: ");
+        for (int i = 0; i < messages.length; i++) {
+            buf.append(messages[i]);
+            if (i < messages.length - 1 && messages.length > 1) {
+                buf.append(lineSep);
+            }
+        }
+        Debug.log(methodContext.getTraceLogLevel(), null, buf.toString(), module);
+    }
+
     @Override
     public String toString() {
         return "<".concat(this.tagName).concat(">");

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java?rev=1337137&r1=1337136&r2=1337137&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java Fri May 11 13:02:35 2012
@@ -61,7 +61,7 @@ import org.w3c.dom.Element;
 /**
  * Contains a block of Mini-language code.
  */
-public final class SimpleMethod {
+public final class SimpleMethod extends MiniLangElement {
 
     public static final String module = SimpleMethod.class.getName();
     public static final String err_resource = "MiniLangErrorUiLabels";
@@ -321,6 +321,7 @@ public final class SimpleMethod {
     private final boolean useTransaction;
 
     public SimpleMethod(Element simpleMethodElement, String fromLocation) throws MiniLangException {
+        super(simpleMethodElement, null);
         if (MiniLangValidate.validationOn()) {
             String locationMsg = " File = ".concat(fromLocation);
             if (simpleMethodElement.getAttribute("method-name").isEmpty()) {
@@ -381,12 +382,18 @@ public final class SimpleMethod {
 
     /** Execute the Simple Method operations */
     public String exec(MethodContext methodContext) throws MiniLangException {
+        if (methodContext.isTraceOn()) {
+            outputTraceMessage(methodContext, "Begin simple-method. Script is running as " + (methodContext.getMethodType() == MethodContext.EVENT ? "an event." : "a service."));
+        }
         Locale locale = methodContext.getLocale();
         GenericValue userLogin = methodContext.getUserLogin();
         if (loginRequired) {
             if (userLogin == null) {
                 Map<String, Object> messageMap = UtilMisc.<String, Object> toMap("shortDescription", shortDescription);
                 String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.must_logged_process", messageMap, locale) + ".";
+                if (methodContext.isTraceOn()) {
+                    outputTraceMessage(methodContext, "\"login-required\" attribute set to \"true\" but UserLogin GenericValue was not found, returning error message:", errMsg);
+                }
                 return returnError(methodContext, errMsg);
             }
         }
@@ -412,11 +419,16 @@ public final class SimpleMethod {
         // if using transaction, try to start here
         boolean beganTransaction = false;
         if (useTransaction) {
+            if (methodContext.isTraceOn()) {
+                outputTraceMessage(methodContext, "\"use-transaction\" attribute set to \"true\", beginning transaction.");
+            }
             try {
                 beganTransaction = TransactionUtil.begin();
             } catch (GenericTransactionException e) {
                 String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_begin_transaction", locale) + ": " + e.getMessage();
-                Debug.logWarning(e, errMsg, module);
+                if (methodContext.isTraceOn()) {
+                    outputTraceMessage(methodContext, "An exception was thrown while beginning a transaction, returning error message:", errMsg);
+                }
                 return returnError(methodContext, errMsg);
             }
         }
@@ -424,14 +436,22 @@ public final class SimpleMethod {
         String errorMsg = "";
         boolean finished = false;
         try {
+            if (methodContext.isTraceOn()) {
+                outputTraceMessage(methodContext, "Begin running sub-elements.");
+            }
             finished = runSubOps(methodOperations, methodContext);
         } catch (Throwable t) {
             // make SURE nothing gets thrown through
             String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_running", locale) + ": " + t.getMessage();
-            Debug.logWarning(t, errMsg, module);
+            if (methodContext.isTraceOn()) {
+                outputTraceMessage(methodContext, "An exception was thrown while running sub-elements, error message was:", errMsg);
+            }
             finished = false;
             errorMsg += errMsg;
         }
+        if (methodContext.isTraceOn()) {
+            outputTraceMessage(methodContext, "End running sub-elements.");
+        }
         String returnValue = null;
         String response = null;
         StringBuilder summaryErrorStringBuffer = new StringBuilder();
@@ -524,24 +544,37 @@ public final class SimpleMethod {
             doCommit = false;
         }
         if (doCommit) {
+            if (methodContext.isTraceOn()) {
+                outputTraceMessage(methodContext, "Begin commit transaction.");
+            }
             // commit here passing beganTransaction to perform it properly
             try {
                 TransactionUtil.commit(beganTransaction);
             } catch (GenericTransactionException e) {
                 String errMsg = "Error trying to commit transaction, could not process method: " + e.getMessage();
-                Debug.logWarning(e, errMsg, module);
+                if (methodContext.isTraceOn()) {
+                    outputTraceMessage(methodContext, "An exception was thrown while committing a transaction, returning error message:", errMsg);
+                }
                 errorMsg += errMsg;
             }
         } else {
+            if (methodContext.isTraceOn()) {
+                outputTraceMessage(methodContext, "Begin roll back transaction.");
+            }
             // rollback here passing beganTransaction to either rollback, or set rollback only
             try {
                 TransactionUtil.rollback(beganTransaction, "Error in simple-method [" + this.getShortDescription() + "]: " + summaryErrorStringBuffer, null);
             } catch (GenericTransactionException e) {
                 String errMsg = "Error trying to rollback transaction, could not process method: " + e.getMessage();
-                Debug.logWarning(e, errMsg, module);
+                if (methodContext.isTraceOn()) {
+                    outputTraceMessage(methodContext, "An exception was thrown while rolling back a transaction, returning error message:", errMsg);
+                }
                 errorMsg += errMsg;
             }
         }
+        if (methodContext.isTraceOn()) {
+            outputTraceMessage(methodContext, "End simple-method.");
+        }
         return returnValue;
     }
 
@@ -604,6 +637,10 @@ public final class SimpleMethod {
         return this.eventSessionName;
     }
 
+    public String getFileName() {
+        return this.fromLocation.substring(this.fromLocation.lastIndexOf("/") + 1);
+    }
+
     public String getFromLocation() {
         return this.fromLocation;
     }
@@ -660,6 +697,11 @@ public final class SimpleMethod {
         return this.shortDescription + " [" + this.fromLocation + "#" + this.methodName + "]";
     }
 
+    @Override
+    public SimpleMethod getSimpleMethod() {
+        return this;
+    }
+
     public String getUserLoginEnvName() {
         return "userLogin";
     }

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=1337137&r1=1337136&r2=1337137&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 Fri May 11 13:02:35 2012
@@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletRes
 
 import javolution.util.FastMap;
 
+import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilHttp;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
@@ -62,6 +63,8 @@ public class MethodContext implements It
     protected Map<String, Object> results = null;
     protected Security security;
     protected TimeZone timeZone;
+    private int traceCount = 0;
+    private int traceLogLevel = Debug.INFO;
     protected GenericValue userLogin;
 
     public MethodContext(DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) {
@@ -247,10 +250,18 @@ public class MethodContext implements It
         return this.timeZone;
     }
 
+    public int getTraceLogLevel() {
+        return this.traceLogLevel;
+    }
+
     public GenericValue getUserLogin() {
         return this.userLogin;
     }
 
+    public boolean isTraceOn() {
+        return this.traceCount > 0;
+    }
+
     public Iterator<Map.Entry<String, Object>> iterator() {
         return this.env.entrySet().iterator();
     }
@@ -319,6 +330,20 @@ public class MethodContext implements It
         }
     }
 
+    public void setTraceOff() {
+        if (this.traceCount > 0) {
+            this.traceCount--;
+        }
+    }
+
+    public void setTraceOn(int logLevel) {
+        if (this.traceCount == 0) {
+            // Outermost trace element sets the logging level
+            this.traceLogLevel = logLevel;
+        }
+        this.traceCount++;
+    }
+
     public void setUserLogin(GenericValue userLogin, String userLoginEnvName) {
         this.userLogin = userLogin;
         this.putEnv(userLoginEnvName, userLogin);

Added: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Trace.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Trace.java?rev=1337137&view=auto
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Trace.java (added)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Trace.java Fri May 11 13:02:35 2012
@@ -0,0 +1,98 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.minilang.method.otherops;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
+import org.ofbiz.minilang.MiniLangException;
+import org.ofbiz.minilang.MiniLangValidate;
+import org.ofbiz.minilang.SimpleMethod;
+import org.ofbiz.minilang.method.MethodContext;
+import org.ofbiz.minilang.method.MethodOperation;
+import org.w3c.dom.Element;
+
+/**
+ * Enables trace log messages in sub-elements.
+ */
+public final class Trace extends MethodOperation {
+
+    public static final String module = Trace.class.getName();
+
+    private final int level;
+    private final List<MethodOperation> methodOperations;
+
+    public Trace(Element element, SimpleMethod simpleMethod) throws MiniLangException {
+        super(element, simpleMethod);
+        if (MiniLangValidate.validationOn()) {
+            MiniLangValidate.attributeNames(simpleMethod, element, "level");
+            MiniLangValidate.constantAttributes(simpleMethod, element, "level");
+        }
+        String levelAttribute = MiniLangValidate.checkAttribute(element.getAttribute("level"), "info");
+        Integer levelInt = Debug.getLevelFromString(levelAttribute);
+        if (levelInt == null) {
+            MiniLangValidate.handleError("Invalid level attribute", simpleMethod, element);
+            this.level = Debug.INFO;
+        } else {
+            this.level = levelInt.intValue();
+        }
+        methodOperations = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
+    }
+
+    @Override
+    public boolean exec(MethodContext methodContext) throws MiniLangException {
+        methodContext.setTraceOn(this.level);
+        outputTraceMessage(methodContext, "Trace on.");
+        try {
+            return SimpleMethod.runSubOps(methodOperations, methodContext);
+        } finally {
+            methodContext.setTraceOff();
+            outputTraceMessage(methodContext, "Trace off.");
+        }
+    }
+
+    @Override
+    public String expandedString(MethodContext methodContext) {
+        return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
+    }
+
+    @Override
+    public String rawString() {
+        return toString();
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<trace ");
+        sb.append("level=\"").append(Log.LEVEL_ARRAY[this.level]).append("\" >");
+        return sb.toString();
+    }
+
+    public static final class TraceFactory implements Factory<Trace> {
+        public Trace createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
+            return new Trace(element, simpleMethod);
+        }
+
+        public String getName() {
+            return "trace";
+        }
+    }
+}

Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Trace.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Trace.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL