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 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
47
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
57
58
59 @Resource
60 private MsnClient msnClient;
61
62 @Resource
63 private ConfigurationService configurationService;
64
65
66
67
68
69
70
71
72 private String fromAddress;
73
74
75
76
77 private String fromPassword;
78
79
80
81
82
83
84
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
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
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 }