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.function.Function;
24  
25  import org.apache.maven.api.Artifact;
26  import org.apache.maven.api.ArtifactCoordinates;
27  import org.apache.maven.api.Dependency;
28  import org.apache.maven.api.DependencyCoordinates;
29  import org.apache.maven.api.LocalRepository;
30  import org.apache.maven.api.Node;
31  import org.apache.maven.api.RemoteRepository;
32  import org.apache.maven.api.Repository;
33  import org.apache.maven.api.Session;
34  import org.apache.maven.api.WorkspaceRepository;
35  import org.apache.maven.api.annotations.Nonnull;
36  import org.apache.maven.api.annotations.Nullable;
37  import org.apache.maven.api.services.Request;
38  import org.apache.maven.api.services.RequestTrace;
39  import org.apache.maven.api.services.Result;
40  import org.eclipse.aether.RepositorySystem;
41  import org.eclipse.aether.RepositorySystemSession;
42  
43  import static org.apache.maven.impl.Utils.cast;
44  
45  public interface InternalSession extends Session {
46  
47      static InternalSession from(Session session) {
48          return cast(InternalSession.class, session, "session should be an " + InternalSession.class);
49      }
50  
51      static InternalSession from(org.eclipse.aether.RepositorySystemSession session) {
52          return cast(InternalSession.class, session.getData().get(InternalSession.class), "session");
53      }
54  
55      static void associate(org.eclipse.aether.RepositorySystemSession rsession, Session session) {
56          if (!rsession.getData().set(InternalSession.class, null, from(session))) {
57              throw new IllegalStateException("A maven session is already associated with the repository session");
58          }
59      }
60  
61      /**
62       * Executes and optionally caches a request using the provided supplier function. If caching is enabled
63       * for this session, the result will be cached and subsequent identical requests will return the cached
64       * value without re-executing the supplier.
65       *
66       * @param <REQ> The request type
67       * @param <REP> The response type
68       * @param req The request object used as the cache key
69       * @param supplier The function to execute and cache the result
70       * @return The result from the supplier (either fresh or cached)
71       * @throws RuntimeException Any exception thrown by the supplier will be cached and re-thrown on subsequent calls
72       */
73      <REQ extends Request<?>, REP extends Result<REQ>> REP request(REQ req, Function<REQ, REP> supplier);
74  
75      <REQ extends Request<?>, REP extends Result<REQ>> List<REP> requests(
76              List<REQ> req, Function<List<REQ>, List<REP>> supplier);
77  
78      RemoteRepository getRemoteRepository(org.eclipse.aether.repository.RemoteRepository repository);
79  
80      LocalRepository getLocalRepository(org.eclipse.aether.repository.LocalRepository repository);
81  
82      WorkspaceRepository getWorkspaceRepository(org.eclipse.aether.repository.WorkspaceRepository repository);
83  
84      Repository getRepository(org.eclipse.aether.repository.ArtifactRepository repository);
85  
86      Node getNode(org.eclipse.aether.graph.DependencyNode node);
87  
88      Node getNode(org.eclipse.aether.graph.DependencyNode node, boolean verbose);
89  
90      @Nonnull
91      Artifact getArtifact(@Nonnull org.eclipse.aether.artifact.Artifact artifact);
92  
93      @Nonnull
94      <T extends Artifact> T getArtifact(@Nonnull Class<T> clazz, @Nonnull org.eclipse.aether.artifact.Artifact artifact);
95  
96      @Nonnull
97      Dependency getDependency(@Nonnull org.eclipse.aether.graph.Dependency dependency);
98  
99      List<org.eclipse.aether.repository.RemoteRepository> toRepositories(List<RemoteRepository> repositories);
100 
101     org.eclipse.aether.repository.RemoteRepository toRepository(RemoteRepository repository);
102 
103     org.eclipse.aether.repository.LocalRepository toRepository(LocalRepository repository);
104 
105     List<org.eclipse.aether.graph.Dependency> toDependencies(
106             Collection<DependencyCoordinates> dependencies, boolean managed);
107 
108     org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinates dependency, boolean managed);
109 
110     List<org.eclipse.aether.artifact.Artifact> toArtifacts(Collection<? extends Artifact> artifacts);
111 
112     org.eclipse.aether.artifact.Artifact toArtifact(Artifact artifact);
113 
114     org.eclipse.aether.artifact.Artifact toArtifact(ArtifactCoordinates coords);
115 
116     RepositorySystemSession getSession();
117 
118     RepositorySystem getRepositorySystem();
119 
120     /**
121      * Sets the current request trace for the session.
122      * The request trace provides contextual information about the current operation
123      * being performed and can be used for debugging and monitoring purposes.
124      * The trace is stored in thread-local storage, allowing for concurrent operations
125      * with different traces.
126      *
127      * @param trace the trace to set as current, may be null to clear the trace
128      * @see RequestTraceHelper#enter(Session, Object) For the recommended way to manage traces
129      */
130     void setCurrentTrace(@Nullable RequestTrace trace);
131 
132     /**
133      * Gets the current request trace for the session from thread-local storage.
134      * Each thread maintains its own trace context, ensuring thread-safety for
135      * concurrent operations.
136      *
137      * @return the current request trace, or null if no trace is set
138      * @see RequestTraceHelper#enter(Session, Object) For the recommended way to manage traces
139      */
140     RequestTrace getCurrentTrace();
141 }