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.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    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    // ****************************** Inner Classes *****************************
49  
50    // ********************************* Methods ********************************
51  
52    // --- init -----------------------------------------------------------------
53  
54    // --- get&set --------------------------------------------------------------
55  
56    // --- business -------------------------------------------------------------
57  
58    /**
59     * {@inheritDoc}
60     */
61    public final void execute() throws MojoExecutionException,
62      MojoFailureException
63    {
64      if (isToSkip())
65      {
66        return;
67      }
68  
69      super.execute();
70  
71      final BugzillaCommandFactory bugzillaCommandFactory =
72          new BugzillaCommandFactory();
73  
74      final MavenProject project = getProject();
75      commandFactory = new MavenCommandFactory(project, bugzillaCommandFactory);
76  
77      final boolean loginRequired = StringUtils.isNotBlank(server.getUsername());
78      loginOnDemand(loginRequired);
79  
80      run();
81  
82      logoutOnDemand(loginRequired);
83    }
84  
85    private void loginOnDemand(final boolean loginRequired)
86      throws MojoFailureException
87    {
88      if (loginRequired)
89      {
90        final LoginCommand loginCommand =
91            commandFactory.createLoginCommand(server);
92        console.execute(loginCommand);
93      }
94      else
95      {
96        final LogLevel logLevel = new LogLevel(verbose);
97        if (logLevel.isVerbose())
98        {
99          getLog()
100             .info(
101                 "Login skipped since no credentials specified.\n"
102                     + "Please refer to\n"
103                     + "  http://www.smartics.eu/bugzilla-maven-plugin/usage.html#Basic_Configuration\n"
104                     + "for information on how to configure login credentials.");
105       }
106     }
107   }
108 
109   private void logoutOnDemand(final boolean loginRequired)
110     throws MojoFailureException
111   {
112     if (loginRequired)
113     {
114       final LogoutCommand logoutCommand = commandFactory.createLogoutCommand();
115       console.execute(logoutCommand);
116     }
117   }
118 
119   /**
120    * Runs the Mojo after login and before logout.
121    *
122    * @throws MojoExecutionException if an unexpected problem occurs. Throwing
123    *           this exception causes a "BUILD ERROR" message to be displayed.
124    * @throws MojoFailureException if an expected problem (such as a compilation
125    *           failure) occurs. Throwing this exception causes a "BUILD FAILURE"
126    *           message to be displayed.
127    */
128   protected abstract void run() throws MojoExecutionException,
129     MojoFailureException;
130 
131   // --- object basics --------------------------------------------------------
132 
133 }