001/*
002 * JDrupes Builder
003 * Copyright (C) 2025 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.core;
020
021import java.nio.file.Path;
022import java.util.concurrent.ExecutorService;
023import java.util.concurrent.Executors;
024import java.util.stream.Stream;
025import org.apache.commons.cli.CommandLine;
026import org.jdrupes.builder.api.BuildContext;
027import org.jdrupes.builder.api.Resource;
028import org.jdrupes.builder.api.ResourceProvider;
029import org.jdrupes.builder.api.ResourceRequest;
030import org.jdrupes.builder.core.FutureStreamCache.Key;
031
032/// A context for building.
033///
034public class DefaultBuildContext implements BuildContext {
035
036    /// The key for specifying the builder directory in the properties file.
037    public static final String JDBLD_DIRECTORY = "jdbldDirectory";
038    private final FutureStreamCache cache;
039    private ExecutorService executor
040        = Executors.newVirtualThreadPerTaskExecutor();
041
042    /// Instantiates a new default build. By default, the build uses
043    /// a virtual thread per task executor.
044    ///
045    /* default */ DefaultBuildContext() {
046        cache = new FutureStreamCache();
047    }
048
049    /// Returns the executor service used by this build to create futures.
050    ///
051    /// @return the executor service
052    ///
053    public ExecutorService executor() {
054        return executor;
055    }
056
057    /// Sets the executor service used by this build to create futures.
058    ///
059    /// @param executor the executor
060    ///
061    public void executor(ExecutorService executor) {
062        this.executor = executor;
063    }
064
065    @Override
066    public <T extends Resource> Stream<T> get(ResourceProvider provider,
067            ResourceRequest<T> request) {
068        return cache.computeIfAbsent(new Key<>(provider, request),
069            k -> new FutureStream<T>(executor, k.provider(), k.requested()))
070            .stream();
071    }
072
073    @Override
074    public Path jdbldDirectory() {
075        return Path
076            .of(LauncherSupport.jdbldProperties().getProperty(JDBLD_DIRECTORY));
077    }
078
079    @Override
080    public CommandLine commandLine() {
081        return LauncherSupport.commandLine();
082    }
083
084    @Override
085    public String property(String name) {
086        return LauncherSupport.jdbldProperties().getProperty(name);
087    }
088
089}