@@ -36,6 +36,7 @@ public class JsonConfig {
36
36
37
37
public static void forServer (Path json ) throws IOException {
38
38
try (Reader reader = Files .newBufferedReader (json )) {
39
+ boolean hasMissingEntries = false ;
39
40
SyncConfig config = SyncConfig .getConfig ();
40
41
JsonObject root = Json .parse (reader ).asObject ();
41
42
if (root .isNull ()) {
@@ -51,58 +52,75 @@ public static void forServer(Path json) throws IOException {
51
52
config .SYNC_MODE = getInt (general , PROP_SYNC_MODE );
52
53
config .SERVER_PORT = getInt (connection , PROP_PORT );
53
54
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
+ }
97
108
98
109
String [] localeParts = getString (misc , PROP_LOCALE , "en_US" ).split ("_" );
99
110
config .LOCALE = new Locale (localeParts [0 ], localeParts [1 ]);
100
111
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
+ }
101
118
}
102
119
}
103
120
104
121
public static void forClient (Path json ) throws IOException {
105
122
try (Reader reader = Files .newBufferedReader (json )) {
123
+ boolean hasMissingEntries = false ;
106
124
SyncConfig config = SyncConfig .getConfig ();
107
125
JsonObject root = Json .parse (reader ).asObject ();
108
126
if (root .isNull ()) {
@@ -118,22 +136,34 @@ public static void forClient(Path json) throws IOException {
118
136
config .SERVER_IP = getString (connection , PROP_ADDRESS , "127.0.0.1" );
119
137
config .SERVER_PORT = getInt (connection , PROP_PORT );
120
138
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
+ }
133
156
134
157
String [] localeParts = getString (misc , PROP_LOCALE , "en_US" ).split ("_" );
135
158
config .LOCALE = new Locale (localeParts [0 ], localeParts [1 ]);
136
159
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
+ }
137
167
}
138
168
}
139
169
0 commit comments