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-8239] Add missing ModelCacheFactory #1728

Merged
merged 2 commits into from
Sep 18, 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
Expand Up @@ -146,9 +146,6 @@ public interface ModelBuilderRequest {
@Nonnull
ModelRepositoryHolder getModelRepositoryHolder();

@Nullable
ModelCache getModelCache();

@Nullable
Object getListener();

Expand Down Expand Up @@ -210,7 +207,6 @@ class ModelBuilderRequestBuilder {
Map<String, String> userProperties;
ModelResolver modelResolver;
ModelRepositoryHolder modelRepositoryHolder;
ModelCache modelCache;
Object listener;
ModelBuilderResult interimResult;
ModelTransformerContextBuilder transformerContextBuilder;
Expand All @@ -233,7 +229,6 @@ class ModelBuilderRequestBuilder {
this.userProperties = request.getUserProperties();
this.modelResolver = request.getModelResolver();
this.modelRepositoryHolder = request.getModelRepositoryHolder();
this.modelCache = request.getModelCache();
this.listener = request.getListener();
this.interimResult = request.getInterimResult();
this.transformerContextBuilder = request.getTransformerContextBuilder();
Expand Down Expand Up @@ -310,11 +305,6 @@ public ModelBuilderRequestBuilder modelRepositoryHolder(ModelRepositoryHolder mo
return this;
}

public ModelBuilderRequestBuilder modelCache(ModelCache modelCache) {
this.modelCache = modelCache;
return this;
}

public ModelBuilderRequestBuilder listener(Object listener) {
this.listener = listener;
return this;
Expand Down Expand Up @@ -352,7 +342,6 @@ public ModelBuilderRequest build() {
userProperties,
modelResolver,
modelRepositoryHolder,
modelCache,
listener,
interimResult,
transformerContextBuilder,
Expand All @@ -373,7 +362,6 @@ private static class DefaultModelBuilderRequest extends BaseRequest implements M
private final Map<String, String> userProperties;
private final ModelResolver modelResolver;
private final ModelRepositoryHolder modelRepositoryHolder;
private final ModelCache modelCache;
private final Object listener;
private final ModelBuilderResult interimResult;
private final ModelTransformerContextBuilder transformerContextBuilder;
Expand All @@ -395,7 +383,6 @@ private static class DefaultModelBuilderRequest extends BaseRequest implements M
Map<String, String> userProperties,
ModelResolver modelResolver,
ModelRepositoryHolder modelRepositoryHolder,
ModelCache modelCache,
Object listener,
ModelBuilderResult interimResult,
ModelTransformerContextBuilder transformerContextBuilder,
Expand All @@ -415,7 +402,6 @@ private static class DefaultModelBuilderRequest extends BaseRequest implements M
this.userProperties = userProperties != null ? Map.copyOf(userProperties) : session.getUserProperties();
this.modelResolver = modelResolver;
this.modelRepositoryHolder = modelRepositoryHolder;
this.modelCache = modelCache;
this.listener = listener;
this.interimResult = interimResult;
this.transformerContextBuilder = transformerContextBuilder;
Expand Down Expand Up @@ -487,11 +473,6 @@ public ModelRepositoryHolder getModelRepositoryHolder() {
return modelRepositoryHolder;
}

@Override
public ModelCache getModelCache() {
return modelCache;
}

public Object getListener() {
return listener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ModelSource resolve(ModelLocator locator, String relative) {

@Override
public boolean equals(Object o) {
return this == o || o instanceof PathSource ps && Objects.equals(path, ps.path);
return this == o || o.getClass() == getClass() && Objects.equals(path, ((PathSource) o).path);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,33 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.services;
package org.apache.maven.api.services.model;

import java.util.function.Supplier;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.ThreadSafe;
import org.apache.maven.api.services.Source;

/**
* Caches auxiliary data used during model building like already processed raw/effective models. The data in the cache
* is meant for exclusive consumption by the model builder and is opaque to the cache implementation. The cache key is
* formed by a combination of group id, artifact id, version and tag. The first three components generally refer to the
* identity of a model. The tag allows for further classification of the associated data on the sole discretion of the
* model builder.
* formed by a combination of group id, artifact id, version and tag, or by the pom path on the filesystem and tag.
* The tag allows for further classification of the associated data on the sole discretion of the model builder.
* The cache is expected to be valid through the lifetime of the session, so the model builder is not allowed to
* store data which may change during the session, especially effective models which may be different if the
* user properties or activate profiles change between two invocations of the model builder.
* The cache implementation is expected to be thread-safe.
*
* @since 4.0.0
*/
@Experimental
@ThreadSafe
public interface ModelCache {

<T> T computeIfAbsent(String groupId, String artifactId, String version, String tag, Supplier<T> data);

<T> T computeIfAbsent(Source path, String tag, Supplier<T> data);

void clear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.services.model;

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

/**
* Factory for creating model caches.
* <p>
* The model cache is meant for exclusive consumption by the model builder and is opaque to the cache implementation.
* The cache is created once per session and is valid through the lifetime of the session.
* <p>
* The cache implementation could be annotated with {@code SessionScoped} to be created once per session, but
* this would make tests more complicated to write as they would all need to enter the session scope.
* This is similar to the {@code CIFriendlyVersionModelTransformer}.
*
* @since 4.0.0
*/
@Experimental
public interface ModelCacheFactory {

@Nonnull
ModelCache newInstance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.SessionData;
import org.apache.maven.api.Type;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.annotations.Nullable;
Expand All @@ -67,7 +68,6 @@
import org.apache.maven.api.services.ModelBuilderException;
import org.apache.maven.api.services.ModelBuilderRequest;
import org.apache.maven.api.services.ModelBuilderResult;
import org.apache.maven.api.services.ModelCache;
import org.apache.maven.api.services.ModelProblem;
import org.apache.maven.api.services.ModelProblemCollector;
import org.apache.maven.api.services.ModelResolver;
Expand All @@ -86,6 +86,8 @@
import org.apache.maven.api.services.model.LifecycleBindingsInjector;
import org.apache.maven.api.services.model.ModelBuildingEvent;
import org.apache.maven.api.services.model.ModelBuildingListener;
import org.apache.maven.api.services.model.ModelCache;
import org.apache.maven.api.services.model.ModelCacheFactory;
import org.apache.maven.api.services.model.ModelInterpolator;
import org.apache.maven.api.services.model.ModelNormalizer;
import org.apache.maven.api.services.model.ModelPathTranslator;
Expand All @@ -101,7 +103,6 @@
import org.apache.maven.api.services.model.WorkspaceModelResolver;
import org.apache.maven.api.services.xml.XmlReaderException;
import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.internal.impl.resolver.DefaultModelCache;
import org.apache.maven.internal.impl.resolver.DefaultModelRepositoryHolder;
import org.apache.maven.internal.impl.resolver.DefaultModelResolver;
import org.apache.maven.model.v4.MavenTransformer;
Expand Down Expand Up @@ -145,6 +146,7 @@ public class DefaultModelBuilder implements ModelBuilder {
private final ModelTransformer transformer;
private final ModelVersionParser versionParser;
private final List<org.apache.maven.api.spi.ModelTransformer> transformers;
private final ModelCacheFactory modelCacheFactory;

@SuppressWarnings("checkstyle:ParameterNumber")
@Inject
Expand All @@ -167,7 +169,8 @@ public DefaultModelBuilder(
ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator,
ModelTransformer transformer,
ModelVersionParser versionParser,
List<org.apache.maven.api.spi.ModelTransformer> transformers) {
List<org.apache.maven.api.spi.ModelTransformer> transformers,
ModelCacheFactory modelCacheFactory) {
this.modelProcessor = modelProcessor;
this.modelValidator = modelValidator;
this.modelNormalizer = modelNormalizer;
Expand All @@ -187,6 +190,7 @@ public DefaultModelBuilder(
this.transformer = transformer;
this.versionParser = versionParser;
this.transformers = transformers;
this.modelCacheFactory = modelCacheFactory;
}

@Override
Expand All @@ -206,9 +210,6 @@ public ModelBuilderResult build(ModelBuilderRequest request) throws ModelBuilder

private static ModelBuilderRequest fillRequestDefaults(ModelBuilderRequest request) {
ModelBuilderRequest.ModelBuilderRequestBuilder builder = ModelBuilderRequest.builder(request);
if (request.getModelCache() == null) {
builder.modelCache(new DefaultModelCache());
}
if (request.getModelRepositoryHolder() == null) {
builder.modelRepositoryHolder(new DefaultModelRepositoryHolder(
request.getSession(),
Expand Down Expand Up @@ -1465,7 +1466,6 @@ private Model doLoadDependencyManagement(
.userProperties(request.getUserProperties())
.source(importSource)
.modelResolver(modelResolver)
.modelCache(request.getModelCache())
.modelRepositoryHolder(
request.getModelRepositoryHolder().copy())
.twoPhaseBuilding(false)
Expand All @@ -1486,21 +1486,11 @@ private Model doLoadDependencyManagement(

private static <T> T cache(
ModelCache cache, String groupId, String artifactId, String version, String tag, Callable<T> supplier) {
Supplier<T> s = asSupplier(supplier);
if (cache == null) {
return s.get();
} else {
return cache.computeIfAbsent(groupId, artifactId, version, tag, s);
}
return cache.computeIfAbsent(groupId, artifactId, version, tag, asSupplier(supplier));
}

private static <T> T cache(ModelCache cache, Source source, String tag, Callable<T> supplier) {
Supplier<T> s = asSupplier(supplier);
if (cache == null) {
return s.get();
} else {
return cache.computeIfAbsent(source, tag, s);
}
return cache.computeIfAbsent(source, tag, asSupplier(supplier));
}

private static <T> Supplier<T> asSupplier(Callable<T> supplier) {
Expand Down Expand Up @@ -1557,8 +1547,10 @@ ModelProcessor getModelProcessor() {
return modelProcessor;
}

private static ModelCache getModelCache(ModelBuilderRequest request) {
return request.getModelCache();
private ModelCache getModelCache(ModelBuilderRequest request) {
return request.getSession()
.getData()
.computeIfAbsent(SessionData.key(ModelCache.class), modelCacheFactory::newInstance);
}

private static ModelBuildingListener getModelBuildingListener(ModelBuilderRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.internal.impl.resolver;
package org.apache.maven.internal.impl.model;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;

import org.apache.maven.api.services.ModelCache;
import org.apache.maven.api.services.Source;
import org.eclipse.aether.RepositoryCache;
import org.eclipse.aether.RepositorySystemSession;
import org.apache.maven.api.services.model.ModelCache;

import static java.util.Objects.requireNonNull;

Expand All @@ -35,25 +33,6 @@
*
*/
public class DefaultModelCache implements ModelCache {
private static final String KEY = DefaultModelCache.class.getName();

@SuppressWarnings("unchecked")
public static ModelCache newInstance(RepositorySystemSession session, boolean anew) {
ConcurrentHashMap<Object, Supplier<?>> cache;
RepositoryCache repositoryCache = session != null ? session.getCache() : null;
if (repositoryCache == null) {
return new DefaultModelCache(new ConcurrentHashMap<>());
} else {
if (anew) {
cache = new ConcurrentHashMap<>();
repositoryCache.put(session, KEY, cache);
} else {
cache = (ConcurrentHashMap<Object, Supplier<?>>)
repositoryCache.computeIfAbsent(session, KEY, ConcurrentHashMap::new);
}
return new DefaultModelCache(cache);
}
}

private final ConcurrentMap<Object, Supplier<?>> cache;

Expand All @@ -77,6 +56,11 @@ public <T> T computeIfAbsent(Source path, String tag, Supplier<T> data) {
return (T) computeIfAbsent(new SourceCacheKey(path, tag), data);
}

@Override
public void clear() {
cache.clear();
}

protected Object computeIfAbsent(Object key, Supplier<?> data) {
return cache.computeIfAbsent(key, k -> new CachingSupplier<>(data)).get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.internal.impl.model;

import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.services.model.ModelCache;
import org.apache.maven.api.services.model.ModelCacheFactory;

@Named
@Singleton
public class DefaultModelCacheFactory implements ModelCacheFactory {
@Override
public ModelCache newInstance() {
return new DefaultModelCache();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ private Model loadPom(
.userProperties(Map.of())
.modelResolver(modelResolver)
.modelRepositoryHolder(modelRepositoryHolder)
.modelCache(DefaultModelCache.newInstance(session, false))
.repositories(repositories)
.build();

Expand Down
Loading