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