27
27
import java .io .InputStreamReader ;
28
28
import java .io .Reader ;
29
29
import java .nio .charset .StandardCharsets ;
30
+ import java .nio .file .DirectoryStream ;
30
31
import java .nio .file .Files ;
32
+ import java .nio .file .Path ;
31
33
import java .nio .file .Paths ;
32
34
import java .util .ArrayList ;
33
35
import java .util .Arrays ;
42
44
import java .util .regex .Matcher ;
43
45
import java .util .regex .Pattern ;
44
46
import java .util .stream .Collectors ;
47
+ import java .util .stream .StreamSupport ;
45
48
46
49
/**
47
50
* Parses JVM options from a file and prints a single line with all JVM options to standard output.
@@ -52,80 +55,88 @@ final class JvmOptionsParser {
52
55
* The main entry point. The exit code is 0 if the JVM options were successfully parsed, otherwise the exit code is 1. If an improperly
53
56
* formatted line is discovered, the line is output to standard error.
54
57
*
55
- * @param args the args to the program which should consist of a single option, the path to the JVM options
58
+ * @param args the args to the program which should consist of a single option, the path to ES_PATH_CONF
56
59
*/
57
60
public static void main (final String [] args ) throws InterruptedException , IOException {
58
61
if (args .length != 1 ) {
59
- throw new IllegalArgumentException ("expected one argument specifying path to jvm.options but was " + Arrays .toString (args ));
60
- }
61
- final List <String > jvmOptions = new ArrayList <>();
62
- final SortedMap <Integer , String > invalidLines = new TreeMap <>();
63
- try (
64
- InputStream is = Files .newInputStream (Paths .get (args [0 ]));
65
- Reader reader = new InputStreamReader (is , StandardCharsets .UTF_8 );
66
- BufferedReader br = new BufferedReader (reader )
67
- ) {
68
- parse (JavaVersion .majorVersion (JavaVersion .CURRENT ), br , new JvmOptionConsumer () {
69
- @ Override
70
- public void accept (final String jvmOption ) {
71
- jvmOptions .add (jvmOption );
72
- }
73
- }, new InvalidLineConsumer () {
74
- @ Override
75
- public void accept (final int lineNumber , final String line ) {
76
- invalidLines .put (lineNumber , line );
77
- }
78
- });
62
+ throw new IllegalArgumentException ("expected one argument specifying path to ES_PATH_CONF but was " + Arrays .toString (args ));
79
63
}
80
64
81
- if (invalidLines .isEmpty ()) {
82
- // now append the JVM options from ES_JAVA_OPTS
83
- final String environmentJvmOptions = System .getenv ("ES_JAVA_OPTS" );
84
- if (environmentJvmOptions != null ) {
85
- jvmOptions .addAll (
86
- Arrays .stream (environmentJvmOptions .split ("\\ s+" )).filter (s -> s .trim ().isEmpty () == false ).collect (Collectors .toList ())
87
- );
65
+ final ArrayList <Path > jvmOptionsFiles = new ArrayList <>();
66
+ jvmOptionsFiles .add (Paths .get (args [0 ], "jvm.options" ));
67
+
68
+ final Path jvmOptionsDirectory = Paths .get (args [0 ], "jvm.options.d" );
69
+
70
+ if (Files .isDirectory (jvmOptionsDirectory )) {
71
+ try (
72
+ DirectoryStream <Path > jvmOptionsDirectoryStream = Files .newDirectoryStream (Paths .get (args [0 ], "jvm.options.d" ), "*.options" )
73
+ ) {
74
+ // collect the matching JVM options files after sorting them by Path::compareTo
75
+ StreamSupport .stream (jvmOptionsDirectoryStream .spliterator (), false ).sorted ().forEach (jvmOptionsFiles ::add );
88
76
}
89
- final Map <String , String > substitutions = new HashMap <>();
90
- substitutions .put ("ES_TMPDIR" , System .getenv ("ES_TMPDIR" ));
91
- if (null != System .getenv ("ES_PATH_CONF" )) {
92
- substitutions .put ("ES_PATH_CONF" , System .getenv ("ES_PATH_CONF" ));
77
+ }
78
+
79
+ final List <String > jvmOptions = new ArrayList <>();
80
+
81
+ for (final Path jvmOptionsFile : jvmOptionsFiles ) {
82
+ final SortedMap <Integer , String > invalidLines = new TreeMap <>();
83
+ try (
84
+ InputStream is = Files .newInputStream (jvmOptionsFile );
85
+ Reader reader = new InputStreamReader (is , StandardCharsets .UTF_8 );
86
+ BufferedReader br = new BufferedReader (reader )
87
+ ) {
88
+ parse (JavaVersion .majorVersion (JavaVersion .CURRENT ), br , jvmOptions ::add , invalidLines ::put );
93
89
}
94
- final List <String > substitutedJvmOptions = substitutePlaceholders (jvmOptions , Collections .unmodifiableMap (substitutions ));
95
- final List <String > ergonomicJvmOptions = JvmErgonomics .choose (substitutedJvmOptions );
96
- final List <String > systemJvmOptions = SystemJvmOptions .systemJvmOptions ();
97
- final List <String > finalJvmOptions = new ArrayList <>(
98
- systemJvmOptions .size () + substitutedJvmOptions .size () + ergonomicJvmOptions .size ()
99
- );
100
- finalJvmOptions .addAll (systemJvmOptions ); // add the system JVM options first so that they can be overridden
101
- finalJvmOptions .addAll (substitutedJvmOptions );
102
- finalJvmOptions .addAll (ergonomicJvmOptions );
103
- final String spaceDelimitedJvmOptions = spaceDelimitJvmOptions (finalJvmOptions );
104
- Launchers .outPrintln (spaceDelimitedJvmOptions );
105
- Launchers .exit (0 );
106
- } else {
107
- final String errorMessage = String .format (
108
- Locale .ROOT ,
109
- "encountered [%d] error%s parsing [%s]" ,
110
- invalidLines .size (),
111
- invalidLines .size () == 1 ? "" : "s" ,
112
- args [0 ]
113
- );
114
- Launchers .errPrintln (errorMessage );
115
- int count = 0 ;
116
- for (final Map .Entry <Integer , String > entry : invalidLines .entrySet ()) {
117
- count ++;
118
- final String message = String .format (
90
+ if (invalidLines .isEmpty () == false ) {
91
+ final String errorMessage = String .format (
119
92
Locale .ROOT ,
120
- "[%d]: encountered improperly formatted JVM option line [%s] on line number [%d ]" ,
121
- count ,
122
- entry . getValue () ,
123
- entry . getKey ()
93
+ "encountered [%d] error%s parsing [%s ]" ,
94
+ invalidLines . size () ,
95
+ invalidLines . size () == 1 ? "" : "s" ,
96
+ jvmOptionsFile
124
97
);
125
- Launchers .errPrintln (message );
98
+ Launchers .errPrintln (errorMessage );
99
+ int count = 0 ;
100
+ for (final Map .Entry <Integer , String > entry : invalidLines .entrySet ()) {
101
+ count ++;
102
+ final String message = String .format (
103
+ Locale .ROOT ,
104
+ "[%d]: encountered improperly formatted JVM option in [%s] on line number [%d]: [%s]" ,
105
+ count ,
106
+ jvmOptionsFile ,
107
+ entry .getKey (),
108
+ entry .getValue ()
109
+ );
110
+ Launchers .errPrintln (message );
111
+ }
112
+ Launchers .exit (1 );
126
113
}
127
- Launchers .exit (1 );
128
114
}
115
+
116
+ // now append the JVM options from ES_JAVA_OPTS
117
+ final String environmentJvmOptions = System .getenv ("ES_JAVA_OPTS" );
118
+ if (environmentJvmOptions != null ) {
119
+ jvmOptions .addAll (
120
+ Arrays .stream (environmentJvmOptions .split ("\\ s+" )).filter (s -> s .trim ().isEmpty () == false ).collect (Collectors .toList ())
121
+ );
122
+ }
123
+ final Map <String , String > substitutions = new HashMap <>();
124
+ substitutions .put ("ES_TMPDIR" , System .getenv ("ES_TMPDIR" ));
125
+ if (null != System .getenv ("ES_PATH_CONF" )) {
126
+ substitutions .put ("ES_PATH_CONF" , System .getenv ("ES_PATH_CONF" ));
127
+ }
128
+ final List <String > substitutedJvmOptions = substitutePlaceholders (jvmOptions , Collections .unmodifiableMap (substitutions ));
129
+ final List <String > ergonomicJvmOptions = JvmErgonomics .choose (substitutedJvmOptions );
130
+ final List <String > systemJvmOptions = SystemJvmOptions .systemJvmOptions ();
131
+ final List <String > finalJvmOptions = new ArrayList <>(
132
+ systemJvmOptions .size () + substitutedJvmOptions .size () + ergonomicJvmOptions .size ()
133
+ );
134
+ finalJvmOptions .addAll (systemJvmOptions ); // add the system JVM options first so that they can be overridden
135
+ finalJvmOptions .addAll (substitutedJvmOptions );
136
+ finalJvmOptions .addAll (ergonomicJvmOptions );
137
+ final String spaceDelimitedJvmOptions = spaceDelimitJvmOptions (finalJvmOptions );
138
+ Launchers .outPrintln (spaceDelimitedJvmOptions );
139
+ Launchers .exit (0 );
129
140
}
130
141
131
142
static List <String > substitutePlaceholders (final List <String > jvmOptions , final Map <String , String > substitutions ) {
0 commit comments