Advertisement
Guest User

Neo4j spring data configuration heroku

a guest
Apr 17th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package com.example.config;
  2.  
  3. import lombok.SneakyThrows;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.neo4j.graphdb.GraphDatabaseService;
  6. import org.neo4j.graphdb.factory.GraphDatabaseFactory;
  7. import org.neo4j.io.fs.FileUtils;
  8. import org.neo4j.rest.graphdb.RestAPIImpl;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.Profile;
  13. import org.springframework.core.env.Environment;
  14. import org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase;
  15.  
  16. import java.io.File;
  17. import java.net.URL;
  18.  
  19. @Configuration
  20. @Slf4j
  21. public class Neo4jConfiguration {
  22.  
  23.     private static final String HEROKU_NEO4J_ENV="GRAPHSTORY_URL";
  24.  
  25.     @Autowired
  26.     private Environment environment;
  27.  
  28.     @Bean(name = "graphDatabaseService")
  29.     @Profile("!unit")
  30.     @SneakyThrows
  31.     public GraphDatabaseService graphDatabaseServiceHeroku() {
  32.         log.info("Using heroku neo4j");
  33.         if (!environment.containsProperty(HEROKU_NEO4J_ENV))
  34.             throw new RuntimeException(String.format("The heroku environment variable `%s` was not found", HEROKU_NEO4J_ENV));
  35.         String url = environment.getProperty(HEROKU_NEO4J_ENV);
  36.         URL realUrl = new URL(url);
  37.         String newUrl = String.format("%s://%s:%d", realUrl.getProtocol(), realUrl.getHost(), realUrl.getPort());
  38.         String[] auth = realUrl.getUserInfo().split(":");
  39.         String user = auth[0];
  40.         String passwd = auth[1];
  41.         log.info("Using credentials: D='{}' U='{}' P='{}'", newUrl, user, passwd);
  42.         return new SpringCypherRestGraphDatabase(new RestAPIImpl(newUrl, user, passwd));
  43.     }
  44.  
  45.  
  46.     @Bean(name = "graphDatabaseService", destroyMethod = "shutdown")
  47.     @SneakyThrows
  48.     @Profile("unit")
  49.     public GraphDatabaseService graphDatabaseServiceUnit() {
  50.         log.info("Using embedded neo4j");
  51.         File tmpDbLocation = new File("tmp/db");
  52.         FileUtils.deleteRecursively(tmpDbLocation);
  53.         return new GraphDatabaseFactory().newEmbeddedDatabase(tmpDbLocation.getPath());
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement