001/**
002 * Copyright (C) 2006-2025 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.runtime.manager.finder;
017
018import java.util.Optional;
019
020import org.talend.sdk.component.container.Container;
021import org.talend.sdk.component.runtime.manager.ComponentManager;
022import org.talend.sdk.component.runtime.serialization.LightContainer;
023import org.talend.sdk.component.runtime.serialization.TCCLContainerFinder;
024
025import lombok.extern.slf4j.Slf4j;
026
027// NOTE: ensure it is aligned with the components, this is hardcoded cause must be standard!
028
029/**
030 * a {@link org.talend.sdk.component.runtime.serialization.ContainerFinder}
031 * which will starts and manage a single {@link ComponentManager} for the JVM
032 * (root classloader actually) life.
033 */
034@Slf4j
035public class StandaloneContainerFinder extends TCCLContainerFinder {
036
037    // IMPORTANT: don't abuse of lambdas here, it is on the runtime codepath
038    @Override
039    public LightContainer find(final String plugin) {
040        final ComponentManager manager = ComponentManager.instance();
041        Optional<Container> optionalContainer = manager.findPlugin(plugin);
042        if (!optionalContainer.isPresent()) {
043            log.info("Didn't find plugin " + plugin + ", had: " + manager.availablePlugins());
044
045            // we assume we use a fatjar created with nested-maven-repository extensions
046            // (default nested loading)
047            // so we have the plugin in TALEND-INF/plugins.properties and the jar located as
048            // nested in current jar.
049            try {
050                optionalContainer = manager.findPlugin(manager.addPlugin(plugin));
051            } catch (final IllegalArgumentException iae) { // concurrent request?
052                optionalContainer = manager.findPlugin(plugin);
053            }
054        }
055        if (optionalContainer.isPresent()) {
056            final LightContainer lightContainer = optionalContainer.get().get(LightContainer.class);
057            if (lightContainer != null) {
058                return lightContainer;
059            }
060        }
061        return super.find(plugin); // TCCL
062    }
063}