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" role-hint="repositorySearch"
44 */
45 public class RepositorySearchExposer
46 extends AbstractLogEnabled
47 implements JarIdentificationExposer
48 {
49 /**
50 * The repository searcher to use.
51 *
52 * @plexus.requirement
53 * @todo this currently only provides for the 'empty' repository search, which isn't very useful
54 */
55 private RepositoryHashSearch repositoryHashSearch;
56
57 /**
58 * The hash analyzer for the entire file.
59 *
60 * @plexus.requirement role-hint="file"
61 */
62 private JarHashAnalyzer fileHashAnalyzer;
63
64 /**
65 * The hash analyzer for the file's bytecode.
66 *
67 * @plexus.requirement role-hint="bytecode"
68 */
69 private JarHashAnalyzer bytecodeHashAnalyzer;
70
71 public void expose( JarIdentification identification, JarAnalyzer jarAnalyzer )
72 {
73 List repohits = new ArrayList();
74
75 String hash = fileHashAnalyzer.computeHash( jarAnalyzer );
76 if ( hash != null )
77 {
78 repohits.addAll( repositoryHashSearch.searchFileHash( hash ) );
79 }
80
81 String bytecodehash = bytecodeHashAnalyzer.computeHash( jarAnalyzer );
82 if ( bytecodehash != null )
83 {
84 repohits.addAll( repositoryHashSearch.searchBytecodeHash( bytecodehash ) );
85 }
86
87 if ( !repohits.isEmpty() )
88 {
89 // Found hits in the repository.
90 Iterator it = repohits.iterator();
91 while ( it.hasNext() )
92 {
93 Artifact artifact = (Artifact) it.next();
94 identification.addAndSetGroupId( artifact.getGroupId() );
95 identification.addAndSetArtifactId( artifact.getArtifactId() );
96 identification.addAndSetVersion( artifact.getVersion() );
97 }
98 }
99 }
100
101 public void setRepositoryHashSearch( RepositoryHashSearch repo )
102 {
103 this.repositoryHashSearch = repo;
104 }
105
106 public void setFileHashAnalyzer( JarHashAnalyzer fileHashAnalyzer )
107 {
108 this.fileHashAnalyzer = fileHashAnalyzer;
109 }
110
111 public void setBytecodeHashAnalyzer( JarHashAnalyzer bytecodeHashAnalyzer )
112 {
113 this.bytecodeHashAnalyzer = bytecodeHashAnalyzer;
114 }
115 }