KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > factory > JMSTargetFactory


1 /*
2  * JMSTopicTargetFactory.java
3  *
4  * Created on August 26, 2001, 5:17 PM
5  */

6
7 package org.apache.avalon.excalibur.logger.factory;
8
9 import org.apache.avalon.excalibur.logger.LogTargetFactory;
10
11 import org.apache.log.LogTarget;
12 import org.apache.log.output.jms.JMSTopicTarget;
13 import org.apache.log.output.jms.JMSQueueTarget;
14 import org.apache.log.output.jms.MessageBuilder;
15 import org.apache.log.output.jms.ObjectMessageBuilder;
16 import org.apache.log.output.jms.TextMessageBuilder;
17 import org.apache.log.format.Formatter;
18 import org.apache.log.output.jms.PropertyInfo;
19 import org.apache.log.output.jms.PropertyType;
20
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24
25 import javax.jms.ConnectionFactory JavaDoc;
26 import javax.jms.TopicConnectionFactory JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.Topic JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NameNotFoundException JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36
37 /**
38  * Factory for JMS LogTarget-s. The configuration looks like this:
39  *
40  * <pre>
41  * &lt;jms id="name"&gt;
42  * &lt;connection-factory&gt;java:/TopicConectionFactory&lt;/connection-factory&gt;
43  * &lt;destination type="topic|queue"&gt;jms/LogDestination&lt;/destination&gt;
44  * &lt;message type="object|text"&gt;
45  *
46  * -if type="text":
47  * &lt;property&gt;
48  * &lt;category&gt;CATEGORY&lt;/category&gt;
49  * &lt;priority&gt;PRIORITY&lt;/priority&gt;
50  * &lt;time&gt;TIME&lt;/time&gt;
51  * &lt;rtime&gt;RTIME&lt;/rtime&gt;
52  * &lt;throwable&gt;THROWABLE&lt;/throwable&gt;
53  * &lt;hostname&gt;HOSTNAME&lt;/hostname&gt;
54  * &lt;static aux="234523454325"&gt;SYSTEM&lt;/static&gt;
55  * &lt;context aux="principal"&gt;PRINCIPAL&lt;/context&gt;
56  * &lt;context aux="ipaddress"&gt;IPADDRESS&lt;/context&gt;
57  * &lt;context aux="username"&gt;USERNAME&lt;/context&gt;
58  * &lt;/property&gt;
59  * &lt;format type="exteded"&gt;%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\n%{throwable}&lt;/format&gt;
60  * &lt;/message&gt;
61  * &lt;/jms&gt;
62  * </pre>
63  *
64  * @author <a HREF="mailto:mirceatoma@home.com">Mircea Toma</a>;
65  * @version CVS $Revision: 1.2 $ $Date: 2001/12/19 23:36:25 $
66  */

67 public class JMSTargetFactory implements LogTargetFactory
68 {
69     
70     public LogTarget createTarget( final Configuration configuration )
71     throws ConfigurationException
72     {
73         final String JavaDoc factoryName =
74             configuration.getChild( "connection-factory", true ).getValue();
75         
76         final Configuration destinationConf =
77             configuration.getChild( "destination", true );
78         
79         final String JavaDoc destinationName = destinationConf.getValue();
80         final String JavaDoc destinationType =
81             destinationConf.getAttribute( "type", "topic" );
82         
83         final Configuration messageConf =
84             configuration.getChild( "message", true );
85         
86         final MessageBuilder messageBuilder = getMessageBuilder( messageConf );
87         final ConnectionFactory JavaDoc factory;
88         final Destination JavaDoc destination;
89         final LogTarget logTarget;
90         
91         try
92         {
93             Context JavaDoc ctx = new InitialContext JavaDoc();
94             factory = (ConnectionFactory JavaDoc) ctx.lookup( factoryName );
95             destination = (Destination JavaDoc) ctx.lookup( destinationName );
96         }
97         catch ( NameNotFoundException JavaDoc nnfe )
98         {
99             throw new ConfigurationException( "Cannot lookup object", nnfe );
100         }
101         catch ( NamingException JavaDoc ne )
102         {
103             throw new ConfigurationException( "Cannot get naming context", ne );
104         }
105         
106         if ( "queue".equals( destinationType ) )
107         {
108             logTarget = new JMSQueueTarget( messageBuilder,
109                     (QueueConnectionFactory JavaDoc) factory, (Queue JavaDoc) destination );
110         }
111         else
112         {
113             logTarget = new JMSTopicTarget( messageBuilder,
114                 (TopicConnectionFactory JavaDoc) factory, (Topic JavaDoc) destination );
115         }
116         
117         return logTarget;
118     }
119     
120     private MessageBuilder getMessageBuilder( final Configuration configuration )
121     throws ConfigurationException
122     {
123         final String JavaDoc messageType = configuration.getAttribute( "type", "object" );
124         
125         if ( "text".equals( messageType ) )
126         {
127             final Configuration[] propertyConf =
128                 configuration.getChild( "property", true ).getChildren();
129             final Configuration formatterConf = configuration.getChild( "format" );
130
131             final PropertyInfo[] properties = new PropertyInfo[ propertyConf.length ];
132             
133             for (int i = 0; i < properties.length; i++)
134             {
135                 final String JavaDoc name = propertyConf[i].getValue();
136                 final int type = PropertyType.getTypeIdFor( propertyConf[i].getName() );
137                 final String JavaDoc aux = propertyConf[i].getAttribute( "aux", null );
138
139                 properties[i] = new PropertyInfo(name, type, aux);
140             }
141
142             final Formatter formatter = getFormatter( formatterConf );
143             
144             return new TextMessageBuilder( properties, formatter );
145         }
146                 
147         return new ObjectMessageBuilder();
148     }
149
150     protected Formatter getFormatter( final Configuration conf )
151     {
152         Formatter formatter = null;
153
154         if ( null != conf )
155         {
156             final FormatterFactory formatterFactory = new FormatterFactory();
157             formatter = formatterFactory.createFormatter( conf );
158         }
159         
160         return formatter;
161     }
162 }
163
Popular Tags