KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > jms > TextMessageBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.jms;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.Session JavaDoc;
24 import javax.jms.TextMessage JavaDoc;
25 import org.apache.log.ContextMap;
26 import org.apache.log.LogEvent;
27 import org.apache.log.format.Formatter;
28
29 /**
30  * Basic message factory that stores LogEvent in Message.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @author Peter Donald
34  */

35 public class TextMessageBuilder
36     implements MessageBuilder
37 {
38     private final PropertyInfo[] m_properties;
39     private final Formatter m_formatter;
40
41     /**
42      * Creation of a new text message builder.
43      * @param formatter the message formatter
44      */

45     public TextMessageBuilder( final Formatter formatter )
46     {
47         m_properties = new PropertyInfo[ 0 ];
48         m_formatter = formatter;
49     }
50
51     /**
52      * Creation of a new text message builder.
53      * @param properties the property info set
54      * @param formatter the message formatter
55      */

56     public TextMessageBuilder( final PropertyInfo[] properties,
57                                final Formatter formatter )
58     {
59         m_properties = properties;
60         m_formatter = formatter;
61     }
62
63     /**
64      * Build a message from the supplied session for the supplied event
65      * @param session the session
66      * @param event the log event
67      * @return the message
68      * @exception JMSException if a messaging related error occurs
69      */

70     public Message JavaDoc buildMessage( final Session JavaDoc session, final LogEvent event )
71         throws JMSException JavaDoc
72     {
73         synchronized( session )
74         {
75             final TextMessage JavaDoc 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     /**
88      * Set a property
89      * @param message the text message
90      * @param index the index
91      * @param event the log event
92      */

93     private void setProperty( final TextMessage JavaDoc message,
94                               final int index,
95                               final LogEvent event )
96         throws JMSException JavaDoc
97     {
98         final PropertyInfo info = m_properties[ index ];
99         final String JavaDoc 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 JavaDoc( "Unknown PropertyType: " + info.getType() );
138         }
139
140     }
141
142     private String JavaDoc 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 JavaDoc getStackTrace( final Throwable JavaDoc throwable )
155     {
156         if( null == throwable )
157         {
158             return "";
159         }
160
161         final StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
162         final PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc( stringWriter );
163         throwable.printStackTrace( printWriter );
164
165         return stringWriter.getBuffer().toString();
166     }
167
168     private String JavaDoc getContextMap( final ContextMap map, final String JavaDoc aux )
169     {
170         if( null == map )
171         {
172             return "";
173         }
174         return map.get( aux, "" ).toString();
175     }
176 }
177
Popular Tags