Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8385] Introduce proto session #1929

Merged
merged 10 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api;

import java.nio.file.Path;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.annotations.ThreadSafe;

import static java.util.Objects.requireNonNull;

/**
* The proto session, material used to create {@link Session}.
*
* @since 4.0.0
*/
@Experimental
@ThreadSafe
public interface ProtoSession {

/**
* Returns immutable user properties to use for interpolation. The user properties have been configured directly
* by the user, e.g. via the {@code -Dkey=value} parameter on the command line.
*
* @return the user properties, never {@code null}
*/
@Nonnull
Map<String, String> getUserProperties();

/**
* Returns immutable system properties to use for interpolation. The system properties are collected from the
* runtime environment such as {@link System#getProperties()} and environment variables
* (prefixed with {@code env.}).
*
* @return the system properties, never {@code null}
*/
@Nonnull
Map<String, String> getSystemProperties();

/**
* Returns the start time of the session.
*
* @return the start time as an Instant object, never {@code null}
*/
@Nonnull
Instant getStartTime();

/**
* Gets the directory of the topmost project being built, usually the current directory or the
* directory pointed at by the {@code -f/--file} command line argument.
*
* @return the directory of the topmost project, never {@code null}
* @see Project#isTopProject()
* @see #getRootDirectory()
*/
@Nonnull
Path getTopDirectory();

/**
* Gets the root directory of the session, which is the root directory for the top directory project.
*
* @return the root directory, never {@code null}
* @throws IllegalStateException if the root directory could not be found
* @see #getTopDirectory()
* @see Project#getRootDirectory()
* @see Project#isRootProject()
*/
@Nonnull
Path getRootDirectory();

/**
* Returns a proto session builder of this instance.
*/
@Nonnull
default Builder toBuilder() {
try {
return new Builder(
getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), getRootDirectory());
} catch (IllegalStateException e) {
return new Builder(getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), null);
}
}

/**
* Returns new builder from scratch.
*/
static Builder newBuilder() {
return new Builder().withStartTime(Instant.now());
}

class Builder {
private Map<String, String> userProperties;
private Map<String, String> systemProperties;
private Instant startTime;
private Path topDirectory;
private Path rootDirectory;

private Builder() {}

private Builder(
Map<String, String> userProperties,
Map<String, String> systemProperties,
Instant startTime,
Path topDirectory,
Path rootDirectory) {
this.userProperties = userProperties;
this.systemProperties = systemProperties;
this.startTime = startTime;
this.topDirectory = topDirectory;
this.rootDirectory = rootDirectory;
}

public Builder withUserProperties(@Nonnull Map<String, String> userProperties) {
this.userProperties = new HashMap<>(userProperties);
return this;
}

public Builder withSystemProperties(@Nonnull Map<String, String> systemProperties) {
this.systemProperties = new HashMap<>(systemProperties);
return this;
}

public Builder withStartTime(@Nonnull Instant startTime) {
this.startTime = requireNonNull(startTime, "startTime");
return this;
}

public Builder withTopDirectory(@Nonnull Path topDirectory) {
this.topDirectory = requireNonNull(topDirectory, "topDirectory");
return this;
}

public Builder withRootDirectory(@Nullable Path rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}

public ProtoSession build() {
return new Impl(userProperties, systemProperties, startTime, topDirectory, rootDirectory);
}

private static class Impl implements ProtoSession {
private final Map<String, String> userProperties;
private final Map<String, String> systemProperties;
private final Instant startTime;
private final Path topDirectory;
private final Path rootDirectory;

private Impl(
Map<String, String> userProperties,
Map<String, String> systemProperties,
Instant startTime,
Path topDirectory,
Path rootDirectory) {
this.userProperties = requireNonNull(userProperties);
this.systemProperties = requireNonNull(systemProperties);
this.startTime = requireNonNull(startTime);
this.topDirectory = requireNonNull(topDirectory);
this.rootDirectory = rootDirectory;
}

@Override
public Map<String, String> getUserProperties() {
return userProperties;
}

@Override
public Map<String, String> getSystemProperties() {
return systemProperties;
}

@Override
public Instant getStartTime() {
return startTime;
}

@Override
public Path getTopDirectory() {
return topDirectory;
}

@Override
public Path getRootDirectory() {
if (rootDirectory == null) {
throw new IllegalStateException("root directory not set");
}
return rootDirectory;
}
}
}
}
69 changes: 9 additions & 60 deletions api/maven-api-core/src/main/java/org/apache/maven/api/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.maven.api;

import java.nio.file.Path;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -46,7 +45,15 @@
*/
@Experimental
@ThreadSafe
public interface Session {
public interface Session extends ProtoSession {

/**
* Returns the current maven version.
*
* @return the maven version, never {@code null}
*/
@Nonnull
Version getMavenVersion();

/**
* Retrieves the settings for the current session.
Expand Down Expand Up @@ -80,25 +87,6 @@ public interface Session {
@Nonnull
SessionData getData();

/**
* Returns immutable user properties to use for interpolation. The user properties have been configured directly
* by the user, e.g. via the {@code -Dkey=value} parameter on the command line.
*
* @return the user properties, never {@code null}
*/
@Nonnull
Map<String, String> getUserProperties();

/**
* Returns immutable system properties to use for interpolation. The system properties are collected from the
* runtime environment such as {@link System#getProperties()} and environment variables
* (prefixed with {@code env.}).
*
* @return the system properties, never {@code null}
*/
@Nonnull
Map<String, String> getSystemProperties();

/**
* Each invocation computes a new map of effective properties. To be used in interpolation.
* <p>
Expand All @@ -121,52 +109,13 @@ public interface Session {
@Nonnull
Map<String, String> getEffectiveProperties(@Nullable Project project);

/**
* Returns the current maven version.
*
* @return the maven version, never {@code null}
*/
@Nonnull
Version getMavenVersion();

/**
* Returns the degree of concurrency for the build.
*
* @return the degree of concurrency
*/
int getDegreeOfConcurrency();

/**
* Returns the start time of the session.
*
* @return the start time as an Instant object, never {@code null}
*/
@Nonnull
Instant getStartTime();

/**
* Gets the directory of the topmost project being built, usually the current directory or the
* directory pointed at by the {@code -f/--file} command line argument.
*
* @return the directory of the topmost project, never {@code null}
* @see Project#isTopProject()
* @see #getRootDirectory()
*/
@Nonnull
Path getTopDirectory();

/**
* Gets the root directory of the session, which is the root directory for the top directory project.
*
* @return the root directory, never {@code null}
* @throws IllegalStateException if the root directory could not be found
* @see #getTopDirectory()
* @see Project#getRootDirectory()
* @see Project#isRootProject()
*/
@Nonnull
Path getRootDirectory();

/**
* Retrieves a list of projects associated with the session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public ArtifactCoordinatesFactoryRequest build() {
session, groupId, artifactId, version, classifier, extension, type, coordinateString);
}

private static class DefaultArtifactFactoryRequestArtifact extends BaseRequest
private static class DefaultArtifactFactoryRequestArtifact extends BaseRequest<Session>
implements ArtifactCoordinatesFactoryRequest {
private final String groupId;
private final String artifactId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public ArtifactDeployerRequest build() {
return new DefaultArtifactDeployerRequest(session, repository, artifacts, retryFailedDeploymentCount);
}

private static class DefaultArtifactDeployerRequest extends BaseRequest implements ArtifactDeployerRequest {
private static class DefaultArtifactDeployerRequest extends BaseRequest<Session>
implements ArtifactDeployerRequest {

private final RemoteRepository repository;
private final Collection<ProducedArtifact> artifacts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public ArtifactFactoryRequest build() {
session, groupId, artifactId, version, classifier, extension, type);
}

private static class DefaultArtifactFactoryRequest extends BaseRequest implements ArtifactFactoryRequest {
private static class DefaultArtifactFactoryRequest extends BaseRequest<Session>
implements ArtifactFactoryRequest {
private final String groupId;
private final String artifactId;
private final String version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public ArtifactInstallerRequest build() {
return new DefaultArtifactInstallerRequest(session, artifacts);
}

static class DefaultArtifactInstallerRequest extends BaseRequest implements ArtifactInstallerRequest {
static class DefaultArtifactInstallerRequest extends BaseRequest<Session> implements ArtifactInstallerRequest {

private final Collection<ProducedArtifact> artifacts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public ArtifactResolverRequest build() {
return new DefaultArtifactResolverRequest(session, coordinates, repositories);
}

private static class DefaultArtifactResolverRequest extends BaseRequest implements ArtifactResolverRequest {
private static class DefaultArtifactResolverRequest extends BaseRequest<Session>
implements ArtifactResolverRequest {
@Nonnull
private final Collection<? extends ArtifactCoordinates> coordinates;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.Collection;
import java.util.Collections;

import org.apache.maven.api.Session;
import org.apache.maven.api.ProtoSession;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;

Expand All @@ -32,16 +32,16 @@
* @since 4.0.0
*/
@Experimental
abstract class BaseRequest {
abstract class BaseRequest<S extends ProtoSession> {

private final Session session;
private final S session;

protected BaseRequest(@Nonnull Session session) {
protected BaseRequest(@Nonnull S session) {
this.session = nonNull(session, "session cannot be null");
}

@Nonnull
public Session getSession() {
public S getSession() {
return session;
}

Expand Down
Loading