Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.Closeable;
import java.io.File;
import java.util.List;
import java.util.Map;

import org.eclipse.aether.artifact.ArtifactTypeRegistry;
Expand Down Expand Up @@ -55,9 +56,12 @@ public interface RepositorySystemSession {
* Immutable session that is closeable, should be handled as a resource. These session instances can be
* created with {@link SessionBuilder}.
*
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
*
* @since TBD
*/
interface CloseableRepositorySystemSession extends RepositorySystemSession, Closeable {
interface CloseableSession extends RepositorySystemSession, Closeable {
/**
* Returns the ID of this closeable session instance. Each closeable session has different ID, unique within
* repository system they were created with.
Expand Down Expand Up @@ -88,8 +92,11 @@ interface CloseableRepositorySystemSession extends RepositorySystemSession, Clos
}

/**
* Builder for building {@link CloseableRepositorySystemSession} instances. Builder instances can be created with
* {@link RepositorySystem#createSessionBuilder()} method.
* Builder for building {@link CloseableSession} instances. Builder instances can be created with
* {@link RepositorySystem#createSessionBuilder()} method. Instances are not thread-safe nor immutable.
*
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
*
* @since TBD
*/
Expand Down Expand Up @@ -386,12 +393,48 @@ interface SessionBuilder {
SessionBuilder setCache(RepositoryCache cache);

/**
* Shortcut method to set up local repository manager.
* Shortcut method to set up local repository manager directly onto builder. There must be at least one non-null
* {@link File} passed in this method. In case multiple files, session builder will use chained local repository
* manager.
*
* @param baseDirectories The local repository base directories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
*/
SessionBuilder withLocalRepositoryBaseDirectories(File... baseDirectories);

/**
* Shortcut method to set up local repository manager directly onto builder. There must be at least one non-null
* {@link File} present in passed in list. In case multiple files, session builder will use chained local
* repository manager.
*
* @param baseDirectories The local repository base directories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
*/
SessionBuilder withLocalRepositoryBaseDirectories(List<File> baseDirectories);

/**
* Shortcut method to set up local repository manager directly onto builder. There must be at least one non-null
* {@link LocalRepository} passed in this method. In case multiple local repositories, session builder will
* use chained local repository manager.
*
* @param localRepositories The local repositories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
*/
SessionBuilder withLocalRepositories(LocalRepository... localRepositories);

/**
* Shortcut method to set up local repository manager directly onto builder. There must be at least one non-null
* {@link LocalRepository} present in passed in list. In case multiple local repositories, session builder will
* use chained local repository manager.
*
* @param basedir The local repository base directory, may be {@code null} if none.
* @param localRepositories The local repositories.
* @return This session for chaining, never {@code null}.
* @see #newLocalRepositoryManager(LocalRepository...)
*/
SessionBuilder withLocalRepository(File basedir);
SessionBuilder withLocalRepositories(List<LocalRepository> localRepositories);

/**
* Shortcut method to shallow-copy passed in session into current builder.
Expand All @@ -404,7 +447,7 @@ interface SessionBuilder {
/**
* Creates a session instance.
*/
CloseableRepositorySystemSession build();
CloseableSession build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public final class AuthenticationContext implements Closeable {
/**
* Gets an authentication context for the specified repository.
*
* @param session The repository system session during which the repository is accessed, must not be {@code null}.
* @param session The repository system session during which the repository is accessed, may be {@code null}.
* @param repository The repository for which to create an authentication context, must not be {@code null}.
* @return An authentication context for the repository or {@code null} if no authentication is configured for it.
*/
Expand All @@ -153,7 +153,7 @@ public static AuthenticationContext forRepository(RepositorySystemSession sessio
/**
* Gets an authentication context for the proxy of the specified repository.
*
* @param session The repository system session during which the repository is accessed, must not be {@code null}.
* @param session The repository system session during which the repository is accessed, may be {@code null}.
* @param repository The repository for whose proxy to create an authentication context, must not be {@code null}.
* @return An authentication context for the proxy or {@code null} if no proxy is set or no authentication is
* configured for it.
Expand All @@ -173,17 +173,17 @@ private static AuthenticationContext newInstance(

private AuthenticationContext(
RepositorySystemSession session, RemoteRepository repository, Proxy proxy, Authentication auth) {
this.session = requireNonNull(session, "repository system session cannot be null");
this.repository = repository;
this.session = session;
this.repository = requireNonNull(repository, "null repository");
this.proxy = proxy;
this.auth = auth;
authData = new HashMap<>();
}

/**
* Gets the repository system session during which the authentication happens.
* Gets the repository system session during which the authentication happens (if within session).
*
* @return The repository system session, never {@code null}.
* @return The repository system session, may be {@code null} if context is created outside of session.
*/
public RepositorySystemSession getSession() {
return session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.eclipse.aether.impl;

import org.eclipse.aether.RepositorySystemSession.CloseableRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;

/**
* Lifecycle managing component for repository system.
Expand Down Expand Up @@ -49,25 +49,25 @@ public interface RepositorySystemLifecycle {
*
* @since TBD
*/
void sessionStarted(CloseableRepositorySystemSession session);
void sessionStarted(CloseableSession session);

/**
* Signals that passed in session was ended, it will not be used anymore. Repository system
* will invoke the registered handlers for this session, if any. This method throws if the passed in session
* instance was not passed to method {@link #sessionStarted(CloseableRepositorySystemSession)} beforehand.
* instance was not passed to method {@link #sessionStarted(CloseableSession)} beforehand.
* <p>
* <em>Same session instance can be ended only once.</em>
*
* @since TBD
*/
void sessionEnded(CloseableRepositorySystemSession session);
void sessionEnded(CloseableSession session);

/**
* Registers an "on session end" handler.
* <p>
* Throws if session was not passed to {@link #sessionStarted(CloseableRepositorySystemSession)} beforehand.
* Throws if session was not passed to {@link #sessionStarted(CloseableSession)} beforehand.
*
* @since TBD
*/
void addOnSessionEndedHandle(CloseableRepositorySystemSession session, Runnable handler);
void addOnSessionEndedHandle(CloseableSession session, Runnable handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public LocalRepositoryManager newLocalRepositoryManager(
RepositorySystemSession session, LocalRepository localRepository) {
requireNonNull(session, "session cannot be null");
requireNonNull(localRepository, "localRepository cannot be null");
validateSystem();

try {
return localRepositoryProvider.newLocalRepositoryManager(session, localRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.aether.MultiRuntimeException;
import org.eclipse.aether.RepositorySystemSession.CloseableRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.impl.RepositorySystemLifecycle;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -89,7 +89,7 @@ public void addOnSystemEndedHandler(Runnable handler) {
}

@Override
public void sessionStarted(CloseableRepositorySystemSession session) {
public void sessionStarted(CloseableSession session) {
requireNonNull(session, "session cannot be null");
requireNotShutdown();
String sessionId = session.sessionId();
Expand All @@ -102,7 +102,7 @@ public void sessionStarted(CloseableRepositorySystemSession session) {
}

@Override
public void sessionEnded(CloseableRepositorySystemSession session) {
public void sessionEnded(CloseableSession session) {
requireNonNull(session, "session cannot be null");
requireNotShutdown();
String sessionId = session.sessionId();
Expand All @@ -127,7 +127,7 @@ public void sessionEnded(CloseableRepositorySystemSession session) {
}

@Override
public void addOnSessionEndedHandle(CloseableRepositorySystemSession session, Runnable handler) {
public void addOnSessionEndedHandle(CloseableSession session, Runnable handler) {
requireNonNull(session, "session cannot be null");
requireNonNull(handler, "handler cannot be null");
requireNotShutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
package org.eclipse.aether.internal.impl.session;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.aether.RepositoryCache;
import org.eclipse.aether.RepositoryListener;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession.CloseableRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.SessionData;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.DependencyGraphTransformer;
Expand All @@ -43,13 +44,15 @@
import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
import org.eclipse.aether.resolution.ResolutionErrorPolicy;
import org.eclipse.aether.transfer.TransferListener;
import org.eclipse.aether.util.repository.ChainedLocalRepositoryManager;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

/**
* A default implementation of repository system session that is immutable.
* A default implementation of repository system session that is immutable and thread-safe.
*/
public final class DefaultCloseableRepositorySystemSession implements CloseableRepositorySystemSession {
public final class DefaultCloseableSession implements CloseableSession {
private final String sessionId;

private final AtomicBoolean closed;
Expand Down Expand Up @@ -109,7 +112,7 @@ public final class DefaultCloseableRepositorySystemSession implements CloseableR
private final RepositorySystemLifecycle repositorySystemLifecycle;

@SuppressWarnings("checkstyle:parameternumber")
public DefaultCloseableRepositorySystemSession(
public DefaultCloseableSession(
String sessionId,
AtomicBoolean closed,
boolean offline,
Expand All @@ -120,6 +123,7 @@ public DefaultCloseableRepositorySystemSession(
String artifactUpdatePolicy,
String metadataUpdatePolicy,
LocalRepositoryManager localRepositoryManager,
List<LocalRepository> localRepositories,
WorkspaceReader workspaceReader,
RepositoryListener repositoryListener,
TransferListener transferListener,
Expand Down Expand Up @@ -148,7 +152,6 @@ public DefaultCloseableRepositorySystemSession(
this.checksumPolicy = checksumPolicy;
this.artifactUpdatePolicy = artifactUpdatePolicy;
this.metadataUpdatePolicy = metadataUpdatePolicy;
this.localRepositoryManager = requireNonNull(localRepositoryManager);
this.workspaceReader = workspaceReader;
this.repositoryListener = repositoryListener;
this.transferListener = transferListener;
Expand All @@ -170,11 +173,35 @@ public DefaultCloseableRepositorySystemSession(
this.repositorySystem = requireNonNull(repositorySystem);
this.repositorySystemLifecycle = requireNonNull(repositorySystemLifecycle);

this.localRepositoryManager = getOrCreateLocalRepositoryManager(localRepositoryManager, localRepositories);

if (closed == null) {
repositorySystemLifecycle.sessionStarted(this);
}
}

private LocalRepositoryManager getOrCreateLocalRepositoryManager(
LocalRepositoryManager localRepositoryManager, List<LocalRepository> localRepositories) {
if (localRepositoryManager != null) {
return localRepositoryManager;
} else if (localRepositories != null) {
if (localRepositories.isEmpty()) {
throw new IllegalArgumentException("empty localRepositories");
} else if (localRepositories.size() == 1) {
return repositorySystem.newLocalRepositoryManager(this, localRepositories.get(0));
} else {
LocalRepositoryManager head =
repositorySystem.newLocalRepositoryManager(this, localRepositories.get(0));
List<LocalRepositoryManager> tail = localRepositories.subList(1, localRepositories.size()).stream()
.map(l -> repositorySystem.newLocalRepositoryManager(this, l))
.collect(toList());
return new ChainedLocalRepositoryManager(head, tail, this);
}
} else {
throw new IllegalStateException("No local repository manager or local repositories set on session");
}
}

@Override
public String sessionId() {
return sessionId;
Expand Down
Loading