View Javadoc

1   package org.apache.maven.continuum.notification.msn;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.annotation.Resource;
27  
28  import org.apache.continuum.model.project.ProjectScmRoot;
29  import org.apache.maven.continuum.configuration.ConfigurationService;
30  import org.apache.maven.continuum.model.project.BuildDefinition;
31  import org.apache.maven.continuum.model.project.BuildResult;
32  import org.apache.maven.continuum.model.project.Project;
33  import org.apache.maven.continuum.model.project.ProjectNotifier;
34  import org.apache.maven.continuum.notification.AbstractContinuumNotifier;
35  import org.apache.maven.continuum.notification.ContinuumNotificationDispatcher;
36  import org.apache.maven.continuum.notification.MessageContext;
37  import org.apache.maven.continuum.notification.NotificationException;
38  import org.codehaus.plexus.msn.MsnClient;
39  import org.codehaus.plexus.msn.MsnException;
40  import org.codehaus.plexus.util.StringUtils;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  import org.springframework.stereotype.Service;
44  
45  /**
46   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
47   * @version $Id: MsnContinuumNotifier.java 769240 2009-04-28 04:59:03Z evenisse $
48   */
49  @Service("notifier#msn")
50  public class MsnContinuumNotifier
51      extends AbstractContinuumNotifier
52  {
53      private static final Logger log = LoggerFactory.getLogger( MsnContinuumNotifier.class );
54  
55      // ----------------------------------------------------------------------
56      // Requirements
57      // ----------------------------------------------------------------------
58  
59      @Resource
60      private MsnClient msnClient;
61  
62      @Resource
63      private ConfigurationService configurationService;
64  
65      // ----------------------------------------------------------------------
66      // Configuration
67      // ----------------------------------------------------------------------
68  
69      /**
70       * @plexus.configuration
71       */
72      private String fromAddress;
73  
74      /**
75       * @plexus.configuration
76       */
77      private String fromPassword;
78  
79      // ----------------------------------------------------------------------
80      //
81      // ----------------------------------------------------------------------
82  
83      // ----------------------------------------------------------------------
84      // Notifier Implementation
85      // ----------------------------------------------------------------------
86  
87      public String getType()
88      {
89          return "msn";
90      }
91  
92      public void sendMessage( String messageId, MessageContext context )
93          throws NotificationException
94      {
95          Project project = context.getProject();
96  
97          List<ProjectNotifier> notifiers = context.getNotifiers();
98  
99          BuildDefinition buildDefinition = context.getBuildDefinition();
100 
101         BuildResult build = context.getBuildResult();
102 
103         ProjectScmRoot projectScmRoot = context.getProjectScmRoot();
104 
105         boolean isPrepareBuildComplete =
106             messageId.equals( ContinuumNotificationDispatcher.MESSAGE_ID_PREPARE_BUILD_COMPLETE );
107 
108         if ( projectScmRoot == null && isPrepareBuildComplete )
109         {
110             return;
111         }
112 
113         // ----------------------------------------------------------------------
114         // If there wasn't any building done, don't notify
115         // ----------------------------------------------------------------------
116 
117         if ( build == null && !isPrepareBuildComplete )
118         {
119             return;
120         }
121 
122         // ----------------------------------------------------------------------
123         //
124         // ----------------------------------------------------------------------
125 
126         List<String> recipients = new ArrayList<String>();
127         for ( ProjectNotifier notifier : notifiers )
128         {
129             Map<String, String> configuration = notifier.getConfiguration();
130             if ( configuration != null && StringUtils.isNotEmpty( configuration.get( ADDRESS_FIELD ) ) )
131             {
132                 recipients.add( configuration.get( ADDRESS_FIELD ) );
133             }
134         }
135         if ( recipients.size() == 0 )
136         {
137             log.info( "No MSN recipients for '" + project.getName() + "'." );
138 
139             return;
140         }
141 
142         // ----------------------------------------------------------------------
143         //
144         // ----------------------------------------------------------------------
145 
146         if ( messageId.equals( ContinuumNotificationDispatcher.MESSAGE_ID_BUILD_COMPLETE ) )
147         {
148             for ( ProjectNotifier notifier : notifiers )
149             {
150                 buildComplete( project, notifier, build, buildDefinition );
151             }
152         }
153         else if ( isPrepareBuildComplete )
154         {
155             for ( ProjectNotifier notifier : notifiers )
156             {
157                 prepareBuildComplete( projectScmRoot, notifier );
158             }
159         }
160     }
161 
162     // ----------------------------------------------------------------------
163     //
164     // ----------------------------------------------------------------------
165 
166     private void buildComplete( Project project, ProjectNotifier notifier, BuildResult build, BuildDefinition buildDef )
167         throws NotificationException
168     {
169         // ----------------------------------------------------------------------
170         // Check if the message should be sent at all
171         // ----------------------------------------------------------------------
172 
173         BuildResult previousBuild = getPreviousBuild( project, buildDef, build );
174 
175         if ( !shouldNotify( build, previousBuild, notifier ) )
176         {
177             return;
178         }
179 
180         sendMessage( notifier.getConfiguration(), generateMessage( project, build, configurationService ) );
181     }
182 
183     private void prepareBuildComplete( ProjectScmRoot projectScmRoot, ProjectNotifier notifier )
184         throws NotificationException
185     {
186         if ( !shouldNotify( projectScmRoot, notifier ) )
187         {
188             return;
189         }
190 
191         sendMessage( notifier.getConfiguration(), generateMessage( projectScmRoot, configurationService ) );
192     }
193 
194     private void sendMessage( Map<String, String> configuration, String message )
195         throws NotificationException
196     {
197         msnClient.setLogin( getUsername( configuration ) );
198 
199         msnClient.setPassword( getPassword( configuration ) );
200 
201         try
202         {
203             msnClient.login();
204 
205             if ( configuration != null && StringUtils.isNotEmpty( configuration.get( ADDRESS_FIELD ) ) )
206             {
207                 String address = configuration.get( ADDRESS_FIELD );
208                 String[] recipients = StringUtils.split( address, "," );
209                 for ( String recipient : recipients )
210                 {
211                     msnClient.sendMessage( recipient, message );
212                 }
213             }
214         }
215         catch ( MsnException e )
216         {
217             throw new NotificationException( "Exception while sending message.", e );
218         }
219         finally
220         {
221             try
222             {
223                 msnClient.logout();
224             }
225             catch ( MsnException e )
226             {
227 
228             }
229         }
230     }
231 
232     private String getUsername( Map<String, String> configuration )
233     {
234         if ( configuration.containsKey( "login" ) )
235         {
236             return configuration.get( "login" );
237         }
238 
239         return fromAddress;
240     }
241 
242     private String getPassword( Map<String, String> configuration )
243     {
244         if ( configuration.containsKey( "password" ) )
245         {
246             return configuration.get( "password" );
247         }
248 
249         return fromPassword;
250     }
251 }