View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.jar.identification.exposers;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.shared.jar.JarAnalyzer;
30  import org.apache.maven.shared.jar.identification.JarIdentification;
31  import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
32  import org.apache.maven.shared.jar.identification.hash.JarHashAnalyzer;
33  import org.apache.maven.shared.jar.identification.repository.RepositoryHashSearch;
34  
35  import static java.util.Objects.requireNonNull;
36  
37  /**
38   * Exposer that examines a Maven repository for identical files to the JAR being analyzed. It will look for both
39   * identical files, and files with identical classes.
40   */
41  @Singleton
42  @Named("repositorySearch")
43  public class RepositorySearchExposer implements JarIdentificationExposer {
44      /**
45       * The repository searcher to use.
46       *
47       * @todo this currently only provides for the 'empty' repository search, which isn't very useful
48       */
49      private final RepositoryHashSearch repositoryHashSearch;
50  
51      /**
52       * The hash analyzer for the entire file.
53       */
54      private final JarHashAnalyzer fileHashAnalyzer;
55  
56      /**
57       * The hash analyzer for the file's bytecode.
58       */
59      private final JarHashAnalyzer bytecodeHashAnalyzer;
60  
61      @Inject
62      public RepositorySearchExposer(
63              RepositoryHashSearch repositoryHashSearch,
64              @Named("file") JarHashAnalyzer fileHashAnalyzer,
65              @Named("bytecode") JarHashAnalyzer bytecodeHashAnalyzer) {
66          this.repositoryHashSearch = requireNonNull(repositoryHashSearch);
67          this.fileHashAnalyzer = requireNonNull(fileHashAnalyzer);
68          this.bytecodeHashAnalyzer = requireNonNull(bytecodeHashAnalyzer);
69      }
70  
71      @Override
72      public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) {
73          List<Artifact> repohits = new ArrayList<>();
74  
75          String hash = fileHashAnalyzer.computeHash(jarAnalyzer);
76          if (hash != null) {
77              repohits.addAll(repositoryHashSearch.searchFileHash(hash));
78          }
79  
80          String bytecodehash = bytecodeHashAnalyzer.computeHash(jarAnalyzer);
81          if (bytecodehash != null) {
82              repohits.addAll(repositoryHashSearch.searchBytecodeHash(bytecodehash));
83          }
84  
85          // Found hits in the repository.
86          for (Artifact artifact : repohits) {
87              identification.addAndSetGroupId(artifact.getGroupId());
88              identification.addAndSetArtifactId(artifact.getArtifactId());
89              identification.addAndSetVersion(artifact.getVersion());
90          }
91      }
92  }