View Javadoc
1   package org.apache.maven.shared.jar.identification.exposers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.shared.jar.JarAnalyzer;
24  import org.apache.maven.shared.jar.identification.JarIdentification;
25  import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
26  import org.apache.maven.shared.jar.identification.hash.JarHashAnalyzer;
27  import org.apache.maven.shared.jar.identification.repository.RepositoryHashSearch;
28  import org.codehaus.plexus.logging.AbstractLogEnabled;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * Exposer that examines a Maven repository for identical files to the JAR being analyzed. It will look for both
36   * identical files, and files with identical classes.
37   * <p/>
38   * Note: if not using Plexus, you must call the following methods to be able to expose any data from the class:
39   * {@link #setBytecodeHashAnalyzer(org.apache.maven.shared.jar.identification.hash.JarHashAnalyzer)},
40   * {@link #setFileHashAnalyzer(org.apache.maven.shared.jar.identification.hash.JarHashAnalyzer)},
41   * {@link #setRepositoryHashSearch(org.apache.maven.shared.jar.identification.repository.RepositoryHashSearch)}
42   *
43   * @plexus.component role="org.apache.maven.shared.jar.identification.JarIdentificationExposer"
44   *                   role-hint="repositorySearch"
45   */
46  public class RepositorySearchExposer
47      extends AbstractLogEnabled
48      implements JarIdentificationExposer
49  {
50      /**
51       * The repository searcher to use.
52       *
53       * @plexus.requirement
54       * @todo this currently only provides for the 'empty' repository search, which isn't very useful
55       */
56      private RepositoryHashSearch repositoryHashSearch;
57  
58      /**
59       * The hash analyzer for the entire file.
60       *
61       * @plexus.requirement role-hint="file"
62       */
63      private JarHashAnalyzer fileHashAnalyzer;
64  
65      /**
66       * The hash analyzer for the file's bytecode.
67       *
68       * @plexus.requirement role-hint="bytecode"
69       */
70      private JarHashAnalyzer bytecodeHashAnalyzer;
71  
72      public void expose( JarIdentification identification, JarAnalyzer jarAnalyzer )
73      {
74          List repohits = new ArrayList();
75  
76          String hash = fileHashAnalyzer.computeHash( jarAnalyzer );
77          if ( hash != null )
78          {
79              repohits.addAll( repositoryHashSearch.searchFileHash( hash ) );
80          }
81  
82          String bytecodehash = bytecodeHashAnalyzer.computeHash( jarAnalyzer );
83          if ( bytecodehash != null )
84          {
85              repohits.addAll( repositoryHashSearch.searchBytecodeHash( bytecodehash ) );
86          }
87  
88          if ( !repohits.isEmpty() )
89          {
90              // Found hits in the repository.
91              Iterator it = repohits.iterator();
92              while ( it.hasNext() )
93              {
94                  Artifact artifact = (Artifact) it.next();
95                  identification.addAndSetGroupId( artifact.getGroupId() );
96                  identification.addAndSetArtifactId( artifact.getArtifactId() );
97                  identification.addAndSetVersion( artifact.getVersion() );
98              }
99          }
100     }
101 
102     public void setRepositoryHashSearch( RepositoryHashSearch repo )
103     {
104         this.repositoryHashSearch = repo;
105     }
106 
107     public void setFileHashAnalyzer( JarHashAnalyzer fileHashAnalyzer )
108     {
109         this.fileHashAnalyzer = fileHashAnalyzer;
110     }
111 
112     public void setBytecodeHashAnalyzer( JarHashAnalyzer bytecodeHashAnalyzer )
113     {
114         this.bytecodeHashAnalyzer = bytecodeHashAnalyzer;
115     }
116 }