Skip to content
Closed
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 @@ -18,14 +18,13 @@
*/
package org.apache.maven.repository.legacy;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;

import org.apache.maven.artifact.Artifact;
Expand All @@ -37,19 +36,29 @@
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.aether.internal.impl.TrackingFileManager;

/**
* DefaultUpdateCheckManager
*/
@Component(role = UpdateCheckManager.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure would I migrate this from plx to sisu; moreover, undoing this change will make other UT changes unneeded (index scanning)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I afraid that I need add scaning to UT as implementation of TrackingFileManager is sisiu

@Named
@Singleton
public class DefaultUpdateCheckManager extends AbstractLogEnabled implements UpdateCheckManager {
private final TrackingFileManager trackingFileManager;

private static final String ERROR_KEY_SUFFIX = ".error";

public DefaultUpdateCheckManager() {}
@Inject
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make/leave this plain old plx component with field injection instead, and as said above, it will leave things less intrusive re UTs

public DefaultUpdateCheckManager(TrackingFileManager trackingFileManager) {
this.trackingFileManager = trackingFileManager;
}

public DefaultUpdateCheckManager(Logger logger) {
/**
* For testing purposes.
*/
public DefaultUpdateCheckManager(Logger logger, TrackingFileManager trackingFileManager) {
enableLogging(logger);
this.trackingFileManager = trackingFileManager;
}

public static final String LAST_UPDATE_TAG = ".lastUpdated";
Expand Down Expand Up @@ -148,7 +157,7 @@ public void touch(Artifact artifact, ArtifactRepository repository, String error
File touchfile = getTouchfile(artifact);

if (file.exists()) {
touchfile.delete();
trackingFileManager.delete(touchfile);
} else {
writeLastUpdated(touchfile, getRepositoryKey(repository), error);
}
Expand Down Expand Up @@ -192,70 +201,10 @@ String getRepositoryKey(ArtifactRepository repository) {
}

private void writeLastUpdated(File touchfile, String key, String error) {
synchronized (touchfile.getAbsolutePath().intern()) {
if (!touchfile.getParentFile().exists()
&& !touchfile.getParentFile().mkdirs()) {
getLogger()
.debug("Failed to create directory: " + touchfile.getParent()
+ " for tracking artifact metadata resolution.");
return;
}

FileChannel channel = null;
FileLock lock = null;
try {
Properties props = new Properties();

channel = new RandomAccessFile(touchfile, "rw").getChannel();
lock = channel.lock();

if (touchfile.canRead()) {
getLogger().debug("Reading resolution-state from: " + touchfile);
props.load(Channels.newInputStream(channel));
}

props.setProperty(key, Long.toString(System.currentTimeMillis()));

if (error != null) {
props.setProperty(key + ERROR_KEY_SUFFIX, error);
} else {
props.remove(key + ERROR_KEY_SUFFIX);
}

getLogger().debug("Writing resolution-state to: " + touchfile);
channel.truncate(0);
props.store(Channels.newOutputStream(channel), "Last modified on: " + new Date());

lock.release();
lock = null;

channel.close();
channel = null;
} catch (IOException e) {
getLogger()
.debug(
"Failed to record lastUpdated information for resolution.\nFile: "
+ touchfile.toString() + "; key: " + key,
e);
} finally {
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
getLogger()
.debug("Error releasing exclusive lock for resolution tracking file: " + touchfile, e);
}
}

if (channel != null) {
try {
channel.close();
} catch (IOException e) {
getLogger().debug("Error closing FileChannel for resolution tracking file: " + touchfile, e);
}
}
}
}
HashMap<String, String> update = new HashMap<>();
update.put(key, Long.toString(System.currentTimeMillis()));
update.put(key + ERROR_KEY_SUFFIX, error); // error==null => remove mapping
trackingFileManager.update(touchfile, update);
}

Date readLastUpdated(File touchfile, String key) {
Expand Down Expand Up @@ -284,53 +233,7 @@ private String getError(File touchFile, String key) {
}

private Properties read(File touchfile) {
if (!touchfile.canRead()) {
getLogger().debug("Skipped unreadable resolution tracking file " + touchfile);
return null;
}

synchronized (touchfile.getAbsolutePath().intern()) {
FileInputStream in = null;
FileLock lock = null;

try {
Properties props = new Properties();

in = new FileInputStream(touchfile);
lock = in.getChannel().lock(0, Long.MAX_VALUE, true);

getLogger().debug("Reading resolution-state from: " + touchfile);
props.load(in);

lock.release();
lock = null;

in.close();
in = null;

return props;
} catch (IOException e) {
getLogger().debug("Failed to read resolution tracking file " + touchfile, e);

return null;
} finally {
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
getLogger().debug("Error releasing shared lock for resolution tracking file: " + touchfile, e);
}
}

if (in != null) {
try {
in.close();
} catch (IOException e) {
getLogger().debug("Error closing FileChannel for resolution tracking file: " + touchfile, e);
}
}
}
}
return trackingFileManager.read(touchfile);
}

File getTouchfile(Artifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@
import org.apache.maven.repository.legacy.resolver.transform.LatestArtifactTransformation;
import org.apache.maven.repository.legacy.resolver.transform.ReleaseArtifactTransformation;
import org.apache.maven.repository.legacy.resolver.transform.SnapshotTransformation;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusTestCase;

/** @author Jason van Zyl */
public class TransformationManagerTest extends PlexusTestCase {

@Override
protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
configuration.setAutoWiring(true);
configuration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
}

public void testTransformationManager() throws Exception {
ArtifactTransformationManager tm = lookup(ArtifactTransformationManager.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.eclipse.aether.internal.impl.DefaultTrackingFileManager;

public class DefaultUpdateCheckManagerTest extends AbstractArtifactComponentTestCase {

Expand All @@ -42,7 +43,7 @@ protected String component() {
protected void setUp() throws Exception {
super.setUp();

updateCheckManager = new DefaultUpdateCheckManager(new ConsoleLogger(Logger.LEVEL_DEBUG, "test"));
updateCheckManager = new DefaultUpdateCheckManager(new ConsoleLogger(Logger.LEVEL_DEBUG, "test"), new DefaultTrackingFileManager());
}

public void testArtifact() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.apache.maven.wagon.events.TransferListener;
import org.apache.maven.wagon.observers.AbstractTransferListener;
import org.apache.maven.wagon.observers.Debug;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;

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

private ArtifactRepositoryFactory artifactRepositoryFactory;

@Override
protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
configuration.setAutoWiring(true);
configuration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
}

protected void setUp() throws Exception {
super.setUp();
wagonManager = (DefaultWagonManager) lookup(WagonManager.class);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ under the License.
<securityDispatcherVersion>2.0</securityDispatcherVersion>
<cipherVersion>2.0</cipherVersion>
<jxpathVersion>1.4.0</jxpathVersion>
<resolverVersion>1.9.24</resolverVersion>
<resolverVersion>1.9.25-SNAPSHOT</resolverVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<xmlunitVersion>2.11.0</xmlunitVersion>
<powermockVersion>2.0.9</powermockVersion>
Expand Down
Loading