001/** 002 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.component.api.service.dependency; 017 018import java.io.ByteArrayInputStream; 019import java.io.File; 020import java.io.InputStream; 021import java.nio.charset.StandardCharsets; 022import java.util.Collection; 023import java.util.List; 024 025public interface Resolver { 026 027 /** 028 * Creates a classloader from the passed descriptor (dependencies.txt). 029 * 030 * WARNING: note it is very important to close the descriptor once no more used otherwise 031 * you can leak memory. 032 * 033 * @param descriptor the dependencies.txt InputStream. 034 * @return the classloader initialized with the resolved dependencies. 035 */ 036 ClassLoaderDescriptor mapDescriptorToClassLoader(InputStream descriptor); 037 038 /** 039 * Alias to load dependencies from a plain list of gav (groupId:artifactId:version). 040 * 041 * @param gavs the dependencies to use to resolve dependencies. 042 * @return the collection of file representing the available dependencies. 043 */ 044 default ClassLoaderDescriptor mapDescriptorToClassLoader(final List<String> gavs) { 045 return mapDescriptorToClassLoader(new ByteArrayInputStream(String.join("\n", gavs).getBytes(StandardCharsets.UTF_8))); 046 } 047 048 /** 049 * Resolves the dependencies from the descriptor passed as an InputStream. 050 * 051 * IMPORTANT: this is to use when you are sure the file is resolvable if you don't have a fallback. 052 * In that last case, prefer the <code>mapDescriptorToClassLoader</code>. 053 * 054 * @param descriptor the dependencies.txt to use to resolve dependencies. 055 * @return the collection of file representing the available dependencies. 056 */ 057 Collection<File> resolveFromDescriptor(InputStream descriptor); 058 059 /** 060 * Alias to load dependencies from a plain list of gav (groupId:artifactId:version). 061 * 062 * @param gavs the dependencies to use to resolve dependencies. 063 * @return the collection of file representing the available dependencies. 064 */ 065 default Collection<File> resolveFromDescriptor(final List<String> gavs) { 066 return resolveFromDescriptor(new ByteArrayInputStream(String.join("\n", gavs).getBytes(StandardCharsets.UTF_8))); 067 } 068 069 /** 070 * Abstract a classloader adding the metadata about the resolution done to create it. 071 */ 072 interface ClassLoaderDescriptor extends AutoCloseable { 073 074 /** 075 * @return the underlying classloader. 076 */ 077 ClassLoader asClassLoader(); 078 079 /** 080 * @return the dependencies who matched the resolution and were used to create the classloader. 081 */ 082 Collection<String> resolvedDependencies(); 083 } 084}