@@ -141,6 +141,13 @@ public final class Indexer {
141
141
private static OptionParser optParser = null ;
142
142
private static boolean verbose = false ;
143
143
144
+ private static final String [] ON_OFF = {ON , OFF };
145
+ private static final String [] REMOTE_REPO_CHOICES = {ON , OFF , DIRBASED , UIONLY };
146
+ private static final String [] LUCENE_LOCKS = {ON , OFF , "simple" , "native" };
147
+ private static final String OPENGROK_JAR = "opengrok.jar" ;
148
+
149
+ private static final int WEBAPP_CONNECT_TIMEOUT = 1000 ; // in milliseconds
150
+
144
151
public static Indexer getInstance () {
145
152
return index ;
146
153
}
@@ -161,12 +168,10 @@ public static void main(String[] argv) {
161
168
162
169
boolean createDict = false ;
163
170
164
- int CONNECT_TIMEOUT = 1000 ; // in milliseconds
165
-
166
171
try {
167
172
argv = parseOptions (argv );
168
173
169
- if (webappURI != null && !HostUtil .isReachable (webappURI , CONNECT_TIMEOUT )) {
174
+ if (webappURI != null && !HostUtil .isReachable (webappURI , WEBAPP_CONNECT_TIMEOUT )) {
170
175
System .err .println (webappURI + " is not reachable." );
171
176
System .exit (1 );
172
177
}
@@ -236,17 +241,16 @@ public static void main(String[] argv) {
236
241
canonicalRoots .addAll (cfg .getCanonicalRoots ());
237
242
cfg .setCanonicalRoots (canonicalRoots );
238
243
239
- // Assemble the unprocessed command line arguments (possibly
240
- // a list of paths). This will be used to perform more fine
241
- // grained checking in invalidateRepositories().
244
+ // Assemble the unprocessed command line arguments (possibly a list of paths).
245
+ // This will be used to perform more fine-grained checking in invalidateRepositories().
242
246
for (String arg : argv ) {
243
247
String path = Paths .get (cfg .getSourceRoot (), arg ).toString ();
244
248
subFilesList .add (path );
245
249
}
246
250
247
- // If an user used customizations for projects he perhaps just
251
+ // If a user used customizations for projects he perhaps just
248
252
// used the key value for project without a name but the code
249
- // expects a name for the project. Therefore we fill the name
253
+ // expects a name for the project. Therefore, we fill the name
250
254
// according to the project key which is the same.
251
255
for (Entry <String , Project > entry : cfg .getProjects ().entrySet ()) {
252
256
if (entry .getValue ().getName () == null ) {
@@ -257,8 +261,8 @@ public static void main(String[] argv) {
257
261
// Check version of index(es) versus current Lucene version and exit
258
262
// with return code upon failure.
259
263
if (checkIndex ) {
260
- if (cfg == null ) {
261
- System .err .println ("Need configuration to check index (use -R)" );
264
+ if (cfg . getDataRoot () == null || cfg . getDataRoot (). isEmpty () ) {
265
+ System .err .println ("Need data root in configuration for index check (use -R)" );
262
266
System .exit (1 );
263
267
}
264
268
@@ -374,7 +378,7 @@ public static void main(String[] argv) {
374
378
375
379
writeConfigToFile (env , configFilename );
376
380
377
- // Finally ping webapp to refresh indexes in the case of partial reindex
381
+ // Finally, ping webapp to refresh indexes in the case of partial reindex
378
382
// or send new configuration to the web application in the case of full reindex.
379
383
if (webappURI != null ) {
380
384
if (!subFiles .isEmpty ()) {
@@ -413,19 +417,15 @@ public static void main(String[] argv) {
413
417
*/
414
418
public static String [] parseOptions (String [] argv ) throws ParseException {
415
419
final String [] usage = {HELP_OPT_1 };
416
- final String program = "opengrok.jar" ;
417
- final String [] ON_OFF = {ON , OFF };
418
- final String [] REMOTE_REPO_CHOICES = {ON , OFF , DIRBASED , UIONLY };
419
- final String [] LUCENE_LOCKS = {ON , OFF , "simple" , "native" };
420
420
421
421
if (argv .length == 0 ) {
422
422
argv = usage ; // will force usage output
423
423
status = 1 ; // with non-zero EXIT STATUS
424
424
}
425
425
426
426
/*
427
- * Pre-match any of the --help options so that some possible exception-
428
- * generating args handlers (e.g. -R) can be short-circuited.
427
+ * Pre-match any of the --help options so that some possible exception-generating args handlers (e.g. -R)
428
+ * can be short-circuited.
429
429
*/
430
430
boolean preHelp = Arrays .stream (argv ).anyMatch (s -> HELP_OPT_1 .equals (s ) ||
431
431
HELP_OPT_2 .equals (s ) || HELP_OPT_3 .equals (s ));
@@ -448,8 +448,7 @@ public static String[] parseOptions(String[] argv) throws ParseException {
448
448
// Limit usage lines to 72 characters for concise formatting.
449
449
450
450
optParser = OptionParser .execute (parser -> {
451
- parser .setPrologue (
452
- String .format ("\n Usage: java -jar %s [options] [subDir1 [...]]%n" , program ));
451
+ parser .setPrologue (String .format ("%nUsage: java -jar %s [options] [subDir1 [...]]%n" , OPENGROK_JAR ));
453
452
454
453
parser .on (HELP_OPT_3 , HELP_OPT_2 , HELP_OPT_1 , "=[mode]" ,
455
454
"With no mode specified, display this usage summary. Or specify a mode:" ,
@@ -813,7 +812,7 @@ public static String[] parseOptions(String[] argv) throws ParseException {
813
812
814
813
parser .on ("--userPage" , "=URL" ,
815
814
"Base URL of the user Information provider." ,
816
- "Example: \" http ://www.myserver .org/viewProfile.jspa?username=\" ." ,
815
+ "Example: \" https ://www.example .org/viewProfile.jspa?username=\" ." ,
817
816
"Use \" none\" to disable link." ).execute (v -> cfg .setUserPage ((String ) v ));
818
817
819
818
parser .on ("--userPageSuffix" , "=URL-suffix" ,
@@ -864,7 +863,7 @@ private static void checkConfiguration() {
864
863
die ("Missing webappURI setting" );
865
864
}
866
865
867
- if (repositories .size () > 0 && !cfg .isHistoryEnabled ()) {
866
+ if (! repositories .isEmpty () && !cfg .isHistoryEnabled ()) {
868
867
die ("Repositories were specified; history is off however" );
869
868
}
870
869
@@ -965,9 +964,6 @@ public void prepareIndexer(RuntimeEnvironment env,
965
964
* generated for repositories (at least for those which support getting
966
965
* history per directory).
967
966
*
968
- * PMD wants us to use length() > 0 && charAt(0) instead of startsWith()
969
- * for performance. We prefer clarity over performance here, so silence it.
970
- *
971
967
* @param env runtime environment
972
968
* @param searchPaths list of paths in which to search for repositories
973
969
* @param addProjects if true, add projects
@@ -978,7 +974,6 @@ public void prepareIndexer(RuntimeEnvironment env,
978
974
* @throws IndexerException indexer exception
979
975
* @throws IOException I/O exception
980
976
*/
981
- @ SuppressWarnings ("PMD.SimplifyStartsWith" )
982
977
public void prepareIndexer (RuntimeEnvironment env ,
983
978
Set <String > searchPaths ,
984
979
boolean addProjects ,
@@ -992,39 +987,12 @@ public void prepareIndexer(RuntimeEnvironment env,
992
987
}
993
988
994
989
// Projects need to be created first since when adding repositories below,
995
- // some of the project properties might be needed for that.
990
+ // some project properties might be needed for that.
996
991
if (addProjects ) {
997
992
File [] files = env .getSourceRootFile ().listFiles ();
998
993
Map <String , Project > projects = env .getProjects ();
999
994
1000
- // Keep a copy of the old project list so that we can preserve
1001
- // the customization of existing projects.
1002
- Map <String , Project > oldProjects = new HashMap <>();
1003
- for (Project p : projects .values ()) {
1004
- oldProjects .put (p .getName (), p );
1005
- }
1006
-
1007
- projects .clear ();
1008
-
1009
- // Add a project for each top-level directory in source root.
1010
- for (File file : files ) {
1011
- String name = file .getName ();
1012
- String path = '/' + name ;
1013
- if (oldProjects .containsKey (name )) {
1014
- // This is an existing object. Reuse the old project,
1015
- // possibly with customizations, instead of creating a
1016
- // new with default values.
1017
- Project p = oldProjects .get (name );
1018
- p .setPath (path );
1019
- p .setName (name );
1020
- p .completeWithDefaults ();
1021
- projects .put (name , p );
1022
- } else if (!name .startsWith ("." ) && file .isDirectory ()) {
1023
- // Found a new directory with no matching project, so
1024
- // create a new project with default properties.
1025
- projects .put (name , new Project (name , path ));
1026
- }
1027
- }
995
+ addProjects (files , projects );
1028
996
}
1029
997
1030
998
if (!searchPaths .isEmpty ()) {
@@ -1053,6 +1021,37 @@ public void prepareIndexer(RuntimeEnvironment env,
1053
1021
}
1054
1022
}
1055
1023
1024
+ private void addProjects (File [] files , Map <String , Project > projects ) {
1025
+ // Keep a copy of the old project list so that we can preserve
1026
+ // the customization of existing projects.
1027
+ Map <String , Project > oldProjects = new HashMap <>();
1028
+ for (Project p : projects .values ()) {
1029
+ oldProjects .put (p .getName (), p );
1030
+ }
1031
+
1032
+ projects .clear ();
1033
+
1034
+ // Add a project for each top-level directory in source root.
1035
+ for (File file : files ) {
1036
+ String name = file .getName ();
1037
+ String path = '/' + name ;
1038
+ if (oldProjects .containsKey (name )) {
1039
+ // This is an existing object. Reuse the old project,
1040
+ // possibly with customizations, instead of creating a
1041
+ // new with default values.
1042
+ Project p = oldProjects .get (name );
1043
+ p .setPath (path );
1044
+ p .setName (name );
1045
+ p .completeWithDefaults ();
1046
+ projects .put (name , p );
1047
+ } else if (!name .startsWith ("." ) && file .isDirectory ()) {
1048
+ // Found a new directory with no matching project, so
1049
+ // create a new project with default properties.
1050
+ projects .put (name , new Project (name , path ));
1051
+ }
1052
+ }
1053
+ }
1054
+
1056
1055
/**
1057
1056
* This is the second phase of the indexer which generates Lucene index
1058
1057
* by passing source code files through ctags, generating xrefs
@@ -1068,9 +1067,9 @@ public void doIndexerExecution(final boolean update, List<String> subFiles,
1068
1067
IndexChangedListener progress )
1069
1068
throws IOException {
1070
1069
Statistics elapsed = new Statistics ();
1071
- RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
1072
1070
LOGGER .info ("Starting indexing" );
1073
1071
1072
+ RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
1074
1073
IndexerParallelizer parallelizer = env .getIndexerParallelizer ();
1075
1074
final CountDownLatch latch ;
1076
1075
if (subFiles == null || subFiles .isEmpty ()) {
0 commit comments