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.comm;
17  
18  import java.util.Properties;
19  
20  import de.smartics.ci.comm.command.CommandException;
21  import de.smartics.ci.comm.command.CommandResult;
22  import de.smartics.ci.comm.command.LoginCommandException;
23  import de.smartics.ci.comm.hudson.command.HudsonCheckStatusJobCommand;
24  import de.smartics.ci.comm.hudson.command.HudsonCreateJobCommand;
25  import de.smartics.ci.comm.hudson.command.HudsonDeleteJobCommand;
26  import de.smartics.ci.comm.hudson.command.HudsonDisableJobCommand;
27  import de.smartics.ci.comm.hudson.command.HudsonEnableJobCommand;
28  import de.smartics.ci.comm.hudson.command.HudsonGenericJobCommand;
29  import de.smartics.ci.comm.hudson.command.HudsonLoginCommand;
30  import de.smartics.ci.comm.hudson.command.HudsonUpdateJobCommand;
31  
32  /**
33   * Controls the ci server. Right now only hudson.
34   */
35  public final class CiController
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The credentials used to login to the ci server.
45     */
46    private Credentials loginCredentials;
47  
48    /**
49     * The proxy information used to connect to the network using a proxy.
50     */
51    private ProxyInformation proxy;
52  
53    /**
54     * The login command to use to login to the ci system.
55     */
56    private HudsonLoginCommand loginCommand;
57  
58    /**
59     * The command to delete a job.
60     */
61    private HudsonDeleteJobCommand deleteJobCommand;
62  
63    /**
64     * The command to create a job.
65     */
66    private HudsonCreateJobCommand createJobCommand;
67  
68    /**
69     * the command to update a job.
70     */
71    private HudsonUpdateJobCommand updateJobCommand;
72  
73    /**
74     * The command to enable a job.
75     */
76    private HudsonEnableJobCommand enableJobCommand;
77  
78    /**
79     * The command to disable a job.
80     */
81    private HudsonDisableJobCommand disableJobCommand;
82  
83    /**
84     * The command to check the status of a job.
85     */
86    private HudsonCheckStatusJobCommand checkStatusJobCommand;
87  
88    /**
89     * The command to execute a generic command for a job.
90     */
91    private HudsonGenericJobCommand genericJobCommand;
92  
93    /**
94     * The target system on which the commands are executed.
95     */
96    private final CiSystem target;
97  
98    /**
99     * The log level for this controller.
100    */
101   private LogLevel logLevel;
102 
103   // ****************************** Initializer *******************************
104 
105   // ****************************** Constructors ******************************
106 
107   /**
108    * Default constructor.
109    *
110    * @param system the system on which the commands are executed.
111    */
112   public CiController(final CiSystem system)
113   {
114     this.target = system;
115   }
116 
117   // ****************************** Inner Classes *****************************
118 
119   // ********************************* Methods ********************************
120 
121   // --- init -----------------------------------------------------------------
122 
123   // --- get&set --------------------------------------------------------------
124 
125   /**
126    * Set the log level for this controller.
127    *
128    * @param logLevel the log level to use.
129    */
130   public void setLogLevel(final LogLevel logLevel)
131   {
132     this.logLevel = logLevel;
133   }
134 
135   /**
136    * Sets the credentials used to login to the ci server.
137    *
138    * @param loginCredentials the credentials used to login to the ci server.
139    */
140   public void setLoginCredentials(final Credentials loginCredentials)
141   {
142     this.loginCredentials = loginCredentials;
143   }
144 
145   /**
146    * Sets the proxy information used to connect to the network using a proxy.
147    *
148    * @param proxy the proxy information used to connect to the network using a
149    *          proxy.
150    */
151   public void setProxyInformation(final ProxyInformation proxy)
152   {
153     this.proxy = proxy;
154   }
155 
156   /**
157    * Sets the login command to use to login to the ci system.
158    *
159    * @param loginCommand the login command to use to login to the ci system.
160    */
161   public void setLoginCommand(final HudsonLoginCommand loginCommand)
162   {
163     this.loginCommand = loginCommand;
164     loginCommand.setLogLevel(logLevel);
165   }
166 
167   /**
168    * Sets the command to delete a job.
169    *
170    * @param deleteJobCommand the command to delete a job.
171    */
172   public void setDeleteJobCommand(final HudsonDeleteJobCommand deleteJobCommand)
173   {
174     this.deleteJobCommand = deleteJobCommand;
175     deleteJobCommand.setLogLevel(logLevel);
176   }
177 
178   /**
179    * Sets the command to create a job.
180    *
181    * @param createJobCommand the command to create a job.
182    */
183   public void setCreateJobCommand(final HudsonCreateJobCommand createJobCommand)
184   {
185     this.createJobCommand = createJobCommand;
186     createJobCommand.setLogLevel(logLevel);
187   }
188 
189   /**
190    * Sets the value for updateJobCommand.
191    * <p>
192    * the command to update a job.
193    *
194    * @param updateJobCommand the value for updateJobCommand.
195    */
196   public void setUpdateJobCommand(final HudsonUpdateJobCommand updateJobCommand)
197   {
198     this.updateJobCommand = updateJobCommand;
199     updateJobCommand.setLogLevel(logLevel);
200   }
201 
202   /**
203    * Sets the command to enable a job.
204    *
205    * @param enableJobCommand the command to enable a job.
206    */
207   public void setEnableJobCommand(final HudsonEnableJobCommand enableJobCommand)
208   {
209     this.enableJobCommand = enableJobCommand;
210     enableJobCommand.setLogLevel(logLevel);
211   }
212 
213   /**
214    * Sets the command to disable a job.
215    *
216    * @param disableJobCommand the command to disable a job.
217    */
218   public void setDisableJobCommand(
219       final HudsonDisableJobCommand disableJobCommand)
220   {
221     this.disableJobCommand = disableJobCommand;
222     disableJobCommand.setLogLevel(logLevel);
223   }
224 
225   /**
226    * Sets the command to check the status of a job.
227    *
228    * @param statusJobCommand the command to check a job.
229    */
230   public void setStatusJobCommand(
231       final HudsonCheckStatusJobCommand statusJobCommand)
232   {
233     this.checkStatusJobCommand = statusJobCommand;
234     checkStatusJobCommand.setLogLevel(logLevel);
235   }
236 
237   /**
238    * Sets the generic job command.
239    *
240    * @param genericJobCommand the command to execute on a job.
241    */
242   public void setGenericJobCommand(
243       final HudsonGenericJobCommand genericJobCommand)
244   {
245     this.genericJobCommand = genericJobCommand;
246     genericJobCommand.setLogLevel(logLevel);
247   }
248 
249   // --- business -------------------------------------------------------------
250 
251   /**
252    * Login to the target.
253    *
254    * @throws LoginCommandException when the login failed.
255    */
256   public void login() throws LoginCommandException
257   {
258     loginCommand.setLoginCredentials(loginCredentials);
259     loginCommand.setProxyInformation(proxy);
260     loginCommand.execute(target);
261   }
262 
263   /**
264    * Create a job on the target system.
265    *
266    * @param jobName the name of the job.
267    * @param configString the config string.
268    * @throws CommandException when the command failed.
269    */
270   public void createJob(final String jobName, final String configString)
271     throws CommandException
272   {
273     createJobCommand.setJobName(jobName);
274     createJobCommand.setConfigString(configString);
275     createJobCommand.execute(target);
276   }
277 
278   /**
279    * Update a job on the target system.
280    *
281    * @param jobName the name of the job.
282    * @param configString the config string.
283    * @throws CommandException when the command failed.
284    */
285   public void updateJob(final String jobName, final String configString)
286     throws CommandException
287   {
288     updateJobCommand.setJobName(jobName);
289     updateJobCommand.setConfigString(configString);
290     updateJobCommand.execute(target);
291   }
292 
293   /**
294    * Disable a job on the target system.
295    *
296    * @param jobName the name of the job.
297    * @throws CommandException when the command failed.
298    */
299   public void disableJob(final String jobName) throws CommandException
300   {
301     disableJobCommand.setJobName(jobName);
302     disableJobCommand.execute(target);
303   }
304 
305   /**
306    * Enable a job on the target system.
307    *
308    * @param jobName the name of the job.
309    * @throws CommandException when the command failed.
310    */
311   public void enableJob(final String jobName) throws CommandException
312   {
313     enableJobCommand.setJobName(jobName);
314     enableJobCommand.execute(target);
315   }
316 
317   /**
318    * Delete a job on the target system.
319    *
320    * @param jobName the name of the job.
321    * @throws CommandException when the command failed.
322    */
323   public void deleteJob(final String jobName) throws CommandException
324   {
325     deleteJobCommand.setJobName(jobName);
326     deleteJobCommand.execute(target);
327   }
328 
329   /**
330    * Check status of a job on the target system and return the result.
331    *
332    * @param jobName the name of the job.
333    * @param xmlQueryString the xml api queryString to run at the ci server.
334    * @param properties a list of key value pairs used to replace variables in
335    *          the xml query string.
336    * @return the result of the command or null.
337    * @throws CommandException when the command failed.
338    */
339   public CommandResult<?> checkStatusJob(final String jobName,
340       final String xmlQueryString, final Properties properties)
341     throws CommandException
342   {
343     checkStatusJobCommand.setJobName(jobName);
344     checkStatusJobCommand.setXmlQueryString(xmlQueryString);
345     checkStatusJobCommand.setKeyValuePairs(properties);
346     checkStatusJobCommand.execute(target);
347     return checkStatusJobCommand.getResult();
348   }
349 
350   /**
351    * Check status of a job on the target system and return the result.
352    *
353    * @param jobName the name of the job.
354    * @param apiUrl the api url.
355    * @param xmlQueryString the xml api queryString to run at the ci server.
356    * @param regexp the regexp to check the result.
357    * @param properties a list of key value pairs used to replace variables in
358    *          the xml query string.
359    * @return the result of the command or null.
360    * @throws CommandException when the command failed.
361    */
362   public CommandResult<?> executeGenericJobCommand(final String jobName,
363       final String apiUrl, final String xmlQueryString, final String regexp,
364       final Properties properties) throws CommandException
365   {
366     genericJobCommand.setJobName(jobName);
367     genericJobCommand.setCiXmlJobApiUrl(apiUrl);
368     genericJobCommand.setXmlQueryString(xmlQueryString);
369     genericJobCommand.setResponseCheckingRegexp(regexp);
370     genericJobCommand.setKeyValuePairs(properties);
371     genericJobCommand.execute(target);
372     return genericJobCommand.getResult();
373   }
374 
375   // --- object basics --------------------------------------------------------
376 
377 }