01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.components.util;
18:
19: import javax.jbi.messaging.MessageExchange;
20: import javax.jbi.messaging.MessagingException;
21: import javax.jbi.messaging.NormalizedMessage;
22: import javax.xml.transform.TransformerException;
23:
24: import org.apache.commons.logging.Log;
25: import org.apache.commons.logging.LogFactory;
26: import org.apache.servicemix.MessageExchangeListener;
27: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
28:
29: /**
30: * A simple tracing component which can be placed inside a pipeline
31: * to trace the message exchange though the component.
32: *
33: * @version $Revision: 564374 $
34: */
35: public class TraceComponent extends ComponentSupport implements
36: MessageExchangeListener {
37:
38: private Log log = LogFactory.getLog(TraceComponent.class);
39:
40: private SourceTransformer sourceTransformer = new SourceTransformer();
41:
42: public Log getLog() {
43: return log;
44: }
45:
46: public void setLog(Log log) {
47: this .log = log;
48: }
49:
50: public SourceTransformer getSourceTransformer() {
51: return sourceTransformer;
52: }
53:
54: public void setSourceTransformer(SourceTransformer sourceTransformer) {
55: this .sourceTransformer = sourceTransformer;
56: }
57:
58: public void onMessageExchange(MessageExchange exchange)
59: throws MessagingException {
60: // lets dump the incoming message
61: NormalizedMessage message = exchange.getMessage("in");
62: if (message == null) {
63: log
64: .warn("Received null message from exchange: "
65: + exchange);
66: } else {
67: log.info("Exchange: " + exchange + " received IN message: "
68: + message);
69: try {
70: log.info("Body is: "
71: + sourceTransformer.toString(message
72: .getContent()));
73: } catch (TransformerException e) {
74: log.error(
75: "Failed to turn message body into text: " + e,
76: e);
77: }
78: }
79: done(exchange);
80: }
81: }
|