LocalEnvironment implements Environment { @Override public ClassLoader getClassLoader( Object caller, String... classpathEntries ) { caller = Objects.requireNonNull(caller); Set<ClassLoader> delegates = new LinkedHashSet<>(); List<String> urls = Arrays.stream(classpathEntries).filter(StringUtil::notBlank).collect(Collectors.toList()); if (urls.isEmpty()) { StringURLClassLoader urlClassLoader = new StringURLClassLoader(urls); if (urlClassLoader.getURLs().length > 0) { delegates.add(urlClassLoader); } } ClassLoader currentLoader = getClass().getClassLoader(); ClassLoader callerLoader = caller.getClass().getClassLoader(); ClassLoader tccl = Thread.currentThread().getContextClassLoader(); if (!callerLoader.equals(tccl) && !currentLoader.equals(tccl)) { delegates.add(tccl); } if (!callerLoader.equals(currentLoader)) { if (currentLoader.equals(callerLoader.getParent())) { currentLoader = callerLoader; } else { delegates.add(callerLoader); } } return delegates.isEmpty() ? currentLoader : new DelegatingClassLoader(currentLoader, delegates); } LocalEnvironment(); @Override Channel getChannel(String name); @Override ClassLoader getClassLoader( Object caller,
                                       String... classpathEntries );  }
@Test public void shouldLoadDatabaseFromConfiguration() throws Exception { String pathToStorage = "target/repo/Library/content"; FileUtil.delete(pathToStorage); String configFilePath = "config/repo-config-inmemory-local-environment.json"; InputStream configFileStream = getClass().getClassLoader().getResourceAsStream(configFilePath); RepositoryConfiguration repositoryConfiguration = RepositoryConfiguration.read(configFileStream, "doesn't matter"); LocalEnvironment environment = new LocalEnvironment(); assertNotNull(environment.getDb(repositoryConfiguration.getPersistenceConfiguration())); }
@Test public void shouldStartRepositoryUsingLocalEnvironmentWithDefaultPersistenceConfiguration() throws Exception { String configFilePath = "config/repo-config-inmemory-no-persistence.json"; InputStream configFileStream = getClass().getClassLoader().getResourceAsStream(configFilePath); RepositoryConfiguration repositoryConfiguration = RepositoryConfiguration.read(configFileStream, "doesn't matter"); LocalEnvironment environment = new LocalEnvironment(); repositoryConfiguration = repositoryConfiguration.with(environment); ModeShapeEngine engine = new ModeShapeEngine(); engine.start(); try { JcrRepository repository = engine.deploy(repositoryConfiguration); Session session = repository.login(); Node root = session.getRootNode(); root.addNode("Library", "nt:folder"); session.save(); session.logout(); session = repository.login(); Node library = session.getNode("/Library"); assertThat(library, is(notNullValue())); assertThat(library.getPrimaryNodeType().getName(), is("nt:folder")); session.logout(); } finally { engine.shutdown().get(); environment.shutdown(); } }