Skip to content

Commit

Permalink
Merge branch '2.7.x' into 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Mar 15, 2023
2 parents 5d21c36 + aea57fb commit dcb41b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.logging.Log;

import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.log.LogMessage;

/**
* Imports {@link ConfigData} by {@link ConfigDataLocationResolver resolving} and
Expand Down Expand Up @@ -117,16 +118,20 @@ private Map<ConfigDataResolutionResult, ConfigData> load(ConfigDataLoaderContext
ConfigDataResolutionResult candidate = candidates.get(i);
ConfigDataLocation location = candidate.getLocation();
ConfigDataResource resource = candidate.getResource();
this.logger.trace(LogMessage.format("Considering resource %s from location %s", resource, location));
if (resource.isOptional()) {
this.optionalLocations.add(location);
}
if (this.loaded.contains(resource)) {
this.logger
.trace(LogMessage.format("Already loaded resource %s ignoring location %s", resource, location));
this.loadedLocations.add(location);
}
else {
try {
ConfigData loaded = this.loaders.load(loaderContext, resource);
if (loaded != null) {
this.logger.trace(LogMessage.format("Loaded resource %s from location %s", resource, location));
this.loaded.add(resource);
this.loadedLocations.add(location);
result.put(candidate, loaded);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,10 @@

package org.springframework.boot.context.config;

import java.io.File;
import java.io.IOException;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -96,12 +98,21 @@ public boolean equals(Object obj) {
return false;
}
StandardConfigDataResource other = (StandardConfigDataResource) obj;
return this.resource.equals(other.resource) && this.emptyDirectory == other.emptyDirectory;
return (this.emptyDirectory == other.emptyDirectory) && isSameUnderlyingResource(this.resource, other.resource);
}

private boolean isSameUnderlyingResource(Resource ours, Resource other) {
return ours.equals(other) || isSameFile(getUnderlyingFile(ours), getUnderlyingFile(other));
}

private boolean isSameFile(File ours, File other) {
return (ours != null) && (other != null) && ours.equals(other);
}

@Override
public int hashCode() {
return this.resource.hashCode();
File underlyingFile = getUnderlyingFile(this.resource);
return (underlyingFile != null) ? underlyingFile.hashCode() : this.resource.hashCode();
}

@Override
Expand All @@ -116,4 +127,17 @@ public String toString() {
return this.resource.toString();
}

private File getUnderlyingFile(Resource resource) {
try {
if (resource instanceof ClassPathResource || resource instanceof FileSystemResource
|| resource instanceof FileUrlResource) {
File file = resource.getFile();
return (file != null) ? file.getAbsoluteFile() : null;
}
}
catch (IOException ex) {
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package org.springframework.boot.context.config;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -66,4 +69,15 @@ void equalsWhenResourceIsDifferentReturnsFalse() {
assertThat(location).isNotEqualTo(other);
}

@Test // gh-34212
void equalsAndHashCodeWhenSameUnderlyingResource() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("log4j2.springboot");
FileUrlResource fileUrlResource = new FileUrlResource(classPathResource.getURL());
ConfigDataResource classPathConfigDataResource = new StandardConfigDataResource(this.reference,
classPathResource);
ConfigDataResource fileUrlConfigDataResource = new StandardConfigDataResource(this.reference, fileUrlResource);
assertThat(classPathConfigDataResource.hashCode()).isEqualTo(fileUrlConfigDataResource.hashCode());
assertThat(classPathConfigDataResource).isEqualTo(fileUrlConfigDataResource);
}

}

0 comments on commit dcb41b0

Please sign in to comment.