Skip to content

Commit

Permalink
Merge branch 'DanielThomas-max-age'
Browse files Browse the repository at this point in the history
  • Loading branch information
melix committed Oct 4, 2016
2 parents 1b99150 + fef98b6 commit f603ddb
Show file tree
Hide file tree
Showing 34 changed files with 381 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ public class CachedChangingModulesIntegrationTest extends AbstractHttpDependency

when:
server.resetExpectations()
module.metaData.expectGet()
sourceArtifact.expectHead()
module.pom.expectHead()
module.metaData.expectGetRevalidate()
sourceArtifact.expectHeadRevalidate()
module.pom.expectHeadRevalidate()
then:
run 'retrieve'

when:
module.publishWithChangedContent()
server.resetExpectations()

module.metaData.expectGet()
module.pom.sha1.expectGet()
module.pom.expectHead()
module.pom.expectGet()
sourceArtifact.expectHead()
sourceArtifact.expectGet()
sourceArtifact.sha1.expectGet()
module.metaData.expectGetRevalidate()
module.pom.sha1.expectGetRevalidate()
module.pom.expectHeadRevalidate()
module.pom.expectGetRevalidate()
sourceArtifact.expectHeadRevalidate()
sourceArtifact.expectGetRevalidate()
sourceArtifact.sha1.expectGetRevalidate()
then:
run 'retrieve'

Expand Down Expand Up @@ -133,8 +133,8 @@ public class CachedChangingModulesIntegrationTest extends AbstractHttpDependency
when:
server.resetExpectations()
module.metaData.expectGetMissing()
sourceArtifact.expectHead()
module.pom.expectHead()
sourceArtifact.expectHeadRevalidate()
module.pom.expectHeadRevalidate()
then:
run 'retrieve'

Expand All @@ -143,12 +143,12 @@ public class CachedChangingModulesIntegrationTest extends AbstractHttpDependency
server.resetExpectations()

module.metaData.expectGetMissing()
module.pom.sha1.expectGet()
module.pom.expectHead()
module.pom.expectGet()
sourceArtifact.expectHead()
sourceArtifact.expectGet()
sourceArtifact.sha1.expectGet()
module.pom.sha1.expectGetRevalidate()
module.pom.expectHeadRevalidate()
module.pom.expectGetRevalidate()
sourceArtifact.expectHeadRevalidate()
sourceArtifact.expectGetRevalidate()
sourceArtifact.sha1.expectGetRevalidate()
then:
run 'retrieve'

Expand Down Expand Up @@ -200,21 +200,21 @@ public class CachedChangingModulesIntegrationTest extends AbstractHttpDependency

when:
server.resetExpectations()
module.ivy.expectHead()
module.ivy.expectHeadRevalidate()
module.getArtifact(classifier: 'source').expectHead()
then:
run 'retrieve'

when:
module.publishWithChangedContent()
server.resetExpectations()
module.ivy.expectHead()
module.ivy.expectHeadRevalidate()
module.getArtifact(classifier: 'source').expectHead()

module.ivy.sha1.expectGet()
module.ivy.expectGet()
module.getArtifact(classifier: 'source').expectGet()
module.getArtifact(classifier: 'source').sha1.expectGet()
module.ivy.sha1.expectGetRevalidate()
module.ivy.expectGetRevalidate()
module.getArtifact(classifier: 'source').expectGetRevalidate()
module.getArtifact(classifier: 'source').sha1.expectGetRevalidate()

then:
run 'retrieve'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private boolean staticResourceExists(List<ResourcePattern> patternList, ModuleCo
result.attempted(location);
LOGGER.debug("Loading {}", location);
try {
if (repository.getResourceMetaData(location.getUri()) != null) {
if (repository.getResourceMetaData(location.getUri(), true) != null) {
return true;
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public MavenMetadata load(URI metadataLocation) throws ResourceException {
}

private void parseMavenMetadataInfo(final URI metadataLocation, final MavenMetadata metadata) {
ExternalResource resource = repository.getResource(metadataLocation);
ExternalResource resource = repository.getResource(metadataLocation, true);
if (resource == null) {
throw new MissingResourceException(metadataLocation, String.format("Maven meta-data not available: %s", metadataLocation));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ public LocallyAvailableExternalResource getResource(final URI location, final Re

// If we have no caching options, just get the thing directly
if (cached == null && (localCandidates == null || localCandidates.isNone())) {
return copyToCache(location, fileStore, delegate.withProgressLogging().getResource(location));
return copyToCache(location, fileStore, delegate.withProgressLogging().getResource(location, false));
}

// We might be able to use a cached/locally available version
if (cached != null && !externalResourceCachePolicy.mustRefreshExternalResource(getAgeMillis(timeProvider, cached))) {
return new DefaultLocallyAvailableExternalResource(location, new DefaultLocallyAvailableResource(cached.getCachedFile()), cached.getExternalResourceMetaData());
}

// We have a cached version, but it might be out of date, so we tell the upstreams to revalidate too
final boolean revalidate = true;

// Get the metadata first to see if it's there
final ExternalResourceMetaData remoteMetaData = delegate.getResourceMetaData(location);
final ExternalResourceMetaData remoteMetaData = delegate.getResourceMetaData(location, revalidate);
if (remoteMetaData == null) {
return null;
}
Expand Down Expand Up @@ -112,7 +115,7 @@ public ExternalResourceMetaData create() {
HashValue remoteChecksum = remoteMetaData.getSha1();

if (remoteChecksum == null) {
remoteChecksum = getResourceSha1(location);
remoteChecksum = getResourceSha1(location, revalidate);
}

if (remoteChecksum != null) {
Expand All @@ -129,13 +132,13 @@ public ExternalResourceMetaData create() {
}

// All local/cached options failed, get directly
return copyToCache(location, fileStore, delegate.withProgressLogging().getResource(location));
return copyToCache(location, fileStore, delegate.withProgressLogging().getResource(location, revalidate));
}

private HashValue getResourceSha1(URI location) {
private HashValue getResourceSha1(URI location, boolean revalidate) {
try {
URI sha1Location = new URI(location.toASCIIString() + ".sha1");
ExternalResource resource = delegate.getResource(sha1Location);
ExternalResource resource = delegate.getResource(sha1Location, revalidate);
if (resource == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public ProgressLoggingExternalResourceAccessor(ExternalResourceAccessor delegate
this.delegate = delegate;
}

public ExternalResourceReadResponse openResource(URI location) {
ExternalResourceReadResponse resource = delegate.openResource(location);
public ExternalResourceReadResponse openResource(URI location, boolean revalidate) {
ExternalResourceReadResponse resource = delegate.openResource(location, revalidate);
if (resource != null) {
return new ProgressLoggingExternalResource(location, resource);
} else {
Expand All @@ -42,8 +42,8 @@ public ExternalResourceReadResponse openResource(URI location) {
}

@Nullable
public ExternalResourceMetaData getMetaData(URI location) {
return delegate.getMetaData(location);
public ExternalResourceMetaData getMetaData(URI location, boolean revalidate) {
return delegate.getMetaData(location, revalidate);
}

private class ProgressLoggingExternalResource implements ExternalResourceReadResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public ExternalResourceRepository withProgressLogging() {
return new DefaultExternalResourceRepository(name, loggingAccessor, loggingUploader, lister, loggingAccessor, loggingUploader);
}

public ExternalResource getResource(URI source) {
ExternalResourceReadResponse response = accessor.openResource(source);
public ExternalResource getResource(URI source, boolean revalidate) {
ExternalResourceReadResponse response = accessor.openResource(source, revalidate);
return response == null ? null : new DefaultExternalResource(source, response);
}

public ExternalResourceMetaData getResourceMetaData(URI source) {
return accessor.getMetaData(source);
public ExternalResourceMetaData getResourceMetaData(URI source, boolean revalidate) {
return accessor.getMetaData(source, revalidate);
}

public void put(LocalResource source, URI destination) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public interface ExternalResourceRepository {
/**
* Attempts to fetch the given resource.
*
* @param source The location of the resource to obtain
* @param revalidate Ensure the external resource is not stale
* @return null if the resource is not found.
* @throws ResourceException On failure to fetch resource.
*/
@Nullable
ExternalResource getResource(URI source) throws ResourceException;
ExternalResource getResource(URI source, boolean revalidate) throws ResourceException;

/**
* Transfer a resource to the repository
Expand All @@ -54,11 +56,12 @@ public interface ExternalResourceRepository {
* Fetches only the metadata for the result.
*
* @param source The location of the resource to obtain the metadata for
* @param revalidate Ensure the external resource is not stale
* @return The resource metadata, or null if the resource does not exist
* @throws ResourceException On failure to fetch resource metadata.
*/
@Nullable
ExternalResourceMetaData getResourceMetaData(URI source) throws ResourceException;
ExternalResourceMetaData getResourceMetaData(URI source, boolean revalidate) throws ResourceException;

/**
* Return a listing of child resources names.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ public void put(LocalResource source, URI destination) throws IOException {
}
}

public LocallyAvailableExternalResource getResource(URI uri) {
public LocallyAvailableExternalResource getResource(URI uri, boolean revalidate) {
File localFile = getFile(uri);
if (!localFile.exists()) {
return null;
}
return new DefaultLocallyAvailableExternalResource(uri, new DefaultLocallyAvailableResource(localFile));
}

public ExternalResourceMetaData getResourceMetaData(URI location) {
ExternalResource resource = getResource(location);
public ExternalResourceMetaData getResourceMetaData(URI location, boolean revalidate) {
ExternalResource resource = getResource(location, revalidate);
return resource == null ? null : resource.getMetaData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public NoOpCacheAwareExternalResourceAccessor(FileResourceConnector connector) {
}

public LocallyAvailableExternalResource getResource(URI source, ResourceFileStore fileStore, @Nullable LocallyAvailableResourceCandidates localCandidates) throws IOException {
return connector.getResource(source);
return connector.getResource(source, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MavenVersionListerTest extends Specification {
result.attempted == [metaDataResource.toString()]

and:
1 * repository.getResource(metaDataResource) >> resource
1 * repository.getResource(metaDataResource, true) >> resource
1 * resource.withContent(_) >> { Action action -> action.execute(new ByteArrayInputStream("""
<metadata>
<versioning>
Expand Down Expand Up @@ -92,7 +92,7 @@ class MavenVersionListerTest extends Specification {
result.attempted == [location1.toString(), location2.toString()]

and:
1 * repository.getResource(location1) >> resource1
1 * repository.getResource(location1, true) >> resource1
1 * resource1.withContent(_) >> { Action action -> action.execute(new ByteArrayInputStream("""
<metadata>
<versioning>
Expand All @@ -103,7 +103,7 @@ class MavenVersionListerTest extends Specification {
</versioning>
</metadata>""".bytes))
}
1 * repository.getResource(location2) >> resource2
1 * repository.getResource(location2, true) >> resource2
1 * resource2.withContent(_) >> { Action action -> action.execute(new ByteArrayInputStream("""
<metadata>
<versioning>
Expand All @@ -130,7 +130,7 @@ class MavenVersionListerTest extends Specification {
result.attempted == [metaDataResource.toString()]

and:
1 * repository.getResource(metaDataResource) >> resource
1 * repository.getResource(metaDataResource, true) >> resource
1 * resource.withContent(_) >> { Action action -> action.execute(new ByteArrayInputStream("""
<metadata>
<versioning>
Expand Down Expand Up @@ -159,7 +159,7 @@ class MavenVersionListerTest extends Specification {
result.attempted == [metaDataResource.toString()]

and:
1 * repository.getResource(metaDataResource) >> null
1 * repository.getResource(metaDataResource, true) >> null
0 * repository._
}

Expand All @@ -181,7 +181,7 @@ class MavenVersionListerTest extends Specification {

and:
1 * resource.close()
1 * repository.getResource(metaDataResource) >> resource;
1 * repository.getResource(metaDataResource, true) >> resource;
1 * resource.withContent(_) >> { Action action -> action.execute(new ByteArrayInputStream("yo".bytes)) }
0 * repository._
}
Expand All @@ -202,7 +202,7 @@ class MavenVersionListerTest extends Specification {
result.attempted == [metaDataResource.toString()]

and:
1 * repository.getResource(metaDataResource) >> { throw failure }
1 * repository.getResource(metaDataResource, true) >> { throw failure }
0 * repository._
}

Expand Down
Loading

0 comments on commit f603ddb

Please sign in to comment.