package org.apache.maven.continuum.notification;

/*
 * Copyright 2004-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.maven.continuum.project.ContinuumNotifier;
import org.apache.maven.continuum.project.ContinuumProject;

import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.notification.AbstractRecipientSource;
import org.codehaus.plexus.notification.NotificationException;
import org.codehaus.plexus.notification.RecipientSource;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.util.StringUtils;

/**
 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 * @version $Id$
 */
public class ContinuumRecipientSource
    extends AbstractRecipientSource
    implements Initializable
{
    public static String ADDRESS_FIELD = "address";

    /** @configuration */
    private String toOverride;

    // ----------------------------------------------------------------------
    // Component Lifecycle
    // ----------------------------------------------------------------------

    public void initialize()
    {
        // ----------------------------------------------------------------------
        // To address
        // ----------------------------------------------------------------------

        if ( StringUtils.isEmpty( toOverride ) )
        {
            getLogger().info( "To override address is not configured, will use the nag email address from the project." );
        }
        else
        {
            getLogger().warn( "Using '" + toOverride + "' as the to address for all emails." );
        }
    }

    // ----------------------------------------------------------------------
    // RecipientSource Implementation
    // ----------------------------------------------------------------------

    public Set getRecipients( String notifierType, String messageId, Map configuration, Map context )
        throws NotificationException
    {
        ContinuumProject project = (ContinuumProject) context.get( ContinuumNotificationDispatcher.CONTEXT_PROJECT );

        if ( project == null )
        {
            throw new NotificationException( "Missing project from the notification context." );
        }

        Set recipients = new HashSet();

        if ( !StringUtils.isEmpty( toOverride ) )
        {
            recipients.add( toOverride );
        }
        else if ( project.getNotifiers() != null && !project.getNotifiers().isEmpty() )
        {
            for ( Iterator i = project.getNotifiers().iterator(); i.hasNext(); )
            {
                ContinuumNotifier notifier = (ContinuumNotifier) i.next();

                if ( notifier.getType().equals( notifierType ) && notifier.getConfiguration().containsKey( ADDRESS_FIELD ) )
                {
                    recipients.add( notifier.getConfiguration().getProperty( ADDRESS_FIELD ) );
                }
            }
        }

        if ( recipients.isEmpty() )
        {
            return Collections.EMPTY_SET;
        }
        else
        {
            return recipients;
        }
    }
}

@@ -919,63 +919,8 @@
 t;%0A%0A
-import org.codehaus.plexus.logging.AbstractLogEnabled;%0A
 impo
@@ -1047,65 +1047,8 @@
 on;%0A
-import org.codehaus.plexus.notification.RecipientSource;%0A
 impo
@@ -1430,16 +1430,23 @@
    /** @
+plexus.
 configur
@@ -3106,17 +3106,32 @@
 terator 
-i
+notifierIterator
  = proje
@@ -3160,17 +3160,32 @@
 ator(); 
-i
+notifierIterator
 .hasNext
@@ -3269,17 +3269,32 @@
 tifier) 
-i
+notifierIterator
 .next();
@@ -3458,31 +3458,37 @@
         
-recipients.add(
+String addressField =
  notifie
@@ -3536,19 +3536,272 @@
 _FIELD )
- );
+;%0A%0A                    String%5B%5D addresses = StringUtils.split( addressField, %22,%22 );%0A%0A                    for ( int i = 0; i %3C addresses.length; i++ )%0A                    %7B%0A                        recipients.add( addresses%5Bi%5D.trim() );%0A                    %7D
 %0A       

Allow mutiple addresses.