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.maven.bugzilla;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.maven.model.IssueManagement;
20  import org.apache.maven.plugin.AbstractMojo;
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.MojoFailureException;
23  import org.apache.maven.plugin.logging.Log;
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.settings.Server;
26  import org.apache.maven.settings.Settings;
27  
28  import de.smartics.maven.issue.command.CommandTarget;
29  import de.smartics.maven.issue.util.LogLevel;
30  import de.smartics.maven.issue.util.MavenProjectWrapper;
31  
32  /**
33   * Base implementation for Bugzilla mojos.
34   */
35  public abstract class AbstractIssueMojo extends AbstractMojo
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    // ... Mojo infrastructure ..................................................
44  
45    /**
46     * The Maven project.
47     *
48     * @parameter expression="${project}"
49     * @required
50     * @readonly
51     * @since 1.0
52     */
53    private MavenProject project;
54  
55    /**
56     * The user's settings.
57     *
58     * @parameter expression="${settings}"
59     * @required
60     * @readonly
61     * @since 1.0
62     */
63    protected Settings settings;
64  
65    // ... parameter ............................................................
66  
67    /**
68     * The name of the server configuration in the <code>settings.xml</code> that
69     * provides credentials to access the issues server. Only used if no server
70     * matches the POM's <code>issueManagement/url</code> and
71     * <code>issueManagement/system</code>.
72     *
73     * @parameter default-value="bugzilla"
74     * @since 1.0
75     */
76    protected String issueServerId;
77  
78    // ... command ..............................................................
79  
80    /**
81     * Information about the issue server.
82     */
83    protected IssueServer server;
84  
85    /**
86     * The console to execute commands.
87     */
88    protected Console console;
89  
90    /**
91     * Defines the verboseness of the output.
92     * <ul>
93     * <li><code>QUIET</code> - prints no info messages.</li>
94     * <li><code>NORMAL</code> - only prints the returned page titles.</li>
95     * <li><code>VERBOSE</code> - adds information about the called service and
96     * parameters.</li>
97     * <li><code>TRACE</code> - provides the most verbose output with dumping the
98     * contents of the returned pages.</li>
99     * </ul>
100    *
101    * @parameter expression="${verbose}" default-value="NORMAL"
102    * @since 1.0
103    */
104   protected String verbose;
105 
106   // ... fixes ................................................................
107 
108   // ****************************** Initializer *******************************
109 
110   // ****************************** Constructors ******************************
111 
112   // ****************************** Inner Classes *****************************
113 
114   /**
115    * Provides access information for the continuous integration server.
116    */
117   protected static final class IssueServer extends Server
118   {
119     /**
120      * The class version identifier.
121      * <p>
122      * The value of this constant is {@value}.
123      * </p>
124      */
125     private static final long serialVersionUID = 1L;
126 
127     /**
128      * The URL to the server.
129      */
130     private final String url;
131 
132     /**
133      * Default constructor.
134      *
135      * @param url the URL to the server.
136      * @param server the server information from the <code>settings.xml</code>.
137      */
138     private IssueServer(final Server server, final String url)
139     {
140       this.url = url;
141 
142       if (server != null)
143       {
144         this.setId(server.getId());
145         this.setUsername(server.getUsername());
146 
147         this.setPassword(server.getPassword());
148         this.setPassphrase(server.getPassphrase());
149         this.setPrivateKey(server.getPrivateKey());
150 
151         this.setDirectoryPermissions(server.getDirectoryPermissions());
152         this.setFilePermissions(server.getFilePermissions());
153         this.setConfiguration(server.getConfiguration());
154 
155         this.setSourceLevel(server.getSourceLevel());
156       }
157     }
158 
159     /**
160      * Returns the URL to the server.
161      *
162      * @return the URL to the server.
163      */
164     public String getUrl()
165     {
166       return url;
167     }
168   }
169 
170   // ********************************* Methods ********************************
171 
172   // --- init -----------------------------------------------------------------
173 
174   // --- get&set --------------------------------------------------------------
175 
176   /**
177    * Returns the Maven project.
178    *
179    * @return the Maven project.
180    */
181   protected final MavenProject getProject()
182   {
183     return new MavenProjectWrapper(project);
184   }
185 
186   // --- business -------------------------------------------------------------
187 
188   /**
189    * Returns the issue server configuration to run a remote access.
190    *
191    * @return the server information. If no access information is found, only the
192    *         URL to the issue server is returned.
193    * @throws MojoExecutionException if no issue server URL is specified.
194    */
195   protected final IssueServer getIssueServer() throws MojoExecutionException
196   {
197     final IssueManagement issueManagement = project.getIssueManagement();
198     if (issueManagement == null)
199     {
200       throw new MojoExecutionException(
201           "No issue management specified. Please provide issue information"
202               + " within the 'issueManagement' block of your POM.");
203     }
204 
205     final Server server = getIssueServerFromSettings(issueManagement);
206     final String ciUrl = issueManagement.getUrl();
207     return new IssueServer(server, ciUrl);
208   }
209 
210   // CHECKSTYLE:OFF
211   /**
212    * {@inheritDoc}
213    *
214    * @see org.apache.maven.plugin.Mojo#execute()
215    */
216   @Override
217   public void execute() throws MojoExecutionException, MojoFailureException
218   // CHECKSTYLE:ON
219   {
220     final Log log = getLog();
221 
222     server = getIssueServer();
223     final String url = server.getUrl();
224     final LogLevel logLevel = new LogLevel(verbose);
225     final CommandTarget target = new CommandTarget(url);
226 
227     console = new Console(target, log, logLevel);
228   }
229 
230   private Server getIssueServerFromSettings(
231       final IssueManagement issueManagement) throws MojoExecutionException
232   {
233     final String issueUrl = issueManagement.getUrl();
234     if (StringUtils.isBlank(issueUrl))
235     {
236       throw new MojoExecutionException(
237           "No issue URL specified. Please provide issue information within the"
238               + " 'issueManagement/url' block of your POM.");
239     }
240 
241     Server server;
242     if (StringUtils.isNotBlank(issueServerId))
243     {
244       server = settings.getServer(issueServerId);
245       if (server != null)
246       {
247         return server;
248       }
249     }
250 
251     final String ciName = issueManagement.getSystem();
252     if (StringUtils.isNotBlank(ciName))
253     {
254       server = settings.getServer(ciName);
255       if (server != null)
256       {
257         return server;
258       }
259     }
260 
261     server = settings.getServer(issueUrl);
262 
263     return server;
264   }
265 
266   /**
267    * Checks whether or not the plugin should execute.
268    *
269    * @return <code>true</code> if the execution of the plugin should be skipped,
270    *         <code>false</code> if it should run.
271    */
272   protected final boolean isToSkip()
273   {
274     return !project.isExecutionRoot();
275   }
276 
277   // --- object basics --------------------------------------------------------
278 
279 }