001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.output.jms;
056:
057: import java.io.PrintWriter;
058: import java.io.StringWriter;
059: import javax.jms.JMSException;
060: import javax.jms.Message;
061: import javax.jms.Session;
062: import javax.jms.TextMessage;
063: import org.apache.log.ContextMap;
064: import org.apache.log.LogEvent;
065: import org.apache.log.format.Formatter;
066:
067: /**
068: * Basic message factory that stores LogEvent in Message.
069: *
070: * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
071: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
072: */
073: public class TextMessageBuilder implements MessageBuilder {
074: private final PropertyInfo[] m_properties;
075: private final Formatter m_formatter;
076:
077: /**
078: * Creation of a new text message builder.
079: * @param formatter the message formatter
080: */
081: public TextMessageBuilder(final Formatter formatter) {
082: m_properties = new PropertyInfo[0];
083: m_formatter = formatter;
084: }
085:
086: /**
087: * Creation of a new text message builder.
088: * @param properties the property info set
089: * @param formatter the message formatter
090: */
091: public TextMessageBuilder(final PropertyInfo[] properties,
092: final Formatter formatter) {
093: m_properties = properties;
094: m_formatter = formatter;
095: }
096:
097: /**
098: * Build a message from the supplied session for the supplied event
099: * @param session the session
100: * @param event the log event
101: * @return the message
102: * @exception JMSException if a messaging related error occurs
103: */
104: public Message buildMessage(final Session session,
105: final LogEvent event) throws JMSException {
106: synchronized (session) {
107: final TextMessage message = session.createTextMessage();
108:
109: message.setText(getText(event));
110: for (int i = 0; i < m_properties.length; i++) {
111: setProperty(message, i, event);
112: }
113:
114: return message;
115: }
116: }
117:
118: /**
119: * Set a property
120: * @param message the text message
121: * @param index the index
122: * @param event the log event
123: */
124: private void setProperty(final TextMessage message,
125: final int index, final LogEvent event) throws JMSException {
126: final PropertyInfo info = m_properties[index];
127: final String name = info.getName();
128:
129: switch (info.getType()) {
130: case PropertyType.MESSAGE:
131: message.setStringProperty(name, event.getMessage());
132: break;
133:
134: case PropertyType.RELATIVE_TIME:
135: message.setLongProperty(name, event.getRelativeTime());
136: break;
137:
138: case PropertyType.TIME:
139: message.setLongProperty(name, event.getTime());
140: break;
141:
142: case PropertyType.CATEGORY:
143: message.setStringProperty(name, event.getCategory());
144: break;
145:
146: case PropertyType.PRIORITY:
147: message.setStringProperty(name, event.getPriority()
148: .getName());
149: break;
150:
151: case PropertyType.CONTEXT:
152: message.setStringProperty(name, getContextMap(event
153: .getContextMap(), info.getAux()));
154: break;
155:
156: case PropertyType.STATIC:
157: message.setStringProperty(name, info.getAux());
158: break;
159:
160: case PropertyType.THROWABLE:
161: message.setStringProperty(name, getStackTrace(event
162: .getThrowable()));
163: break;
164:
165: default:
166: throw new IllegalStateException("Unknown PropertyType: "
167: + info.getType());
168: }
169:
170: }
171:
172: private String getText(final LogEvent event) {
173: if (null == m_formatter) {
174: return event.getMessage();
175: } else {
176: return m_formatter.format(event);
177: }
178: }
179:
180: private String getStackTrace(final Throwable throwable) {
181: if (null == throwable) {
182: return "";
183: }
184:
185: final StringWriter stringWriter = new StringWriter();
186: final PrintWriter printWriter = new PrintWriter(stringWriter);
187: throwable.printStackTrace(printWriter);
188:
189: return stringWriter.getBuffer().toString();
190: }
191:
192: private String getContextMap(final ContextMap map, final String aux) {
193: if (null == map) {
194: return "";
195: }
196: return map.get(aux, "").toString();
197: }
198: }
|