proyal 2002/10/07 10:46:43
Modified: logger/src/java/org/apache/avalon/excalibur/logger/factory
SMTPTargetFactory.java
Log:
Support creating a JavaMail session on the fly
Revision Changes Path
1.5 +37 -15
jakarta-avalon-excalibur/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java
Index: SMTPTargetFactory.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SMTPTargetFactory.java 2 Oct 2002 01:52:22 -0000 1.4
+++ SMTPTargetFactory.java 7 Oct 2002 17:46:43 -0000 1.5
@@ -56,6 +56,7 @@
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.parameters.Parameters;
import org.apache.log.LogTarget;
import org.apache.log.format.Formatter;
import org.apache.log.output.net.SMTPOutputLogTarget;
@@ -112,15 +113,10 @@
public final LogTarget createTarget( final Configuration config )
throws ConfigurationException
{
- if( m_context == null )
- {
- throw new ConfigurationException( "Context not available" );
- }
-
try
{
return new SMTPOutputLogTarget(
- getSession(),
+ getSession( config ),
getToAddresses( config ),
getFromAddress( config ),
getSubject( config ),
@@ -159,19 +155,45 @@
}
/**
- * Helper method to obtain the JavaMail <code>Session</code> object
- * from this factories context object. Override this method if you
- * need to obtain the JavaMail session using some other way.
+ * Helper method to create a JavaMail <code>Session</code> object.
+ *
+ * If your session object has simple needs, you can nest a configuration element
+ * named <b>session</b> containing name-value pairs that are passed to
+ * <code>Session.getInstance()</code>.
+ *
+ * If no configuration is found, a <code>Session</code> will be loaded from this
+ * factory's context object.
+ *
+ * You can override this method if you need ot obtain the JavaMail session using
+ * some other means.
*
* @return JavaMail <code>Session</code> instance
* @exception ContextException if an error occurs
+ * @exception ConfigurationException if invalid session configuration
*/
- protected Session getSession()
- throws ContextException
+ protected Session getSession( Configuration config )
+ throws ContextException, ConfigurationException
{
- final String contextkey =
- m_configuration.getAttribute( "context-key", "session-context" );
- return (Session)m_context.get( contextkey );
+ final Configuration sessionConfig = config.getChild( "session", false );
+
+ if ( null == sessionConfig )
+ {
+ final String contextkey =
+ m_configuration.getAttribute( "context-key", "session-context" );
+
+ if( m_context == null )
+ {
+ throw new ConfigurationException( "Context not available" );
+ }
+
+ return (Session) m_context.get( contextkey );
+ }
+ else
+ {
+ return Session.getInstance(
+ Parameters.toProperties(
+ Parameters.fromConfiguration( sessionConfig ) ) );
+ }
}
/**
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>