View Javadoc

1   /*
2    * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Provides the information required to connect to a SCM system.
31   *
32   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
33   * @version $Revision:591 $
34   */
35  public final class ScmConnectionInfo implements Serializable
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * The class version identifier.
43     * <p>
44     * The value of this constant is {@value}.
45     * </p>
46     */
47    private static final long serialVersionUID = 1L;
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The URL to connect to the SCM system.
53     */
54    private String connectionUrl;
55  
56    /**
57     * The user name to authenticate against the SCM system.
58     */
59    private String userName;
60  
61    /**
62     * The password to authenticate against the SCM system.
63     */
64    private String password;
65  
66    /**
67     * The private key to authenticate against the SCM system.
68     */
69    private String privateKey;
70  
71    /**
72     * The pass phrase to authenticate against the SCM system.
73     */
74    private String passPhrase;
75  
76    /**
77     * The url of tags base directory (used by svn protocol).
78     */
79    private String tagBase;
80  
81    // ****************************** Initializer *******************************
82  
83    // ****************************** Constructors ******************************
84  
85    // ****************************** Inner Classes *****************************
86  
87    // ********************************* Methods ********************************
88  
89    // --- init -----------------------------------------------------------------
90  
91    // --- get&set --------------------------------------------------------------
92  
93    /**
94     * Returns the URL to connect to the SCM system.
95     *
96     * @return the URL to connect to the SCM system.
97     */
98    public String getConnectionUrl()
99    {
100     return connectionUrl;
101   }
102 
103   /**
104    * Sets the URL to connect to the SCM system.
105    *
106    * @param connectionUrl the URL to connect to the SCM system.
107    */
108   public void setScmConnectionUrl(final String connectionUrl)
109   {
110     this.connectionUrl = connectionUrl;
111   }
112 
113   /**
114    * Returns the user name to authenticate against the SCM system.
115    *
116    * @return the user name to authenticate against the SCM system.
117    */
118   public String getUserName()
119   {
120     return userName;
121   }
122 
123   /**
124    * Sets the user name to authenticate against the SCM system.
125    *
126    * @param userName the user name to authenticate against the SCM system.
127    */
128   public void setUserName(final String userName)
129   {
130     this.userName = userName;
131   }
132 
133   /**
134    * Returns the password to authenticate against the SCM system.
135    *
136    * @return the password to authenticate against the SCM system.
137    */
138   public String getPassword()
139   {
140     return password;
141   }
142 
143   /**
144    * Sets the password to authenticate against the SCM system.
145    *
146    * @param password the password to authenticate against the SCM system.
147    */
148   public void setPassword(final String password)
149   {
150     this.password = password;
151   }
152 
153   /**
154    * Returns the private key to authenticate against the SCM system.
155    *
156    * @return the private key to authenticate against the SCM system.
157    */
158   public String getPrivateKey()
159   {
160     return privateKey;
161   }
162 
163   /**
164    * Sets the private key to authenticate against the SCM system.
165    *
166    * @param privateKey the private key to authenticate against the SCM system.
167    */
168   public void setPrivateKey(final String privateKey)
169   {
170     this.privateKey = privateKey;
171   }
172 
173   /**
174    * Returns the pass phrase to authenticate against the SCM system.
175    *
176    * @return the pass phrase to authenticate against the SCM system.
177    */
178   public String getPassPhrase()
179   {
180     return passPhrase;
181   }
182 
183   /**
184    * Sets the pass phrase to authenticate against the SCM system.
185    *
186    * @param passPhrase the pass phrase to authenticate against the SCM system.
187    */
188   public void setPassPhrase(final String passPhrase)
189   {
190     this.passPhrase = passPhrase;
191   }
192 
193   /**
194    * Returns the url of tags base directory (used by svn protocol).
195    *
196    * @return the url of tags base directory (used by svn protocol).
197    */
198   public String getTagBase()
199   {
200     return tagBase;
201   }
202 
203   /**
204    * Sets the url of tags base directory (used by svn protocol).
205    *
206    * @param tagBase the url of tags base directory (used by svn protocol).
207    */
208   public void setTagBase(final String tagBase)
209   {
210     this.tagBase = tagBase;
211   }
212 
213   // --- business -------------------------------------------------------------
214 
215   /**
216    * Creates and configures the SCM repository.
217    *
218    * @param scmManager the manager to create the repository dependent on the
219    *          {@link #getConnectionUrl() connection URL}.
220    * @return the repository implementation to connect to the SCM system.
221    * @throws ScmException if the repository implementation cannot be created or
222    *           configured. This happens especially if no provider exists for the
223    *           given connection URL.
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    * Configures the repository with authentication information.
261    *
262    * @param repository the repository instance to configure.
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    * Configures the repository with private key and password information.
279    *
280    * @param repository the repository instance to configure.
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    * Configures the repository with the tag base information.
297    *
298    * @param repository the repository instance to configure.
299    */
300   protected void configure(final SvnScmProviderRepository repository)
301   {
302     repository.setTagBase(tagBase);
303   }
304 
305   // --- object basics --------------------------------------------------------
306 
307   /**
308    * Returns the string representation of the object.
309    * Sensitive information is masked.
310    *
311    * @return the string representation of the object.
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 }