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  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    static CiController createController(final String ciHost,
76        final LogLevel logLevel)
77    {
78      final CiSystem system = new CiSystem(ciHost);
79      final CiController controller =
80          HudsonCiSystemFactory.createCiController(system, logLevel);
81      return controller;
82    }
83  
84    static String determineJobName(final LoaderPlan plan)
85    {
86      final String jobNameMainPart =
87          plan.getMavenConfig().getPom().getArtifactId();
88      final String jobNameDiscriminator = determineDiscriminator(plan);
89      return calculateJobName(jobNameMainPart, jobNameDiscriminator);
90    }
91  
92    static String calculateJobName(final String jobNameMainPart,
93        final String jobNameDiscriminator)
94    {
95      if (StringUtils.isBlank(jobNameDiscriminator))
96      {
97        return jobNameMainPart;
98      }
99      else
100     {
101       return jobNameMainPart + "-" + jobNameDiscriminator;
102     }
103   }
104 
105   static String determineDiscriminator(final LoaderPlan plan)
106   {
107     final String id = plan.getId();
108     if ("default".equalsIgnoreCase(id))
109     {
110       return "";
111     }
112     else
113     {
114       return id;
115     }
116   }
117 
118   static boolean checkForManualConfiguration(final String jobName,
119       final String jobConfigFile)
120   {
121     return !StringUtils.isBlank(jobName) || !StringUtils.isBlank(jobConfigFile);
122   }
123 
124   static boolean checkIfProxyIsConfigured(final String proxyId,
125       final String proxyDomain, final String proxyClientName)
126   {
127     if (StringUtils.isBlank(proxyId) || StringUtils.isBlank(proxyDomain)
128         || StringUtils.isBlank(proxyClientName))
129     {
130       return false;
131     }
132     return true;
133   }
134 
135   static void applyLoginCredentials(final CiServer ciServer,
136       final CiController controller) throws MojoExecutionException
137   {
138     final Credentials loginCredentials =
139         new Credentials(ciServer.getUsername(), ciServer.getPassword());
140     controller.setLoginCredentials(loginCredentials);
141   }
142 
143   static boolean checkIfHudsonJobConfigFileIsSpecified(
144       final String jobConfigFile)
145   {
146     return !StringUtils.isBlank(jobConfigFile);
147   }
148 
149   static LoaderPlanLoader createLoaderPlanLoader(final MavenConfig mavenConfig)
150   {
151     final LoaderPlanLoader loader = new LoaderPlanLoader(mavenConfig);
152     return loader;
153   }
154 
155   static String prettyFormat(final String input, final int indent)
156       throws MojoExecutionException
157     {
158       try
159       {
160         final Source xmlInput = new StreamSource(new StringReader(input));
161         final StringWriter stringWriter = new StringWriter();
162         final StreamResult xmlOutput = new StreamResult(stringWriter);
163         final TransformerFactory transformerFactory =
164             TransformerFactory.newInstance();
165         final Transformer transformer = transformerFactory.newTransformer();
166         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
167         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
168         transformer.setOutputProperty(
169             OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, String.valueOf(indent));
170         transformer.transform(xmlInput, xmlOutput);
171         return xmlOutput.getWriter().toString();
172       }
173       catch (final Exception e)
174       {
175         throw new MojoExecutionException("Error formatting config-string.", e);
176       }
177     }
178 
179   // --- object basics --------------------------------------------------------
180 
181 }