-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CommunityServerBuilder.java
394 lines (340 loc) · 13.4 KB
/
CommunityServerBuilder.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* Copyright (c) 2002-2017 "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.server.helpers;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Clock;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.neo4j.dbms.DatabaseManagementSystemSettings;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.ListenSocketAddress;
import org.neo4j.kernel.GraphDatabaseDependencies;
import org.neo4j.kernel.configuration.BoltConnector;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.HttpConnector;
import org.neo4j.kernel.configuration.HttpConnector.Encryption;
import org.neo4j.kernel.configuration.ssl.LegacySslPolicyConfig;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.server.CommunityNeoServer;
import org.neo4j.server.ServerTestUtils;
import org.neo4j.server.configuration.ServerSettings;
import org.neo4j.server.database.Database;
import org.neo4j.server.database.LifecycleManagingDatabase;
import org.neo4j.server.preflight.PreFlightTasks;
import org.neo4j.server.rest.paging.LeaseManager;
import org.neo4j.server.rest.web.DatabaseActions;
import org.neo4j.test.ImpermanentGraphDatabase;
import org.neo4j.time.Clocks;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.server.ServerTestUtils.asOneLine;
import static org.neo4j.server.database.LifecycleManagingDatabase.lifecycleManagingDatabase;
public class CommunityServerBuilder
{
protected static final ListenSocketAddress ANY_ADDRESS = new ListenSocketAddress( "localhost", 0 );
protected final LogProvider logProvider;
private ListenSocketAddress address = new ListenSocketAddress( "localhost", 7474 );
private ListenSocketAddress httpsAddress = new ListenSocketAddress( "localhost", 7473 );
private String maxThreads;
private String dataDir;
private String managementUri = "/db/manage/";
private String restUri = "/db/data/";
private PreFlightTasks preflightTasks;
private final HashMap<String, String> thirdPartyPackages = new HashMap<>();
private final Properties arbitraryProperties = new Properties();
static
{
System.setProperty( "sun.net.http.allowRestrictedHeaders", "true" );
}
private static LifecycleManagingDatabase.GraphFactory IN_MEMORY_DB = ( config, dependencies ) ->
{
File storeDir = config.get( DatabaseManagementSystemSettings.database_path );
config.augment( stringMap( GraphDatabaseFacadeFactory.Configuration.ephemeral.name(), "true",
new BoltConnector( "bolt" ).listen_address.name(), "localhost:0" ) );
return new ImpermanentGraphDatabase( storeDir, config,
GraphDatabaseDependencies.newDependencies( dependencies ) );
};
private Clock clock;
private String[] autoIndexedNodeKeys;
private final String[] autoIndexedRelationshipKeys = null;
private String[] securityRuleClassNames;
private boolean persistent;
private boolean httpsEnabled;
public static CommunityServerBuilder server( LogProvider logProvider )
{
return new CommunityServerBuilder( logProvider );
}
public static CommunityServerBuilder server()
{
return new CommunityServerBuilder( NullLogProvider.getInstance() );
}
public static CommunityServerBuilder serverOnRandomPorts()
{
return server().onRandomPorts();
}
public CommunityNeoServer build() throws IOException
{
if ( dataDir == null && persistent )
{
throw new IllegalStateException( "Must specify path" );
}
final File configFile = buildBefore();
Log log = logProvider.getLog( getClass() );
Config config = Config.fromFile( configFile ).withServerDefaults().build();
config.setLogger( log );
return build( configFile, config, GraphDatabaseDependencies.newDependencies().userLogProvider( logProvider )
.monitors( new Monitors() ) );
}
protected CommunityNeoServer build( File configFile, Config config,
GraphDatabaseFacadeFactory.Dependencies dependencies )
{
return new TestCommunityNeoServer( config, configFile, dependencies, logProvider );
}
public File createConfigFiles() throws IOException
{
File temporaryConfigFile = ServerTestUtils.createTempConfigFile();
File temporaryFolder = ServerTestUtils.createTempDir();
ServerTestUtils.writeConfigToFile( createConfiguration( temporaryFolder ), temporaryConfigFile );
return temporaryConfigFile;
}
public CommunityServerBuilder withClock( Clock clock )
{
this.clock = clock;
return this;
}
public Map<String, String> createConfiguration( File temporaryFolder )
{
Map<String, String> properties = stringMap(
ServerSettings.management_api_path.name(), managementUri,
ServerSettings.rest_api_path.name(), restUri );
ServerTestUtils.addDefaultRelativeProperties( properties, temporaryFolder );
if ( dataDir != null )
{
properties.put( DatabaseManagementSystemSettings.data_directory.name(), dataDir );
}
if ( maxThreads != null )
{
properties.put( ServerSettings.webserver_max_threads.name(), maxThreads );
}
if ( thirdPartyPackages.keySet().size() > 0 )
{
properties.put( ServerSettings.third_party_packages.name(), asOneLine( thirdPartyPackages ) );
}
if ( autoIndexedNodeKeys != null && autoIndexedNodeKeys.length > 0 )
{
properties.put( "dbms.auto_index.nodes.enabled", "true" );
String propertyKeys = org.apache.commons.lang.StringUtils.join( autoIndexedNodeKeys, "," );
properties.put( "dbms.auto_index.nodes.keys", propertyKeys );
}
if ( autoIndexedRelationshipKeys != null && autoIndexedRelationshipKeys.length > 0 )
{
properties.put( "dbms.auto_index.relationships.enabled", "true" );
String propertyKeys = org.apache.commons.lang.StringUtils.join( autoIndexedRelationshipKeys, "," );
properties.put( "dbms.auto_index.relationships.keys", propertyKeys );
}
if ( securityRuleClassNames != null && securityRuleClassNames.length > 0 )
{
String propertyKeys = org.apache.commons.lang.StringUtils.join( securityRuleClassNames, "," );
properties.put( ServerSettings.security_rules.name(), propertyKeys );
}
HttpConnector httpConnector = new HttpConnector( "http", Encryption.NONE );
HttpConnector httpsConnector = new HttpConnector( "https", Encryption.TLS );
properties.put( httpConnector.type.name(), "HTTP" );
properties.put( httpConnector.enabled.name(), "true" );
properties.put( httpConnector.address.name(), address.toString() );
properties.put( httpConnector.encryption.name(), "NONE" );
properties.put( httpsConnector.type.name(), "HTTP" );
properties.put( httpsConnector.enabled.name(), String.valueOf( httpsEnabled ) );
properties.put( httpsConnector.address.name(), httpsAddress.toString() );
properties.put( httpsConnector.encryption.name(), "TLS" );
properties.put( GraphDatabaseSettings.auth_enabled.name(), "false" );
properties.put( LegacySslPolicyConfig.certificates_directory.name(),
new File( temporaryFolder, "certificates" ).getAbsolutePath() );
properties.put( GraphDatabaseSettings.logs_directory.name(),
new File( temporaryFolder, "logs" ).getAbsolutePath() );
properties.put( GraphDatabaseSettings.logical_logs_location.name(),
new File( temporaryFolder, "transaction-logs" ).getAbsolutePath() );
properties.put( GraphDatabaseSettings.pagecache_memory.name(), "8m" );
for ( Object key : arbitraryProperties.keySet() )
{
properties.put( String.valueOf( key ), String.valueOf( arbitraryProperties.get( key ) ) );
}
return properties;
}
protected CommunityServerBuilder( LogProvider logProvider )
{
this.logProvider = logProvider;
}
public CommunityServerBuilder persistent()
{
this.persistent = true;
return this;
}
public CommunityServerBuilder withMaxJettyThreads( int maxThreads )
{
this.maxThreads = String.valueOf( maxThreads );
return this;
}
public CommunityServerBuilder usingDataDir( String dataDir )
{
this.dataDir = dataDir;
return this;
}
public CommunityServerBuilder withRelativeManagementApiUriPath( String uri )
{
try
{
URI theUri = new URI( uri );
if ( theUri.isAbsolute() )
{
this.managementUri = theUri.getPath();
}
else
{
this.managementUri = theUri.toString();
}
}
catch ( URISyntaxException e )
{
throw new RuntimeException( e );
}
return this;
}
public CommunityServerBuilder withRelativeRestApiUriPath( String uri )
{
try
{
URI theUri = new URI( uri );
if ( theUri.isAbsolute() )
{
this.restUri = theUri.getPath();
}
else
{
this.restUri = theUri.toString();
}
}
catch ( URISyntaxException e )
{
throw new RuntimeException( e );
}
return this;
}
public CommunityServerBuilder withDefaultDatabaseTuning()
{
return this;
}
public CommunityServerBuilder withThirdPartyJaxRsPackage( String packageName, String mountPoint )
{
thirdPartyPackages.put( packageName, mountPoint );
return this;
}
public CommunityServerBuilder withAutoIndexingEnabledForNodes( String... keys )
{
autoIndexedNodeKeys = keys;
return this;
}
public CommunityServerBuilder onRandomPorts()
{
this.onHttpsAddress( ANY_ADDRESS );
this.onAddress( ANY_ADDRESS );
return this;
}
public CommunityServerBuilder onAddress( ListenSocketAddress address )
{
this.address = address;
return this;
}
public CommunityServerBuilder onHttpsAddress( ListenSocketAddress address )
{
this.httpsAddress = address;
return this;
}
public CommunityServerBuilder withSecurityRules( String... securityRuleClassNames )
{
this.securityRuleClassNames = securityRuleClassNames;
return this;
}
public CommunityServerBuilder withHttpsEnabled()
{
httpsEnabled = true;
return this;
}
public CommunityServerBuilder withProperty( String key, String value )
{
arbitraryProperties.put( key, value );
return this;
}
protected DatabaseActions createDatabaseActionsObject( Database database, Config config )
{
Clock clockToUse = (clock != null) ? clock : Clocks.systemClock();
return new DatabaseActions(
new LeaseManager( clockToUse ),
config.get( ServerSettings.script_sandboxing_enabled ), database.getGraph() );
}
private File buildBefore() throws IOException
{
File configFile = createConfigFiles();
if ( preflightTasks == null )
{
preflightTasks = new PreFlightTasks( NullLogProvider.getInstance() )
{
@Override
public boolean run()
{
return true;
}
};
}
return configFile;
}
private class TestCommunityNeoServer extends CommunityNeoServer
{
private final File configFile;
private TestCommunityNeoServer( Config config, File configFile, GraphDatabaseFacadeFactory
.Dependencies dependencies, LogProvider logProvider )
{
super( config, lifecycleManagingDatabase( persistent ? COMMUNITY_FACTORY : IN_MEMORY_DB ), dependencies,
logProvider );
this.configFile = configFile;
}
@Override
protected DatabaseActions createDatabaseActions()
{
return createDatabaseActionsObject( database, getConfig() );
}
@Override
public void stop()
{
super.stop();
if ( configFile != null )
{
configFile.delete();
}
}
}
}