1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.buildmetadata.scm.maven;
17
18 import java.io.Serializable;
19
20 import org.apache.maven.scm.manager.ScmManager;
21 import org.apache.maven.scm.provider.ScmProviderRepository;
22 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
23 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
24 import org.apache.maven.scm.repository.ScmRepository;
25 import org.codehaus.plexus.util.StringUtils;
26
27 import de.smartics.maven.plugin.buildmetadata.scm.ScmException;
28
29
30
31
32
33
34
35 public final class ScmConnectionInfo implements Serializable
36 {
37
38
39
40
41
42
43
44
45
46
47 private static final long serialVersionUID = 1L;
48
49
50
51
52
53
54 private String connectionUrl;
55
56
57
58
59 private String userName;
60
61
62
63
64 private String password;
65
66
67
68
69 private String privateKey;
70
71
72
73
74 private String passPhrase;
75
76
77
78
79 private String tagBase;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public String getConnectionUrl()
99 {
100 return connectionUrl;
101 }
102
103
104
105
106
107
108 public void setScmConnectionUrl(final String connectionUrl)
109 {
110 this.connectionUrl = connectionUrl;
111 }
112
113
114
115
116
117
118 public String getUserName()
119 {
120 return userName;
121 }
122
123
124
125
126
127
128 public void setUserName(final String userName)
129 {
130 this.userName = userName;
131 }
132
133
134
135
136
137
138 public String getPassword()
139 {
140 return password;
141 }
142
143
144
145
146
147
148 public void setPassword(final String password)
149 {
150 this.password = password;
151 }
152
153
154
155
156
157
158 public String getPrivateKey()
159 {
160 return privateKey;
161 }
162
163
164
165
166
167
168 public void setPrivateKey(final String privateKey)
169 {
170 this.privateKey = privateKey;
171 }
172
173
174
175
176
177
178 public String getPassPhrase()
179 {
180 return passPhrase;
181 }
182
183
184
185
186
187
188 public void setPassPhrase(final String passPhrase)
189 {
190 this.passPhrase = passPhrase;
191 }
192
193
194
195
196
197
198 public String getTagBase()
199 {
200 return tagBase;
201 }
202
203
204
205
206
207
208 public void setTagBase(final String tagBase)
209 {
210 this.tagBase = tagBase;
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225 public ScmRepository createRepository(final ScmManager scmManager)
226 throws ScmException
227 {
228 try
229 {
230 final ScmRepository repository =
231 scmManager.makeScmRepository(connectionUrl);
232
233 final ScmProviderRepository providerRepository =
234 repository.getProviderRepository();
235 configure(providerRepository);
236
237 if (repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost)
238 {
239 final ScmProviderRepositoryWithHost providerRepositoryWithHost =
240 (ScmProviderRepositoryWithHost) repository.getProviderRepository();
241 configure(providerRepositoryWithHost);
242 }
243
244 if (!StringUtils.isEmpty(tagBase)
245 && repository.getProvider().equals("svn"))
246 {
247 final SvnScmProviderRepository svnRepository =
248 (SvnScmProviderRepository) repository.getProviderRepository();
249 configure(svnRepository);
250 }
251 return repository;
252 }
253 catch (final Exception e)
254 {
255 throw new ScmException("The SCM provider cannot be created.", e);
256 }
257 }
258
259
260
261
262
263
264 protected void configure(final ScmProviderRepository repository)
265 {
266 if (!StringUtils.isEmpty(userName))
267 {
268 repository.setUser(userName);
269 }
270
271 if (!StringUtils.isEmpty(password))
272 {
273 repository.setPassword(password);
274 }
275 }
276
277
278
279
280
281
282 protected void configure(final ScmProviderRepositoryWithHost repository)
283 {
284 if (!StringUtils.isEmpty(privateKey))
285 {
286 repository.setPrivateKey(privateKey);
287 }
288
289 if (!StringUtils.isEmpty(passPhrase))
290 {
291 repository.setPassphrase(passPhrase);
292 }
293 }
294
295
296
297
298
299
300 protected void configure(final SvnScmProviderRepository repository)
301 {
302 repository.setTagBase(tagBase);
303 }
304
305
306
307
308
309
310
311
312
313 @Override
314 public String toString()
315 {
316 final StringBuilder buffer = new StringBuilder();
317
318 buffer.append("SCM connection info: url=").append(connectionUrl);
319 appendIfExists(buffer, "user", userName);
320 appendSensibleDataIfExists(buffer, "password", password);
321 appendSensibleDataIfExists(buffer, "privateKey", privateKey);
322 appendSensibleDataIfExists(buffer, "passPhrase", passPhrase);
323 appendIfExists(buffer, "tagBase", tagBase);
324
325 return buffer.toString();
326 }
327
328 private static void appendIfExists(final StringBuilder buffer,
329 final String label, final String value)
330 {
331 if (StringUtils.isNotBlank(value))
332 {
333 buffer.append(", ").append(label).append('=').append(value);
334 }
335 }
336
337 private static void appendSensibleDataIfExists(final StringBuilder buffer,
338 final String label, final String value)
339 {
340 if (StringUtils.isNotBlank(value))
341 {
342 buffer.append(", ").append(label).append('=').append(mask(value));
343 }
344 }
345
346 private static String mask(final String input)
347 {
348 return StringUtils.repeat("*", input.length());
349 }
350 }