1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45 public final class HudsonUtils
46 {
47
48
49
50
51
52
53
54
55
56
57
58
59
60 private HudsonUtils()
61 {
62
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
93
94
95
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
133
134
135
136
137
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
147
148
149
150
151
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
166
167
168
169
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
181
182
183
184
185 public static boolean checkIfHudsonJobConfigFileIsSpecified(
186 final String jobConfigFile)
187 {
188 return !StringUtils.isBlank(jobConfigFile);
189 }
190
191
192
193
194
195
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
206
207
208
209
210
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
237
238 }