001/**
002 * Copyright (C) 2006-2024 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(
046                new ByteArrayInputStream(String.join("\n", gavs).getBytes(StandardCharsets.UTF_8)));
047    }
048
049    /**
050     * Resolves the dependencies from the descriptor passed as an InputStream.
051     *
052     * IMPORTANT: this is to use when you are sure the file is resolvable if you don't have a fallback.
053     * In that last case, prefer the <code>mapDescriptorToClassLoader</code>.
054     *
055     * @param descriptor the dependencies.txt to use to resolve dependencies.
056     * @return the collection of file representing the available dependencies.
057     */
058    Collection<File> resolveFromDescriptor(InputStream descriptor);
059
060    /**
061     * Alias to load dependencies from a plain list of gav (groupId:artifactId:version).
062     *
063     * @param gavs the dependencies to use to resolve dependencies.
064     * @return the collection of file representing the available dependencies.
065     */
066    default Collection<File> resolveFromDescriptor(final List<String> gavs) {
067        return resolveFromDescriptor(
068                new ByteArrayInputStream(String.join("\n", gavs).getBytes(StandardCharsets.UTF_8)));
069    }
070
071    /**
072     * Abstract a classloader adding the metadata about the resolution done to create it.
073     */
074    interface ClassLoaderDescriptor extends AutoCloseable {
075
076        /**
077         * @return the underlying classloader.
078         */
079        ClassLoader asClassLoader();
080
081        /**
082         * @return the dependencies who matched the resolution and were used to create the classloader.
083         */
084        Collection<String> resolvedDependencies();
085    }
086}