org.commonjava.maven.firth.model.index.ArtifactMap.java Source code

Java tutorial

Introduction

Here is the source code for org.commonjava.maven.firth.model.index.ArtifactMap.java

Source

/*******************************************************************************
 * Copyright (c) 2014 Red Hat, Inc..
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 *   
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 *******************************************************************************/
package org.commonjava.maven.firth.model.index;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.text.StrSubstitutor;
import org.commonjava.maven.firth.model.dto.BuildManifest;
import org.commonjava.maven.firth.model.dto.SourceManifest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArtifactMap {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final Map<String, String> basePaths;

    private final String packagePathFormat;

    public ArtifactMap(final String packagePathFormat, final List<SourceManifest> manifests) {
        this.packagePathFormat = packagePathFormat;
        basePaths = new HashMap<>();
        for (final SourceManifest manifest : manifests) {
            mapManifestArtifacts(manifest);
        }
    }

    private void mapManifestArtifacts(final SourceManifest manifest) {
        for (final BuildManifest bmanifest : manifest.getBuilds().values()) {
            for (final String file : bmanifest.getFiles()) {
                final String existingPath = basePaths.get(file);
                if (existingPath != null) {
                    logger.warn("Skipping: {} in {}. It's already mapped to: {}", file, manifest.getSource(),
                            existingPath);
                    continue;
                }

                // Sample package-path: "/brewroot/packages/org.commonjava.maven.ext-pom-manipulation-ext-0.5.2/1/maven"
                // Or, the format: "/brewroot/packages/%(groupId)-%(artifactId)-%(versionNoDashes)/%(build)/maven"
                basePaths.put(file, StrSubstitutor.replace(packagePathFormat, bmanifest.getPathVariables()));
            }
        }
    }

    public String buildArtifactUrl(final String baseUrl, final String artifactPath) {
        final String basePath = basePaths.get(artifactPath);
        if (basePath == null) {
            return null;
        }

        return Paths.get(baseUrl, basePath, artifactPath).toString();
    }

}