View Javadoc

1   /**
2    * Copyright (C) 2011 White Source Ltd.
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 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  //import org.eclipse.aether.RepositorySystemSession;
36  //import org.eclipse.aether.repository.Authentication;
37  //import org.eclipse.aether.repository.AuthenticationContext;
38  //import org.eclipse.aether.repository.Proxy;
39  //import org.eclipse.aether.repository.RemoteRepository;
40  
41  /**
42   * Concrete implementation holding common functionality to all goals in this plugin.
43   *
44   * @author Edo.Shor
45   */
46  public abstract class WhitesourceMojo extends AbstractMojo {
47  
48      /* --- Members --- */
49  
50      /**
51       * Indicates whether the build will continue even if there are errors.
52       */
53      @Parameter(defaultValue = "false")
54      protected boolean failOnError;
55  
56      /**
57       * Set this to 'true' to skip the maven execution.
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       * The project dependency resolver to use.
70       */
71      @Component( hint = "default" )
72      protected ProjectDependenciesResolver projectDependenciesResolver;
73  
74      /**
75       * The current repository/network configuration of Maven.
76       */
77      @Parameter( defaultValue = "${repositorySystemSession}", readonly = true, required = true )
78      protected RepositorySystemSession repoSession;
79  
80      protected WhitesourceService service;
81  
82      /* --- Abstract methods --- */
83  
84      public abstract void doExecute() throws MojoExecutionException, MojoFailureException;
85  
86      /* --- Concrete implementation methods --- */
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     /* --- Protected methods --- */
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         // get proxy configuration from session
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 //        //TODO: uncomment the code below and replace with the above when we need to support maven 3.1.1 (which migrated from Sonatype Aether to Eclipse Aether)
134 //        RemoteRepository.Builder remoteRepositoryBuilder = new RemoteRepository.Builder(null, null, serviceUrl);
135 //        RemoteRepository dummyRepo = remoteRepositoryBuilder.build();
136 //        RepositorySystemSession repositorySystemSession = session.getRepositorySession();
137 //        final Proxy proxy = repositorySystemSession.getProxySelector().getProxy(dummyRepo);
138 //        if (proxy != null) {
139 //            String username = null;
140 //            String password = null;
141 //            final Authentication auth = proxy.getAuthentication();
142 //            if (auth != null) {
143 //                dummyRepo = remoteRepositoryBuilder.setAuthentication(auth).build();
144 //                AuthenticationContext authenticationContext = AuthenticationContext.forRepository( repositorySystemSession, dummyRepo );
145 //                try {
146 //                    auth.fill(authenticationContext, null, null);
147 //                    username = authenticationContext.get(AuthenticationContext.USERNAME, String.class);
148 //                    password = authenticationContext.get(AuthenticationContext.PASSWORD, String.class);
149 //                }
150 //                finally {
151 //                    AuthenticationContext.close(authenticationContext);
152 //                }
153 //            }
154 //            service.getClient().setProxy(proxy.getHost(), proxy.getPort(), username, password);
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 }