1 package org.apache.maven.continuum.notification.msn;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.continuum.model.project.ProjectScmRoot;
23 import org.apache.maven.continuum.configuration.ConfigurationService;
24 import org.apache.maven.continuum.model.project.BuildDefinition;
25 import org.apache.maven.continuum.model.project.BuildResult;
26 import org.apache.maven.continuum.model.project.Project;
27 import org.apache.maven.continuum.model.project.ProjectNotifier;
28 import org.apache.maven.continuum.notification.AbstractContinuumNotifier;
29 import org.apache.maven.continuum.notification.ContinuumNotificationDispatcher;
30 import org.apache.maven.continuum.notification.MessageContext;
31 import org.apache.maven.continuum.notification.NotificationException;
32 import org.codehaus.plexus.msn.MsnClient;
33 import org.codehaus.plexus.msn.MsnException;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.stereotype.Service;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Map;
42 import javax.annotation.Resource;
43
44
45
46
47
48 @Service( "notifier#msn" )
49 public class MsnContinuumNotifier
50 extends AbstractContinuumNotifier
51 {
52 private static final Logger log = LoggerFactory.getLogger( MsnContinuumNotifier.class );
53
54
55
56
57
58 @Resource
59 private MsnClient msnClient;
60
61 @Resource
62 private ConfigurationService configurationService;
63
64
65
66
67
68
69
70
71 private String fromAddress;
72
73
74
75
76 private String fromPassword;
77
78
79
80
81
82
83
84
85
86 public String getType()
87 {
88 return "msn";
89 }
90
91 public void sendMessage( String messageId, MessageContext context )
92 throws NotificationException
93 {
94 Project project = context.getProject();
95
96 List<ProjectNotifier> notifiers = context.getNotifiers();
97
98 BuildDefinition buildDefinition = context.getBuildDefinition();
99
100 BuildResult build = context.getBuildResult();
101
102 ProjectScmRoot projectScmRoot = context.getProjectScmRoot();
103
104 boolean isPrepareBuildComplete = messageId.equals(
105 ContinuumNotificationDispatcher.MESSAGE_ID_PREPARE_BUILD_COMPLETE );
106
107 if ( projectScmRoot == null && isPrepareBuildComplete )
108 {
109 return;
110 }
111
112
113
114
115
116 if ( build == null && !isPrepareBuildComplete )
117 {
118 return;
119 }
120
121
122
123
124
125 List<String> recipients = new ArrayList<String>();
126 for ( ProjectNotifier notifier : notifiers )
127 {
128 Map<String, String> configuration = notifier.getConfiguration();
129 if ( configuration != null && StringUtils.isNotEmpty( configuration.get( ADDRESS_FIELD ) ) )
130 {
131 recipients.add( configuration.get( ADDRESS_FIELD ) );
132 }
133 }
134 if ( recipients.size() == 0 )
135 {
136 log.info( "No MSN recipients for '" + project.getName() + "'." );
137
138 return;
139 }
140
141
142
143
144
145 if ( messageId.equals( ContinuumNotificationDispatcher.MESSAGE_ID_BUILD_COMPLETE ) )
146 {
147 for ( ProjectNotifier notifier : notifiers )
148 {
149 buildComplete( project, notifier, build, buildDefinition );
150 }
151 }
152 else if ( isPrepareBuildComplete )
153 {
154 for ( ProjectNotifier notifier : notifiers )
155 {
156 prepareBuildComplete( projectScmRoot, notifier );
157 }
158 }
159 }
160
161
162
163
164
165 private void buildComplete( Project project, ProjectNotifier notifier, BuildResult build, BuildDefinition buildDef )
166 throws NotificationException
167 {
168
169
170
171
172 BuildResult previousBuild = getPreviousBuild( project, buildDef, build );
173
174 if ( !shouldNotify( build, previousBuild, notifier ) )
175 {
176 return;
177 }
178
179 sendMessage( notifier.getConfiguration(), generateMessage( project, build, configurationService ) );
180 }
181
182 private void prepareBuildComplete( ProjectScmRoot projectScmRoot, ProjectNotifier notifier )
183 throws NotificationException
184 {
185 if ( !shouldNotify( projectScmRoot, notifier ) )
186 {
187 return;
188 }
189
190 sendMessage( notifier.getConfiguration(), generateMessage( projectScmRoot, configurationService ) );
191 }
192
193 private void sendMessage( Map<String, String> configuration, String message )
194 throws NotificationException
195 {
196 msnClient.setLogin( getUsername( configuration ) );
197
198 msnClient.setPassword( getPassword( configuration ) );
199
200 try
201 {
202 msnClient.login();
203
204 if ( configuration != null && StringUtils.isNotEmpty( configuration.get( ADDRESS_FIELD ) ) )
205 {
206 String address = configuration.get( ADDRESS_FIELD );
207 String[] recipients = StringUtils.split( address, "," );
208 for ( String recipient : recipients )
209 {
210 msnClient.sendMessage( recipient, message );
211 }
212 }
213 }
214 catch ( MsnException e )
215 {
216 throw new NotificationException( "Exception while sending message.", e );
217 }
218 finally
219 {
220 try
221 {
222 msnClient.logout();
223 }
224 catch ( MsnException e )
225 {
226
227 }
228 }
229 }
230
231 private String getUsername( Map<String, String> configuration )
232 {
233 if ( configuration.containsKey( "login" ) )
234 {
235 return configuration.get( "login" );
236 }
237
238 return fromAddress;
239 }
240
241 private String getPassword( Map<String, String> configuration )
242 {
243 if ( configuration.containsKey( "password" ) )
244 {
245 return configuration.get( "password" );
246 }
247
248 return fromPassword;
249 }
250 }