KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jms.Message JavaDoc;
20 import javax.jms.Session JavaDoc;
21
22 import org.apache.log.ErrorHandler;
23 import org.apache.log.LogEvent;
24 import org.apache.log.output.AbstractTarget;
25
26 /**
27  * A target that writes to a JMS Topic.
28  *
29  * @author Peter Donald
30  */

31 public abstract class AbstractJMSTarget
32     extends AbstractTarget
33 {
34     ///Appropriate MessageBuilder
35
private MessageBuilder m_builder;
36
37     public AbstractJMSTarget( final MessageBuilder builder )
38     {
39         m_builder = builder;
40     }
41
42     public AbstractJMSTarget( final MessageBuilder builder,
43                               final ErrorHandler errorHandler )
44     {
45         super( errorHandler );
46         m_builder = builder;
47     }
48
49     protected abstract void send( Message JavaDoc message );
50
51     protected abstract Session JavaDoc getSession();
52
53     /**
54      * Process a log event, via formatting and outputting it.
55      *
56      * @param event the log event
57      */

58     protected void doProcessEvent( final LogEvent event )
59         throws Exception JavaDoc
60     {
61         final Message JavaDoc message =
62             m_builder.buildMessage( getSession(), event );
63         send( message );
64     }
65
66     /**
67      * Startup log session.
68      *
69      */

70     protected synchronized void open()
71     {
72         if( !isOpen() )
73         {
74             super.open();
75             openConnection();
76         }
77     }
78
79     /**
80      * Shutdown target.
81      * Attempting to write to target after close() will cause errors to be logged.
82      *
83      */

84     public synchronized void close()
85     {
86         if( isOpen() )
87         {
88             closeConnection();
89             super.close();
90         }
91     }
92
93     protected abstract void openConnection();
94
95     protected abstract void closeConnection();
96 }
97
Popular Tags