KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > TraceComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.MessageExchangeListener;
22 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
23
24 import javax.jbi.messaging.MessageExchange;
25 import javax.jbi.messaging.MessagingException;
26 import javax.jbi.messaging.NormalizedMessage;
27 import javax.xml.transform.TransformerException JavaDoc;
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: 426415 $
34  */

35 public class TraceComponent extends ComponentSupport implements MessageExchangeListener {
36
37     private Log log = LogFactory.getLog(TraceComponent.class);
38
39     private SourceTransformer sourceTransformer = new SourceTransformer();
40
41     public Log getLog() {
42         return log;
43     }
44
45     public void setLog(Log log) {
46         this.log = log;
47     }
48
49     public SourceTransformer getSourceTransformer() {
50         return sourceTransformer;
51     }
52
53     public void setSourceTransformer(SourceTransformer sourceTransformer) {
54         this.sourceTransformer = sourceTransformer;
55     }
56
57     public void onMessageExchange(MessageExchange exchange) throws MessagingException {
58         // lets dump the incoming message
59
NormalizedMessage message = exchange.getMessage("in");
60         if (message == null) {
61             log.warn("Received null message from exchange: " + exchange);
62         }
63         else {
64             log.info("Exchange: " + exchange + " received IN message: " + message);
65             try {
66                 log.info("Body is: " + sourceTransformer.toString(message.getContent()));
67             }
68             catch (TransformerException JavaDoc e) {
69                 log.error("Failed to turn message body into text: " + e, e);
70             }
71         }
72         done(exchange);
73     }
74 }
75
Popular Tags