1 package org.apache.maven.shared.jar.identification;
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.shared.jar.JarAnalyzer;
23 import org.codehaus.plexus.util.StringUtils;
24
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28
29 /**
30 * Analyze the JAR file to identify Maven artifact metadata. This class is thread safe and immutable as long as all
31 * provided exposers are, as it retains no state.
32 * <p/>
33 * If using Plexus, the class will use all available exposers in the container.
34 * <p/>
35 * If not using Plexus, the exposers must be set using {@link #setExposers(java.util.List)} before calling
36 * {@link #analyze(org.apache.maven.shared.jar.JarAnalyzer)}
37 * <p/>
38 * Note that you must first create an instance of {@link org.apache.maven.shared.jar.JarAnalyzer} - see its Javadoc for
39 * a typical use.
40 *
41 * @plexus.component role="org.apache.maven.shared.jar.identification.JarIdentificationAnalysis" role-hint="default"
42 */
43 public class JarIdentificationAnalysis
44 {
45 /**
46 * The Maven information exposers to use during identification.
47 *
48 * @plexus.requirement role="org.apache.maven.shared.jar.identification.JarIdentificationExposer"
49 */
50 private List exposers;
51
52 /**
53 * Analyze a JAR and find any associated Maven metadata. Note that if the provided JAR analyzer has previously
54 * analyzed the JAR, the cached results will be returned. You must obtain a new JAR analyzer to the re-read the
55 * contents of the file.
56 *
57 * @param jarAnalyzer the JAR to analyze. This must not yet have been closed.
58 * @return the Maven metadata discovered
59 */
60 public JarIdentification analyze( JarAnalyzer jarAnalyzer )
61 {
62 JarIdentification taxon = jarAnalyzer.getJarData().getJarIdentification();
63 if ( taxon != null )
64 {
65 return taxon;
66 }
67
68 taxon = new JarIdentification();
69
70 for ( Iterator i = exposers.iterator(); i.hasNext(); )
71 {
72 JarIdentificationExposer exposer = (JarIdentificationExposer) i.next();
73 exposer.expose( taxon, jarAnalyzer );
74 }
75
76 normalize( taxon );
77
78 jarAnalyzer.getJarData().setJarIdentification( taxon );
79
80 return taxon;
81 }
82
83 private void normalize( JarIdentification taxon )
84 {
85 if ( StringUtils.isEmpty( taxon.getGroupId() ) )
86 {
87 taxon.setGroupId( pickSmallest( taxon.getPotentialGroupIds() ) );
88 }
89
90 if ( StringUtils.isEmpty( taxon.getArtifactId() ) )
91 {
92 taxon.setArtifactId( pickLargest( taxon.getPotentialArtifactIds() ) );
93 }
94
95 if ( StringUtils.isEmpty( taxon.getVersion() ) )
96 {
97 taxon.setVersion( pickSmallest( taxon.getPotentialVersions() ) );
98 }
99
100 if ( StringUtils.isEmpty( taxon.getName() ) )
101 {
102 taxon.setName( pickLargest( taxon.getPotentialNames() ) );
103 }
104
105 if ( StringUtils.isEmpty( taxon.getVendor() ) )
106 {
107 taxon.setVendor( pickLargest( taxon.getPotentialVendors() ) );
108 }
109 }
110
111 private String pickSmallest( List list )
112 {
113 String smallest = null;
114
115 if ( !list.isEmpty() )
116 {
117 int size = Integer.MAX_VALUE;
118 Iterator it = list.iterator();
119 while ( it.hasNext() )
120 {
121 String val = (String) it.next();
122
123 if ( StringUtils.isNotEmpty( val ) )
124 {
125 if ( val.length() < size )
126 {
127 smallest = val;
128 size = val.length();
129 }
130 }
131 }
132 }
133
134 return smallest;
135 }
136
137 private String pickLargest( List list )
138 {
139 String largest = null;
140 if ( !list.isEmpty() )
141 {
142 int size = Integer.MIN_VALUE;
143 Iterator it = list.iterator();
144 while ( it.hasNext() )
145 {
146 String val = (String) it.next();
147 if ( StringUtils.isNotEmpty( val ) )
148 {
149 if ( val.length() > size )
150 {
151 largest = val;
152 size = val.length();
153 }
154 }
155 }
156 }
157 return largest;
158 }
159
160 public void setExposers( List exposers )
161 {
162 this.exposers = Collections.unmodifiableList( exposers );
163 }
164 }