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.camel.processor.interceptor; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Message; 021 import org.apache.camel.spi.UnitOfWork; 022 import org.apache.camel.util.ObjectHelper; 023 024 /** 025 * @version $Revision: 676850 $ 026 */ 027 public class TraceFormatter { 028 private boolean showBreadCrumb = true; 029 private boolean showNode = true; 030 private boolean showExchangeId; 031 private boolean showProperties = true; 032 private boolean showHeaders = true; 033 private boolean showBody = true; 034 private boolean showBodyType = true; 035 036 public Object format(TraceInterceptor interceptor, Exchange exchange) { 037 Message in = exchange.getIn(); 038 Throwable exception = exchange.getException(); 039 return (showBreadCrumb ? getBreadCrumbID(exchange) + " " : "") 040 + "-> " + getNodeMessage(interceptor) + " " 041 + (showNode ? interceptor.getNode() + " " : "") 042 + exchange.getPattern() 043 + (showExchangeId ? " Id: " + exchange.getExchangeId() : "") 044 + (showProperties ? " Properties:" + exchange.getProperties() : "") 045 + (showHeaders ? " Headers:" + in.getHeaders() : "") 046 + (showBodyType ? " BodyType:" + getBodyTypeAsString(in) : "") 047 + (showBody ? " Body:" + getBodyAsString(in) : "") 048 + (exception != null ? " Exception: " + exception : ""); 049 } 050 051 public boolean isShowBody() { 052 return showBody; 053 } 054 055 public void setShowBody(boolean showBody) { 056 this.showBody = showBody; 057 } 058 059 public boolean isShowBodyType() { 060 return showBodyType; 061 } 062 063 public void setShowBodyType(boolean showBodyType) { 064 this.showBodyType = showBodyType; 065 } 066 067 public boolean isShowBreadCrumb() { 068 return showBreadCrumb; 069 } 070 071 public void setShowBreadCrumb(boolean showBreadCrumb) { 072 this.showBreadCrumb = showBreadCrumb; 073 } 074 075 public boolean isShowExchangeId() { 076 return showExchangeId; 077 } 078 079 public void setShowExchangeId(boolean showExchangeId) { 080 this.showExchangeId = showExchangeId; 081 } 082 083 public boolean isShowHeaders() { 084 return showHeaders; 085 } 086 087 public void setShowHeaders(boolean showHeaders) { 088 this.showHeaders = showHeaders; 089 } 090 091 public boolean isShowProperties() { 092 return showProperties; 093 } 094 095 public void setShowProperties(boolean showProperties) { 096 this.showProperties = showProperties; 097 } 098 099 public boolean isShowNode() { 100 return showNode; 101 } 102 103 public void setShowNode(boolean showNode) { 104 this.showNode = showNode; 105 } 106 107 // Implementation methods 108 //------------------------------------------------------------------------- 109 protected Object getBreadCrumbID(Exchange exchange) { 110 UnitOfWork unitOfWork = exchange.getUnitOfWork(); 111 return unitOfWork.getId(); 112 } 113 114 protected Object getBodyAsString(Message in) { 115 Object answer = in.getBody(String.class); 116 if (answer == null) { 117 answer = in.getBody(); 118 } 119 return answer; 120 } 121 122 protected Object getBodyTypeAsString(Message message) { 123 String answer = ObjectHelper.className(message.getBody()); 124 if (answer.startsWith("java.lang.")) { 125 return answer.substring(10); 126 } 127 return answer; 128 } 129 130 protected String getNodeMessage(TraceInterceptor interceptor) { 131 return interceptor.getNode().idOrCreate(); 132 } 133 134 }