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  
17  package de.smartics.ci.maven;
18  
19  import java.io.StringReader;
20  import java.io.StringWriter;
21  
22  import javax.xml.transform.OutputKeys;
23  import javax.xml.transform.Source;
24  import javax.xml.transform.Transformer;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.stream.StreamResult;
27  import javax.xml.transform.stream.StreamSource;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.xml.serializer.OutputPropertiesFactory;
32  
33  import de.smartics.ci.comm.CiController;
34  import de.smartics.ci.comm.CiSystem;
35  import de.smartics.ci.comm.Credentials;
36  import de.smartics.ci.comm.LogLevel;
37  import de.smartics.ci.comm.hudson.HudsonCiSystemFactory;
38  import de.smartics.ci.config.load.LoaderPlan;
39  import de.smartics.ci.config.load.LoaderPlanLoader;
40  import de.smartics.ci.config.maven.MavenConfig;
41  
42  /**
43   * Utilities for hudson.
44   */
45  public final class HudsonUtils
46  {
47    // ********************************* Fields *********************************
48  
49    // --- constants ------------------------------------------------------------
50  
51    // --- members --------------------------------------------------------------
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default constructor.
59     */
60    private HudsonUtils()
61    {
62  
63    }
64  
65    // ****************************** Inner Classes *****************************
66  
67    // ********************************* Methods ********************************
68  
69    // --- init -----------------------------------------------------------------
70  
71    // --- get&set --------------------------------------------------------------
72  
73    // --- business -------------------------------------------------------------
74  
75    /**
76     * Helper that creates the contoller.
77     *
78     * @param ciHost the host to which the controller si connected to.
79     * @param logLevel the loglevel to use.
80     * @return the ci controller to controll a ci system.
81     */
82    public static CiController createController(final String ciHost,
83        final LogLevel logLevel)
84    {
85      final CiSystem system = new CiSystem(ciHost);
86      final CiController controller =
87          HudsonCiSystemFactory.createCiController(system, logLevel);
88      return controller;
89    }
90  
91    /**
92     * Helper that determines the job name to use.
93     *
94     * @param plan the current plan.
95     * @return the job name to use.
96     */
97    public static String determineJobName(final LoaderPlan plan)
98    {
99      final String jobNameMainPart =
100         plan.getMavenConfig().getPom().getArtifactId();
101     final String jobNameDiscriminator = determineDiscriminator(plan);
102     return calculateJobName(jobNameMainPart, jobNameDiscriminator);
103   }
104 
105   static String calculateJobName(final String jobNameMainPart,
106       final String jobNameDiscriminator)
107   {
108     if (StringUtils.isBlank(jobNameDiscriminator))
109     {
110       return jobNameMainPart;
111     }
112     else
113     {
114       return jobNameMainPart + "-" + jobNameDiscriminator;
115     }
116   }
117 
118   static String determineDiscriminator(final LoaderPlan plan)
119   {
120     final String id = plan.getId();
121     if ("default".equalsIgnoreCase(id))
122     {
123       return "";
124     }
125     else
126     {
127       return id;
128     }
129   }
130 
131   /**
132    * Helper that checks if the loader plans and the configured ci-config shall
133    * be used, or if a manual configuration has been made.
134    *
135    * @param jobName the jobName to use.
136    * @param jobConfigFile the config file.
137    * @return if there is a manual configuration.
138    */
139   public static boolean checkForManualConfiguration(final String jobName,
140       final String jobConfigFile)
141   {
142     return !StringUtils.isBlank(jobName) || !StringUtils.isBlank(jobConfigFile);
143   }
144 
145   /**
146    * Helper that checks if a proxy is configured.
147    *
148    * @param proxyId the id of the proxy as it is used in the maven settings.xml.
149    * @param proxyDomain the proxy domain.
150    * @param proxyClientName the client name.
151    * @return true if a proxy is configured.
152    */
153   public static boolean checkIfProxyIsConfigured(final String proxyId,
154       final String proxyDomain, final String proxyClientName)
155   {
156     if (StringUtils.isBlank(proxyId) || StringUtils.isBlank(proxyDomain)
157         || StringUtils.isBlank(proxyClientName))
158     {
159       return false;
160     }
161     return true;
162   }
163 
164   /**
165    * Helper that applys the login credentials to the controller.
166    *
167    * @param ciServer the ci server that has stored the credentials.
168    * @param controller the controller that needs the credentials.
169    * @throws MojoExecutionException when the ci server throws an exception.
170    */
171   public static void applyLoginCredentials(final CiServer ciServer,
172       final CiController controller) throws MojoExecutionException
173   {
174     final Credentials loginCredentials =
175         new Credentials(ciServer.getUsername(), ciServer.getPassword());
176     controller.setLoginCredentials(loginCredentials);
177   }
178 
179   /**
180    * Helper that checks if a config file is specified.
181    *
182    * @param jobConfigFile the configFile.
183    * @return true if the config file is not null.
184    */
185   public static boolean checkIfHudsonJobConfigFileIsSpecified(
186       final String jobConfigFile)
187   {
188     return !StringUtils.isBlank(jobConfigFile);
189   }
190 
191   /**
192    * Helper that creates a loder.
193    *
194    * @param mavenConfig the mavenconfig.
195    * @return a loader plan loader.
196    */
197   public static LoaderPlanLoader createLoaderPlanLoader(
198       final MavenConfig mavenConfig)
199   {
200     final LoaderPlanLoader loader = new LoaderPlanLoader(mavenConfig);
201     return loader;
202   }
203 
204   /**
205    * Helper that prettyfies the xml output.
206    *
207    * @param input the input that sahll be pretified.
208    * @param indent the indent.
209    * @return the pretified output.
210    * @throws MojoExecutionException when thrown.
211    */
212   public static String prettyFormat(final String input, final int indent)
213     throws MojoExecutionException
214   {
215     try
216     {
217       final Source xmlInput = new StreamSource(new StringReader(input));
218       final StringWriter stringWriter = new StringWriter();
219       final StreamResult xmlOutput = new StreamResult(stringWriter);
220       final TransformerFactory transformerFactory =
221           TransformerFactory.newInstance();
222       final Transformer transformer = transformerFactory.newTransformer();
223       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
224       transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
225       transformer.setOutputProperty(
226           OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, String.valueOf(indent));
227       transformer.transform(xmlInput, xmlOutput);
228       return xmlOutput.getWriter().toString();
229     }
230     catch (final Exception e)
231     {
232       throw new MojoExecutionException("Error formatting config-string.", e);
233     }
234   }
235 
236   // --- object basics --------------------------------------------------------
237 
238 }