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      try
81      {
82        run();
83      }
84      finally
85      {
86        logoutOnDemand(loginRequired);
87      }
88    }
89  
90    private void loginOnDemand(final boolean loginRequired)
91      throws MojoFailureException
92    {
93      if (loginRequired)
94      {
95        final LoginCommand loginCommand =
96            commandFactory.createLoginCommand(server);
97        console.execute(loginCommand);
98      }
99      else
100     {
101       final LogLevel logLevel = new LogLevel(verbose);
102       if (logLevel.isVerbose())
103       {
104         getLog()
105             .info(
106                 "Login skipped since no credentials specified.\n"
107                     + "Please refer to\n"
108                     + "  http://www.smartics.eu/bugzilla-maven-plugin/usage.html#Basic_Configuration\n"
109                     + "for information on how to configure login credentials.");
110       }
111     }
112   }
113 
114   private void logoutOnDemand(final boolean loginRequired)
115     throws MojoFailureException
116   {
117     if (loginRequired)
118     {
119       final LogoutCommand logoutCommand = commandFactory.createLogoutCommand();
120       console.execute(logoutCommand);
121     }
122   }
123 
124   /**
125    * Runs the Mojo after login and before logout.
126    *
127    * @throws MojoExecutionException if an unexpected problem occurs. Throwing
128    *           this exception causes a "BUILD ERROR" message to be displayed.
129    * @throws MojoFailureException if an expected problem (such as a compilation
130    *           failure) occurs. Throwing this exception causes a "BUILD FAILURE"
131    *           message to be displayed.
132    */
133   protected abstract void run() throws MojoExecutionException,
134     MojoFailureException;
135 
136   // --- object basics --------------------------------------------------------
137 
138 }