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 24 package jdk.test.lib.artifacts; 25 26 import java.io.FileNotFoundException; 27 import java.lang.reflect.Method; 28 import java.nio.file.Path; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 public class JibArtifactManager implements ArtifactManager { 33 private static final String JIB_SERVICE_FACTORY = "com.oracle.jib.api.JibServiceFactory"; 34 private static String jibVersion = "1.0"; 35 private Object installerObject; 36 37 private JibArtifactManager(Object o) { 38 installerObject = o; 39 } 40 41 public static JibArtifactManager newInstance() throws ClassNotFoundException { 42 try { 43 Class jibServiceFactory = Class.forName(JIB_SERVICE_FACTORY); 44 Object jibArtifactInstaller = jibServiceFactory.getMethod("createJibArtifactInstaller").invoke(null); 45 return new JibArtifactManager(jibArtifactInstaller); 46 } catch (Exception e) { 47 throw new ClassNotFoundException("Could not resolve " + JIB_SERVICE_FACTORY, e); 48 } 49 } 50 51 private Path download(String jibVersion, HashMap<String, Object> artifactDescription) throws Exception { 52 return invokeInstallerMethod("download", jibVersion, artifactDescription); 53 } 54 55 private Path install(String jibVersion, HashMap<String, Object> artifactDescription) throws Exception { 56 return invokeInstallerMethod("install", jibVersion, artifactDescription); 57 } 58 59 private Path invokeInstallerMethod(String methodName, String jibVersion, HashMap<String, Object> artifactDescription) throws Exception { 60 Method m = Class.forName("com.oracle.jib.api.JibArtifactInstaller").getMethod(methodName, String.class, Map.class); 61 return (Path)m.invoke(installerObject, jibVersion, artifactDescription); 62 } 63 64 @Override 65 public Path resolve(Artifact artifact) throws ArtifactResolverException { 66 Path path; 67 // Use the DefaultArtifactManager to enable users to override locations 68 try { 69 ArtifactManager manager = new DefaultArtifactManager(); 70 path = manager.resolve(artifact); 71 } catch (ArtifactResolverException e) { 72 // Location hasn't been overridden, continue to automatically try to resolve the dependency 73 try { 74 HashMap<String, Object> artifactDescription = new HashMap<>(); 75 artifactDescription.put("module", artifact.name()); 76 artifactDescription.put("organization", artifact.organization()); 77 artifactDescription.put("ext", artifact.extension()); 78 artifactDescription.put("revision", artifact.revision()); 79 if (artifact.classifier().length() > 0) { 80 artifactDescription.put("classifier", artifact.classifier()); 81 } 82 83 path = download(jibVersion, artifactDescription); 84 if (artifact.unpack()) { 85 path = install(jibVersion, artifactDescription); 86 } 87 } catch (Exception e2) { 88 throw new ArtifactResolverException("Failed to resolve the artifact " + artifact, e2); 89 } 90 } 91 return path; 92 } 93 }