1 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 ; 26 import javax.jms.TopicConnectionFactory ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.Destination ; 29 import javax.jms.Topic ; 30 import javax.jms.Queue ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NameNotFoundException ; 34 import javax.naming.NamingException ; 35 36 37 67 public class JMSTargetFactory implements LogTargetFactory 68 { 69 70 public LogTarget createTarget( final Configuration configuration ) 71 throws ConfigurationException 72 { 73 final String factoryName = 74 configuration.getChild( "connection-factory", true ).getValue(); 75 76 final Configuration destinationConf = 77 configuration.getChild( "destination", true ); 78 79 final String destinationName = destinationConf.getValue(); 80 final String 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 factory; 88 final Destination destination; 89 final LogTarget logTarget; 90 91 try 92 { 93 Context ctx = new InitialContext (); 94 factory = (ConnectionFactory ) ctx.lookup( factoryName ); 95 destination = (Destination ) ctx.lookup( destinationName ); 96 } 97 catch ( NameNotFoundException nnfe ) 98 { 99 throw new ConfigurationException( "Cannot lookup object", nnfe ); 100 } 101 catch ( NamingException 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 ) factory, (Queue ) destination ); 110 } 111 else 112 { 113 logTarget = new JMSTopicTarget( messageBuilder, 114 (TopicConnectionFactory ) factory, (Topic ) destination ); 115 } 116 117 return logTarget; 118 } 119 120 private MessageBuilder getMessageBuilder( final Configuration configuration ) 121 throws ConfigurationException 122 { 123 final String 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 name = propertyConf[i].getValue(); 136 final int type = PropertyType.getTypeIdFor( propertyConf[i].getName() ); 137 final String 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 |