1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29 public final class Console
30 {
31
32
33
34
35
36
37
38
39
40 private final CommandTarget target;
41
42
43
44
45 private final Log log;
46
47
48
49
50 private final LogLevel logLevel;
51
52
53
54
55
56
57
58
59
60
61
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
144
145 }