001/**
002 * Copyright (C) 2006-2026 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     * Creates a classloader from the passed classloader configuration and descriptor (dependencies.txt).
051     *
052     * WARNING: note it is very important to close the descriptor once no more used otherwise
053     * you can leak memory.
054     *
055     * <p>
056     * The default implementation of this method is unsupported and will always throw
057     * {@link UnsupportedOperationException}. Implementations that support configurable
058     * classloader creation must override this method.
059     * </p>
060     *
061     * @param descriptor the dependencies.txt InputStream.
062     * @param configuration the classloader configuration to apply when creating the classloader.
063     * @return the classloader initialized with the configuration provided and the resolved dependencies.
064     * @throws UnsupportedOperationException if this implementation does not support configurable
065     * classloader creation.
066     */
067    default ClassLoaderDescriptor mapDescriptorToClassLoader(InputStream descriptor,
068            final ClassLoaderDefinition configuration) {
069        throw new UnsupportedOperationException(
070                "ClassLoader configuration is not supported by this implementation; "
071                        + "override mapDescriptorToClassLoader(InputStream, ClassLoaderDefinition) to provide an implementation");
072    }
073
074    default ClassLoaderDescriptor mapDescriptorToClassLoader(final List<String> gavs,
075            final ClassLoaderDefinition configuration) {
076        return mapDescriptorToClassLoader(
077                new ByteArrayInputStream(String.join("\n", gavs).getBytes(StandardCharsets.UTF_8)), configuration);
078    }
079
080    /**
081     * Resolves the dependencies from the descriptor passed as an InputStream.
082     *
083     * IMPORTANT: this is to use when you are sure the file is resolvable if you don't have a fallback.
084     * In that last case, prefer the <code>mapDescriptorToClassLoader</code>.
085     *
086     * @param descriptor the dependencies.txt to use to resolve dependencies.
087     * @return the collection of file representing the available dependencies.
088     */
089    Collection<File> resolveFromDescriptor(InputStream descriptor);
090
091    /**
092     * Alias to load dependencies from a plain list of gav (groupId:artifactId:version).
093     *
094     * @param gavs the dependencies to use to resolve dependencies.
095     * @return the collection of file representing the available dependencies.
096     */
097    default Collection<File> resolveFromDescriptor(final List<String> gavs) {
098        return resolveFromDescriptor(
099                new ByteArrayInputStream(String.join("\n", gavs).getBytes(StandardCharsets.UTF_8)));
100    }
101
102    /**
103     * Abstract a classloader adding the metadata about the resolution done to create it.
104     */
105    interface ClassLoaderDescriptor extends AutoCloseable {
106
107        /**
108         * @return the underlying classloader.
109         */
110        ClassLoader asClassLoader();
111
112        /**
113         * @return the dependencies who matched the resolution and were used to create the classloader.
114         */
115        Collection<String> resolvedDependencies();
116    }
117}