/** * @return True if current configuration is setup to use embedded HA. */ public boolean isEmbeddedHA() { boolean isEmbeddedHA = false; if (this.neo4jConfLocation != null) { try { URL url = ResourceUtils.getResourceUrl(neo4jConfLocation); Properties neo4Properties = new Properties(); neo4Properties.load(url.openStream()); isEmbeddedHA = "HA".equalsIgnoreCase(neo4Properties.getProperty("dbms.mode", "-")); } catch (IOException e) { throw new UncheckedIOException("Could not load neo4j.conf at location " + neo4jConfLocation, e); } } return isEmbeddedHA; }
/** * Extracts an URL from the name of a resource. The idea is similar to Springs * {@code org.springframework.core.io.DefaultResourceLoader} respectively * {@code org.springframework.util.ResourceUtils}. * * @param resourceLocation The location of the resource, must not be null. * @return URL of the given resource location * @throws FileNotFoundException In all cases where an URL cannot be determined */ public static URL getResourceUrl(String resourceLocation) throws FileNotFoundException { Objects.requireNonNull(resourceLocation, "Resource location must not be null"); URL resourceUrl; if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { resourceUrl = getClasspathResourceUrl(resourceLocation.substring(CLASSPATH_URL_PREFIX.length())); } else { try { resourceUrl = new URL(resourceLocation); } catch (MalformedURLException e) { // Not a protocol? Try classpath resource URL, again resourceUrl = getClasspathResourceUrl(resourceLocation); } } return resourceUrl; }
@Test public void fileUrlsShouldWork() throws IOException { Path path = Files.createTempFile("ResourceUtilsTest", ".properties"); URL url = ResourceUtils.getResourceUrl(path.toUri().toURL().toString()); assertThat(url).isNotNull(); }
@Test public void classpathUrlsShouldWork() throws FileNotFoundException { URL url = ResourceUtils.getResourceUrl("classpath:some.properties"); assertThat(url).isNotNull(); }
@Test public void protocollessUrlsShouldBeTreatedAsClasspathUrls() throws FileNotFoundException { URL url = ResourceUtils.getResourceUrl("some.properties"); assertThat(url).isNotNull(); }
@Override public synchronized void configure(Configuration newConfiguration) { super.configure(newConfiguration); try { String fileStoreUri = newConfiguration.getURI(); // if no URI is set, create a temporary folder for the graph db // that will persist only for the duration of the JVM // This is effectively what the ImpermanentDatabase does. if (fileStoreUri == null) { fileStoreUri = createTemporaryFileStore(); } else { createPermanentFileStore(fileStoreUri); } File file = new File(new URI(fileStoreUri)); if (!file.exists()) { throw new RuntimeException("Could not create/open filestore: " + fileStoreUri); } GraphDatabaseBuilder graphDatabaseBuilder = getGraphDatabaseFactory(newConfiguration) .newEmbeddedDatabaseBuilder(file); String neo4jConfLocation = newConfiguration.getNeo4jConfLocation(); if (neo4jConfLocation != null) { URL neo4ConfUrl = ResourceUtils.getResourceUrl(neo4jConfLocation); graphDatabaseBuilder = graphDatabaseBuilder.loadPropertiesFromURL(neo4ConfUrl); } this.graphDatabaseService = graphDatabaseBuilder.newGraphDatabase(); } catch (Exception e) { throw new ConnectionException("Error connecting to embedded graph", e); } }
@Override public synchronized void configure(Configuration config) { super.configure(config); try { String fileStoreUri = config.getURI(); // if no URI is set, create a temporary folder for the graph db // that will persist only for the duration of the JVM // This is effectively what the ImpermanentDatabase does. if (fileStoreUri == null) { fileStoreUri = createTemporaryFileStore(); } else { createPermanentFileStore(fileStoreUri); } File file = new File(new URI(fileStoreUri)); if (!file.exists()) { throw new RuntimeException("Could not create/open filestore: " + fileStoreUri); } GraphDatabaseBuilder graphDatabaseBuilder = getGraphDatabaseFactory(configuration) .newEmbeddedDatabaseBuilder(file); String neo4jConfLocation = config.getNeo4jConfLocation(); if(neo4jConfLocation != null) { URL neo4ConfUrl = ResourceUtils.getResourceUrl(neo4jConfLocation); graphDatabaseBuilder = graphDatabaseBuilder.loadPropertiesFromURL(neo4ConfUrl); } this.graphDatabaseService = graphDatabaseBuilder.newGraphDatabase(); } catch (Exception e) { throw new ConnectionException("Error connecting to embedded graph", e); } }
@Test public void shouldThrowFileNotFoundOnInvalidUrls() { assertThatExceptionOfType(FileNotFoundException.class) .isThrownBy(() -> ResourceUtils.getResourceUrl("invalid://test.com")); }
@Test public void shouldThrowFileNotFoundOnInvalidResources() { assertThatExceptionOfType(FileNotFoundException.class) .isThrownBy(() -> ResourceUtils.getResourceUrl("idontexists")); } }