View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.impl;
20  
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.function.Function;
25  import java.util.function.Supplier;
26  
27  import org.apache.maven.api.Artifact;
28  import org.apache.maven.api.ArtifactCoordinates;
29  import org.apache.maven.api.Dependency;
30  import org.apache.maven.api.DependencyCoordinates;
31  import org.apache.maven.api.LocalRepository;
32  import org.apache.maven.api.Node;
33  import org.apache.maven.api.RemoteRepository;
34  import org.apache.maven.api.Repository;
35  import org.apache.maven.api.Service;
36  import org.apache.maven.api.Session;
37  import org.apache.maven.api.WorkspaceRepository;
38  import org.apache.maven.api.annotations.Nonnull;
39  import org.apache.maven.api.annotations.Nullable;
40  import org.apache.maven.api.services.Request;
41  import org.apache.maven.api.services.RequestTrace;
42  import org.apache.maven.api.services.Result;
43  import org.eclipse.aether.RepositorySystem;
44  import org.eclipse.aether.RepositorySystemSession;
45  
46  import static org.apache.maven.impl.ImplUtils.cast;
47  
48  public interface InternalSession extends Session {
49  
50      static InternalSession from(Session session) {
51          return cast(InternalSession.class, session, "session should be an " + InternalSession.class);
52      }
53  
54      static InternalSession from(org.eclipse.aether.RepositorySystemSession session) {
55          return cast(InternalSession.class, session.getData().get(InternalSession.class), "session");
56      }
57  
58      static void associate(org.eclipse.aether.RepositorySystemSession rsession, Session session) {
59          if (!rsession.getData().set(InternalSession.class, null, from(session))) {
60              throw new IllegalStateException("A maven session is already associated with the repository session");
61          }
62      }
63  
64      /**
65       * Executes and optionally caches a request using the provided supplier function. If caching is enabled
66       * for this session, the result will be cached and subsequent identical requests will return the cached
67       * value without re-executing the supplier.
68       *
69       * @param <REQ> The request type
70       * @param <REP> The response type
71       * @param req The request object used as the cache key
72       * @param supplier The function to execute and cache the result
73       * @return The result from the supplier (either fresh or cached)
74       * @throws RuntimeException Any exception thrown by the supplier will be cached and re-thrown on subsequent calls
75       */
76      <REQ extends Request<?>, REP extends Result<REQ>> REP request(REQ req, Function<REQ, REP> supplier);
77  
78      <REQ extends Request<?>, REP extends Result<REQ>> List<REP> requests(
79              List<REQ> req, Function<List<REQ>, List<REP>> supplier);
80  
81      RemoteRepository getRemoteRepository(org.eclipse.aether.repository.RemoteRepository repository);
82  
83      LocalRepository getLocalRepository(org.eclipse.aether.repository.LocalRepository repository);
84  
85      WorkspaceRepository getWorkspaceRepository(org.eclipse.aether.repository.WorkspaceRepository repository);
86  
87      Repository getRepository(org.eclipse.aether.repository.ArtifactRepository repository);
88  
89      Node getNode(org.eclipse.aether.graph.DependencyNode node);
90  
91      Node getNode(org.eclipse.aether.graph.DependencyNode node, boolean verbose);
92  
93      @Nonnull
94      Artifact getArtifact(@Nonnull org.eclipse.aether.artifact.Artifact artifact);
95  
96      @Nonnull
97      <T extends Artifact> T getArtifact(@Nonnull Class<T> clazz, @Nonnull org.eclipse.aether.artifact.Artifact artifact);
98  
99      @Nonnull
100     Dependency getDependency(@Nonnull org.eclipse.aether.graph.Dependency dependency);
101 
102     List<org.eclipse.aether.repository.RemoteRepository> toRepositories(List<RemoteRepository> repositories);
103 
104     List<org.eclipse.aether.repository.RemoteRepository> toResolvingRepositories(List<RemoteRepository> repositories);
105 
106     org.eclipse.aether.repository.RemoteRepository toRepository(RemoteRepository repository);
107 
108     org.eclipse.aether.repository.LocalRepository toRepository(LocalRepository repository);
109 
110     List<org.eclipse.aether.graph.Dependency> toDependencies(
111             Collection<DependencyCoordinates> dependencies, boolean managed);
112 
113     org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinates dependency, boolean managed);
114 
115     List<org.eclipse.aether.artifact.Artifact> toArtifacts(Collection<? extends Artifact> artifacts);
116 
117     org.eclipse.aether.artifact.Artifact toArtifact(Artifact artifact);
118 
119     org.eclipse.aether.artifact.Artifact toArtifact(ArtifactCoordinates coords);
120 
121     RepositorySystemSession getSession();
122 
123     RepositorySystem getRepositorySystem();
124 
125     /**
126      * Sets the current request trace for the session.
127      * The request trace provides contextual information about the current operation
128      * being performed and can be used for debugging and monitoring purposes.
129      * The trace is stored in thread-local storage, allowing for concurrent operations
130      * with different traces.
131      *
132      * @param trace the trace to set as current, may be null to clear the trace
133      * @see RequestTraceHelper#enter(Session, Object) For the recommended way to manage traces
134      */
135     void setCurrentTrace(@Nullable RequestTrace trace);
136 
137     /**
138      * Gets the current request trace for the session from thread-local storage.
139      * Each thread maintains its own trace context, ensuring thread-safety for
140      * concurrent operations.
141      *
142      * @return the current request trace, or null if no trace is set
143      * @see RequestTraceHelper#enter(Session, Object) For the recommended way to manage traces
144      */
145     RequestTrace getCurrentTrace();
146 
147     /**
148      * Retrieves a map of all services.
149      *
150      * @see #getService(Class)
151      */
152     @Nonnull
153     Map<Class<? extends Service>, Supplier<? extends Service>> getAllServices();
154 }