Author: scheu
Date: Thu Apr 24 07:47:37 2008
New Revision: 651283
URL: http://svn.apache.org/viewvc?rev=651283&view=rev
Log:
Added logDebug(OMElement om,Log log...) methods to CommonUtils.
The logDebug(...) is a convenience method that serializes the OMElement to a
special output stream, LogOutputStream,
which logs the message. The implementation ensures that the entire message is
not stored in-core; thus it reduces
the chances of an OutOfMemory condition.
A side effect of logDebug is that it returns the length of the message.
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/LogOutputStream.java
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CommonUtils.java
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPEnvelopeBuildTest.java
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CommonUtils.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CommonUtils.java?rev=651283&r1=651282&r2=651283&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CommonUtils.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/CommonUtils.java
Thu Apr 24 07:47:37 2008
@@ -19,6 +19,12 @@
package org.apache.axiom.om.util;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.commons.logging.Log;
+
+import javax.xml.stream.XMLStreamException;
+
/**
* Common Utilities
*/
@@ -92,4 +98,62 @@
text = replace(text, "at ", "DEBUG_FRAME = ");
return text;
}
+
+ /**
+ * Writes the om to a log.debug.
+ * This method assumes optimized mtom attachments
+ * Also calculates the length of the message.
+ * @param om OMElement
+ * @param log Log
+ * @return length of entire message
+ */
+ public static long logDebug(OMElement om, Log log) {
+ return logDebug(om, log, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Writes the om to a log.debug.
+ * This method assumes optimized mtom attachments
+ * Also calculates the length of the message.
+ * @param om OMElement
+ * @param log Log
+ * @param int limit of message to write
+ * @return length of entire message
+ */
+ public static long logDebug(OMElement om, Log log, int limit) {
+ OMOutputFormat format = new OMOutputFormat();
+ format.setDoOptimize(true);
+ format.setAutoCloseWriter(true);
+ format.setIgnoreXMLDeclaration(true);
+ return logDebug(om, log, limit, format);
+ }
+
+ /**
+ * Writes the om to a log.debug.
+ * Also calculates the length of the message.
+ * @param om OMElement
+ * @param log Log
+ * @param int limit of message to write
+ * @param format OMOutputFormat
+ * @return length of entire message
+ */
+ public static long logDebug(OMElement om,
+ Log log,
+ int limit,
+ OMOutputFormat format) {
+ LogOutputStream logStream = new LogOutputStream(log, limit);
+
+ try {
+ om.serialize(logStream, format);
+ logStream.flush();
+ logStream.close();
+ } catch (Throwable t) {
+ // Problem occur while logging. Log error and continue
+ log.debug(t);
+ log.error(t);
+ }
+
+ return logStream.getLength();
+ }
+
}
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/LogOutputStream.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/LogOutputStream.java?rev=651283&view=auto
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/LogOutputStream.java
(added)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/LogOutputStream.java
Thu Apr 24 07:47:37 2008
@@ -0,0 +1,116 @@
+/*
+ * 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.apache.axiom.om.util;
+
+import org.apache.commons.logging.Log;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * LogOutputStream
+ * Writes to log.debug()
+ * Also counts the number of bytes
+ */
+public class LogOutputStream extends OutputStream {
+
+ private byte[] temp = new byte[1];
+ private boolean isDebugEnabled = false;
+ private long count = 0;
+ private Log log;
+ private int BUFFER_LEN = 4 * 1024;
+ private byte[] buffer = new byte[BUFFER_LEN];
+ private int bufferIndex = 0;
+ private int limit;
+
+ public LogOutputStream(Log log, int limit) {
+ isDebugEnabled = log.isDebugEnabled();
+ this.log = log;
+ this.limit = limit;
+ }
+
+ public long getLength() {
+ return count;
+ }
+
+ public void close() throws IOException {
+ if (bufferIndex > 0) {
+ log.debug(new String(buffer, 0, bufferIndex));
+ bufferIndex = 0;
+ }
+ buffer = null;
+ temp = null;
+ log = null;
+ }
+
+
+ public void flush() throws IOException {
+ // noop
+ }
+
+
+ public void write(byte[] b, int off, int len) throws IOException {
+
+ // Adjust total count
+ // Adjust length to write
+ if (count >= limit) {
+ count += len;
+ return;
+ } else if (count + len >= limit) {
+ count += len;
+ len = (int) (len - (limit - count)); // adjust length to write
+ } else {
+ count += len;
+ }
+
+ if (isDebugEnabled) {
+ if (len + bufferIndex < BUFFER_LEN) {
+ // buffer the text
+ System.arraycopy(b, off, buffer, bufferIndex, len);
+ bufferIndex += len;
+ } else {
+ // write buffered text
+ if (bufferIndex > 0) {
+ log.debug(new String(buffer, 0, bufferIndex));
+ bufferIndex = 0;
+ }
+ // buffer or write remaining text
+ if (len + bufferIndex < BUFFER_LEN) {
+ System.arraycopy(b, off, buffer, bufferIndex, len);
+ bufferIndex += len;
+ } else {
+ log.debug(new String(b, off, len));
+ }
+ }
+ }
+
+ }
+
+ public void write(byte[] b) throws IOException {
+ this.write(b, 0, b.length);
+ }
+
+
+
+ public void write(int b) throws IOException {
+ temp[0] = (byte) b;
+ this.write(temp, 0, 1);
+ }
+
+}
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPEnvelopeBuildTest.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPEnvelopeBuildTest.java?rev=651283&r1=651282&r2=651283&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPEnvelopeBuildTest.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/soap/SOAPEnvelopeBuildTest.java
Thu Apr 24 07:47:37 2008
@@ -26,6 +26,7 @@
import junit.framework.TestCase;
+import org.apache.axiom.om.util.CommonUtils;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.OMElement;
@@ -33,6 +34,8 @@
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -135,4 +138,63 @@
fail("No children of the Body element");
}
}
+
+ public void testTrace() throws Exception{
+ XMLStreamReader parser = StAXUtils.createXMLStreamReader(new
StringReader(testMessage));
+ StAXSOAPModelBuilder sob = new StAXSOAPModelBuilder(parser,
null);
+ SOAPEnvelope se = (SOAPEnvelope)sob.getDocumentElement();
+ SOAPHeader sh = se.getHeader();
+
+ MyDebugLogger log = new MyDebugLogger();
+ long length = CommonUtils.logDebug(se, log);
+ assertTrue(length > 100);
+ System.out.println(log.output);
+ System.out.println(length);
+ assertTrue(log.output.contains("x:Content"));
+
+ }
+
+ class MyDebugLogger implements Log {
+
+ String output = "";
+ public void debug(Object arg0) {
+ output = output.concat(arg0.toString());
+ }
+ public void debug(Object arg0, Throwable arg1) {}
+ public void error(Object arg0) {}
+ public void error(Object arg0, Throwable arg1) {}
+ public void fatal(Object arg0) {}
+ public void fatal(Object arg0, Throwable arg1) {}
+ public void info(Object arg0) {}
+ public void info(Object arg0, Throwable arg1) {}
+
+ public boolean isDebugEnabled() {
+ return true;
+ }
+
+ public boolean isErrorEnabled() {
+ return false;
+ }
+
+ public boolean isFatalEnabled() {
+ return false;
+ }
+
+ public boolean isInfoEnabled() {
+ return false;
+ }
+
+ public boolean isTraceEnabled() {
+ return false;
+ }
+
+ public boolean isWarnEnabled() {
+ return false;
+ }
+
+ public void trace(Object arg0) {}
+ public void trace(Object arg0, Throwable arg1) {}
+ public void warn(Object arg0) {}
+ public void warn(Object arg0, Throwable arg1) {}
+ }
}