1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.whitesource.maven;
17
18
19 import org.apache.maven.execution.MavenSession;
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugin.logging.Log;
24 import org.apache.maven.plugins.annotations.Component;
25 import org.apache.maven.plugins.annotations.Parameter;
26 import org.apache.maven.project.MavenProject;
27 import org.apache.maven.project.ProjectDependenciesResolver;
28 import org.sonatype.aether.RepositorySystemSession;
29 import org.sonatype.aether.repository.Authentication;
30 import org.sonatype.aether.repository.Proxy;
31 import org.sonatype.aether.repository.RemoteRepository;
32 import org.whitesource.agent.client.ClientConstants;
33 import org.whitesource.agent.client.WhitesourceService;
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class WhitesourceMojo extends AbstractMojo {
47
48
49
50
51
52
53 @Parameter(defaultValue = "false")
54 protected boolean failOnError;
55
56
57
58
59 @Parameter(defaultValue = "false")
60 protected boolean skip;
61
62 @Component
63 protected MavenSession session;
64
65 @Component
66 protected MavenProject mavenProject;
67
68
69
70
71 @Component( hint = "default" )
72 protected ProjectDependenciesResolver projectDependenciesResolver;
73
74
75
76
77 @Parameter( defaultValue = "${repositorySystemSession}", readonly = true, required = true )
78 protected RepositorySystemSession repoSession;
79
80 protected WhitesourceService service;
81
82
83
84 public abstract void doExecute() throws MojoExecutionException, MojoFailureException;
85
86
87
88 @Override
89 public void execute() throws MojoExecutionException, MojoFailureException {
90 final long startTime = System.currentTimeMillis();
91
92 if (skip) {
93 info("Skipping update");
94 } else {
95 try {
96 createService();
97 doExecute();
98 } catch (MojoExecutionException e) {
99 handleError(e);
100 } catch (RuntimeException e) {
101 throw new MojoFailureException("Unexpected error", e);
102 } finally {
103 if (service != null) {
104 service.shutdown();
105 }
106 }
107 }
108
109 info("Total execution time is " + (System.currentTimeMillis() - startTime) + " [msec]");
110 }
111
112
113
114 protected void createService() {
115 String serviceUrl = session.getSystemProperties().getProperty(
116 ClientConstants.SERVICE_URL_KEYWORD, ClientConstants.DEFAULT_SERVICE_URL);
117
118 service = new WhitesourceService(Constants.AGENT_TYPE, Constants.AGENT_VERSION, serviceUrl);
119
120
121 RemoteRepository dummyRepo = new RemoteRepository().setUrl(serviceUrl);
122 final Proxy proxy = session.getRepositorySession().getProxySelector().getProxy(dummyRepo);
123 if (proxy != null) {
124 String username = null;
125 String password = null;
126 final Authentication auth = proxy.getAuthentication();
127 if (auth != null) {
128 username = auth.getUsername();
129 password = auth.getPassword();
130 }
131 service.getClient().setProxy(proxy.getHost(), proxy.getPort(), username, password);
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 }
157
158 protected void handleError(Exception error) throws MojoFailureException {
159 String message = error.getMessage();
160 if (failOnError) {
161 debug(message, error);
162 throw new MojoFailureException(message);
163 } else {
164 error(message, error);
165 }
166 }
167
168 protected void debug(CharSequence content) {
169 final Log log = getLog();
170 if (log != null) {
171 log.debug(content);
172 }
173 }
174
175 protected void debug(CharSequence content, Throwable error) {
176 final Log log = getLog();
177 if (log != null) {
178 log.debug(content, error);
179 }
180 }
181
182 protected void info(CharSequence content) {
183 final Log log = getLog();
184 if (log != null) {
185 log.info(content);
186 }
187 }
188
189 protected void warn(CharSequence content, Throwable error) {
190 final Log log = getLog();
191 if (log != null) {
192 log.debug(content, error);
193 log.warn(content);
194 }
195 }
196
197 protected void warn(CharSequence content) {
198 final Log log = getLog();
199 if (log != null) {
200 log.warn(content);
201 }
202 }
203
204 protected void error(CharSequence content, Throwable error) {
205 final Log log = getLog();
206 if (log != null) {
207 log.debug(content, error);
208 log.error(content);
209 }
210 }
211
212 protected void error(CharSequence content) {
213 final Log log = getLog();
214 if (log != null) {
215 log.error(content);
216 }
217 }
218 }