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.mojo;
17  
18  import java.util.Properties;
19  
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.plugin.MojoFailureException;
22  
23  import de.smartics.ci.comm.CiController;
24  import de.smartics.ci.comm.LogLevel;
25  import de.smartics.ci.comm.command.CommandException;
26  
27  /**
28   * Check status of a job in hudson ci mojo.
29   *
30   * @goal genericJobApi
31   * @requiresProject
32   * @description checks the status of a job in the ci-server.
33   */
34  public class GenericJobApiUsageHudsonCiMojo extends AbstractHudsonCiMojo
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The xmlQueryString to use for the xml api.
44     *
45     * @parameter expression="${hudson-maven-plugin.xmlQueryString}"
46     *            default-value= "xpath=mavenModuleSet/name"
47     * @since 1.0
48     */
49    private String xmlQueryString;
50  
51    /**
52     * The ci xml job api URL.
53     *
54     * @parameter expression="${hudson-maven-plugin.xmlApiUrl}"
55     *            default-value="@{ciServer}/job/@{jobName}/api/xml"
56     * @since 1.0
57     */
58    private String ciXmlJobApiUrl;
59  
60    /**
61     * The regexp to check the response.
62     *
63     * @parameter expression="${hudson-maven-plugin.responseCheckingRegexp}"
64     *            default-value=".*@{jobName}.*"
65     * @since 1.0
66     */
67    private String regexp;
68  
69    /**
70     * Use this name as the job name.
71     *
72     * @parameter expression="${hudson-maven-plugin.jobName}"
73     * @since 1.0
74     */
75    private String jobName;
76  
77    // ****************************** Initializer *******************************
78  
79    // ****************************** Constructors ******************************
80  
81    // ****************************** Inner Classes *****************************
82  
83    // ********************************* Methods ********************************
84  
85    // --- init -----------------------------------------------------------------
86  
87    // --- get&set --------------------------------------------------------------
88  
89    // --- business -------------------------------------------------------------
90  
91    /**
92     * {@inheritDoc}
93     *
94     * @see org.apache.maven.plugin.Mojo#execute()
95     */
96    public final void execute() throws MojoExecutionException,
97      MojoFailureException
98    {
99      executeManualConfiguration();
100   }
101 
102   private void executeManualConfiguration() throws MojoExecutionException
103   {
104     final LogLevel logLevel = new LogLevel(verbose);
105     try
106     {
107       final CiController controller = loginToCiServer(logLevel);
108       executeCommand(jobName, null, controller);
109     }
110     catch (final Exception e)
111     {
112       logError("Terminated handling jobs. Rollback not possile. Please check status manually at: "
113                + getCiServer().getUrl());
114       throw new MojoExecutionException("Cannot handle jobName " + jobName, e);
115     }
116   }
117 
118   /**
119    * {@inheritDoc}
120    *
121    * @throws CommandException
122    */
123   public final void executeCommand(final String jobName,
124       final String jobConfigString, final CiController controller)
125     throws CommandException
126   {
127     executeGenericJob(controller, jobName);
128   }
129 
130   private void executeGenericJob(final CiController controller,
131       final String jobName) throws CommandException
132   {
133     final Properties properties = project.getProperties();
134     addCiServerUrl(properties);
135     controller.executeGenericJobCommand(jobName, ciXmlJobApiUrl,
136         xmlQueryString, regexp, properties);
137   }
138 
139   private void addCiServerUrl(final Properties properties)
140   {
141     try
142     {
143       final String ciServerUrl = getCiServer().getUrl();
144       properties.put("ciServer", ciServerUrl);
145     }
146     catch (final MojoExecutionException e)
147     {
148       if (logger.isWarnEnabled())
149       {
150         logger.warn("ciServerUrl could not be retrieved.");
151       }
152     }
153   }
154 
155   // --- object basics --------------------------------------------------------
156 
157 }