Skip to content

Commit 18e21bd

Browse files
committed
use lazy instantiation of the working state of Perforce
1 parent d452bab commit 18e21bd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/PerforceRepository.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2323
* Portions Copyright (c) 2019, Chris Ross <[email protected]>.
2424
*/
@@ -29,13 +29,15 @@
2929
import java.io.OutputStream;
3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.function.Supplier;
3233
import java.util.logging.Level;
3334
import java.util.logging.Logger;
3435

3536
import org.opengrok.indexer.configuration.CommandTimeoutType;
3637
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3738
import org.opengrok.indexer.logger.LoggerFactory;
3839
import org.opengrok.indexer.util.Executor;
40+
import org.opengrok.indexer.util.LazilyInstantiate;
3941

4042
/**
4143
* Access to a Perforce repository.
@@ -56,6 +58,12 @@ public class PerforceRepository extends Repository {
5658
*/
5759
public static final String CMD_FALLBACK = "p4";
5860

61+
/**
62+
* This is a static replacement for 'working' field. Effectively, check if p4 is working once in a JVM
63+
* instead of calling it for every {@code PerforceRepository} instance.
64+
*/
65+
private static final Supplier<Boolean> P4_IS_WORKING = LazilyInstantiate.using(PerforceRepository::isP4Working);
66+
5967
public PerforceRepository() {
6068
type = "Perforce";
6169

@@ -200,11 +208,19 @@ boolean isRepositoryFor(File file, CommandTimeoutType cmdType) {
200208
return isInP4Depot(file, cmdType);
201209
}
202210

211+
private static boolean isP4Working() {
212+
String repoCommand = getCommand(PerforceRepository.class, CMD_PROPERTY_KEY, CMD_FALLBACK);
213+
boolean works = checkCmd(repoCommand, "help");
214+
LOGGER.log(Level.WARNING, "Command ''{0}'' does not work. " +
215+
"Some operations with Perforce repositories will fail as a result.", repoCommand);
216+
return works;
217+
}
218+
203219
@Override
204220
public boolean isWorking() {
205221
if (working == null) {
222+
working = P4_IS_WORKING.get();
206223
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
207-
working = checkCmd(RepoCommand, "help");
208224
}
209225
return working;
210226
}

0 commit comments

Comments
 (0)