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 19 import org.apache.maven.plugin.MojoExecutionException; 20 21 import de.smartics.ci.comm.CiController; 22 import de.smartics.ci.comm.LogLevel; 23 import de.smartics.ci.comm.command.CommandException; 24 import de.smartics.ci.comm.command.LoginCommandException; 25 import de.smartics.ci.maven.CiServer; 26 import de.smartics.ci.maven.HudsonUtils; 27 28 /** 29 * Hudson CI mojo. 30 */ 31 public abstract class AbstractHudsonCiMojo extends AbstractCiMojo 32 { 33 // ********************************* Fields ********************************* 34 35 // --- constants ------------------------------------------------------------ 36 37 // --- members -------------------------------------------------------------- 38 39 /** 40 * Control whether the <code>scm</code> element is to be added to the 41 * generated Job <code>config.xml</code> with full content. The 42 * <code>scm</code> is an element in addition to the SCM configuration as an 43 * entry in the same document. 44 * <p> 45 * In some documents the element is expected to be stated redundantly. Some 46 * configurations allow to reference the SCM information of an entry element 47 * with an XPath expression. Adding an element with that expression is 48 * <strong>not</strong> controllable with this flag. 49 * </p> 50 * 51 * @parameter default-value="false" 52 * @since 1.0 53 */ 54 protected boolean addScmElementWithFullContent; 55 56 /** 57 * Controls the removal of the <code>cascadingChildrenNames</code> element 58 * from the job configuration. 59 * <p> 60 * The element is added by Hudson if the <code>config.xml</code> is retrieved. 61 * The presence of the element rejects the configuration from being pushed on 62 * the server. 63 * </p> 64 * 65 * @parameter default-value="true" 66 * @since 1.0 67 */ 68 protected boolean removeCascadingChildrenNamesElement; 69 70 /** 71 * Controls the removal of the <code>cascading-job-properties</code> element 72 * from the job configuration. 73 * <p> 74 * The element is added by Hudson if the <code>config.xml</code> is retrieved. 75 * The presence of the element rejects the configuration from being pushed on 76 * the server. 77 * </p> 78 * 79 * @parameter default-value="true" 80 * @since 1.0 81 */ 82 protected boolean removeCascadingJobPropertiesElement; 83 84 // ****************************** Initializer ******************************* 85 86 // ****************************** Constructors ****************************** 87 88 // ****************************** Inner Classes ***************************** 89 90 // ********************************* Methods ******************************** 91 92 // --- init ----------------------------------------------------------------- 93 94 // --- get&set -------------------------------------------------------------- 95 96 // --- business ------------------------------------------------------------- 97 98 /** 99 * Called to execute the job on the conroller. 100 * 101 * @param jobName the name of the job for which a command shall be executed. 102 * @param jobConfigString the configuration needed to execute the command. 103 * @param controller the controller to execute a job. 104 * @throws CommandException when the command failed. 105 */ 106 protected abstract void executeCommand(final String jobName, 107 final String jobConfigString, final CiController controller) 108 throws CommandException; 109 110 /** 111 * Login. 112 * 113 * @param logLevel the logLevel to use. 114 * @return the ciController to control the ci server. 115 * @throws MojoExecutionException when the mojo could not be executed. 116 * @throws LoginCommandException when the login failed. 117 */ 118 protected final CiController loginToCiServer(final LogLevel logLevel) 119 throws MojoExecutionException, LoginCommandException 120 { 121 final CiServer ciServerInfo = getCiServer(); 122 123 final String ciHost = ciServerInfo.getUrl(); 124 125 final CiController controller = HudsonUtils.createController(ciHost, logLevel); 126 handleLogin(controller); 127 return controller; 128 } 129 130 /** 131 * Handle the login. Overwrite this method when immediately before or after 132 * the login something has to be done. 133 * 134 * @param controller the controller to control hudson. 135 * @throws LoginCommandException when the login fails 136 * @throws MojoExecutionException when the mojo is misconfigured. 137 */ 138 protected void handleLogin(final CiController controller) 139 throws LoginCommandException, MojoExecutionException 140 { 141 final CiServer ciServer = getCiServer(); 142 HudsonUtils.applyLoginCredentials(ciServer, controller); 143 controller.login(); 144 } 145 146 // --- object basics -------------------------------------------------------- 147 148 }