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 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 SourceTransformer getSourceTransformer() {
063            return sourceTransformer;
064        }
065    
066        public void setSourceTransformer(SourceTransformer sourceTransformer) {
067            this.sourceTransformer = sourceTransformer;
068        }
069    
070        public int getMaxMsgDisplaySize() {
071            return maxMsgDisplaySize;
072        }
073    
074        public void setMaxMsgDisplaySize(int maxMsgDisplaySize) {
075            this.maxMsgDisplaySize = maxMsgDisplaySize;
076        }
077    
078        /**
079         * Intercepts the {@link MessageExchange} to output the message and its
080         * properties for debugging purposes.
081         *
082         * @param exchange A JBI {@link MessageExchange} between two endpoints
083         */
084        public void onMessageExchange(MessageExchange exchange) throws MessagingException {
085            if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
086                // lets dump the incoming message
087                NormalizedMessage message = exchange.getMessage("in");
088                StringBuffer sb = new StringBuffer();
089                sb.append("[\n");
090                sb.append("  id: ").append(exchange.getExchangeId()).append('\n');
091                sb.append("  mep: ").append(exchange.getPattern()).append('\n');
092                sb.append("  status: ").append(exchange.getStatus()).append('\n');
093                sb.append("  role: ").append(exchange.getRole() == MessageExchange.Role.CONSUMER ? "consumer" : "provider").append('\n');
094                if (exchange.getInterfaceName() != null) {
095                    sb.append("  interface: ").append(exchange.getInterfaceName()).append('\n');
096                }
097                if (exchange.getService() != null) {
098                    sb.append("  service: ").append(exchange.getService()).append('\n');
099                }
100                if (exchange.getEndpoint() != null) {
101                    sb.append("  endpoint: ").append(exchange.getEndpoint().getEndpointName()).append('\n');
102                }
103                if (exchange.getOperation() != null) {
104                    sb.append("  operation: ").append(exchange.getOperation()).append('\n');
105                }
106                if (exchange.getPropertyNames().size() > 0) {
107                    sb.append("  properties: [").append('\n');
108                    for (String key : (Set<String>) exchange.getPropertyNames()) {
109                        sb.append("      ").append(key).append(" = ");
110                        Object contents = exchange.getProperty(key);
111                        if (contents instanceof Source) {
112                            try {
113                                contents = sourceTransformer.toString((Source) contents);
114                            } catch (Exception e) { }
115                        }
116                        sb.append(contents);
117                        sb.append('\n');
118                    }
119                    sb.append("  ]").append('\n');
120                }
121                display(exchange, "in", sb);
122                log.info("Exchange received " + sb.toString());
123                if (exchange instanceof InOut) {
124                    MessageUtil.transferInToOut(exchange, exchange);
125                    send(exchange);
126                } else {
127                    done(exchange);
128                }
129            }
130        }
131    
132        private void display(MessageExchange exchange, String msg, StringBuffer sb) {
133            NormalizedMessage message = exchange.getMessage(msg);
134            if (message != null) {
135                sb.append("  ").append(msg).append(": [").append('\n');
136                sb.append("    content: ");
137                try {
138                    if (message.getContent() != null) {
139                        Node node = sourceTransformer.toDOMNode(message.getContent());
140                        message.setContent(new DOMSource(node));
141                        String str = sourceTransformer.toString(node);
142                        if (maxMsgDisplaySize < 0 || str.length() <= maxMsgDisplaySize) { 
143                            sb.append(str);
144                        } else {
145                            sb.append(str.substring(0, maxMsgDisplaySize)).append("...");
146                        }                                         
147                    } else {
148                        sb.append("null");
149                    }
150                } catch (Exception e) {
151                    sb.append("Unable to display: ").append(e);
152                }
153                sb.append('\n');
154                if (message.getAttachmentNames().size() > 0) {
155                    sb.append("    attachments: [").append('\n');
156                    for (String key : (Set<String>) message.getAttachmentNames()) {
157                        sb.append("      ").append(key).append(" = ").append(message.getAttachment(key)).append('\n');
158                    }
159                    sb.append("    ]").append('\n');
160                }
161                if (message.getPropertyNames().size() > 0) {
162                    sb.append("    properties: [").append('\n');
163                    for (String key : (Set<String>) message.getPropertyNames()) {
164                        sb.append("      ").append(key).append(" = ");
165                        Object contents = message.getProperty(key);
166                        if (contents instanceof Source) {
167                            try {
168                                contents = sourceTransformer.toString((Source) contents);
169                            } catch (Exception e) { }
170                        }
171                        sb.append(contents);
172                        sb.append('\n');
173                    }
174                    sb.append("    ]").append('\n');
175                }
176                sb.append("  ]").append('\n');
177            }
178        }
179    
180    }