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