@@ -36,6 +36,7 @@ public class JsonConfig {
3636
3737 public static void forServer (Path json ) throws IOException {
3838 try (Reader reader = Files .newBufferedReader (json )) {
39+ boolean hasMissingEntries = false ;
3940 SyncConfig config = SyncConfig .getConfig ();
4041 JsonObject root = Json .parse (reader ).asObject ();
4142 if (root .isNull ()) {
@@ -51,58 +52,75 @@ public static void forServer(Path json) throws IOException {
5152 config .SYNC_MODE = getInt (general , PROP_SYNC_MODE );
5253 config .SERVER_PORT = getInt (connection , PROP_PORT );
5354
54- JsonArray directoryIncludeList = getArray (rules , PROP_DIRECTORIES );
55- config .DIRECTORY_INCLUDE_LIST = directoryIncludeList
56- .values ()
57- .stream ()
58- .map (v -> {
59- if (v .isObject ()) {
60- return new DirectoryEntry (
61- v .asObject ().get ("path" ).asString (),
62- EDirectoryMode .valueOf (v .asObject ().get ("mode" ).asString ().toLowerCase ())
63- );
64- }
65- return new DirectoryEntry (v .asString (), EDirectoryMode .mirror );
66- })
67- .collect (Collectors .toList ());
68-
69- JsonObject files = getObject (rules , PROP_FILES );
70- config .FILE_INCLUDE_LIST = getArray (files , PROP_FILES_INCLUDE )
71- .values ()
72- .stream ()
73- .map (v -> {
74- if (v .isObject ()) {
75- // Ditching description as we don't use it for anything
76- return v .asObject ().get ("pattern" ).asString ();
77- }
78- return v .asString ();
79- })
80- .collect (Collectors .toList ());
81- config .FILE_IGNORE_LIST = getArray (files , PROP_FILES_IGNORE )
82- .values ()
83- .stream ()
84- .map (v -> {
85- if (v .isObject ()) {
86- // Ditching description as we don't use it for anything
87- return v .asObject ().get ("pattern" ).asString ();
88- }
89- return v .asString ();
90- })
91- .collect (Collectors .toList ());
92- config .REDIRECT_FILES_LIST = getArray (files , PROP_FILES_REDIRECT )
93- .values ()
94- .stream ()
95- .map (v -> FileRedirect .from (v .asObject ()))
96- .collect (Collectors .toList ());
55+ try {
56+ JsonArray directoryIncludeList = getArray (rules , PROP_DIRECTORIES );
57+ config .DIRECTORY_INCLUDE_LIST = directoryIncludeList
58+ .values ()
59+ .stream ()
60+ .map (v -> {
61+ if (v .isObject ()) {
62+ return new DirectoryEntry (
63+ v .asObject ().get ("path" ).asString (),
64+ EDirectoryMode .valueOf (v .asObject ().get ("mode" ).asString ().toLowerCase ())
65+ );
66+ }
67+ return new DirectoryEntry (v .asString (), EDirectoryMode .mirror );
68+ })
69+ .collect (Collectors .toList ());
70+ } catch (NullPointerException e ) {
71+ Logger .debug ("Missing config entry for directories, using defaults" );
72+ hasMissingEntries = true ;
73+ }
74+
75+ try {
76+ JsonObject files = getObject (rules , PROP_FILES );
77+ config .FILE_INCLUDE_LIST = getArray (files , PROP_FILES_INCLUDE )
78+ .values ()
79+ .stream ()
80+ .map (v -> {
81+ if (v .isObject ()) {
82+ // Ditching description as we don't use it for anything
83+ return v .asObject ().get ("pattern" ).asString ();
84+ }
85+ return v .asString ();
86+ })
87+ .collect (Collectors .toList ());
88+ config .FILE_IGNORE_LIST = getArray (files , PROP_FILES_IGNORE )
89+ .values ()
90+ .stream ()
91+ .map (v -> {
92+ if (v .isObject ()) {
93+ // Ditching description as we don't use it for anything
94+ return v .asObject ().get ("pattern" ).asString ();
95+ }
96+ return v .asString ();
97+ })
98+ .collect (Collectors .toList ());
99+ config .REDIRECT_FILES_LIST = getArray (files , PROP_FILES_REDIRECT )
100+ .values ()
101+ .stream ()
102+ .map (v -> FileRedirect .from (v .asObject ()))
103+ .collect (Collectors .toList ());
104+ } catch (NullPointerException e ) {
105+ Logger .debug ("Missing config entry for files, using defaults" );
106+ hasMissingEntries = true ;
107+ }
97108
98109 String [] localeParts = getString (misc , PROP_LOCALE , "en_US" ).split ("_" );
99110 config .LOCALE = new Locale (localeParts [0 ], localeParts [1 ]);
100111
112+ if (hasMissingEntries ) {
113+ // Generate a new config if we failed to read parts of it
114+ // this will fill in the missing details with defaults
115+ Logger .debug ("Missing config entries detected, saving new server config" );
116+ saveServer (json );
117+ }
101118 }
102119 }
103120
104121 public static void forClient (Path json ) throws IOException {
105122 try (Reader reader = Files .newBufferedReader (json )) {
123+ boolean hasMissingEntries = false ;
106124 SyncConfig config = SyncConfig .getConfig ();
107125 JsonObject root = Json .parse (reader ).asObject ();
108126 if (root .isNull ()) {
@@ -118,22 +136,34 @@ public static void forClient(Path json) throws IOException {
118136 config .SERVER_IP = getString (connection , PROP_ADDRESS , "127.0.0.1" );
119137 config .SERVER_PORT = getInt (connection , PROP_PORT );
120138
121- JsonObject files = getObject (rules , PROP_FILES );
122- config .FILE_IGNORE_LIST = getArray (files , PROP_FILES_IGNORE )
123- .values ()
124- .stream ()
125- .map (v -> {
126- if (v .isObject ()) {
127- // Ditching description as we don't use it for anything
128- return v .asObject ().get ("pattern" ).asString ();
129- }
130- return v .asString ();
131- })
132- .collect (Collectors .toList ());
139+ try {
140+ JsonObject files = getObject (rules , PROP_FILES );
141+ config .FILE_IGNORE_LIST = getArray (files , PROP_FILES_IGNORE )
142+ .values ()
143+ .stream ()
144+ .map (v -> {
145+ if (v .isObject ()) {
146+ // Ditching description as we don't use it for anything
147+ return v .asObject ().get ("pattern" ).asString ();
148+ }
149+ return v .asString ();
150+ })
151+ .collect (Collectors .toList ());
152+ } catch (NullPointerException e ) {
153+ Logger .debug ("Missing config entry for files, using defaults" );
154+ hasMissingEntries = true ;
155+ }
133156
134157 String [] localeParts = getString (misc , PROP_LOCALE , "en_US" ).split ("_" );
135158 config .LOCALE = new Locale (localeParts [0 ], localeParts [1 ]);
136159 config .THEME = ETheme .valueOf (getString (misc , PROP_THEME , "BLUE_YELLOW" ));
160+
161+ if (hasMissingEntries ) {
162+ // Generate a new config if we failed to read parts of it
163+ // this will fill in the missing details with defaults
164+ Logger .debug ("Missing config entries detected, saving new client config" );
165+ saveClient (json );
166+ }
137167 }
138168 }
139169
0 commit comments