ArtifactResolver.java revision 2716:d48c70fef706
1262569Simp/*
2262569Simp * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3262569Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4262569Simp *
5262569Simp * This code is free software; you can redistribute it and/or modify it
6262569Simp * under the terms of the GNU General Public License version 2 only, as
7262569Simp * published by the Free Software Foundation.
8262569Simp *
9262569Simp * This code is distributed in the hope that it will be useful, but WITHOUT
10262569Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11262569Simp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12262569Simp * version 2 for more details (a copy is included in the LICENSE file that
13262569Simp * accompanied this code).
14262569Simp *
15262569Simp * You should have received a copy of the GNU General Public License version
16262569Simp * 2 along with this work; if not, write to the Free Software Foundation,
17262569Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18262569Simp *
19262569Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20262569Simp * or visit www.oracle.com if you need additional information or have any
21262569Simp * questions.
22262569Simp */
23262569Simp
24262569Simppackage jdk.test.lib.artifacts;
25262569Simp
26262569Simpimport java.io.FileNotFoundException;
27262569Simpimport java.nio.file.Path;
28262569Simpimport java.util.HashMap;
29262569Simp
30262569Simppublic class ArtifactResolver {
31262569Simp    public static HashMap<String, Path> resolve(Class klass) throws FileNotFoundException {
32262569Simp        ArtifactManager manager = new DefaultArtifactManager();
33262569Simp        try {
34262569Simp            String managerName = System.getProperty("jdk.test.lib.artifacts.artifactmanager");
35262569Simp            if (managerName != null) {
36262569Simp                manager = (ArtifactManager) Class.forName(managerName).newInstance();
37262569Simp            } else {
38262569Simp                manager = JibArtifactManager.newInstance();
39262569Simp            }
40262569Simp        } catch (Exception e) {
41262569Simp            // If we end up here, we'll use the DefaultArtifactManager
42262569Simp        }
43284090Sian
44262569Simp        ArtifactContainer artifactContainer = (ArtifactContainer) klass.getAnnotation(ArtifactContainer.class);
45262569Simp        HashMap<String, Path> locations = new HashMap<>();
46262569Simp        Artifact[] artifacts;
47262569Simp
48262569Simp        if (artifactContainer == null) {
49262569Simp            artifacts = new Artifact[]{(Artifact) klass.getAnnotation(Artifact.class)};
50262569Simp        } else {
51262569Simp            artifacts = artifactContainer.value();
52262569Simp        }
53262569Simp        for (Artifact artifact : artifacts) {
54262569Simp            locations.put(artifactName(artifact), manager.resolve(artifact));
55262569Simp        }
56262569Simp
57262569Simp        return locations;
58262569Simp    }
59262569Simp
60262569Simp    private static String artifactName(Artifact artifact) {
61262569Simp        // Format of the artifact name is <organization>.<name>-<revision>(-<classifier>)
62262569Simp        String name = String.format("%s.%s-%s", artifact.organization(), artifact.name(), artifact.revision());
63262569Simp        if (artifact.classifier().length() != 0) {
64262569Simp            name = name +"-" + artifact.classifier();
65262569Simp        }
66262569Simp        return name;
67262569Simp    }
68262569Simp}
69262569Simp