Skip to content

Commit b91905a

Browse files
authored
SOLR-16903: Make java.io.File a forbidden API (#3321)
Add java.io.File as a forbidden api and replacing existing usages of File with Path
1 parent 2041d1d commit b91905a

21 files changed

Lines changed: 105 additions & 47 deletions

File tree

gradle/validation/forbidden-apis/defaults.all.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,7 @@ java.util.Locale#<init>(**)
9999
java.nio.file.Paths#get(**)
100100

101101
@defaultMessage You probably meant to call String.startsWith
102-
java.nio.file.Path#startsWith(java.lang.String)
102+
java.nio.file.Path#startsWith(java.lang.String)
103+
104+
@defaultMessage Use NIO Path instead of File
105+
java.io.File

solr/CHANGES.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,10 @@ Other Changes
166166

167167
* SOLR-17321: Minimum Java version for Apache Solr is now 21, and for SolrJ, it is 17. (Sanjay Dutt, David Smiley)
168168

169-
* SOLR-16903: Update CLI tools and Solr Core to use java.nio.file.Path instead of java.io.File (Andrey Bozhko, Matthew Biscocho)
169+
* SOLR-16903: Replace all java.io.File usages to java.nio.file.Path (NIO) (Andrey Bozhko, Matthew Biscocho)
170170

171171
* SOLR-17568: SolrCloud no longer reroutes/proxies a core request to another node if not found locally. (David Smiley)
172172

173-
* SOLR-17548: Switch all public Java APIs from File to Path. (Matthew Biscocho via Eric Pugh)
174-
175173
* SOLR-17675: Remove DirContext param from DirectoryFactory.create method. (Bruno Roustant, David Smiley)
176174

177175
* SOLR-16903: Switch CoreContainer#getSolrHome to return Path instead of String (Andrey Bozhko)

solr/core/src/java/org/apache/solr/core/CoreContainer.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import jakarta.inject.Singleton;
3636
import java.io.IOException;
3737
import java.lang.invoke.MethodHandles;
38+
import java.nio.file.Files;
3839
import java.nio.file.Path;
3940
import java.text.SimpleDateFormat;
4041
import java.util.Arrays;
@@ -1002,28 +1003,67 @@ private void loadInternal() {
10021003
Path dataHome =
10031004
cfg.getSolrDataHome() != null ? cfg.getSolrDataHome() : cfg.getCoreRootDirectory();
10041005
solrMetricsContext.gauge(
1005-
() -> dataHome.toFile().getTotalSpace(),
1006+
() -> {
1007+
try {
1008+
return Files.getFileStore(dataHome).getTotalSpace();
1009+
} catch (IOException e) {
1010+
throw new SolrException(
1011+
ErrorCode.SERVER_ERROR,
1012+
"Error retrieving total space for data home directory" + dataHome,
1013+
e);
1014+
}
1015+
},
10061016
true,
10071017
"totalSpace",
10081018
SolrInfoBean.Category.CONTAINER.toString(),
10091019
"fs");
1020+
10101021
solrMetricsContext.gauge(
1011-
() -> dataHome.toFile().getUsableSpace(),
1022+
() -> {
1023+
try {
1024+
return Files.getFileStore(dataHome).getUsableSpace();
1025+
} catch (IOException e) {
1026+
throw new SolrException(
1027+
ErrorCode.SERVER_ERROR,
1028+
"Error retrieving usable space for data home directory" + dataHome,
1029+
e);
1030+
}
1031+
},
10121032
true,
10131033
"usableSpace",
10141034
SolrInfoBean.Category.CONTAINER.toString(),
10151035
"fs");
10161036
solrMetricsContext.gauge(
10171037
dataHome::toString, true, "path", SolrInfoBean.Category.CONTAINER.toString(), "fs");
10181038
solrMetricsContext.gauge(
1019-
() -> cfg.getCoreRootDirectory().toFile().getTotalSpace(),
1039+
() -> {
1040+
try {
1041+
return Files.getFileStore(cfg.getCoreRootDirectory()).getTotalSpace();
1042+
} catch (IOException e) {
1043+
throw new SolrException(
1044+
SolrException.ErrorCode.SERVER_ERROR,
1045+
"Error retrieving total space for core root directory: "
1046+
+ cfg.getCoreRootDirectory(),
1047+
e);
1048+
}
1049+
},
10201050
true,
10211051
"totalSpace",
10221052
SolrInfoBean.Category.CONTAINER.toString(),
10231053
"fs",
10241054
"coreRoot");
10251055
solrMetricsContext.gauge(
1026-
() -> cfg.getCoreRootDirectory().toFile().getUsableSpace(),
1056+
() -> {
1057+
try {
1058+
return Files.getFileStore(cfg.getCoreRootDirectory()).getUsableSpace();
1059+
} catch (IOException e) {
1060+
throw new SolrException(
1061+
SolrException.ErrorCode.SERVER_ERROR,
1062+
"Error retrieving usable space for core root directory: "
1063+
+ cfg.getCoreRootDirectory(),
1064+
e);
1065+
}
1066+
},
10271067
true,
10281068
"usableSpace",
10291069
SolrInfoBean.Category.CONTAINER.toString(),

solr/core/src/java/org/apache/solr/filestore/DistribFileStore.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,12 @@ public MetaData getMetaData() {
285285

286286
@Override
287287
public Date getTimeStamp() {
288-
return new Date(realPath().toFile().lastModified());
288+
try {
289+
return new Date(Files.getLastModifiedTime(realPath()).toMillis());
290+
} catch (IOException e) {
291+
throw new SolrException(
292+
SERVER_ERROR, "Failed to retrieve the last modified time for: " + realPath(), e);
293+
}
289294
}
290295

291296
@Override
@@ -295,7 +300,12 @@ public boolean isDir() {
295300

296301
@Override
297302
public long size() {
298-
return realPath().toFile().length();
303+
try {
304+
return Files.size(realPath());
305+
} catch (IOException e) {
306+
throw new SolrException(
307+
SERVER_ERROR, "Failed to retrieve the file size for: " + realPath(), e);
308+
}
299309
}
300310

301311
@Override

solr/core/src/java/org/apache/solr/packagemanager/DefaultPackageRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.stream.Collectors;
3232
import org.apache.commons.io.FileUtils;
3333
import org.apache.commons.io.FilenameUtils;
34+
import org.apache.commons.io.file.PathUtils;
3435
import org.apache.solr.common.SolrException;
3536
import org.apache.solr.common.SolrException.ErrorCode;
3637
import org.slf4j.Logger;
@@ -80,7 +81,7 @@ public boolean hasPackage(String packageName) {
8081
@Override
8182
public Path download(String artifactName) throws SolrException, IOException {
8283
Path tmpDirectory = Files.createTempDirectory("solr-packages");
83-
tmpDirectory.toFile().deleteOnExit();
84+
PathUtils.deleteOnExit(tmpDirectory);
8485
URL url = getRepoUri().resolve(artifactName).toURL();
8586
String fileName = FilenameUtils.getName(url.getPath());
8687
Path destination = tmpDirectory.resolve(fileName);

solr/core/src/java/org/apache/solr/update/UpdateLog.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.concurrent.atomic.AtomicInteger;
5656
import java.util.concurrent.atomic.AtomicReference;
5757
import java.util.stream.Stream;
58+
import org.apache.commons.io.file.PathUtils;
5859
import org.apache.lucene.store.Directory;
5960
import org.apache.lucene.util.BytesRef;
6061
import org.apache.solr.common.SolrDocumentBase;
@@ -2439,7 +2440,7 @@ public static void deleteFile(Path file) {
24392440

24402441
if (!success) {
24412442
try {
2442-
file.toFile().deleteOnExit();
2443+
PathUtils.deleteOnExit(file);
24432444
} catch (Exception e) {
24442445
log.error("Error deleting file on exit: {}", file, e);
24452446
}

solr/core/src/test/org/apache/solr/cli/SolrCLIZkToolsTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public void testDownconfig() throws Exception {
146146
verifyZkLocalPathsMatch(tmp.resolve("conf"), "/configs/downconfig2");
147147
// And insure the empty file is a text file
148148
Path destEmpty = tmp2.resolve("conf").resolve("stopwords").resolve("emptyfile");
149-
assertTrue("Empty files should NOT be copied down as directories", destEmpty.toFile().isFile());
149+
assertTrue(
150+
"Empty files should NOT be copied down as directories", Files.isRegularFile(destEmpty));
150151
}
151152

152153
@Test
@@ -326,7 +327,7 @@ public void testCp() throws Exception {
326327

327328
// Next, copy cp7 down and verify that zknode.data exists for cp7
328329
Path zData = tmp.resolve("conf/stopwords/zknode.data");
329-
assertTrue("znode.data should have been copied down", zData.toFile().exists());
330+
assertTrue("znode.data should have been copied down", Files.exists(zData));
330331

331332
// Finally, copy up to cp8 and verify that the data is up there.
332333
args = new String[] {"cp", "--recursive", "--zk-host", zkAddr, "file:" + tmp, "zk:/cp9"};
@@ -385,7 +386,8 @@ public void testCp() throws Exception {
385386
assertEquals("Copy should have succeeded.", 0, res);
386387

387388
Path locEmpty = tmp2.resolve("stopwords/emptyfile");
388-
assertTrue("Empty files should NOT be copied down as directories", locEmpty.toFile().isFile());
389+
assertTrue(
390+
"Empty files should NOT be copied down as directories", Files.isRegularFile(locEmpty));
389391
}
390392

391393
@Test

solr/core/src/test/org/apache/solr/cli/SolrProcessManagerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public static void beforeClass() throws Exception {
5656
long processHttpsValue = isWindows ? processHttps.getKey() : processHttps.getValue().pid();
5757
SolrProcessManager.enableTestingMode = true;
5858
System.setProperty("jetty.port", Integer.toString(processHttp.getKey()));
59-
Path pidDir = Files.createTempDirectory("solr-pid-dir").toAbsolutePath();
60-
pidDir.toFile().deleteOnExit();
59+
Path pidDir = createTempDir("solr-pid-dir");
6160
System.setProperty("solr.pid.dir", pidDir.toString());
6261
Files.writeString(
6362
pidDir.resolve("solr-" + processHttpValue + PID_SUFFIX), Long.toString(processHttpValue));

solr/core/src/test/org/apache/solr/cloud/ShardRoutingCustomTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ private void doCustomSharding() throws Exception {
6161
Files.createDirectories(jettyDir);
6262
setupJettySolrHome(jettyDir);
6363
JettySolrRunner j =
64-
createJetty(
65-
jettyDir, createTempDir().toFile().getAbsolutePath(), "shardA", "solrconfig.xml", null);
64+
createJetty(jettyDir, createTempDir().toString(), "shardA", "solrconfig.xml", null);
6665
j.start();
6766
assertEquals(
6867
0,

solr/core/src/test/org/apache/solr/cloud/TestConfigSetsAPI.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ private void setupBaseConfigSet(String baseConfigSetName, Map<String, String> ol
207207
throws Exception {
208208
final Path configDir = getFile("solr").resolve("configsets/configset-2/conf");
209209
final Path tmpConfigDir = createTempDir();
210-
tmpConfigDir.toFile().deleteOnExit();
211210
PathUtils.copyDirectory(configDir, tmpConfigDir);
212211
if (oldProps != null) {
213212
Files.writeString(
@@ -1568,7 +1567,6 @@ public void testDeleteErrors() throws Exception {
15681567
final SolrClient solrClient = getHttpSolrClient(baseUrl);
15691568
final Path configDir = getFile("solr").resolve("configsets/configset-2/conf");
15701569
final Path tmpConfigDir = createTempDir();
1571-
tmpConfigDir.toFile().deleteOnExit();
15721570
// Ensure ConfigSet is immutable
15731571
PathUtils.copyDirectory(configDir, tmpConfigDir);
15741572
Files.writeString(

0 commit comments

Comments
 (0)