2020import java .nio .file .Path ;
2121import java .nio .file .Paths ;
2222import java .util .*;
23+ import java .util .concurrent .atomic .AtomicReference ;
2324import java .util .logging .Level ;
2425import java .util .logging .Logger ;
2526import java .util .regex .Matcher ;
@@ -41,6 +42,7 @@ public class NatsServerRunner implements AutoCloseable {
4142 private final Map <String , Integer > _ports ;
4243 private final File _configFile ;
4344 private final String _cmdLine ;
45+ private final AtomicReference <JsStorageDir > _jsStorageDir ;
4446 private Process process ;
4547 private OutputLogger nol ;
4648
@@ -330,6 +332,8 @@ protected NatsServerRunner(Builder b) throws IOException {
330332 }
331333 }
332334
335+ _jsStorageDir = new AtomicReference <>();
336+
333337 int aliveCheckTries = b .aliveCheckTries == null ? DefaultProcessAliveCheckTries : b .aliveCheckTries ;
334338 long aliveCheckWait = b .aliveCheckWait == null ? DefaultProcessAliveCheckWait : b .aliveCheckWait ;
335339 int connectValidateTries = b .connectValidateTries == null ? DefaultConnectValidateTries : b .connectValidateTries ;
@@ -342,7 +346,11 @@ protected NatsServerRunner(Builder b) throws IOException {
342346 cmd .add (_executablePath );
343347
344348 try {
345- if (allowCommandLineOnly && b .configFilePath == null && b .configInserts == null ) {
349+ if (allowCommandLineOnly
350+ && !b .jetstream
351+ && b .configFilePath == null
352+ && b .configInserts == null )
353+ {
346354 int port = getPort ();
347355 cmd .add ("--port" );
348356 cmd .add (Integer .toString (port ));
@@ -359,7 +367,7 @@ protected NatsServerRunner(Builder b) throws IOException {
359367 portAlreadyDone = true ;
360368 }
361369 else {
362- processSuppliedConfigFile (writer , b .configFilePath );
370+ processSuppliedConfigFile (writer , b .configFilePath , b . jetstream );
363371 portAlreadyDone = _ports .get (NATS_PORT_KEY ) != -1 ;
364372 }
365373
@@ -368,10 +376,20 @@ protected NatsServerRunner(Builder b) throws IOException {
368376 if (portAlreadyDone && s .startsWith ("port:" )) {
369377 continue ;
370378 }
379+ if (s .contains ("store_dir" )) {
380+ if (_jsStorageDir .get () != null ) {
381+ throw new IOException ("store_dir provided in both config inserts and config file" );
382+ }
383+ _jsStorageDir .set (JsStorageDir .extractedInstance (s ));
384+ }
371385 writeLine (writer , s );
372386 }
373387 }
374388
389+ if (b .jetstream && _jsStorageDir .get () == null ) {
390+ writeJetStreamStorage (writer , (Path )null );
391+ }
392+
375393 writer .flush ();
376394 writer .close ();
377395
@@ -520,8 +538,9 @@ private String getConfigSep(String configPath) {
520538 // ----------------------------------------------------------------------------------------------------
521539 // HELPERS
522540 // ----------------------------------------------------------------------------------------------------
523- private void processSuppliedConfigFile (BufferedWriter writer , Path configFilePath ) throws IOException {
541+ private void processSuppliedConfigFile (BufferedWriter writer , Path configFilePath , boolean jetStream ) throws IOException {
524542 Matcher constructionPortMatcher = Pattern .compile (PORT_REGEX ).matcher ("" );
543+ Matcher constructionJsStoreDirMatcher = Pattern .compile (JS_STORE_DIR_REGEX ).matcher ("" );
525544 Matcher mappedPortMatcher = Pattern .compile (PORT_MAPPED_REGEX ).matcher ("" );
526545
527546 BufferedReader reader = new BufferedReader (new FileReader (configFilePath .toFile ()));
@@ -579,6 +598,13 @@ else if (trim.startsWith("}")) {
579598 }
580599 }
581600
601+ if (jetStream ) {
602+ constructionJsStoreDirMatcher .reset (line );
603+ if (constructionJsStoreDirMatcher .find ()) {
604+ _jsStorageDir .set (JsStorageDir .extractedInstance (line ));
605+ }
606+ }
607+
582608 line = reader .readLine ();
583609 }
584610
@@ -602,9 +628,19 @@ private void writePortLine(BufferedWriter writer, int port) throws IOException {
602628 writeLine (writer , PORT_PROPERTY + port );
603629 }
604630
631+ private void writeJetStreamStorage (BufferedWriter writer , Path path ) throws IOException {
632+ JsStorageDir jssd = path == null
633+ ? JsStorageDir .temporaryInstance ()
634+ : new JsStorageDir (path );
635+ _jsStorageDir .set (jssd );
636+ for (String c : jssd .configInserts ) {
637+ writeLine (writer , c );
638+ }
639+ }
640+
605641 private void writeLine (BufferedWriter writer , String line ) throws IOException {
606642 writer .write (line );
607- writer .write (" \n " );
643+ writer .write (System . lineSeparator () );
608644 }
609645
610646 private void sleep (long sleep ) {
0 commit comments