001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.bean.pojos;
018
019 import java.util.Set;
020
021 import javax.jbi.messaging.MessageExchange;
022 import javax.jbi.messaging.MessagingException;
023 import javax.jbi.messaging.NormalizedMessage;
024 import javax.jbi.messaging.InOut;
025 import javax.jbi.messaging.ExchangeStatus;
026 import javax.xml.transform.Source;
027 import javax.xml.transform.dom.DOMSource;
028
029 import org.w3c.dom.Node;
030
031 import org.apache.servicemix.bean.support.BeanSupport;
032 import org.apache.servicemix.jbi.listener.MessageExchangeListener;
033 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
034 import org.apache.servicemix.common.util.MessageUtil;
035 import org.apache.commons.logging.Log;
036 import org.apache.commons.logging.LogFactory;
037
038 /**
039 * A simple tracing component which can be placed inside a pipeline
040 * to trace the message exchange though the component.
041 * If an InOut exchange is received by this component, it will log the
042 * input message and copy it to the output message.
043 *
044 * @version $Revision: 648504 $
045 */
046 public class LoggingPojo extends BeanSupport implements MessageExchangeListener {
047
048 private Log log = LogFactory.getLog(LoggingPojo.class);
049
050 private final SourceTransformer sourceTransformer = new SourceTransformer();
051
052 private int maxMsgDisplaySize = 1500;
053
054 public Log getLog() {
055 return log;
056 }
057
058 public void setLog(Log log) {
059 this.log = log;
060 }
061
062 public int getMaxMsgDisplaySize() {
063 return maxMsgDisplaySize;
064 }
065
066 public void setMaxMsgDisplaySize(int maxMsgDisplaySize) {
067 this.maxMsgDisplaySize = maxMsgDisplaySize;
068 }
069
070 /**
071 * Intercepts the {@link MessageExchange} to output the message and its
072 * properties for debugging purposes.
073 *
074 * @param exchange A JBI {@link MessageExchange} between two endpoints
075 */
076 public void onMessageExchange(MessageExchange exchange) throws MessagingException {
077 if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
078 // lets dump the incoming message
079 NormalizedMessage message = exchange.getMessage("in");
080 StringBuffer sb = new StringBuffer();
081 sb.append("[\n");
082 sb.append(" id: ").append(exchange.getExchangeId()).append('\n');
083 sb.append(" mep: ").append(exchange.getPattern()).append('\n');
084 sb.append(" status: ").append(exchange.getStatus()).append('\n');
085 sb.append(" role: ").append(exchange.getRole() == MessageExchange.Role.CONSUMER ? "consumer" : "provider").append('\n');
086 if (exchange.getInterfaceName() != null) {
087 sb.append(" interface: ").append(exchange.getInterfaceName()).append('\n');
088 }
089 if (exchange.getService() != null) {
090 sb.append(" service: ").append(exchange.getService()).append('\n');
091 }
092 if (exchange.getEndpoint() != null) {
093 sb.append(" endpoint: ").append(exchange.getEndpoint().getEndpointName()).append('\n');
094 }
095 if (exchange.getOperation() != null) {
096 sb.append(" operation: ").append(exchange.getOperation()).append('\n');
097 }
098 if (exchange.getPropertyNames().size() > 0) {
099 sb.append(" properties: [").append('\n');
100 for (String key : (Set<String>) exchange.getPropertyNames()) {
101 sb.append(" ").append(key).append(" = ");
102 Object contents = exchange.getProperty(key);
103 if (contents instanceof Source) {
104 try {
105 contents = sourceTransformer.toString((Source) contents);
106 } catch (Exception e) { }
107 }
108 sb.append(contents);
109 sb.append('\n');
110 }
111 sb.append(" ]").append('\n');
112 }
113 display(exchange, "in", sb);
114 log.info("Exchange received " + sb.toString());
115 if (exchange instanceof InOut) {
116 MessageUtil.transferInToOut(exchange, exchange);
117 send(exchange);
118 } else {
119 done(exchange);
120 }
121 }
122 }
123
124 private void display(MessageExchange exchange, String msg, StringBuffer sb) {
125 NormalizedMessage message = exchange.getMessage(msg);
126 if (message != null) {
127 sb.append(" ").append(msg).append(": [").append('\n');
128 sb.append(" content: ");
129 try {
130 if (message.getContent() != null) {
131 Node node = sourceTransformer.toDOMNode(message.getContent());
132 message.setContent(new DOMSource(node));
133 String str = sourceTransformer.toString(node);
134 if (maxMsgDisplaySize < 0 || str.length() <= maxMsgDisplaySize) {
135 sb.append(str);
136 } else {
137 sb.append(str.substring(0, maxMsgDisplaySize)).append("...");
138 }
139 } else {
140 sb.append("null");
141 }
142 } catch (Exception e) {
143 sb.append("Unable to display: ").append(e);
144 }
145 sb.append('\n');
146 if (message.getAttachmentNames().size() > 0) {
147 sb.append(" attachments: [").append('\n');
148 for (String key : (Set<String>) message.getAttachmentNames()) {
149 sb.append(" ").append(key).append(" = ").append(message.getAttachment(key)).append('\n');
150 }
151 sb.append(" ]").append('\n');
152 }
153 if (message.getPropertyNames().size() > 0) {
154 sb.append(" properties: [").append('\n');
155 for (String key : (Set<String>) message.getPropertyNames()) {
156 sb.append(" ").append(key).append(" = ");
157 Object contents = message.getProperty(key);
158 if (contents instanceof Source) {
159 try {
160 contents = sourceTransformer.toString((Source) contents);
161 } catch (Exception e) { }
162 }
163 sb.append(contents);
164 sb.append('\n');
165 }
166 sb.append(" ]").append('\n');
167 }
168 sb.append(" ]").append('\n');
169 }
170 }
171
172 }