18
18
*/
19
19
20
20
/*
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.
22
22
* Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected] >.
23
23
* Portions Copyright (c) 2019, Chris Ross <[email protected] >.
24
24
*/
29
29
import java .io .OutputStream ;
30
30
import java .util .ArrayList ;
31
31
import java .util .List ;
32
+ import java .util .function .Supplier ;
32
33
import java .util .logging .Level ;
33
34
import java .util .logging .Logger ;
34
35
35
36
import org .opengrok .indexer .configuration .CommandTimeoutType ;
36
37
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
37
38
import org .opengrok .indexer .logger .LoggerFactory ;
38
39
import org .opengrok .indexer .util .Executor ;
40
+ import org .opengrok .indexer .util .LazilyInstantiate ;
39
41
40
42
/**
41
43
* Access to a Perforce repository.
@@ -56,6 +58,12 @@ public class PerforceRepository extends Repository {
56
58
*/
57
59
public static final String CMD_FALLBACK = "p4" ;
58
60
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
+
59
67
public PerforceRepository () {
60
68
type = "Perforce" ;
61
69
@@ -200,11 +208,19 @@ boolean isRepositoryFor(File file, CommandTimeoutType cmdType) {
200
208
return isInP4Depot (file , cmdType );
201
209
}
202
210
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
+
203
219
@ Override
204
220
public boolean isWorking () {
205
221
if (working == null ) {
222
+ working = P4_IS_WORKING .get ();
206
223
ensureCommand (CMD_PROPERTY_KEY , CMD_FALLBACK );
207
- working = checkCmd (RepoCommand , "help" );
208
224
}
209
225
return working ;
210
226
}
0 commit comments