-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Config.java
270 lines (238 loc) · 8.48 KB
/
Config.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.configuration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import org.neo4j.graphdb.config.Configuration;
import org.neo4j.graphdb.config.Setting;
import org.neo4j.kernel.info.DiagnosticsPhase;
import org.neo4j.kernel.info.DiagnosticsProvider;
import org.neo4j.logging.BufferingLog;
import org.neo4j.logging.Log;
import org.neo4j.logging.Logger;
import static java.util.Arrays.asList;
/**
* This class holds the overall configuration of a Neo4j database instance. Use the accessors to convert the internal
* key-value settings to other types.
* <p>
* Users can assume that old settings have been migrated to their new counterparts, and that defaults have been applied.
* <p>
* UI's can change configuration by calling augment(). Any listener, such as services that use this configuration, can
* be notified of changes by implementing the {@link ConfigurationChangeListener} interface.
*/
public class Config implements DiagnosticsProvider, Configuration
{
private final List<ConfigurationChangeListener> listeners = new CopyOnWriteArrayList<>();
private final Map<String, String> params = new ConcurrentHashMap<>();
private final Iterable<Class<?>> settingsClasses;
private final ConfigurationMigrator migrator;
private final ConfigurationValidator validator;
private ConfigValues settingsFunction;
// Messages to this log get replayed into a real logger once logging has been instantiated.
private final BufferingLog bufferedLog = new BufferingLog();
private Log log = bufferedLog;
public static Config empty()
{
return new Config();
}
public static Config defaults()
{
return new Config();
}
private Config()
{
this( new HashMap<>() );
}
public Config( Map<String, String> inputParams )
{
this( inputParams, Collections.<Class<?>>emptyList() );
}
public Config( Map<String, String> inputParams, Class<?>... settingsClasses )
{
this( inputParams, asList( settingsClasses ) );
}
public Config( Map<String, String> params, Iterable<Class<?>> settingsClasses )
{
this.settingsClasses = settingsClasses;
migrator = new AnnotationBasedConfigurationMigrator( settingsClasses );
validator = new ConfigurationValidator( settingsClasses );
replaceSettings( params );
}
/**
* Returns a copy of this config with the given modifications.
*
* @return a new modified config, leaves this config unchanged.
*/
public Config with( Map<String, String> additionalConfig )
{
Map<String, String> newParams = getParams(); // copy is returned
newParams.putAll( additionalConfig );
return new Config( newParams );
}
// TODO: Get rid of this, to allow us to have something more
// elaborate as internal storage (eg. something that can keep meta data with
// properties).
public Map<String, String> getParams()
{
return new HashMap<>( this.params );
}
/**
* Retrieve a configuration property.
*/
@Override
public <T> T get( Setting<T> setting )
{
return setting.apply( settingsFunction );
}
/**
* Unlike the public {@link Setting} instances, the function passed in here has access to
* the raw setting data, meaning it can provide functionality that cross multiple settings
* and other more advanced use cases.
*/
public <T> T view( Function<ConfigValues, T> projection )
{
return projection.apply( settingsFunction );
}
/**
* Augment the existing config with new settings, overriding any conflicting settings, but keeping all old
* non-overlapping ones.
*
* @param changes settings to add and override
*/
public Config augment( Map<String, String> changes )
{
Map<String, String> params = getParams();
params.putAll( changes );
replaceSettings( params );
return this;
}
public Iterable<Class<?>> getSettingsClasses()
{
return settingsClasses;
}
public void setLogger( Log log )
{
if ( this.log == bufferedLog )
{
bufferedLog.replayInto( log );
}
this.log = log;
}
public void addConfigurationChangeListener( ConfigurationChangeListener listener )
{
listeners.add( listener );
}
public void removeConfigurationChangeListener( ConfigurationChangeListener listener )
{
listeners.remove( listener );
}
@Override
public String getDiagnosticsIdentifier()
{
return getClass().getName();
}
@Override
public void acceptDiagnosticsVisitor( Object visitor )
{
// nothing visits configuration
}
@Override
public void dump( DiagnosticsPhase phase, Logger logger )
{
if ( phase.isInitialization() || phase.isExplicitlyRequested() )
{
logger.log( "Neo4j Kernel properties:" );
for ( Map.Entry<String, String> param : params.entrySet() )
{
logger.log( "%s=%s", param.getKey(), param.getValue() );
}
}
}
@Override
public String toString()
{
List<String> keys = new ArrayList<>( params.keySet() );
Collections.sort( keys );
LinkedHashMap<String, String> output = new LinkedHashMap<>();
for ( String key : keys )
{
output.put( key, params.get( key ) );
}
return output.toString();
}
private synchronized void replaceSettings( Map<String, String> newValues )
{
newValues = migrator.apply( newValues, log );
// Make sure all changes are valid
validator.validate( newValues );
// Figure out what changed
if ( listeners.isEmpty() )
{
// Make the change
params.clear();
params.putAll( newValues );
}
else
{
List<ConfigurationChange> configurationChanges = new ArrayList<>();
for ( Map.Entry<String, String> stringStringEntry : newValues.entrySet() )
{
String oldValue = params.get( stringStringEntry.getKey() );
String newValue = stringStringEntry.getValue();
if ( !(oldValue == null && newValue == null) &&
(oldValue == null || newValue == null || !oldValue.equals( newValue )) )
{
configurationChanges.add( new ConfigurationChange( stringStringEntry.getKey(), oldValue,
newValue ) );
}
}
if ( configurationChanges.isEmpty() )
{
// Don't bother... nothing changed.
return;
}
// Make the change
params.clear();
for ( Map.Entry<String, String> entry : newValues.entrySet() )
{
// Filter out nulls because we are using a ConcurrentHashMap under the covers, which doesn't support
// null keys or values.
String value = entry.getValue();
if ( value != null )
{
params.put( entry.getKey(), value );
}
}
// Notify listeners
for ( ConfigurationChangeListener listener : listeners )
{
listener.notifyConfigurationChanges( configurationChanges );
}
}
settingsFunction = new ConfigValues( this.params );
}
}