Skip to content

Commit 2ffbb0c

Browse files
author
Vladimir Kotal
authored
Configuration rework (#2423)
fixes #2382
1 parent 094edb8 commit 2ffbb0c

40 files changed

+1327
-950
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 7 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
import java.beans.XMLEncoder;
2929
import java.io.BufferedInputStream;
3030
import java.io.BufferedOutputStream;
31-
import java.io.BufferedReader;
3231
import java.io.ByteArrayInputStream;
3332
import java.io.ByteArrayOutputStream;
3433
import java.io.File;
3534
import java.io.FileInputStream;
3635
import java.io.FileOutputStream;
37-
import java.io.FileReader;
3836
import java.io.IOException;
3937
import java.io.InputStream;
4038
import java.io.OutputStream;
@@ -62,11 +60,14 @@
6260
import org.opengrok.indexer.index.IgnoredNames;
6361
import org.opengrok.indexer.logger.LoggerFactory;
6462

63+
6564
/**
66-
* Placeholder class for all configuration variables. Due to the multithreaded
65+
* Placeholder class for all configuration variables. Due to the multi-threaded
6766
* nature of the web application, each thread will use the same instance of the
6867
* configuration object for each page request. Class and methods should have
6968
* package scope, but that didn't work with the XMLDecoder/XMLEncoder.
69+
*
70+
* This should be as close to POJO (https://en.wikipedia.org/wiki/Plain_old_Java_object) as possible.
7071
*/
7172
public final class Configuration {
7273

@@ -102,6 +103,9 @@ public final class Configuration {
102103
private static final String NONPOSITIVE_NUMBER_ERROR =
103104
"Invalid value for \"%s\" - \"%s\". Expected value greater than 0";
104105

106+
/**
107+
* Path to {@code ctags} binary.
108+
*/
105109
private String ctags;
106110

107111
/**
@@ -235,7 +239,6 @@ public final class Configuration {
235239
* contains definition tags.
236240
*/
237241
public static final String EFTAR_DTAGS_NAME = "dtags.eftar";
238-
private transient File dtagsEftar = null;
239242

240243
/**
241244
* Revision messages will be collapsible if they exceed this many number of
@@ -1088,167 +1091,38 @@ public void setListDirsFirst(boolean flag) {
10881091
listDirsFirst = flag;
10891092
}
10901093

1091-
/**
1092-
* Get the contents of a file or empty string if the file cannot be read.
1093-
*/
1094-
private static String getFileContent(File file) {
1095-
if (file == null || !file.canRead()) {
1096-
return "";
1097-
}
1098-
FileReader fin = null;
1099-
BufferedReader input = null;
1100-
try {
1101-
fin = new FileReader(file);
1102-
input = new BufferedReader(fin);
1103-
String line;
1104-
StringBuilder contents = new StringBuilder();
1105-
String EOL = System.getProperty("line.separator");
1106-
while ((line = input.readLine()) != null) {
1107-
contents.append(line).append(EOL);
1108-
}
1109-
return contents.toString();
1110-
} catch (java.io.FileNotFoundException e) {
1111-
LOGGER.log(Level.WARNING, "failed to find file: {0}",
1112-
e.getMessage());
1113-
} catch (java.io.IOException e) {
1114-
LOGGER.log(Level.WARNING, "failed to read file: {0}",
1115-
e.getMessage());
1116-
} finally {
1117-
if (input != null) {
1118-
try {
1119-
input.close();
1120-
} catch (Exception e) {
1121-
/*
1122-
* nothing we can do about it
1123-
*/ }
1124-
} else if (fin != null) {
1125-
try {
1126-
fin.close();
1127-
} catch (Exception e) {
1128-
/*
1129-
* nothing we can do about it
1130-
*/ }
1131-
}
1132-
}
1133-
return "";
1134-
}
1135-
11361094
/**
11371095
* The name of the file relative to the <var>DATA_ROOT</var>, which should
11381096
* be included into the footer of generated web pages.
11391097
*/
11401098
public static final String FOOTER_INCLUDE_FILE = "footer_include";
11411099

1142-
private transient String footer = null;
1143-
1144-
/**
1145-
* Get the contents of the footer include file.
1146-
*
1147-
* @param force if true, reload even if already set
1148-
* @return an empty string if it could not be read successfully, the
1149-
* contents of the file otherwise.
1150-
* @see #FOOTER_INCLUDE_FILE
1151-
*/
1152-
public String getFooterIncludeFileContent(boolean force) {
1153-
if (footer == null || force) {
1154-
footer = getFileContent(new File(getIncludeRoot(), FOOTER_INCLUDE_FILE));
1155-
}
1156-
return footer;
1157-
}
1158-
11591100
/**
11601101
* The name of the file relative to the <var>DATA_ROOT</var>, which should
11611102
* be included into the header of generated web pages.
11621103
*/
11631104
public static final String HEADER_INCLUDE_FILE = "header_include";
11641105

1165-
private transient String header = null;
1166-
1167-
/**
1168-
* Get the contents of the header include file.
1169-
*
1170-
* @param force if true, reload even if already set
1171-
* @return an empty string if it could not be read successfully, the
1172-
* contents of the file otherwise.
1173-
* @see #HEADER_INCLUDE_FILE
1174-
*/
1175-
public String getHeaderIncludeFileContent(boolean force) {
1176-
if (header == null || force) {
1177-
header = getFileContent(new File(getIncludeRoot(), HEADER_INCLUDE_FILE));
1178-
}
1179-
return header;
1180-
}
1181-
11821106
/**
11831107
* The name of the file relative to the <var>DATA_ROOT</var>, which should
11841108
* be included into the body of web app's "Home" page.
11851109
*/
11861110
public static final String BODY_INCLUDE_FILE = "body_include";
11871111

1188-
private transient String body = null;
1189-
1190-
/**
1191-
* Get the contents of the body include file.
1192-
*
1193-
* @param force if true, reload even if already set
1194-
* @return an empty string if it could not be read successfully, the
1195-
* contents of the file otherwise.
1196-
* @see Configuration#BODY_INCLUDE_FILE
1197-
*/
1198-
public String getBodyIncludeFileContent(boolean force) {
1199-
if (body == null || force) {
1200-
body = getFileContent(new File(getIncludeRoot(), BODY_INCLUDE_FILE));
1201-
}
1202-
return body;
1203-
}
1204-
12051112
/**
12061113
* The name of the file relative to the <var>DATA_ROOT</var>, which should
12071114
* be included into the error page handling access forbidden errors - HTTP
12081115
* code 403 Forbidden.
12091116
*/
12101117
public static final String E_FORBIDDEN_INCLUDE_FILE = "error_forbidden_include";
12111118

1212-
private transient String eforbidden_content = null;
1213-
1214-
/**
1215-
* Get the contents of the page for forbidden error page (403 Forbidden)
1216-
* include file.
1217-
*
1218-
* @param force if true, reload even if already set
1219-
* @return an empty string if it could not be read successfully, the
1220-
* contents of the file otherwise.
1221-
* @see Configuration#E_FORBIDDEN_INCLUDE_FILE
1222-
*/
1223-
public String getForbiddenIncludeFileContent(boolean force) {
1224-
if (eforbidden_content == null || force) {
1225-
eforbidden_content = getFileContent(new File(getIncludeRoot(), E_FORBIDDEN_INCLUDE_FILE));
1226-
}
1227-
return eforbidden_content;
1228-
}
1229-
12301119
/**
12311120
* @return path to the file holding compiled path descriptions for the web application
12321121
*/
12331122
public Path getDtagsEftarPath() {
12341123
return Paths.get(getDataRoot(), "index", EFTAR_DTAGS_NAME);
12351124
}
12361125

1237-
/**
1238-
* Get the eftar file, which contains definition tags for path descriptions.
1239-
*
1240-
* @return {@code null} if there is no such file, the file otherwise.
1241-
*/
1242-
public File getDtagsEftar() {
1243-
if (dtagsEftar == null) {
1244-
File tmp = getDtagsEftarPath().toFile();
1245-
if (tmp.canRead()) {
1246-
dtagsEftar = tmp;
1247-
}
1248-
}
1249-
return dtagsEftar;
1250-
}
1251-
12521126
public String getCTagsExtraOptionsFile() {
12531127
return CTagsExtraOptionsFile;
12541128
}
@@ -1428,5 +1302,4 @@ public boolean test(Group g) {
14281302

14291303
return conf;
14301304
}
1431-
14321305
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package org.opengrok.indexer.configuration;
25+
26+
import java.io.File;
27+
28+
import static org.opengrok.indexer.util.IOUtils.getFileContent;
29+
30+
public class IncludeFiles {
31+
/**
32+
* Reload the content of all include files.
33+
*/
34+
public void reloadIncludeFiles() {
35+
getBodyIncludeFileContent(true);
36+
getHeaderIncludeFileContent(true);
37+
getFooterIncludeFileContent(true);
38+
getForbiddenIncludeFileContent(true);
39+
}
40+
41+
private transient String footer = null;
42+
43+
/**
44+
* Get the contents of the footer include file.
45+
*
46+
* @param force if true, reload even if already set
47+
* @return an empty string if it could not be read successfully, the
48+
* contents of the file otherwise.
49+
* @see Configuration#FOOTER_INCLUDE_FILE
50+
*/
51+
public String getFooterIncludeFileContent(boolean force) {
52+
if (footer == null || force) {
53+
footer = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
54+
Configuration.FOOTER_INCLUDE_FILE));
55+
}
56+
return footer;
57+
}
58+
59+
private transient String header = null;
60+
61+
/**
62+
* Get the contents of the header include file.
63+
*
64+
* @param force if true, reload even if already set
65+
* @return an empty string if it could not be read successfully, the
66+
* contents of the file otherwise.
67+
* @see Configuration#HEADER_INCLUDE_FILE
68+
*/
69+
public String getHeaderIncludeFileContent(boolean force) {
70+
if (header == null || force) {
71+
header = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
72+
Configuration.HEADER_INCLUDE_FILE));
73+
}
74+
return header;
75+
}
76+
77+
private transient String body = null;
78+
79+
/**
80+
* Get the contents of the body include file.
81+
*
82+
* @param force if true, reload even if already set
83+
* @return an empty string if it could not be read successfully, the
84+
* contents of the file otherwise.
85+
* @see Configuration#BODY_INCLUDE_FILE
86+
*/
87+
public String getBodyIncludeFileContent(boolean force) {
88+
if (body == null || force) {
89+
body = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
90+
Configuration.BODY_INCLUDE_FILE));
91+
}
92+
return body;
93+
}
94+
95+
private transient String eforbidden_content = null;
96+
97+
/**
98+
* Get the contents of the page for forbidden error page (403 Forbidden)
99+
* include file.
100+
*
101+
* @param force if true, reload even if already set
102+
* @return an empty string if it could not be read successfully, the
103+
* contents of the file otherwise.
104+
* @see Configuration#E_FORBIDDEN_INCLUDE_FILE
105+
*/
106+
public String getForbiddenIncludeFileContent(boolean force) {
107+
if (eforbidden_content == null || force) {
108+
eforbidden_content = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
109+
Configuration.E_FORBIDDEN_INCLUDE_FILE));
110+
}
111+
return eforbidden_content;
112+
}
113+
}

0 commit comments

Comments
 (0)