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