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.activemq.transport.logwriters;
018
019 import java.io.IOException;
020
021 import org.apache.activemq.command.BaseCommand;
022 import org.apache.activemq.command.ConnectionInfo;
023 import org.apache.activemq.command.Message;
024 import org.apache.activemq.command.MessageAck;
025 import org.apache.activemq.command.MessageDispatch;
026 import org.apache.activemq.command.ProducerAck;
027 import org.apache.activemq.command.ProducerId;
028 import org.apache.activemq.command.WireFormatInfo;
029 import org.apache.activemq.transport.LogWriter;
030 import org.slf4j.Logger;
031
032 /**
033 * Custom implementation of LogWriter interface.
034 *
035 * @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com
036 *
037 */
038 public class CustomLogWriter implements LogWriter {
039
040 // doc comment inherited from LogWriter
041 public void initialMessage(Logger log) {
042
043 }
044
045 // doc comment inherited from LogWriter
046 public void logRequest (Logger log, Object command) {
047 log.debug("$$ SENDREQ: " + CustomLogWriter.commandToString(command));
048 }
049
050 // doc comment inherited from LogWriter
051 public void logResponse (Logger log, Object response) {
052 log.debug("$$ GOT_RESPONSE: "+response);
053 }
054
055 // doc comment inherited from LogWriter
056 public void logAsyncRequest (Logger log, Object command) {
057 log.debug("$$ SENDING_ASNYC_REQUEST: "+command);
058 }
059
060 // doc comment inherited from LogWriter
061 public void logOneWay (Logger log, Object command) {
062 log.debug("$$ SENDING: " + CustomLogWriter.commandToString(command));
063 }
064
065 // doc comment inherited from LogWriter
066 public void logReceivedCommand (Logger log, Object command) {
067 log.debug("$$ RECEIVED: " + CustomLogWriter.commandToString(command));
068 }
069
070 // doc comment inherited from LogWriter
071 public void logReceivedException (Logger log, IOException error) {
072 log.debug("$$ RECEIVED_EXCEPTION: "+error, error);
073 }
074
075 /**
076 * Transforms a command into a String
077 * @param command An object (hopefully of the BaseCommand class or subclass)
078 * to be transformed into String.
079 * @return A String which will be written by the CustomLogWriter.
080 * If the object is not a BaseCommand, the String
081 * "Unrecognized_object " + command.toString()
082 * will be returned.
083 */
084 private static String commandToString(Object command) {
085 StringBuilder sb = new StringBuilder();
086
087 if (command instanceof BaseCommand) {
088
089 BaseCommand bc = (BaseCommand)command;
090 sb.append(command.getClass().getSimpleName());
091 sb.append(' ');
092 sb.append(bc.isResponseRequired() ? 'T' : 'F');
093
094
095 Message m = null;
096
097 if (bc instanceof Message) {
098 m = (Message)bc;
099 }
100 if (bc instanceof MessageDispatch){
101 m = ((MessageDispatch)bc).getMessage();
102 }
103
104 if (m != null) {
105 sb.append(' ');
106 sb.append(m.getMessageId());
107 sb.append(',');
108 sb.append(m.getCommandId());
109 ProducerId pid = m.getProducerId();
110 long sid = pid.getSessionId();
111 sb.append(',');
112 sb.append(pid.getConnectionId());
113 sb.append(',');
114 sb.append(sid);
115 sb.append(',');
116 sb.append(pid.getValue());
117 sb.append(',');
118 sb.append(m.getCorrelationId());
119 sb.append(',');
120 sb.append(m.getType());
121 }
122
123 if (bc instanceof MessageDispatch){
124 sb.append(" toConsumer:");
125 sb.append(((MessageDispatch)bc).getConsumerId());
126 }
127
128 if (bc instanceof ProducerAck) {
129 sb.append(" ProducerId:");
130 sb.append(((ProducerAck)bc).getProducerId());
131 }
132
133 if (bc instanceof MessageAck) {
134 MessageAck ma = (MessageAck)bc;
135 sb.append(" ConsumerID:");
136 sb.append(ma.getConsumerId());
137 sb.append(" ack:");
138 sb.append(ma.getFirstMessageId());
139 sb.append('-');
140 sb.append(ma.getLastMessageId());
141 }
142
143 if (bc instanceof ConnectionInfo) {
144 ConnectionInfo ci = (ConnectionInfo)bc;
145
146 sb.append(' ');
147 sb.append(ci.getConnectionId());
148 }
149
150 } else if (command instanceof WireFormatInfo){
151 sb.append("WireFormatInfo");
152
153 } else {
154 sb.append("Unrecognized_object ");
155 sb.append(command.toString());
156 }
157
158 return sb.toString();
159 }
160
161 }