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  import org.apache.commons.lang.StringUtils;
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.apache.maven.plugin.MojoFailureException;
21  import org.apache.maven.plugins.annotations.LifecyclePhase;
22  import org.apache.maven.plugins.annotations.Mojo;
23  import org.apache.maven.plugins.annotations.ResolutionScope;
24  import org.apache.maven.project.MavenProject;
25  import org.whitesource.agent.api.dispatch.CheckPoliciesResult;
26  import org.whitesource.agent.api.model.AgentProjectInfo;
27  import org.whitesource.agent.client.WssServiceException;
28  
29  import java.util.Collection;
30  import java.util.Map;
31  
32  /**
33   * Send check policies request of open source software usage information to White Source.
34   *
35   * <p>
36   *     Further documentation for the plugin and its usage can be found in the
37   *     <a href="http://docs.whitesourcesoftware.com/display/serviceDocs/Maven+plugin">online documentation</a>.
38   * </p>
39   *
40   * @author tom.shapira
41   */
42  @Mojo(name = "checkPolicies",
43          defaultPhase = LifecyclePhase.PACKAGE,
44          requiresDependencyResolution = ResolutionScope.TEST,
45          aggregator = true )
46  public class CheckPoliciesMojo extends AgentMojo {
47  
48      /* --- Constructors --- */
49  
50      public CheckPoliciesMojo() {
51      }
52  
53      /* --- Concrete implementation methods --- */
54  
55      @Override
56      public void doExecute() throws MojoExecutionException, MojoFailureException {
57          if (reactorProjects == null) {
58              info("No projects found. Skipping update");
59              return;
60          }
61  
62          // initialize
63          init();
64  
65          // Collect OSS usage information
66          Collection<AgentProjectInfo> projectInfos = extractProjectInfos();
67  
68          // send to white source
69          if (projectInfos == null || projectInfos.isEmpty()) {
70              info("No open source information found.");
71          } else {
72              sendCheckPolicies(projectInfos);
73          }
74      }
75  
76      /* --- Private methods --- */
77  
78      private void init() {
79          // copy token for modules with special names into moduleTokens.
80          for (Map.Entry<Object, Object> entry : specialModuleTokens.entrySet()) {
81              moduleTokens.put(entry.getKey().toString(), entry.getValue().toString());
82          }
83  
84          // take product name and version from top level project
85          MavenProject topLevelProject = session.getTopLevelProject();
86          if (topLevelProject != null) {
87              if (StringUtils.isBlank(product)) {
88                  product = topLevelProject.getName();
89              }
90              if (StringUtils.isBlank(product)) {
91                  product = topLevelProject.getArtifactId();
92              }
93          }
94      }
95  
96      private void sendCheckPolicies(Collection<AgentProjectInfo> projectInfos) throws MojoFailureException, MojoExecutionException {
97          try {
98              info("Checking policies...");
99              CheckPoliciesResult result = service.checkPolicies(orgToken, product, productVersion, projectInfos);
100 
101             if (outputDirectory == null ||
102                     (!outputDirectory.exists() && !outputDirectory.mkdirs())) {
103                 warn("Output directory doesn't exist. Skipping policies check report.");
104             } else {
105                 generateReport(result);
106             }
107 
108             if (result.hasRejections()) {
109                 String msg = "Some dependencies were rejected by the organization's policies.";
110                 throw new MojoExecutionException(msg); // this is handled in base class
111             } else {
112                 info("All dependencies conform with the organization's policies.");
113             }
114         } catch (WssServiceException e) {
115             throw new MojoExecutionException(Constants.ERROR_SERVICE_CONNECTION + e.getMessage(), e);
116         }
117     }
118 
119 }