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.MojoFailureException;
19  import org.apache.maven.plugin.logging.Log;
20  
21  import de.smartics.maven.issue.command.Command;
22  import de.smartics.maven.issue.command.CommandResult;
23  import de.smartics.maven.issue.command.CommandTarget;
24  import de.smartics.maven.issue.util.LogLevel;
25  
26  /**
27   * Console to run commands on the issues server.
28   */
29  public final class Console
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The target to run commands against.
39     */
40    private final CommandTarget target;
41  
42    /**
43     * The logger to log to.
44     */
45    private final Log log;
46  
47    /**
48     * Defines the verboseness of the output.
49     */
50    private final LogLevel logLevel;
51  
52    // ****************************** Initializer *******************************
53  
54    // ****************************** Constructors ******************************
55  
56    /**
57     * Default constructor.
58     *
59     * @param target the target to run commands against.
60     * @param log the logger to log to.
61     * @param logLevel defines the verboseness of the output.
62     */
63    public Console(final CommandTarget target, final Log log,
64        final LogLevel logLevel)
65    {
66      this.target = target;
67      this.log = log;
68      this.logLevel = logLevel;
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    // --- business -------------------------------------------------------------
80  
81    /**
82     * Executes the given command.
83     *
84     * @param command the command to execute.
85     * @return a reference to this console instance.
86     * @throws MojoFailureException if the result is not as expected.
87     */
88    public Console execute(final Command<?> command) throws MojoFailureException
89    {
90      if(command == null)
91      {
92        return this;
93      }
94  
95      if (logLevel.isVerbose())
96      {
97        log.info("Executing: " + command);
98      }
99      final CommandResult<?> result = command.execute(target);
100 
101     if (logLevel.isNormal())
102     {
103       logDescription(result);
104     }
105 
106     final String message =
107         "Executed " + command + "\n    ==> " + result
108             + (logLevel.isTrace() ? '\n' + result.getPage().getContent() : "");
109     checkResult(result, message);
110 
111     if (logLevel.isVerbose())
112     {
113       log.info(message);
114     }
115 
116     return this;
117   }
118 
119   private void logDescription(final CommandResult<?> result)
120   {
121     final String description = result.getDescription();
122     if (description != null)
123     {
124       log.info(description);
125     }
126   }
127 
128   private void checkResult(final CommandResult<?> result, final String message)
129     throws MojoFailureException
130   {
131     if (!result.isExpected())
132     {
133       if ("Product Access Denied".equals(result.getPage().getTitle()))
134       {
135         throw new MojoFailureException(
136             message
137                 + "\nProduct is probably unknown. Try to add the product first.");
138       }
139       throw new MojoFailureException(message);
140     }
141   }
142 
143   // --- object basics --------------------------------------------------------
144 
145 }