View Javadoc
1   package org.neo4j.driver;
2   
3   import org.neo4j.driver.v1.Config;
4   
5   import java.io.File;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.util.HashMap;
9   import java.util.Map;
10  import java.util.Properties;
11  import java.util.concurrent.TimeUnit;
12  
13  /**
14   * Read and construct the Neo4j driver configuration.
15   */
16  public class Configuration {
17  
18      /**
19       * Map with key/value found into properties files.
20       */
21      private Map<String, Object> config = new HashMap<>();
22  
23      /**
24       * Construction that parses property files.
25       */
26      public Configuration() {
27          // Loading neo4j-driver.properties
28          Properties prop = new Properties();
29  
30          try (InputStream stream = this.getClass().getClassLoader().getResourceAsStream("neo4j-driver.properties")) {
31              prop.load(stream);
32              for (final String name : prop.stringPropertyNames()) {
33                  this.config.put(name, prop.getProperty(name));
34              }
35          } catch (IOException e) {
36              throw new RuntimeException("File neo4j-driver.properties not found !!!");
37          }
38  
39          // Loading neo4j-driver-ext.properties to override default config
40          Properties propExt = new Properties();
41  
42          try (InputStream streamExt = this.getClass().getClassLoader().getResourceAsStream("neo4j-driver-ext.properties")) {
43              propExt.load(streamExt);
44              for (final String name : propExt.stringPropertyNames()) {
45                  this.config.put(name, propExt.getProperty(name));
46              }
47          } catch (Exception e) {
48              // silent exception, we don't care
49          }
50      }
51  
52      /**
53       * Convert this instance to a Neo4j driver config class.
54       *
55       * @return A Neo4j Config objects
56       */
57      public Config toDriverConfig() {
58          Config.ConfigBuilder cb = Config.build();
59  
60          if (this.config.containsKey("neo4j.maxIdleConnectionPoolSize")) {
61              cb.withMaxIdleSessions(Integer.valueOf((String) this.config.get("neo4j.maxIdleConnectionPoolSize")));
62          }
63  
64          if (this.config.containsKey("neo4j.idleTimeBeforeConnectionTest")) {
65              cb.withConnectionLivenessCheckTimeout(Long.valueOf((String) this.config.get("neo4j.idleTimeBeforeConnectionTest")), TimeUnit.SECONDS);
66          }
67  
68          if (this.config.containsKey("neo4j.logLeakedSessions")) {
69              if (Boolean.valueOf((String) this.config.get("neo4j.logLeakedSessions"))) {
70                  cb.withLeakedSessionsLogging();
71              }
72          }
73  
74          if (this.config.containsKey("neo4j.encrypted")) {
75              if (Boolean.valueOf((String) this.config.get("neo4j.encrypted"))) {
76                  cb.withEncryption();
77              } else {
78                  cb.withoutEncryption();
79              }
80          }
81  
82          if (this.config.containsKey("neo4j.connectionTimeoutMillis")) {
83              cb.withConnectionTimeout(Long.valueOf((String) this.config.get("neo4j.connectionTimeoutMillis")), TimeUnit.MILLISECONDS);
84          }
85  
86          if (this.config.containsKey("neo4j.trustStrategy")) {
87              switch (this.getStringOrDefault("neo4j.trustStrategy", "").toUpperCase()) {
88                  case "ALL":
89                      cb.withTrustStrategy(Config.TrustStrategy.trustAllCertificates());
90                      break;
91                  case "SYSTEM":
92                      cb.withTrustStrategy(Config.TrustStrategy.trustSystemCertificates());
93                      break;
94                  default:
95                      File cert = new File(this.getStringOrDefault("neo4j.trustStrategy", ""));
96                      if (cert.exists()) {
97                          cb.withTrustStrategy(Config.TrustStrategy.trustCustomCertificateSignedBy(cert));
98                      } else {
99                          throw new RuntimeException("Certificate not found");
100                     }
101                     break;
102             }
103         }
104 
105         return cb.toConfig();
106     }
107 
108     /**
109      * Search a value by its key into {@link #config} and transform it as a String.
110      *
111      * @param key   Key to search.
112      * @param value Default value if nkeyis not found.
113      * @return The desired value as a string.
114      */
115     public String getStringOrDefault(String key, String value) {
116         return (String) this.config.getOrDefault(key, value);
117     }
118 }