|
9 | 9 |
|
10 | 10 | import static java.util.Objects.requireNonNull;
|
11 | 11 |
|
| 12 | +import eu.maveniverse.maven.mima.extensions.mmr.ModelRequest; |
| 13 | +import eu.maveniverse.maven.mima.extensions.mmr.ModelResponse; |
| 14 | +import eu.maveniverse.maven.mima.extensions.mmr.internal.MavenModelReaderImpl; |
12 | 15 | import eu.maveniverse.maven.njord.shared.Session;
|
13 | 16 | import eu.maveniverse.maven.njord.shared.SessionConfig;
|
14 | 17 | import eu.maveniverse.maven.njord.shared.publisher.ArtifactPublisherRedirector;
|
|
36 | 39 | import java.util.concurrent.ConcurrentMap;
|
37 | 40 | import java.util.concurrent.atomic.AtomicInteger;
|
38 | 41 | import java.util.stream.Collectors;
|
| 42 | +import org.apache.maven.model.Model; |
| 43 | +import org.eclipse.aether.artifact.Artifact; |
| 44 | +import org.eclipse.aether.repository.RemoteRepository; |
39 | 45 |
|
40 | 46 | public class DefaultSession extends CloseableConfigSupport<SessionConfig> implements Session {
|
41 | 47 | private final String sessionBoundStoreKey;
|
42 |
| - private final DefaultSessionFactory defaultSessionFactory; |
43 | 48 | private final InternalArtifactStoreManager internalArtifactStoreManager;
|
44 | 49 | private final ArtifactStoreWriter artifactStoreWriter;
|
45 | 50 | private final ArtifactStoreMerger artifactStoreMerger;
|
46 | 51 | private final ArtifactPublisherRedirector artifactPublisherRedirector;
|
47 | 52 | private final Map<String, ArtifactStorePublisherFactory> artifactStorePublisherFactories;
|
48 | 53 | private final Map<String, ArtifactStoreComparatorFactory> artifactStoreComparatorFactories;
|
| 54 | + private final MavenModelReaderImpl mavenModelReader; |
49 | 55 |
|
50 | 56 | public DefaultSession(
|
51 | 57 | SessionConfig sessionConfig,
|
52 |
| - DefaultSessionFactory defaultSessionFactory, |
53 | 58 | InternalArtifactStoreManagerFactory internalArtifactStoreManagerFactory,
|
54 | 59 | ArtifactStoreWriterFactory artifactStoreWriterFactory,
|
55 | 60 | ArtifactStoreMergerFactory artifactStoreMergerFactory,
|
56 | 61 | ArtifactPublisherRedirectorFactory artifactPublisherRedirectorFactory,
|
57 | 62 | Map<String, ArtifactStorePublisherFactory> artifactStorePublisherFactories,
|
58 |
| - Map<String, ArtifactStoreComparatorFactory> artifactStoreComparatorFactories) { |
| 63 | + Map<String, ArtifactStoreComparatorFactory> artifactStoreComparatorFactories, |
| 64 | + MavenModelReaderImpl mavenModelReader) { |
59 | 65 | super(sessionConfig);
|
60 | 66 | this.sessionBoundStoreKey = Session.class.getName() + "." + ArtifactStore.class + "." + UUID.randomUUID();
|
61 |
| - this.defaultSessionFactory = requireNonNull(defaultSessionFactory); |
62 | 67 | this.internalArtifactStoreManager = internalArtifactStoreManagerFactory.create(sessionConfig);
|
63 | 68 | this.artifactStoreWriter = requireNonNull(artifactStoreWriterFactory).create(sessionConfig);
|
64 | 69 | this.artifactStoreMerger = requireNonNull(artifactStoreMergerFactory).create(sessionConfig);
|
65 | 70 | this.artifactPublisherRedirector =
|
66 | 71 | requireNonNull(artifactPublisherRedirectorFactory).create(this);
|
67 | 72 | this.artifactStorePublisherFactories = requireNonNull(artifactStorePublisherFactories);
|
68 | 73 | this.artifactStoreComparatorFactories = requireNonNull(artifactStoreComparatorFactories);
|
| 74 | + this.mavenModelReader = requireNonNull(mavenModelReader); |
69 | 75 | }
|
70 | 76 |
|
71 | 77 | @Override
|
@@ -113,8 +119,30 @@ public Collection<ArtifactStoreComparator> availableComparators() {
|
113 | 119 | .collect(Collectors.toList());
|
114 | 120 | }
|
115 | 121 |
|
| 122 | + @Override |
| 123 | + public Optional<Model> readEffectiveModel(Artifact artifact, List<RemoteRepository> remoteRepositories) { |
| 124 | + requireNonNull(artifact); |
| 125 | + requireNonNull(remoteRepositories); |
| 126 | + checkClosed(); |
| 127 | + try { |
| 128 | + ModelResponse response = mavenModelReader.readModel( |
| 129 | + config.session(), |
| 130 | + ModelRequest.builder() |
| 131 | + .setArtifact(artifact) |
| 132 | + .setRepositories(remoteRepositories) |
| 133 | + .setRequestContext("njord") |
| 134 | + .build()); |
| 135 | + return Optional.ofNullable(response.getEffectiveModel()); |
| 136 | + } catch (Exception e) { |
| 137 | + logger.warn("Could not read effective model: {}", e.getMessage()); |
| 138 | + return Optional.empty(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
116 | 142 | @Override
|
117 | 143 | public ArtifactStoreTemplate selectSessionArtifactStoreTemplate(String uri) {
|
| 144 | + requireNonNull(uri); |
| 145 | + checkClosed(); |
118 | 146 | try {
|
119 | 147 | if (!uri.contains(":")) {
|
120 | 148 | if (uri.isEmpty()) {
|
@@ -151,7 +179,7 @@ public ArtifactStoreTemplate selectSessionArtifactStoreTemplate(String uri) {
|
151 | 179 | @Override
|
152 | 180 | public ArtifactStore getOrCreateSessionArtifactStore(String uri) {
|
153 | 181 | requireNonNull(uri);
|
154 |
| - |
| 182 | + checkClosed(); |
155 | 183 | ConcurrentMap<String, String> sessionBoundStore = getSessionBoundStore();
|
156 | 184 | String storeName = sessionBoundStore.computeIfAbsent(uri, k -> {
|
157 | 185 | try {
|
@@ -179,6 +207,12 @@ public ArtifactStore getOrCreateSessionArtifactStore(String uri) {
|
179 | 207 | } else if (uri.startsWith("store:")) {
|
180 | 208 | // store:xxx
|
181 | 209 | artifactStoreName = uri.substring(6);
|
| 210 | + Optional<ArtifactStore> existingStore = |
| 211 | + internalArtifactStoreManager.selectArtifactStore(artifactStoreName); |
| 212 | + if (!existingStore.isPresent()) { |
| 213 | + throw new IllegalArgumentException("Non existing store: " + artifactStoreName); |
| 214 | + } |
| 215 | + existingStore.orElseThrow(J8Utils.OET).close(); |
182 | 216 | } else {
|
183 | 217 | throw new IllegalArgumentException("Invalid repository URI: " + uri);
|
184 | 218 | }
|
@@ -221,6 +255,7 @@ private String createUsingTemplate(String templateName) throws IOException {
|
221 | 255 |
|
222 | 256 | @Override
|
223 | 257 | public int publishSessionArtifactStores() throws IOException {
|
| 258 | + checkClosed(); |
224 | 259 | ConcurrentMap<String, String> sessionBoundStore = getSessionBoundStore();
|
225 | 260 | if (sessionBoundStore.isEmpty()) {
|
226 | 261 | return 0;
|
@@ -252,6 +287,7 @@ public int publishSessionArtifactStores() throws IOException {
|
252 | 287 |
|
253 | 288 | @Override
|
254 | 289 | public int dropSessionArtifactStores() {
|
| 290 | + checkClosed(); |
255 | 291 | ConcurrentMap<String, String> sessionBoundStore = getSessionBoundStore();
|
256 | 292 | if (sessionBoundStore.isEmpty()) {
|
257 | 293 | return 0;
|
|
0 commit comments