Skip to content

Commit

Permalink
Spring bean instead of static singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Sep 21, 2023
1 parent 9944be3 commit c9ff126
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.sbm.parsers;

import jakarta.annotation.PostConstruct;
import org.apache.maven.graph.GraphBuilder;
import org.codehaus.plexus.*;
import org.codehaus.plexus.classworlds.ClassWorld;
Expand All @@ -25,56 +26,57 @@

import java.net.URL;

@Lazy
class MavenPlexusContainer {

public GraphBuilder lookup(Class<GraphBuilder> aClass) {
try {
return ContainerHolder.INSTANCE.lookup(aClass);
} catch (ComponentLookupException e) {
throw new RuntimeException(e);
}
}

private static class ContainerHolder {
private static final PlexusContainer INSTANCE = create();
public static PlexusContainer create() {
try {
ClassLoader parent = null;
boolean isContainerAutoWiring = false;
String containerClassPathScanning = "on";
String containerComponentVisibility = PlexusConstants.GLOBAL_VISIBILITY;
URL overridingComponentsXml = null; //getClass().getClassLoader().getResource("META-INF/**/components.xml");
private PlexusContainer plexusContainer;

ContainerConfiguration configuration = new DefaultContainerConfiguration();
configuration.setAutoWiring(isContainerAutoWiring)
.setClassPathScanning(containerClassPathScanning)
.setComponentVisibility(containerComponentVisibility)
.setContainerConfigurationURL(overridingComponentsXml);
public MavenPlexusContainer() {

// inspired from https://github.com/jenkinsci/lib-jenkins-maven-embedder/blob/master/src/main/java/hudson/maven/MavenEmbedderUtils.java#L141
ClassWorld classWorld = new ClassWorld();
ClassRealm classRealm = new ClassRealm(classWorld, "maven", PlexusContainer.class.getClassLoader());
ClassLoader effectiveParent = parent == null ? Thread.currentThread().getContextClassLoader() : parent;
ClassRealm parentRealm = new ClassRealm(classWorld, "maven-parent", effectiveParent);
classRealm.setParentRealm(parentRealm);
configuration.setRealm(classRealm);
}

configuration.setClassWorld(classWorld);
DefaultPlexusContainer container = new DefaultPlexusContainer(configuration);
container.setLookupRealm(classRealm);
@PostConstruct
public void postConstruct() {
plexusContainer = create();
}

return container;
} catch (PlexusContainerException e) {
throw new RuntimeException(e);
}
public GraphBuilder lookup(Class<GraphBuilder> aClass) {
try {
return plexusContainer.lookup(aClass);
} catch (ComponentLookupException e) {
throw new RuntimeException(e);
}
}

@Deprecated
public PlexusContainer get() {
return ContainerHolder.INSTANCE;
return plexusContainer;
}

public PlexusContainer create() {
try {
ClassLoader parent = null;
boolean isContainerAutoWiring = false;
String containerClassPathScanning = PlexusConstants.SCANNING_ON;
String containerComponentVisibility = null; //PlexusConstants.GLOBAL_VISIBILITY;
URL overridingComponentsXml = null; //getClass().getClassLoader().getResource("META-INF/**/components.xml");

ContainerConfiguration configuration = new DefaultContainerConfiguration();
configuration.setAutoWiring(isContainerAutoWiring)
.setClassPathScanning(containerClassPathScanning)
.setComponentVisibility(containerComponentVisibility)
.setContainerConfigurationURL(overridingComponentsXml);

// inspired from https://github.com/jenkinsci/lib-jenkins-maven-embedder/blob/master/src/main/java/hudson/maven/MavenEmbedderUtils.java#L141
ClassWorld classWorld = new ClassWorld();
ClassRealm classRealm = new ClassRealm(classWorld, "maven", PlexusContainer.class.getClassLoader());
ClassLoader effectiveParent = parent == null ? Thread.currentThread().getContextClassLoader() : parent;
ClassRealm parentRealm = new ClassRealm(classWorld, "maven-parent", effectiveParent);
classRealm.setParentRealm(parentRealm);
configuration.setRealm(classRealm);
configuration.setClassWorld(classWorld);
return new DefaultPlexusContainer(configuration);
} catch (PlexusContainerException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.sbm.boot.autoconfigure.ParserPropertiesPostProcessor;
import org.springframework.sbm.boot.autoconfigure.ScannerConfiguration;
import org.springframework.sbm.parsers.events.RewriteParsingEventListenerAdapter;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class RewriteParserConfiguration {
private ParserProperties parserProperties;

@Bean
@Lazy
MavenPlexusContainer plexusContainer() {
return new MavenPlexusContainer();
}
Expand Down

0 comments on commit c9ff126

Please sign in to comment.