KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > JmsLogAppenderSupport


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

18 package org.apache.activemq.util;
19
20 import org.apache.log4j.AppenderSkeleton;
21 import org.apache.log4j.spi.LoggingEvent;
22
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.Destination JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageProducer JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * An abstract base class for implementation inheritence for a log4j JMS appender
37  *
38  * @version $Revision: 449919 $
39  */

40 public abstract class JmsLogAppenderSupport extends AppenderSkeleton {
41
42     public static final int JMS_PUBLISH_ERROR_CODE = 61616;
43
44     private Connection JavaDoc connection;
45     private Session JavaDoc session;
46     private MessageProducer JavaDoc producer;
47     private boolean allowTextMessages = true;
48     private String JavaDoc subjectPrefix = "log4j.";
49
50     public JmsLogAppenderSupport() {
51     }
52
53     public Connection JavaDoc getConnection() throws JMSException JavaDoc, NamingException JavaDoc {
54         if (connection == null) {
55             connection = createConnection();
56         }
57         return connection;
58     }
59
60     public void setConnection(Connection JavaDoc connection) {
61         this.connection = connection;
62     }
63
64     public Session JavaDoc getSession() throws JMSException JavaDoc, NamingException JavaDoc {
65         if (session == null) {
66             session = createSession();
67         }
68         return session;
69     }
70
71     public void setSession(Session JavaDoc session) {
72         this.session = session;
73     }
74
75     public MessageProducer JavaDoc getProducer() throws JMSException JavaDoc, NamingException JavaDoc {
76         if (producer == null) {
77             producer = createProducer();
78         }
79         return producer;
80     }
81
82     public void setProducer(MessageProducer JavaDoc producer) {
83         this.producer = producer;
84     }
85
86     public void close() {
87         List JavaDoc errors = new ArrayList JavaDoc();
88         if (producer != null) {
89             try {
90                 producer.close();
91             }
92             catch (JMSException JavaDoc e) {
93                 errors.add(e);
94             }
95         }
96         if (session != null) {
97             try {
98                 session.close();
99             }
100             catch (JMSException JavaDoc e) {
101                 errors.add(e);
102             }
103         }
104         if (connection != null) {
105             try {
106                 connection.close();
107             }
108             catch (JMSException JavaDoc e) {
109                 errors.add(e);
110             }
111         }
112         for (Iterator JavaDoc iter = errors.iterator(); iter.hasNext();) {
113             JMSException JavaDoc e = (JMSException JavaDoc) iter.next();
114             getErrorHandler().error("Error closing JMS resources: " + e, e, JMS_PUBLISH_ERROR_CODE);
115         }
116     }
117
118     public boolean requiresLayout() {
119         return false;
120     }
121
122     public void activateOptions() {
123         try {
124             // lets ensure we're all created
125
getProducer();
126         }
127         catch (Exception JavaDoc e) {
128             getErrorHandler().error("Could not create JMS resources: " + e, e, JMS_PUBLISH_ERROR_CODE);
129         }
130     }
131
132
133     // Implementation methods
134
//-------------------------------------------------------------------------
135
protected abstract Connection JavaDoc createConnection() throws JMSException JavaDoc, NamingException JavaDoc;
136
137     protected Session JavaDoc createSession() throws JMSException JavaDoc, NamingException JavaDoc {
138         return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
139     }
140
141     protected MessageProducer JavaDoc createProducer() throws JMSException JavaDoc, NamingException JavaDoc {
142         return getSession().createProducer(null);
143     }
144
145     protected void append(LoggingEvent event) {
146         try {
147             Message JavaDoc message = createMessage(event);
148             Destination JavaDoc destination = getDestination(event);
149             getProducer().send(destination, message);
150         }
151         catch (Exception JavaDoc e) {
152             getErrorHandler().error("Could not send message due to: " + e, e, JMS_PUBLISH_ERROR_CODE, event);
153         }
154     }
155
156     protected Message JavaDoc createMessage(LoggingEvent event) throws JMSException JavaDoc, NamingException JavaDoc {
157         Message JavaDoc answer = null;
158         Object JavaDoc value = event.getMessage();
159         if (allowTextMessages && value instanceof String JavaDoc) {
160             answer = getSession().createTextMessage((String JavaDoc) value);
161         }
162         else {
163             answer = getSession().createObjectMessage((Serializable JavaDoc) value);
164         }
165         answer.setStringProperty("level", event.getLevel().toString());
166         answer.setIntProperty("levelInt", event.getLevel().toInt());
167         answer.setStringProperty("threadName", event.getThreadName());
168         return answer;
169     }
170
171     protected Destination JavaDoc getDestination(LoggingEvent event) throws JMSException JavaDoc, NamingException JavaDoc {
172         String JavaDoc name = subjectPrefix + event.getLoggerName();
173         return getSession().createTopic(name);
174     }
175 }
176
Popular Tags