JibArtifactManager.java revision 2716:d48c70fef706
1/*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package jdk.test.lib.artifacts;
25
26import java.io.FileNotFoundException;
27import java.lang.reflect.Method;
28import java.nio.file.Path;
29import java.util.HashMap;
30import java.util.Map;
31
32public class JibArtifactManager implements ArtifactManager {
33    private static String jibVersion = "1.0";
34    private Object installerObject;
35
36    private JibArtifactManager(Object o) {
37        installerObject = o;
38    }
39
40    public static JibArtifactManager newInstance() throws ClassNotFoundException {
41        try {
42            Class jibServiceFactory = Class.forName("com.oracle.jib.api.JibServiceFactory");
43            Object jibArtifactInstaller = jibServiceFactory.getMethod("createJibArtifactInstaller").invoke(null);
44            return new JibArtifactManager(jibArtifactInstaller);
45        } catch (Exception e) {
46            throw new ClassNotFoundException();
47        }
48    }
49
50    private Path download(String jibVersion, HashMap<String, Object> artifactDescription) throws Exception {
51        return invokeInstallerMethod("download", jibVersion, artifactDescription);
52    }
53
54    private Path install(String jibVersion, HashMap<String, Object> artifactDescription) throws Exception {
55        return invokeInstallerMethod("install", jibVersion, artifactDescription);
56    }
57
58    private Path invokeInstallerMethod(String methodName, String jibVersion, HashMap<String, Object> artifactDescription) throws Exception {
59        Method m = Class.forName("com.oracle.jib.api.JibArtifactInstaller").getMethod(methodName, String.class, Map.class);
60        return (Path)m.invoke(installerObject, jibVersion, artifactDescription);
61    }
62
63    @Override
64    public Path resolve(Artifact artifact) throws FileNotFoundException {
65        Path path;
66        // Use the DefaultArtifactManager to enable users to override locations
67        try {
68            ArtifactManager manager = new DefaultArtifactManager();
69            path = manager.resolve(artifact);
70        } catch (FileNotFoundException e) {
71            // Location hasn't been overridden, continue to automatically try to resolve the dependency
72            try {
73                HashMap<String, Object> artifactDescription = new HashMap<>();
74                artifactDescription.put("module", artifact.name());
75                artifactDescription.put("organization", artifact.organization());
76                artifactDescription.put("ext", artifact.extension());
77                artifactDescription.put("revision", artifact.revision());
78                if (artifact.classifier().length() > 0) {
79                    artifactDescription.put("classifier", artifact.classifier());
80                }
81
82                path = download(jibVersion, artifactDescription);
83                if (artifact.unpack()) {
84                    path = install(jibVersion, artifactDescription);
85                }
86            } catch (Exception exception) {
87                throw new FileNotFoundException("Failed to resolve the artifact " + artifact);
88            }
89        }
90        return path;
91   }
92}
93