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.java;
020
021import java.io.File;
022import java.util.Objects;
023import java.util.stream.Collectors;
024import java.util.stream.Stream;
025import static org.jdrupes.builder.api.Intent.*;
026import org.jdrupes.builder.api.Project;
027import org.jdrupes.builder.api.Resource;
028import org.jdrupes.builder.api.ResourceProvider;
029import org.jdrupes.builder.api.ResourceRequest;
030import org.jdrupes.builder.api.ResourceRetriever;
031import static org.jdrupes.builder.api.ResourceType.*;
032import org.jdrupes.builder.core.AbstractProvider;
033import org.jdrupes.builder.core.StreamCollector;
034import static org.jdrupes.builder.java.JavaTypes.*;
035
036/// The Class JavaExec.
037///
038public class JavaExecuter extends AbstractProvider
039        implements ResourceRetriever {
040
041    private final StreamCollector<ResourceProvider> providers
042        = StreamCollector.cached();
043    private String mainClass;
044
045    /// Initializes a new java executor.
046    ///
047    /// @param project the project
048    ///
049    public JavaExecuter(Project project) {
050        // Make javadoc happy
051    }
052
053    /// Additionally uses the given providers for obtaining contents for the
054    /// jar.
055    ///
056    /// @param providers the providers
057    /// @return the jar generator
058    ///
059    @Override
060    public JavaExecuter from(ResourceProvider... providers) {
061        from(Stream.of(providers));
062        return this;
063    }
064
065    /// Additionally uses the given providers for obtaining contents for the
066    /// jar.
067    ///
068    /// @param providers the providers
069    /// @return the jar generator
070    ///
071    @Override
072    public JavaExecuter from(Stream<ResourceProvider> providers) {
073        this.providers.add(providers.filter(p -> !p.equals(this)));
074        return this;
075    }
076
077    /// Returns the main class.
078    ///
079    /// @return the main class
080    ///
081    public String mainClass() {
082        return mainClass;
083    }
084
085    /// Sets the main class.
086    ///
087    /// @param mainClass the new main class
088    /// @return the jar generator for method chaining
089    ///
090    public JavaExecuter mainClass(String mainClass) {
091        this.mainClass = Objects.requireNonNull(mainClass);
092        return this;
093    }
094
095    @Override
096    protected <T extends Resource> Stream<T>
097            doProvide(ResourceRequest<T> requested) {
098        if (!requested.accepts(ExecResultType)) {
099            return Stream.empty();
100        }
101
102        // Collect the classpath.
103        var cpResources = newResource(ClasspathType)
104            .addAll(providers.stream()
105                .map(p -> p.resources(of(ClasspathElementType).usingAll()))
106                .flatMap(s -> s));
107        log.finer(() -> "Executing with classpath "
108            + cpResources.stream().map(e -> e.toPath().toString())
109                .collect(Collectors.joining(File.pathSeparator)));
110
111        // Return result
112        return Stream.empty();
113    }
114
115}