1 17 package org.apache.log.output.jms; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 import javax.jms.JMSException ; 22 import javax.jms.Message ; 23 import javax.jms.Session ; 24 import javax.jms.TextMessage ; 25 import org.apache.log.ContextMap; 26 import org.apache.log.LogEvent; 27 import org.apache.log.format.Formatter; 28 29 35 public class TextMessageBuilder 36 implements MessageBuilder 37 { 38 private final PropertyInfo[] m_properties; 39 private final Formatter m_formatter; 40 41 45 public TextMessageBuilder( final Formatter formatter ) 46 { 47 m_properties = new PropertyInfo[ 0 ]; 48 m_formatter = formatter; 49 } 50 51 56 public TextMessageBuilder( final PropertyInfo[] properties, 57 final Formatter formatter ) 58 { 59 m_properties = properties; 60 m_formatter = formatter; 61 } 62 63 70 public Message buildMessage( final Session session, final LogEvent event ) 71 throws JMSException 72 { 73 synchronized( session ) 74 { 75 final TextMessage message = session.createTextMessage(); 76 77 message.setText( getText( event ) ); 78 for( int i = 0; i < m_properties.length; i++ ) 79 { 80 setProperty( message, i, event ); 81 } 82 83 return message; 84 } 85 } 86 87 93 private void setProperty( final TextMessage message, 94 final int index, 95 final LogEvent event ) 96 throws JMSException 97 { 98 final PropertyInfo info = m_properties[ index ]; 99 final String name = info.getName(); 100 101 switch( info.getType() ) 102 { 103 case PropertyType.MESSAGE: 104 message.setStringProperty( name, event.getMessage() ); 105 break; 106 107 case PropertyType.RELATIVE_TIME: 108 message.setLongProperty( name, event.getRelativeTime() ); 109 break; 110 111 case PropertyType.TIME: 112 message.setLongProperty( name, event.getTime() ); 113 break; 114 115 case PropertyType.CATEGORY: 116 message.setStringProperty( name, event.getCategory() ); 117 break; 118 119 case PropertyType.PRIORITY: 120 message.setStringProperty( name, event.getPriority().getName() ); 121 break; 122 123 case PropertyType.CONTEXT: 124 message.setStringProperty( name, getContextMap( event.getContextMap(), 125 info.getAux() ) ); 126 break; 127 128 case PropertyType.STATIC: 129 message.setStringProperty( name, info.getAux() ); 130 break; 131 132 case PropertyType.THROWABLE: 133 message.setStringProperty( name, getStackTrace( event.getThrowable() ) ); 134 break; 135 136 default: 137 throw new IllegalStateException ( "Unknown PropertyType: " + info.getType() ); 138 } 139 140 } 141 142 private String getText( final LogEvent event ) 143 { 144 if( null == m_formatter ) 145 { 146 return event.getMessage(); 147 } 148 else 149 { 150 return m_formatter.format( event ); 151 } 152 } 153 154 private String getStackTrace( final Throwable throwable ) 155 { 156 if( null == throwable ) 157 { 158 return ""; 159 } 160 161 final StringWriter stringWriter = new StringWriter (); 162 final PrintWriter printWriter = new PrintWriter ( stringWriter ); 163 throwable.printStackTrace( printWriter ); 164 165 return stringWriter.getBuffer().toString(); 166 } 167 168 private String getContextMap( final ContextMap map, final String aux ) 169 { 170 if( null == map ) 171 { 172 return ""; 173 } 174 return map.get( aux, "" ).toString(); 175 } 176 } 177 | Popular Tags |