com.marvelution.maven.components.jar.identification.exposer.EnhancedJarClassesExposer Maven / Gradle / Ivy
/*
* Licensed to Marvelution under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Marvelution licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.marvelution.maven.components.jar.identification.exposer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.shared.jar.JarAnalyzer;
import org.apache.maven.shared.jar.classes.JarClasses;
import org.apache.maven.shared.jar.classes.JarClassesAnalysis;
import org.apache.maven.shared.jar.identification.JarIdentification;
import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
import org.codehaus.plexus.logging.AbstractLogEnabled;
/**
* Enhanced Jar Classes Exposer for {@link JarIdentificationAnalysis}
*
* Note: If not using Plexus, the following must be set before executing
* {@link #expose(JarIdentification, JarAnalyzer)}:
*
* - the {@link Logger} implementation using method {@link #enableLogging(org.codehaus.plexus.logging.Logger)}
* - the {@link JarClassesAnalysis} implementation using method {@link #setClassesAnalysis(JarClassesAnalysis)}
*
*
*
* @author Mark Rekveld
*/
public class EnhancedJarClassesExposer extends AbstractLogEnabled implements JarIdentificationExposer {
/**
* {@link JarClassesAnalysis} component to get Class and Package information from a Jar File
*/
private JarClassesAnalysis classesAnalyzer;
/**
* Sets the {@link JarClassesAnalysis} component to use
*
* @param classesAnalysis the {@link JarClassesAnalysis} component
*/
public void setClassesAnalysis(JarClassesAnalysis classesAnalysis) {
this.classesAnalyzer = classesAnalysis;
}
/**
* {@inheritDoc}
*/
public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) {
final JarClasses jarclasses = classesAnalyzer.analyze(jarAnalyzer);
final List packageList = new ArrayList(jarclasses.getPackages());
Collections.copy(packageList, jarclasses.getPackages());
Collections.sort(packageList, Collections.reverseOrder());
String smallestCommonPackage = (String) packageList.get(0);
while (!isSmallestCommonPackage(packageList.iterator(), smallestCommonPackage)) {
if (smallestCommonPackage.indexOf('.') > 0) {
smallestCommonPackage = smallestCommonPackage.substring(0, smallestCommonPackage.lastIndexOf('.'));
} else {
smallestCommonPackage = null;
break;
}
}
if (smallestCommonPackage != null && !((String) packageList.get(0)).equals(smallestCommonPackage)) {
identification.addGroupId(smallestCommonPackage);
}
}
/**
* Checks if the
*
* @param packageIterator the {@link Iterator} of a list of packages
* @param commonPackage a possible common package
* @return true
if the given commonPackage is shared by all packages in the given iterator,
* false
otherwise
*/
private boolean isSmallestCommonPackage(Iterator packageIterator, String commonPackage) {
boolean ret = false;
while (packageIterator.hasNext()) {
final String packageName = (String) packageIterator.next();
if (packageName.startsWith(commonPackage)) {
ret = true;
} else {
ret = false;
}
}
return ret;
}
}