private static URL getClasspathResourceUrl(String path) throws FileNotFoundException { ClassLoader cl = ClassUtils.getDefaultClassLoader(); URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); if (url == null) { String description = "class path resource [" + path + "]"; throw new FileNotFoundException(description + " cannot be resolved to URL because it does not exist"); } return url; }
/** * 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; }
/** * @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; }
private static File createTemporaryGraphStore() { try { Path path = Files.createTempDirectory("graph.db"); Path databaseDirectory = Paths.get(path.toFile().getAbsolutePath() + "/database"); Files.createDirectories(databaseDirectory); Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { FileUtils.deleteDirectory(path); } catch (IOException e) { throw new RuntimeException("Failed to delete temporary files in " + path, e); } })); return databaseDirectory.toFile(); } catch (IOException e) { throw new RuntimeException(e); } }
public ClasspathConfigurationSource(String propertiesFileName) { try (InputStream is = ClassUtils.getDefaultClassLoader().getResourceAsStream(propertiesFileName)) { properties.load(is); } catch (IOException e) { throw new RuntimeException("Could not load properties file: " + propertiesFileName, e); } }
@Test public void classpathUrlsShouldWork() throws FileNotFoundException { URL url = ResourceUtils.getResourceUrl("classpath:some.properties"); assertThat(url).isNotNull(); }
@AfterClass public static void deleteEmbeddedStore() throws IOException { FileUtils.deleteDirectory(TMP_NEO4J); }
public static AttributeConverter<?, ?> getBigDecimalCollectionConverter(String collectionType) { try { Class collectionClazz = Class .forName(collectionType, false, ClassUtils.getDefaultClassLoader()); return new NumberCollectionStringConverter(BigDecimal.class, collectionClazz); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void protocollessUrlsShouldBeTreatedAsClasspathUrls() throws FileNotFoundException { URL url = ResourceUtils.getResourceUrl("some.properties"); assertThat(url).isNotNull(); }
@Test // GH-169 public void shouldCreateDirectoryIfMissing() throws IOException { final String EMBEDDED_DIR = "/var/tmp/ogmEmbeddedDir"; Path path = Paths.get(EMBEDDED_DIR); if (Files.exists(path)) { FileUtils.deleteDirectory(path); } Configuration configuration = new Configuration.Builder().uri("file://" + EMBEDDED_DIR).build(); try (EmbeddedDriver driver = new EmbeddedDriver()) { driver.configure(configuration); assertThat(configuration.getURI()).isEqualTo("file://" + EMBEDDED_DIR); assertThat(driver.unwrap(GraphDatabaseService.class)).isNotNull(); assertThat(Files.exists(path)).isTrue(); } FileUtils.deleteDirectory(path); }
/** * Utility method to load the dedicated driver version of native types. * * @param nativeTypesImplementation the fully qualified name of the class implementing this drivers' natives types. * @return A fully loaded and initialized instance of the class qualified by <code>nativeTypesImplementation</code> * @throws ClassNotFoundException If the required implementation is not on the classpath. Initialization should terminate then. * @since 3.2 */ private static TypeSystem loadNativeTypes(String nativeTypesImplementation) throws ClassNotFoundException { try { Class<TypeSystem> nativeTypesClass = (Class<TypeSystem>) Class .forName(nativeTypesImplementation, true, ClassUtils.getDefaultClassLoader()); Constructor<TypeSystem> ctor = nativeTypesClass.getDeclaredConstructor(); ctor.setAccessible(true); return ctor.newInstance(); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException("Could not load native types implementation " + nativeTypesImplementation); } }
@Test public void fileUrlsShouldWork() throws IOException { Path path = Files.createTempFile("ResourceUtilsTest", ".properties"); URL url = ResourceUtils.getResourceUrl(path.toUri().toURL().toString()); assertThat(url).isNotNull(); }
public static AttributeConverter<?, ?> getBigIntegerCollectionConverter(String collectionType) { try { Class collectionClazz = Class .forName(collectionType, false, ClassUtils.getDefaultClassLoader()); return new NumberCollectionStringConverter(BigInteger.class, collectionClazz); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void shouldThrowFileNotFoundOnInvalidResources() { assertThatExceptionOfType(FileNotFoundException.class) .isThrownBy(() -> ResourceUtils.getResourceUrl("idontexists")); } }
public static StringBuilder readCQLFile(String cqlFileName) { StringBuilder cypher = new StringBuilder(); try (Scanner scanner = new Scanner( ClassUtils.getDefaultClassLoader().getResourceAsStream(cqlFileName))) { scanner.useDelimiter(System.getProperty("line.separator")); while (scanner.hasNext()) { cypher.append(scanner.next()).append(' '); } } return cypher; }
@Test public void shouldThrowFileNotFoundOnInvalidUrls() { assertThatExceptionOfType(FileNotFoundException.class) .isThrownBy(() -> ResourceUtils.getResourceUrl("invalid://test.com")); }
static boolean canRunHATests() { try { Class.forName("org.neo4j.graphdb.factory.HighlyAvailableGraphDatabaseFactory", false, ClassUtils.getDefaultClassLoader()); return true; } catch (ClassNotFoundException e) { return false; } } }
public static AttributeConverter<?, ?> getDateCollectionConverter(String collectionType) { try { Class collectionClazz = Class .forName(collectionType, false, ClassUtils.getDefaultClassLoader()); return new DateCollectionStringConverter(DateString.ISO_8601, collectionClazz); } catch (Exception e) { throw new RuntimeException(e); } }
public static AttributeConverter<?, ?> getEnumCollectionConverter(Class enumClass, String collectionType) { try { Class collectionClazz = Class .forName(collectionType, false, ClassUtils.getDefaultClassLoader()); return new EnumCollectionStringConverter(enumClass, collectionClazz); } catch (Exception e) { throw new RuntimeException(e); } }
public static AttributeConverter<?, ?> getConverterBasedCollectionConverter(AttributeConverter<?, ?> converter, String collectionType) { try { Class collectionClazz = Class .forName(collectionType, false, ClassUtils.getDefaultClassLoader()); return new ConverterBasedCollectionConverter<>(collectionClazz, converter); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }