<commit_before>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 org.apache.maven.continuum.configuration.ConfigurationException;
import org.apache.maven.continuum.configuration.ConfigurationService;
import org.apache.maven.continuum.model.project.BuildResult;
import org.apache.maven.continuum.model.project.Project;
import org.apache.maven.continuum.model.project.ProjectNotifier;
import org.apache.maven.continuum.store.ContinuumStore;
import org.apache.maven.continuum.store.ContinuumStoreException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.notification.NotificationException;
import org.codehaus.plexus.notification.RecipientSource;
import org.codehaus.plexus.notification.notifier.Notifier;
import org.codehaus.plexus.notification.notifier.manager.NotifierManager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author <a href="mailto:<EMAIL>"><NAME>&oslash;l</a>
 * @version $Id$
 */
public class DefaultContinuumNotificationDispatcher
    extends AbstractLogEnabled
    implements ContinuumNotificationDispatcher
{
    /**
     * @plexus.requirement
     */
    private ConfigurationService configurationService;

    /**
     * @plexus.requirement
     */
    private NotifierManager notifierManager;

    /**
     * @plexus.requirement
     */
    private ContinuumStore store;

    /**
     * @plexus.requirement
     */
    private RecipientSource recipientSource;

    // ----------------------------------------------------------------------
    // ContinuumNotificationDispatcher Implementation
    // ----------------------------------------------------------------------

    public void buildStarted( Project project )
    {
        sendNotification( MESSAGE_ID_BUILD_STARTED, project, null );
    }

    public void checkoutStarted( Project project )
    {
        sendNotification( MESSAGE_ID_CHECKOUT_STARTED, project, null );
    }

    public void checkoutComplete( Project project )
    {
        sendNotification( MESSAGE_ID_CHECKOUT_COMPLETE, project, null );
    }

    public void runningGoals( Project project, BuildResult build )
    {
        sendNotification( MESSAGE_ID_RUNNING_GOALS, project, build );
    }

    public void goalsCompleted( Project project, BuildResult build )
    {
        sendNotification( MESSAGE_ID_GOALS_COMPLETED, project, build );
    }

    public void buildComplete( Project project, BuildResult build )
    {
        sendNotification( MESSAGE_ID_BUILD_COMPLETE, project, build );
    }

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    private void sendNotification( String messageId, Project project, BuildResult build )
    {
        Map context = new HashMap();

        // ----------------------------------------------------------------------
        // The objects are reread from the store to make sure they're getting the "final"
        // state of the objects. Ideally this should be done on a per notifier basis or the
        // objects should be made read only.
        // ----------------------------------------------------------------------

        try
        {
            // TODO: remove re-reading?
            // Here we need to get all the project details
            //  - builds are used to detect if the state has changed (TODO: maybe previousState field is better)
            //  - notifiers are used to send the notification
            project = store.getProjectWithAllDetails( project.getId() );

            context.put( CONTEXT_PROJECT, project );

            if ( build != null )
            {
                context.put( CONTEXT_BUILD, build );

                if ( build.getEndTime() != 0 )
                {
                    context.put( CONTEXT_BUILD_OUTPUT,
                                 configurationService.getBuildOutput( build.getId(), project.getId() ) );
                }

                context.put( CONTEXT_UPDATE_SCM_RESULT, build.getScmResult() );
            }
        }
        catch ( ContinuumStoreException e )
        {
            getLogger().error( "Error while population the notification context.", e );

            return;
        }
        catch ( ConfigurationException e )
        {
            getLogger().error( "Error while population the notification context.", e );

            return;
        }

        for ( Iterator i = project.getNotifiers().iterator(); i.hasNext(); )
        {
            ProjectNotifier projectNotifier = (ProjectNotifier) i.next();

            String notifierType = projectNotifier.getType();

            if ( !projectNotifier.isEnabled() )
            {
                getLogger().info( notifierType + " notifier (id=" + projectNotifier.getId() + ") is disabled." );

                continue;
            }

            Map configuration = projectNotifier.getConfiguration();

            try
            {
                context.put( CONTEXT_PROJECT_NOTIFIER, projectNotifier );

                Notifier notifier = notifierManager.getNotifier( notifierType );

                Set recipients = recipientSource.getRecipients( String.valueOf( projectNotifier.getId() ), messageId,
                                                                configuration, context );

                notifier.sendNotification( messageId, recipients, configuration, context );
            }
            catch ( NotificationException e )
            {
                getLogger().error( "Error while trying to use the " + notifierType + "notifier.", e );
            }
        }
    }
}
<commit_msg>this should get the notifiers working dispatch wise...and this is a bit different from the actual loading of the group/project notifiers so I ought to be able to get away with committing this and then rahul's work should start functioning.<commit_after>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 org.apache.maven.continuum.configuration.ConfigurationException;
import org.apache.maven.continuum.configuration.ConfigurationService;
import org.apache.maven.continuum.model.project.BuildResult;
import org.apache.maven.continuum.model.project.Project;
import org.apache.maven.continuum.model.project.ProjectNotifier;
import org.apache.maven.continuum.model.project.ProjectGroup;
import org.apache.maven.continuum.store.ContinuumStore;
import org.apache.maven.continuum.store.ContinuumStoreException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.notification.NotificationException;
import org.codehaus.plexus.notification.RecipientSource;
import org.codehaus.plexus.notification.notifier.Notifier;
import org.codehaus.plexus.notification.notifier.manager.NotifierManager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author <a href="mailto:<EMAIL>"><NAME>&oslash;l</a>
 * @version $Id$
 */
public class DefaultContinuumNotificationDispatcher
    extends AbstractLogEnabled
    implements ContinuumNotificationDispatcher
{
    /**
     * @plexus.requirement
     */
    private ConfigurationService configurationService;

    /**
     * @plexus.requirement
     */
    private NotifierManager notifierManager;

    /**
     * @plexus.requirement
     */
    private ContinuumStore store;

    /**
     * @plexus.requirement
     */
    private RecipientSource recipientSource;

    // ----------------------------------------------------------------------
    // ContinuumNotificationDispatcher Implementation
    // ----------------------------------------------------------------------

    public void buildStarted( Project project )
    {
        sendNotification( MESSAGE_ID_BUILD_STARTED, project, null );
    }

    public void checkoutStarted( Project project )
    {
        sendNotification( MESSAGE_ID_CHECKOUT_STARTED, project, null );
    }

    public void checkoutComplete( Project project )
    {
        sendNotification( MESSAGE_ID_CHECKOUT_COMPLETE, project, null );
    }

    public void runningGoals( Project project, BuildResult build )
    {
        sendNotification( MESSAGE_ID_RUNNING_GOALS, project, build );
    }

    public void goalsCompleted( Project project, BuildResult build )
    {
        sendNotification( MESSAGE_ID_GOALS_COMPLETED, project, build );
    }

    public void buildComplete( Project project, BuildResult build )
    {
        sendNotification( MESSAGE_ID_BUILD_COMPLETE, project, build );
    }

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    private void sendNotification( String messageId, Project project, BuildResult build )
    {
        Map context = new HashMap();

        // ----------------------------------------------------------------------
        // The objects are reread from the store to make sure they're getting the "final"
        // state of the objects. Ideally this should be done on a per notifier basis or the
        // objects should be made read only.
        // ----------------------------------------------------------------------

        try
        {
            // TODO: remove re-reading?
            // Here we need to get all the project details
            //  - builds are used to detect if the state has changed (TODO: maybe previousState field is better)
            //  - notifiers are used to send the notification
            project = store.getProjectWithAllDetails( project.getId() );

            context.put( CONTEXT_PROJECT, project );

            if ( build != null )
            {
                context.put( CONTEXT_BUILD, build );

                if ( build.getEndTime() != 0 )
                {
                    context.put( CONTEXT_BUILD_OUTPUT,
                                 configurationService.getBuildOutput( build.getId(), project.getId() ) );
                }

                context.put( CONTEXT_UPDATE_SCM_RESULT, build.getScmResult() );
            }


            ProjectGroup projectGroup = store.getProjectGroupWithBuildDetails( project.getProjectGroup().getId() );

            // perform the project lvl notifications
            for ( Iterator i = project.getNotifiers().iterator(); i.hasNext(); )
            {
                sendNotification( messageId, (ProjectNotifier) i.next(), context );
            }

            // perform the project group lvl notifications
            if ( projectGroup.getNotifiers() != null )
            {
                for ( Iterator i = projectGroup.getNotifiers().iterator(); i.hasNext(); )
                {
                    sendNotification( messageId, (ProjectNotifier) i.next(), context );
                }
            }            
        }
        catch ( ContinuumStoreException e )
        {
            getLogger().error( "Error while population the notification context.", e );

            return;
        }
        catch ( ConfigurationException e )
        {
            getLogger().error( "Error while population the notification context.", e );

            return;
        }
    }

    private void sendNotification( String messageId, ProjectNotifier projectNotifier, Map context )
    {
        String notifierType = projectNotifier.getType();

        if ( !projectNotifier.isEnabled() )
        {
            getLogger().info( notifierType + " notifier (id=" + projectNotifier.getId() + ") is disabled." );

            return;
        }

        Map configuration = projectNotifier.getConfiguration();

        try
        {
            context.put( CONTEXT_PROJECT_NOTIFIER, projectNotifier );

            Notifier notifier = notifierManager.getNotifier( notifierType );

            Set recipients = recipientSource.getRecipients( String.valueOf( projectNotifier.getId() ), messageId,
                                                            configuration, context );

            notifier.sendNotification( messageId, recipients, configuration, context );
        }
        catch ( NotificationException e )
        {
            getLogger().error( "Error while trying to use the " + notifierType + "notifier.", e );
        }
    }
}

{'fraction_non_alphanumeric': 0.14323803880906533, 'fraction_numerical': 0.0019256406458302473, 'mean_word_length': 1.9367116137451066, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6445996', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3150, 'n_words': 877}