View Javadoc

1   /*
2    * Copyright 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.ci.maven.mojo;
17  
18  import java.util.List;
19  
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.settings.Proxy;
22  
23  import de.smartics.ci.comm.CiController;
24  import de.smartics.ci.comm.ProxyCredentials;
25  import de.smartics.ci.comm.ProxyInformation;
26  import de.smartics.ci.comm.command.LoginCommandException;
27  import de.smartics.ci.maven.HudsonUtils;
28  import de.smartics.ci.maven.ProxyConfiguration;
29  
30  /**
31   * Hudson CI mojo.
32   */
33  public abstract class AbstractProxyEnabledHudsonCiMojo extends
34  AbstractConfigChoiceHudsonCiMojo
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The id of the proxy entry in the settings to declare which proxy shall be
44     * used to connect to the ci server.
45     *
46     * @parameter expression="${hudson-maven-plugin.proxyId}"
47     * @since 1.0
48     */
49    protected String proxyId;
50  
51    /**
52     * The proxy Windows-NT domain name that must be used to get access through
53     * the proxy.
54     *
55     * @parameter expression="${hudson-maven-plugin.proxyDomain}"
56     * @since 1.0
57     */
58    private String proxyDomain;
59  
60    /**
61     * The client name that shall be used to connect to the proxy.
62     *
63     * @parameter expression="${hudson-maven-plugin.proxyClientName}"
64     * @since 1.0
65     */
66    private String proxyClientName;
67  
68    // ****************************** Initializer *******************************
69  
70    // ****************************** Constructors ******************************
71  
72    // ****************************** Inner Classes *****************************
73  
74    // ********************************* Methods ********************************
75  
76    // --- init -----------------------------------------------------------------
77  
78    // --- get&set --------------------------------------------------------------
79  
80    // --- business -------------------------------------------------------------
81  
82    /**
83     * {@inheritDoc}
84     *
85     * @see de.smartics.ci.maven.mojo.AbstractHudsonCiMojo#handleLogin(de.smartics.ci.comm.CiController)
86     */
87    protected void handleLogin(final CiController controller)
88      throws LoginCommandException, MojoExecutionException
89    {
90      applyProxyInformation(controller);
91      super.handleLogin(controller);
92    }
93  
94    /**
95     * Apply the proxy configuration to the controller.
96     *
97     * @param controller the controller that connect to the ci server.
98     * @throws MojoExecutionException when the proxy configuration is invalid.
99     */
100   protected final void applyProxyInformation(final CiController controller)
101     throws MojoExecutionException
102   {
103     if (HudsonUtils.checkIfProxyIsConfigured(this.proxyId, this.proxyDomain,
104         this.proxyClientName))
105     {
106       final ProxyConfiguration proxyConfig = getProxyConfiguration();
107       if (proxyConfig != null)
108       {
109         final ProxyInformation proxy =
110             new ProxyInformation(proxyConfig.getHost(), proxyConfig.getPort());
111         final ProxyCredentials proxyCredentials =
112             new ProxyCredentials(proxyConfig.getUserName(),
113                 proxyConfig.getPassword(), proxyConfig.getClientName(),
114                 proxyConfig.getDomain());
115         proxy.setCredentials(proxyCredentials);
116         controller.setProxyInformation(proxy);
117       }
118     }
119   }
120 
121   /**
122    * Returns the maven proxy configuration.
123    *
124    * @return the proxy information.
125    * @throws MojoExecutionException when the proxy is configured in the mojo,
126    *           but no proxy with the given proxyId can be found in the
127    *           settings.xml.
128    */
129   protected final ProxyConfiguration getProxyConfiguration()
130     throws MojoExecutionException
131   {
132     final Proxy configuredProxy = fetchProxyFromSettings();
133     final ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
134     final String proxyId = configuredProxy.getId();
135     proxyConfiguration.setId(proxyId);
136     proxyConfiguration.setHost(configuredProxy.getHost());
137     proxyConfiguration.setPort(configuredProxy.getPort());
138     proxyConfiguration.setPassword(configuredProxy.getPassword());
139     proxyConfiguration.setUserName(configuredProxy.getUsername());
140     proxyConfiguration.setClientName(this.proxyClientName);
141     proxyConfiguration.setDomain(this.proxyDomain);
142     return proxyConfiguration;
143   }
144 
145   private Proxy fetchProxyFromSettings() throws MojoExecutionException
146   {
147     final List<Proxy> proxies = settings.getProxies();
148     Proxy configuredProxy = null;
149     for (final Proxy proxy : proxies)
150     {
151       final String currentProxyId = proxy.getId();
152       if (this.proxyId.equals(currentProxyId))
153       {
154         configuredProxy = proxy;
155       }
156     }
157     if (configuredProxy == null)
158     {
159       throw new MojoExecutionException(
160           "Proxy is configured, but in the settings.xml there is no porxy-entry for proxy with id: "
161               + this.proxyId);
162     }
163     return configuredProxy;
164   }
165 
166   // --- object basics --------------------------------------------------------
167 
168 }