View Javadoc

1   /*
2    * Copyright 2012-2013 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.maven.plugin.MojoExecutionException;
19  import org.apache.maven.plugin.MojoFailureException;
20  import org.apache.maven.project.MavenProject;
21  import org.codehaus.plexus.util.StringUtils;
22  
23  import de.smartics.maven.bugzilla.command.BugzillaCommandFactory;
24  import de.smartics.maven.issue.command.LoginCommand;
25  import de.smartics.maven.issue.command.LogoutCommand;
26  import de.smartics.maven.issue.util.LogLevel;
27  
28  /**
29   * Base implementation of a issue management mojo.
30   */
31  public abstract class AbstractIssueManagementMojo extends AbstractIssueMojo
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    /**
40     * The helper to create commands.
41     */
42    protected MavenCommandFactory commandFactory;
43  
44    /**
45     * The version of the issue server. Allows the implementation to make
46     * version dependent decisions.
47     *
48     * @parameter expression="${issueServerVersion}"
49     * @since 1.0
50     */
51    protected String issueServerVersion;
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    // ****************************** Inner Classes *****************************
58  
59    // ********************************* Methods ********************************
60  
61    // --- init -----------------------------------------------------------------
62  
63    // --- get&set --------------------------------------------------------------
64  
65    // --- business -------------------------------------------------------------
66  
67    /**
68     * {@inheritDoc}
69     */
70    public final void execute() throws MojoExecutionException,
71      MojoFailureException
72    {
73      if (isToSkip())
74      {
75        return;
76      }
77  
78      super.execute();
79  
80      final BugzillaCommandFactory bugzillaCommandFactory =
81          new BugzillaCommandFactory(issueServerVersion);
82  
83      final MavenProject project = getProject();
84      commandFactory = new MavenCommandFactory(project, bugzillaCommandFactory);
85  
86      final boolean loginRequired = StringUtils.isNotBlank(server.getUsername());
87      loginOnDemand(loginRequired);
88  
89      try
90      {
91        run();
92      }
93      finally
94      {
95        logoutOnDemand(loginRequired);
96      }
97    }
98  
99    private void loginOnDemand(final boolean loginRequired)
100     throws MojoFailureException
101   {
102     if (loginRequired)
103     {
104       final LoginCommand loginCommand =
105           commandFactory.createLoginCommand(server);
106       console.execute(loginCommand);
107     }
108     else
109     {
110       final LogLevel logLevel = new LogLevel(verbose);
111       if (logLevel.isVerbose())
112       {
113         getLog()
114             .info(
115                 "Login skipped since no credentials specified.\n"
116                     + "Please refer to\n"
117                     + "  http://www.smartics.eu/bugzilla-maven-plugin/usage.html#Basic_Configuration\n"
118                     + "for information on how to configure login credentials.");
119       }
120     }
121   }
122 
123   private void logoutOnDemand(final boolean loginRequired)
124     throws MojoFailureException
125   {
126     if (loginRequired)
127     {
128       final LogoutCommand logoutCommand = commandFactory.createLogoutCommand();
129       console.execute(logoutCommand);
130     }
131   }
132 
133   /**
134    * Runs the Mojo after login and before logout.
135    *
136    * @throws MojoExecutionException if an unexpected problem occurs. Throwing
137    *           this exception causes a "BUILD ERROR" message to be displayed.
138    * @throws MojoFailureException if an expected problem (such as a compilation
139    *           failure) occurs. Throwing this exception causes a "BUILD FAILURE"
140    *           message to be displayed.
141    */
142   protected abstract void run() throws MojoExecutionException,
143     MojoFailureException;
144 
145   // --- object basics --------------------------------------------------------
146 
147 }