Skip to content

Commit 2294d62

Browse files
committed
Register the maven protocol handler as a DS component
1 parent 060eba3 commit 2294d62

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

org.eclipse.m2e.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Import-Package: com.google.common.base;version="30.1.0",
5858
org.slf4j;version="[1.7.0,3.0.0)"
5959
Automatic-Module-Name: org.eclipse.m2e.core
6060
Service-Component: OSGI-INF/org.eclipse.m2e.core.embedder.MavenModelManager.xml,
61+
OSGI-INF/org.eclipse.m2e.core.internal.MvnProtocolHandlerService.xml,
6162
OSGI-INF/org.eclipse.m2e.core.internal.embedder.EclipseLoggerManager.xml,
6263
OSGI-INF/org.eclipse.m2e.core.internal.embedder.MavenImpl.xml,
6364
OSGI-INF/org.eclipse.m2e.core.internal.embedder.PlexusContainerManager.xml,

org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/MavenPluginActivator.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import org.osgi.framework.BundleListener;
2424
import org.osgi.framework.Constants;
2525
import org.osgi.framework.FrameworkUtil;
26-
import org.osgi.framework.ServiceRegistration;
2726
import org.osgi.framework.Version;
28-
import org.osgi.service.url.URLConstants;
29-
import org.osgi.service.url.URLStreamHandlerService;
3027
import org.osgi.util.tracker.ServiceTracker;
3128

3229
import org.eclipse.aether.RepositorySystem;
@@ -66,8 +63,6 @@ public class MavenPluginActivator extends Plugin {
6663

6764
private final BundleListener bundleListener = event -> LifecycleMappingFactory.setBundleMetadataSources(null);
6865

69-
private ServiceRegistration<URLStreamHandlerService> protocolHandlerService;
70-
7166
private Map<Class<?>, ServiceTracker<?, ?>> trackers = new ConcurrentHashMap<>();
7267

7368
public IMaven getMaven() {
@@ -110,11 +105,6 @@ public void start(final BundleContext context) throws Exception {
110105
URLConnectionCaches.disable();
111106
// For static access, this also enables any of the services and keep them running forever...
112107
this.bundleContext = context;
113-
//register URL handler, we can't use DS here because this triggers loading of m2e too early
114-
Map<String, Object> properties = Map.of(URLConstants.URL_HANDLER_PROTOCOL, new String[] {"mvn"});
115-
this.protocolHandlerService = context.registerService(URLStreamHandlerService.class,
116-
new MvnProtocolHandlerService(), FrameworkUtil.asDictionary(properties));
117-
118108
}
119109

120110
/**
@@ -123,9 +113,6 @@ public void start(final BundleContext context) throws Exception {
123113
@Override
124114
public void stop(BundleContext context) throws Exception {
125115
super.stop(context);
126-
if(protocolHandlerService != null) {
127-
protocolHandlerService.unregister();
128-
}
129116
context.removeBundleListener(bundleListener);
130117

131118
toDisposeContainers.forEach(PlexusContainer::dispose);

org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/MvnProtocolHandlerService.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
import java.net.URLConnection;
2323
import java.util.List;
2424

25+
import org.osgi.service.component.annotations.Component;
26+
import org.osgi.service.component.annotations.Reference;
2527
import org.osgi.service.url.AbstractURLStreamHandlerService;
28+
import org.osgi.service.url.URLConstants;
29+
import org.osgi.service.url.URLStreamHandlerService;
2630

2731
import org.eclipse.aether.RepositorySystem;
2832
import org.eclipse.aether.RepositorySystemSession;
@@ -40,21 +44,24 @@
4044
import org.apache.maven.RepositoryUtils;
4145
import org.apache.maven.artifact.repository.ArtifactRepository;
4246

43-
import org.eclipse.m2e.core.MavenPlugin;
4447
import org.eclipse.m2e.core.embedder.ICallable;
4548
import org.eclipse.m2e.core.embedder.IMaven;
4649
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
4750

4851

49-
//@Component(service = URLStreamHandlerService.class, property = URLConstants.URL_HANDLER_PROTOCOL + "=mvn", immediate = true)
52+
@Component(service = URLStreamHandlerService.class, property = URLConstants.URL_HANDLER_PROTOCOL + "=mvn")
5053
public class MvnProtocolHandlerService extends AbstractURLStreamHandlerService {
5154

52-
public MvnProtocolHandlerService() {
53-
}
55+
@Reference
56+
IMaven maven;
5457

5558
@Override
5659
public URLConnection openConnection(URL url) {
57-
return new MavenURLConnection(url);
60+
//TODO replace reference with IMaven maven = MavenPlugin.getMaven();
61+
//this is required to make the component active even if m2e itself is not running at the moment
62+
//that will make the m2e protocol available from the start of eclipse
63+
//See https://github.com/eclipse-equinox/equinox/pull/290
64+
return new MavenURLConnection(url, maven);
5865
}
5966

6067
private static final class MavenURLConnection extends URLConnection {
@@ -63,8 +70,11 @@ private static final class MavenURLConnection extends URLConnection {
6370

6471
private ArtifactResult artifactResult;
6572

66-
protected MavenURLConnection(URL url) {
73+
private IMaven maven;
74+
75+
protected MavenURLConnection(URL url, IMaven maven) {
6776
super(url);
77+
this.maven = maven;
6878
}
6979

7080
@Override
@@ -92,9 +102,7 @@ public void connect() throws IOException {
92102
String classifier = coordinates.length > 4 ? coordinates[4] : null;
93103
Artifact artifact = new DefaultArtifact(coordinates[0], coordinates[1], classifier, type, coordinates[2]);
94104
try {
95-
IMaven maven = MavenPlugin.getMaven();
96-
RepositorySystem repoSystem = org.eclipse.m2e.core.internal.MavenPluginActivator.getDefault()
97-
.getRepositorySystem();
105+
RepositorySystem repoSystem = maven.lookup(RepositorySystem.class);
98106
IMavenExecutionContext context = maven.createExecutionContext();
99107
List<ArtifactRepository> artifactRepositories = maven.getArtifactRepositories();
100108
List<RemoteRepository> remoteRepositories = RepositoryUtils.toRepos(artifactRepositories);

0 commit comments

Comments
 (0)