[MNG-8001] Expose CoreRealm internally (#1369)

Expose internally (not for plugin API) CoreRealm.

---

https://issues.apache.org/jira/browse/MNG-8001
This commit is contained in:
Tamas Cservenak 2024-01-10 13:53:42 +01:00 committed by GitHub
parent 076b346c0c
commit 2c06637fb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 104 additions and 9 deletions

View File

@ -36,10 +36,9 @@ import java.util.TreeMap;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.classrealm.ClassRealmRequest.RealmType;
import org.apache.maven.extension.internal.CoreExports;
import org.apache.maven.internal.CoreRealm;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.codehaus.plexus.MutablePlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
@ -87,9 +86,9 @@ public class DefaultClassRealmManager implements ClassRealmManager {
@Inject
public DefaultClassRealmManager(
PlexusContainer container, List<ClassRealmManagerDelegate> delegates, CoreExports exports) {
this.world = ((MutablePlexusContainer) container).getClassWorld();
this.containerRealm = container.getContainerRealm();
CoreRealm coreRealm, List<ClassRealmManagerDelegate> delegates, CoreExports exports) {
this.world = coreRealm.getClassWorld();
this.containerRealm = coreRealm.getRealm();
this.delegates = delegates;
Map<String, ClassLoader> foreignImports = exports.getExportedPackages();

View File

@ -25,7 +25,7 @@ import javax.inject.Singleton;
import java.util.Objects;
import org.codehaus.plexus.PlexusContainer;
import org.apache.maven.internal.CoreRealm;
/**
* CoreExportsProvider
@ -37,8 +37,8 @@ public class CoreExportsProvider implements Provider<CoreExports> {
private final CoreExports exports;
@Inject
public CoreExportsProvider(PlexusContainer container) {
this(new CoreExports(CoreExtensionEntry.discoverFrom(container.getContainerRealm())));
public CoreExportsProvider(CoreRealm coreRealm) {
this(new CoreExports(CoreExtensionEntry.discoverFrom(coreRealm.getRealm())));
}
public CoreExportsProvider(CoreExports exports) {

View File

@ -0,0 +1,51 @@
/*
* 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;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
/**
* Access to core {@link ClassRealm}.
*
* @since 4.0.0
*/
@Experimental
public interface CoreRealm {
/**
* Obtain the {@link ClassRealm} used for Maven Core.
*
* @return the class realm of core.
*/
@Nonnull
ClassRealm getRealm();
/**
* Shorthand method to obtain the {@link ClassWorld} used for Maven Core.
*
* @return the class world in use.
*/
@Nonnull
default ClassWorld getClassWorld() {
return getRealm().getWorld();
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.internal;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.maven.internal.CoreRealm;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
@Named
@Singleton
public class DefaultCoreRealm implements CoreRealm {
private final PlexusContainer container;
@Inject
public DefaultCoreRealm(PlexusContainer container) {
this.container = container;
}
@Override
public ClassRealm getRealm() {
return container.getContainerRealm();
}
}

View File

@ -24,6 +24,7 @@ import java.util.HashSet;
import java.util.List;
import org.apache.maven.extension.internal.CoreExports;
import org.apache.maven.internal.impl.internal.DefaultCoreRealm;
import org.apache.maven.model.Model;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
@ -56,7 +57,7 @@ class DefaultClassRealmManagerTest {
exportedPackages.add("group1:artifact1");
return new DefaultClassRealmManager(
container,
new DefaultCoreRealm(container),
new ArrayList<ClassRealmManagerDelegate>(),
new CoreExports(new ClassRealm(null, "test", null), new HashSet<String>(), exportedPackages));
}