Skip to content

Commit 88681b8

Browse files
authored
[3.9.x] Apply resolver changes and improvements (#11536)
Changes: * allow per-request metadata nature * redirect maven-compat update check manager to resolver
1 parent 148fcc8 commit 88681b8

File tree

5 files changed

+39
-122
lines changed

5 files changed

+39
-122
lines changed

maven-compat/src/main/java/org/apache/maven/repository/legacy/DefaultUpdateCheckManager.java

Lines changed: 19 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@
1919
package org.apache.maven.repository.legacy;
2020

2121
import java.io.File;
22-
import java.io.FileInputStream;
23-
import java.io.IOException;
24-
import java.io.RandomAccessFile;
25-
import java.nio.channels.Channels;
26-
import java.nio.channels.FileChannel;
27-
import java.nio.channels.FileLock;
2822
import java.util.Date;
23+
import java.util.HashMap;
2924
import java.util.Properties;
3025

3126
import org.apache.maven.artifact.Artifact;
@@ -35,21 +30,31 @@
3530
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
3631
import org.apache.maven.repository.Proxy;
3732
import org.codehaus.plexus.component.annotations.Component;
33+
import org.codehaus.plexus.component.annotations.Requirement;
3834
import org.codehaus.plexus.logging.AbstractLogEnabled;
3935
import org.codehaus.plexus.logging.Logger;
36+
import org.eclipse.aether.internal.impl.TrackingFileManager;
4037

4138
/**
4239
* DefaultUpdateCheckManager
4340
*/
4441
@Component(role = UpdateCheckManager.class)
4542
public class DefaultUpdateCheckManager extends AbstractLogEnabled implements UpdateCheckManager {
43+
@Requirement
44+
private TrackingFileManager trackingFileManager;
4645

4746
private static final String ERROR_KEY_SUFFIX = ".error";
4847

49-
public DefaultUpdateCheckManager() {}
48+
public DefaultUpdateCheckManager() {
49+
// for plexus
50+
}
5051

51-
public DefaultUpdateCheckManager(Logger logger) {
52+
/**
53+
* For testing purposes.
54+
*/
55+
public DefaultUpdateCheckManager(Logger logger, TrackingFileManager trackingFileManager) {
5256
enableLogging(logger);
57+
this.trackingFileManager = trackingFileManager;
5358
}
5459

5560
public static final String LAST_UPDATE_TAG = ".lastUpdated";
@@ -148,7 +153,7 @@ public void touch(Artifact artifact, ArtifactRepository repository, String error
148153
File touchfile = getTouchfile(artifact);
149154

150155
if (file.exists()) {
151-
touchfile.delete();
156+
trackingFileManager.delete(touchfile);
152157
} else {
153158
writeLastUpdated(touchfile, getRepositoryKey(repository), error);
154159
}
@@ -192,70 +197,10 @@ String getRepositoryKey(ArtifactRepository repository) {
192197
}
193198

194199
private void writeLastUpdated(File touchfile, String key, String error) {
195-
synchronized (touchfile.getAbsolutePath().intern()) {
196-
if (!touchfile.getParentFile().exists()
197-
&& !touchfile.getParentFile().mkdirs()) {
198-
getLogger()
199-
.debug("Failed to create directory: " + touchfile.getParent()
200-
+ " for tracking artifact metadata resolution.");
201-
return;
202-
}
203-
204-
FileChannel channel = null;
205-
FileLock lock = null;
206-
try {
207-
Properties props = new Properties();
208-
209-
channel = new RandomAccessFile(touchfile, "rw").getChannel();
210-
lock = channel.lock();
211-
212-
if (touchfile.canRead()) {
213-
getLogger().debug("Reading resolution-state from: " + touchfile);
214-
props.load(Channels.newInputStream(channel));
215-
}
216-
217-
props.setProperty(key, Long.toString(System.currentTimeMillis()));
218-
219-
if (error != null) {
220-
props.setProperty(key + ERROR_KEY_SUFFIX, error);
221-
} else {
222-
props.remove(key + ERROR_KEY_SUFFIX);
223-
}
224-
225-
getLogger().debug("Writing resolution-state to: " + touchfile);
226-
channel.truncate(0);
227-
props.store(Channels.newOutputStream(channel), "Last modified on: " + new Date());
228-
229-
lock.release();
230-
lock = null;
231-
232-
channel.close();
233-
channel = null;
234-
} catch (IOException e) {
235-
getLogger()
236-
.debug(
237-
"Failed to record lastUpdated information for resolution.\nFile: "
238-
+ touchfile.toString() + "; key: " + key,
239-
e);
240-
} finally {
241-
if (lock != null) {
242-
try {
243-
lock.release();
244-
} catch (IOException e) {
245-
getLogger()
246-
.debug("Error releasing exclusive lock for resolution tracking file: " + touchfile, e);
247-
}
248-
}
249-
250-
if (channel != null) {
251-
try {
252-
channel.close();
253-
} catch (IOException e) {
254-
getLogger().debug("Error closing FileChannel for resolution tracking file: " + touchfile, e);
255-
}
256-
}
257-
}
258-
}
200+
HashMap<String, String> update = new HashMap<>();
201+
update.put(key, Long.toString(System.currentTimeMillis()));
202+
update.put(key + ERROR_KEY_SUFFIX, error); // error==null => remove mapping
203+
trackingFileManager.update(touchfile, update);
259204
}
260205

261206
Date readLastUpdated(File touchfile, String key) {
@@ -284,53 +229,7 @@ private String getError(File touchFile, String key) {
284229
}
285230

286231
private Properties read(File touchfile) {
287-
if (!touchfile.canRead()) {
288-
getLogger().debug("Skipped unreadable resolution tracking file " + touchfile);
289-
return null;
290-
}
291-
292-
synchronized (touchfile.getAbsolutePath().intern()) {
293-
FileInputStream in = null;
294-
FileLock lock = null;
295-
296-
try {
297-
Properties props = new Properties();
298-
299-
in = new FileInputStream(touchfile);
300-
lock = in.getChannel().lock(0, Long.MAX_VALUE, true);
301-
302-
getLogger().debug("Reading resolution-state from: " + touchfile);
303-
props.load(in);
304-
305-
lock.release();
306-
lock = null;
307-
308-
in.close();
309-
in = null;
310-
311-
return props;
312-
} catch (IOException e) {
313-
getLogger().debug("Failed to read resolution tracking file " + touchfile, e);
314-
315-
return null;
316-
} finally {
317-
if (lock != null) {
318-
try {
319-
lock.release();
320-
} catch (IOException e) {
321-
getLogger().debug("Error releasing shared lock for resolution tracking file: " + touchfile, e);
322-
}
323-
}
324-
325-
if (in != null) {
326-
try {
327-
in.close();
328-
} catch (IOException e) {
329-
getLogger().debug("Error closing FileChannel for resolution tracking file: " + touchfile, e);
330-
}
331-
}
332-
}
333-
}
232+
return trackingFileManager.read(touchfile);
334233
}
335234

336235
File getTouchfile(Artifact artifact) {

maven-compat/src/test/java/org/apache/maven/artifact/transform/TransformationManagerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@
2424
import org.apache.maven.repository.legacy.resolver.transform.LatestArtifactTransformation;
2525
import org.apache.maven.repository.legacy.resolver.transform.ReleaseArtifactTransformation;
2626
import org.apache.maven.repository.legacy.resolver.transform.SnapshotTransformation;
27+
import org.codehaus.plexus.ContainerConfiguration;
28+
import org.codehaus.plexus.PlexusConstants;
2729
import org.codehaus.plexus.PlexusTestCase;
2830

2931
/** @author Jason van Zyl */
3032
public class TransformationManagerTest extends PlexusTestCase {
33+
@Override
34+
protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
35+
configuration.setAutoWiring(true);
36+
configuration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
37+
}
38+
3139
public void testTransformationManager() throws Exception {
3240
ArtifactTransformationManager tm = lookup(ArtifactTransformationManager.class);
3341

maven-compat/src/test/java/org/apache/maven/repository/legacy/DefaultUpdateCheckManagerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
2929
import org.codehaus.plexus.logging.Logger;
3030
import org.codehaus.plexus.logging.console.ConsoleLogger;
31+
import org.eclipse.aether.internal.impl.DefaultTrackingFileManager;
3132

3233
public class DefaultUpdateCheckManagerTest extends AbstractArtifactComponentTestCase {
3334

@@ -42,7 +43,8 @@ protected String component() {
4243
protected void setUp() throws Exception {
4344
super.setUp();
4445

45-
updateCheckManager = new DefaultUpdateCheckManager(new ConsoleLogger(Logger.LEVEL_DEBUG, "test"));
46+
updateCheckManager = new DefaultUpdateCheckManager(
47+
new ConsoleLogger(Logger.LEVEL_DEBUG, "test"), new DefaultTrackingFileManager());
4648
}
4749

4850
public void testArtifact() throws Exception {

maven-compat/src/test/java/org/apache/maven/repository/legacy/DefaultWagonManagerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.apache.maven.wagon.events.TransferListener;
4343
import org.apache.maven.wagon.observers.AbstractTransferListener;
4444
import org.apache.maven.wagon.observers.Debug;
45+
import org.codehaus.plexus.ContainerConfiguration;
46+
import org.codehaus.plexus.PlexusConstants;
4547
import org.codehaus.plexus.PlexusTestCase;
4648
import org.codehaus.plexus.util.FileUtils;
4749

@@ -57,6 +59,12 @@ public class DefaultWagonManagerTest extends PlexusTestCase {
5759

5860
private ArtifactRepositoryFactory artifactRepositoryFactory;
5961

62+
@Override
63+
protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
64+
configuration.setAutoWiring(true);
65+
configuration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
66+
}
67+
6068
protected void setUp() throws Exception {
6169
super.setUp();
6270
wagonManager = (DefaultWagonManager) lookup(WagonManager.class);

maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public VersionRangeResult resolveVersionRange(RepositorySystemSession session, V
156156
} else {
157157
Metadata.Nature wantedNature;
158158
String natureString = ConfigUtils.getString(
159-
session, Metadata.Nature.RELEASE_OR_SNAPSHOT.name(), MAVEN_VERSION_RANGE_RESOLUTION_NATURE);
159+
session, request.getNature().name(), MAVEN_VERSION_RANGE_RESOLUTION_NATURE);
160160
if ("auto".equals(natureString)) {
161161
org.eclipse.aether.artifact.Artifact lowerArtifact = lowerBound != null
162162
? request.getArtifact()

0 commit comments

Comments
 (0)