1 19 package org.apache.avalon.excalibur.logger.factory; 20 21 import javax.mail.Address ; 22 import javax.mail.Session ; 23 import javax.mail.internet.AddressException ; 24 import javax.mail.internet.InternetAddress ; 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.parameters.Parameters; 29 import org.apache.log.LogTarget; 30 import org.apache.log.format.Formatter; 31 import org.apache.log.output.net.SMTPOutputLogTarget; 32 33 89 public class SMTPTargetFactory 90 extends AbstractTargetFactory 91 { 92 99 public final LogTarget createTarget( final Configuration config ) 100 throws ConfigurationException 101 { 102 try 103 { 104 SMTPOutputLogTarget logTarget = new SMTPOutputLogTarget( 105 getSession( config ), 106 getToAddresses( config ), 107 getFromAddress( config ), 108 getSubject( config ), 109 getMaxSize( config ), 110 getMaxDelayTime( config ), 111 getFormatter( config ) 112 ); 113 114 boolean debug = getDebug( config ); 117 if ( debug ) 118 { 119 logTarget.setDebug( debug ); 120 } 121 122 return logTarget; 123 } 124 catch( final ContextException ce ) 125 { 126 throw new ConfigurationException( "Cannot find Session object in " + 127 "application context", ce ); 128 } 129 catch( final AddressException ae ) 130 { 131 throw new ConfigurationException( "Cannot create address", ae ); 132 } 133 } 134 135 141 protected Formatter getFormatter( final Configuration config ) 142 { 143 final Configuration confFormat = config.getChild( "format" ); 144 145 if( null != confFormat ) 146 { 147 final FormatterFactory formatterFactory = new FormatterFactory(); 148 return formatterFactory.createFormatter( confFormat ); 149 } 150 151 return null; 152 } 153 154 171 protected Session getSession( Configuration config ) 172 throws ContextException, ConfigurationException 173 { 174 final Configuration sessionConfig = config.getChild( "session", false ); 175 176 if( null == sessionConfig ) 177 { 178 final String contextkey = 179 m_configuration.getAttribute( "context-key", "session-context" ); 180 181 if( m_context == null ) 182 { 183 throw new ConfigurationException( "Context not available" ); 184 } 185 186 return (Session )m_context.get( contextkey ); 187 } 188 else 189 { 190 return Session.getInstance( 191 Parameters.toProperties( 192 Parameters.fromConfiguration( sessionConfig ) ) ); 193 } 194 } 195 196 203 private String getSubject( Configuration config ) 204 throws ConfigurationException 205 { 206 return config.getChild( "subject" ).getValue(); 207 } 208 209 216 private int getMaxSize( Configuration config ) 217 throws ConfigurationException 218 { 219 return config.getChild( "maximum-size" ).getValueAsInteger( 1 ); 220 } 221 222 229 private int getMaxDelayTime( Configuration config ) 230 throws ConfigurationException 231 { 232 return config.getChild( "maximum-delay-time" ).getValueAsInteger( 0 ); 233 } 234 235 244 private Address [] getToAddresses( final Configuration config ) 245 throws ConfigurationException, AddressException 246 { 247 final Configuration[] toAddresses = config.getChildren( "to" ); 248 final Address [] addresses = new Address [ toAddresses.length ]; 249 250 for( int i = 0; i < toAddresses.length; ++i ) 251 { 252 addresses[ i ] = createAddress( toAddresses[ i ].getValue() ); 253 } 254 255 return addresses; 256 } 257 258 267 private Address getFromAddress( final Configuration config ) 268 throws ConfigurationException, AddressException 269 { 270 return createAddress( config.getChild( "from" ).getValue() ); 271 } 272 273 280 private boolean getDebug( Configuration config ) 281 throws ConfigurationException 282 { 283 return config.getChild( "debug" ).getValueAsBoolean(); 284 } 285 286 296 protected Address createAddress( final String address ) 297 throws AddressException 298 { 299 return new InternetAddress ( address ); 300 } 301 } 302 | Popular Tags |