Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRESOLVER-414] Fully split update policies internally #342

Merged
merged 4 commits into from
Oct 15, 2023
Merged
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 @@ -86,6 +86,11 @@ public String getUpdatePolicy() {
return getSession().getUpdatePolicy();
}

@Override
public String getArtifactUpdatePolicy() {
return getSession().getArtifactUpdatePolicy();
}

@Override
public String getMetadataUpdatePolicy() {
return getSession().getMetadataUpdatePolicy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public final class DefaultRepositorySystemSession implements RepositorySystemSes

private String checksumPolicy;

private String updatePolicy;
private String artifactUpdatePolicy;

private String metadataUpdatePolicy;

Expand Down Expand Up @@ -269,22 +269,52 @@ public DefaultRepositorySystemSession setChecksumPolicy(String checksumPolicy) {

@Override
public String getUpdatePolicy() {
return updatePolicy;
return getArtifactUpdatePolicy();
}

/**
* Sets the global update policy. If set, the global update policy overrides the update policies of the remote
* repositories being used for resolution.
* <p>
* This method is meant for code that does not want to distinguish between artifact and metadata policies.
* Note: applications should either use get/set updatePolicy (this method and
* {@link RepositorySystemSession#getUpdatePolicy()}) or also distinguish between artifact and
* metadata update policies (and use other methods), but <em>should not mix the two!</em>
*
* @param updatePolicy The global update policy, may be {@code null}/empty to apply the per-repository policies.
* @return This session for chaining, never {@code null}.
* @see RepositoryPolicy#UPDATE_POLICY_ALWAYS
* @see RepositoryPolicy#UPDATE_POLICY_DAILY
* @see RepositoryPolicy#UPDATE_POLICY_NEVER
* @see #setArtifactUpdatePolicy(String)
* @see #setMetadataUpdatePolicy(String)
*/
public DefaultRepositorySystemSession setUpdatePolicy(String updatePolicy) {
verifyStateForMutation();
this.updatePolicy = updatePolicy;
setArtifactUpdatePolicy(updatePolicy);
setMetadataUpdatePolicy(updatePolicy);
return this;
}

@Override
public String getArtifactUpdatePolicy() {
return artifactUpdatePolicy;
}

/**
* Sets the global artifact update policy. If set, the global update policy overrides the artifact update policies
* of the remote repositories being used for resolution.
*
* @param artifactUpdatePolicy The global update policy, may be {@code null}/empty to apply the per-repository policies.
* @return This session for chaining, never {@code null}.
* @see RepositoryPolicy#UPDATE_POLICY_ALWAYS
* @see RepositoryPolicy#UPDATE_POLICY_DAILY
* @see RepositoryPolicy#UPDATE_POLICY_NEVER
* @since TBD
*/
public DefaultRepositorySystemSession setArtifactUpdatePolicy(String artifactUpdatePolicy) {
verifyStateForMutation();
this.artifactUpdatePolicy = artifactUpdatePolicy;
return this;
}

Expand All @@ -294,8 +324,8 @@ public String getMetadataUpdatePolicy() {
}

/**
* Sets the global metadata update policy. If set, the global update policy overrides the update policies of the remote
* repositories being used for resolution.
* Sets the global metadata update policy. If set, the global update policy overrides the metadata update policies
* of the remote repositories being used for resolution.
*
* @param metadataUpdatePolicy The global update policy, may be {@code null}/empty to apply the per-repository policies.
* @return This session for chaining, never {@code null}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,29 @@ public interface RepositorySystemSession {
String getChecksumPolicy();

/**
* Gets the global data update policy. If set, the global update policy overrides the update policies of the remote
* repositories being used for resolution.
* Gets the global update policy, or {@code null} if not set.
* <p>
* This method is meant for code that does not want to distinguish between artifact and metadata policies.
* Note: applications should either use get/set updatePolicy (this method and
* {@link DefaultRepositorySystemSession#setUpdatePolicy(String)}) or also distinguish between artifact and
* metadata update policies (and use other methods), but <em>should not mix the two!</em>
*
* @see #getArtifactUpdatePolicy()
* @see #getMetadataUpdatePolicy()
*/
String getUpdatePolicy();

/**
* Gets the global artifact update policy. If set, the global update policy overrides the update policies of the
* remote repositories being used for resolution.
*
* @return The global update policy or {@code null}/empty if not set and the per-repository policies apply.
* @see RepositoryPolicy#UPDATE_POLICY_ALWAYS
* @see RepositoryPolicy#UPDATE_POLICY_DAILY
* @see RepositoryPolicy#UPDATE_POLICY_NEVER
* @since TBD
*/
String getUpdatePolicy();
String getArtifactUpdatePolicy();

/**
* Gets the global metadata update policy. If set, the global update policy overrides the update policies of the remote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public final class RepositoryPolicy {

private final boolean enabled;

private final String updatePolicy;
private final String artifactUpdatePolicy;

private final String metadataUpdatePolicy;

Expand All @@ -85,16 +85,17 @@ public RepositoryPolicy(boolean enabled, String updatePolicy, String checksumPol
* Creates a new policy with the specified settings.
*
* @param enabled A flag whether the associated repository should be accessed or not.
* @param updatePolicy The update interval after which locally cached data from the repository is considered stale
* @param artifactUpdatePolicy The update interval after which locally cached data from the repository is considered stale
* and should be re-fetched, may be {@code null}.
* @param metadataUpdatePolicy The update interval after which locally cached metadata from the repository is considered stale
* and should be re-fetched, may be {@code null}.
* @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
* @since TBD
*/
public RepositoryPolicy(boolean enabled, String updatePolicy, String metadataUpdatePolicy, String checksumPolicy) {
public RepositoryPolicy(
boolean enabled, String artifactUpdatePolicy, String metadataUpdatePolicy, String checksumPolicy) {
this.enabled = enabled;
this.updatePolicy = (updatePolicy != null) ? updatePolicy : "";
this.artifactUpdatePolicy = (artifactUpdatePolicy != null) ? artifactUpdatePolicy : "";
this.metadataUpdatePolicy = (metadataUpdatePolicy != null) ? metadataUpdatePolicy : "";
this.checksumPolicy = (checksumPolicy != null) ? checksumPolicy : "";
}
Expand All @@ -109,12 +110,28 @@ public boolean isEnabled() {
}

/**
* Gets the update policy for locally cached data from the repository.
* This method is not used in Resolver, as resolver internally strictly distinguishes between artifact and metadata
* update policies.
*
* @return The update policy, never {@code null}.
* @see #getArtifactUpdatePolicy()
* @see #getMetadataUpdatePolicy()
* @deprecated This method should not be used. Since version 2 Resolver internally distinguishes between artifact
* update policy and metadata update policy. This method was left only to preserve binary compatibility, and in
* reality invokes {@link #getArtifactUpdatePolicy()}.
*/
@Deprecated
public String getUpdatePolicy() {
return updatePolicy;
return getArtifactUpdatePolicy();
}

/**
* Gets the update policy for locally cached artifacts from the repository.
*
* @return The update policy, never {@code null}.
* @since TBD
*/
public String getArtifactUpdatePolicy() {
return artifactUpdatePolicy;
}

/**
Expand All @@ -140,7 +157,7 @@ public String getChecksumPolicy() {
public String toString() {
return "enabled=" + isEnabled()
+ ", checksums=" + getChecksumPolicy()
+ ", updates=" + getUpdatePolicy()
+ ", artifactUpdates=" + getArtifactUpdatePolicy()
+ ", metadataUpdates=" + getMetadataUpdatePolicy();
}

Expand All @@ -157,15 +174,17 @@ public boolean equals(Object obj) {
RepositoryPolicy that = (RepositoryPolicy) obj;

return enabled == that.enabled
&& updatePolicy.equals(that.updatePolicy)
&& artifactUpdatePolicy.equals(that.artifactUpdatePolicy)
&& metadataUpdatePolicy.equals(that.metadataUpdatePolicy)
&& checksumPolicy.equals(that.checksumPolicy);
}

@Override
public int hashCode() {
int hash = 17;
hash = hash * 31 + (enabled ? 1 : 0);
hash = hash * 31 + updatePolicy.hashCode();
hash = hash * 31 + artifactUpdatePolicy.hashCode();
hash = hash * 31 + metadataUpdatePolicy.hashCode();
hash = hash * 31 + checksumPolicy.hashCode();
return hash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class UpdateCheck<T, E extends RepositoryException> {

private boolean fileValid = true;

private String policy;
private String artifactPolicy;

private String metadataPolicy;

Expand Down Expand Up @@ -149,13 +149,14 @@ public UpdateCheck<T, E> setFileValid(boolean fileValid) {
}

/**
* Gets the policy to use for the data check.
* Gets the policy to use for the artifact check.
*
* @return The policy to use for the data check.
* @return The policy to use for the artifact check.
* @see org.eclipse.aether.repository.RepositoryPolicy
* @since TBD
*/
public String getPolicy() {
return policy;
public String getArtifactPolicy() {
return artifactPolicy;
}

/**
Expand All @@ -170,19 +171,20 @@ public String getMetadataPolicy() {
}

/**
* Sets the policy to use for the check.
* Sets the artifact policy to use for the check.
*
* @param policy The policy to use for the data check, may be {@code null}.
* @param artifactPolicy The policy to use for the artifact check, may be {@code null}.
* @return This object for chaining.
* @see org.eclipse.aether.repository.RepositoryPolicy
* @since TBD
*/
public UpdateCheck<T, E> setPolicy(String policy) {
this.policy = policy;
public UpdateCheck<T, E> setArtifactPolicy(String artifactPolicy) {
this.artifactPolicy = artifactPolicy;
return this;
}

/**
* Sets the policy to use for the check.
* Sets the metadata policy to use for the check.
*
* @param metadataPolicy The policy to use for the metadata check, may be {@code null}.
* @return This object for chaining.
Expand Down Expand Up @@ -282,6 +284,6 @@ public UpdateCheck<T, E> setException(E exception) {

@Override
public String toString() {
return getPolicy() + "/" + getMetadataPolicy() + ": " + getFile() + " < " + getRepository();
return getArtifactPolicy() + "/" + getMetadataPolicy() + ": " + getFile() + " < " + getRepository();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ private List<ArtifactDownload> gatherDownloads(RepositorySystemSession session,
check.setFile(download.getFile());
check.setFileValid(false);
check.setRepository(group.repository);
check.setPolicy(policy.getUpdatePolicy());
check.setArtifactPolicy(policy.getArtifactUpdatePolicy());
check.setMetadataPolicy(policy.getMetadataUpdatePolicy());
item.updateCheck = check;
updateCheckManager.checkArtifact(session, check);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private List<MetadataResult> resolve(
check.setFile(checkFile);
check.setRepository(repository);
check.setAuthoritativeRepository(repo);
check.setPolicy(policy.getUpdatePolicy());
check.setArtifactPolicy(policy.getArtifactUpdatePolicy());
check.setMetadataPolicy(policy.getMetadataUpdatePolicy());

if (lrmResult.isStale()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private RepositoryPolicy merge(
if (globalPolicy) {
policy = merge(
policy1,
session.getUpdatePolicy(),
session.getArtifactUpdatePolicy(),
session.getMetadataUpdatePolicy(),
session.getChecksumPolicy());
} else {
Expand All @@ -248,7 +248,7 @@ private RepositoryPolicy merge(
if (globalPolicy) {
policy = merge(
policy2,
session.getUpdatePolicy(),
session.getArtifactUpdatePolicy(),
session.getMetadataUpdatePolicy(),
session.getChecksumPolicy());
} else {
Expand All @@ -258,7 +258,7 @@ private RepositoryPolicy merge(
if (globalPolicy) {
policy = merge(
policy1,
session.getUpdatePolicy(),
session.getArtifactUpdatePolicy(),
session.getMetadataUpdatePolicy(),
session.getChecksumPolicy());
} else {
Expand All @@ -268,7 +268,7 @@ private RepositoryPolicy merge(
if (globalPolicy) {
policy = merge(
policy2,
session.getUpdatePolicy(),
session.getArtifactUpdatePolicy(),
session.getMetadataUpdatePolicy(),
session.getChecksumPolicy());
} else {
Expand All @@ -284,13 +284,13 @@ private RepositoryPolicy merge(
session, policy1.getChecksumPolicy(), policy2.getChecksumPolicy());
}

String updates = session.getUpdatePolicy();
String artifactUpdates = session.getArtifactUpdatePolicy();
//noinspection StatementWithEmptyBody
if (globalPolicy && updates != null && !updates.isEmpty()) {
if (globalPolicy && artifactUpdates != null && !artifactUpdates.isEmpty()) {
// use global override
} else {
updates = updatePolicyAnalyzer.getEffectiveUpdatePolicy(
session, policy1.getUpdatePolicy(), policy2.getUpdatePolicy());
artifactUpdates = updatePolicyAnalyzer.getEffectiveUpdatePolicy(
session, policy1.getArtifactUpdatePolicy(), policy2.getArtifactUpdatePolicy());
}
String metadataUpdates = session.getMetadataUpdatePolicy();
if (globalPolicy && metadataUpdates != null && !metadataUpdates.isEmpty()) {
Expand All @@ -300,27 +300,28 @@ private RepositoryPolicy merge(
session, policy1.getMetadataUpdatePolicy(), policy2.getMetadataUpdatePolicy());
}

policy = new RepositoryPolicy(true, updates, metadataUpdates, checksums);
policy = new RepositoryPolicy(true, artifactUpdates, metadataUpdates, checksums);
}

return policy;
}

private RepositoryPolicy merge(RepositoryPolicy policy, String updates, String metadataUpdates, String checksums) {
private RepositoryPolicy merge(
RepositoryPolicy policy, String artifactUpdates, String metadataUpdates, String checksums) {
if (policy != null) {
if (updates == null || updates.isEmpty()) {
updates = policy.getUpdatePolicy();
if (artifactUpdates == null || artifactUpdates.isEmpty()) {
artifactUpdates = policy.getArtifactUpdatePolicy();
}
if (metadataUpdates == null || metadataUpdates.isEmpty()) {
metadataUpdates = policy.getMetadataUpdatePolicy();
}
if (checksums == null || checksums.isEmpty()) {
checksums = policy.getChecksumPolicy();
}
if (!policy.getUpdatePolicy().equals(updates)
if (!policy.getArtifactUpdatePolicy().equals(artifactUpdates)
|| !policy.getMetadataUpdatePolicy().equals(metadataUpdates)
|| !policy.getChecksumPolicy().equals(checksums)) {
policy = new RepositoryPolicy(policy.isEnabled(), updates, metadataUpdates, checksums);
policy = new RepositoryPolicy(policy.isEnabled(), artifactUpdates, metadataUpdates, checksums);
}
}
return policy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public DefaultUpdateCheckManager(
public void checkArtifact(RepositorySystemSession session, UpdateCheck<Artifact, ArtifactTransferException> check) {
requireNonNull(session, "session cannot be null");
requireNonNull(check, "check cannot be null");
final String updatePolicy = check.getPolicy();
final String updatePolicy = check.getArtifactPolicy();
if (check.getLocalLastUpdated() != 0
&& !isUpdatedRequired(session, check.getLocalLastUpdated(), updatePolicy)) {
LOGGER.debug("Skipped remote request for {}, locally installed artifact up-to-date", check.getItem());
Expand Down
Loading
Loading