001    package org.kuali.maven.plugins.graph.sanitize;
002    
003    import java.util.Map;
004    
005    import org.apache.maven.artifact.Artifact;
006    import org.kuali.maven.plugins.graph.pojo.MavenContext;
007    import org.kuali.maven.plugins.graph.pojo.State;
008    import org.kuali.maven.plugins.graph.tree.TreeHelper;
009    import org.slf4j.Logger;
010    import org.slf4j.LoggerFactory;
011    
012    public class DuplicateSanitizer extends MavenContextSanitizer {
013        private static final Logger logger = LoggerFactory.getLogger(DuplicateSanitizer.class);
014    
015        public DuplicateSanitizer() {
016            this(null);
017        }
018    
019        public DuplicateSanitizer(Map<String, MavenContext> included) {
020            super(included, State.DUPLICATE);
021        }
022    
023        @Override
024        protected void sanitize(MavenContext context, Map<String, MavenContext> included) {
025            String artifactId = TreeHelper.getArtifactId(context.getDependencyNode().getArtifact());
026            MavenContext replacement = included.get(artifactId);
027    
028            // Nothing more to do
029            if (replacement != null) {
030                logger.debug(artifactId + " meets duplicate node criteria");
031                return;
032            }
033    
034            Artifact related = context.getDependencyNode().getRelatedArtifact();
035            if (related == null) {
036                // This is not ok. There was no exact match and related is null.
037                warnAndSwitch(State.UNKNOWN, artifactId, context, "No related artifact");
038                return;
039            }
040    
041            String relatedArtifactId = TreeHelper.getArtifactId(related);
042            replacement = included.get(relatedArtifactId);
043            if (replacement != null) {
044                /**
045                 * This is ok. Kind of. Maven has marked this as a duplicate because it's a duplicate of a dependency above
046                 * us in the tree. In reality, it is going to get switched out for another artifact because the node above
047                 * us has its dependency on this artifact marked as "conflict". The "duplicate" label is a little bit
048                 * counter intuitive but the overall build tree is in a consistent state.
049                 */
050                // Emit an "info" level log message and switch to CONFLICT
051                State switchTo = State.CONFLICT;
052                logger.info(getSwitchMessage(artifactId, switchTo));
053                logger.info("No identical replacement for a 'duplicate' but the related artifact was found");
054                context.setState(switchTo);
055                return;
056            } else {
057                // This is not ok. A node marked as duplicate has no replacement artifact included in the build
058                warnAndSwitch(State.UNKNOWN, artifactId, context, "No identical replacement. Related artifact not found");
059                return;
060            }
061        }
062    
063    }