Skip to content

Commit

Permalink
Merge be7b89d into d95ec15
Browse files Browse the repository at this point in the history
  • Loading branch information
yaseno2186 authored Jul 26, 2024
2 parents d95ec15 + be7b89d commit 4a76fdb
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected interface StoreOperation {
* If the execution fails.
*/
void withStore(CliConfiguration configuration, StoreOperation storeOperation) throws CliExecutionException {
Store store = storeFactory.getStore(configuration.store(), () -> new File(DEFAULT_STORE_DIRECTORY));
Store store = storeFactory.getStore(configuration.store(), configuration.server(), () -> new File(DEFAULT_STORE_DIRECTORY));
try {
store.start();
storeOperation.run(store);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ protected boolean isConnectorRequired() {
public void run(CliConfiguration configuration, Options options) throws CliExecutionException {
withStore(configuration, store -> {
EmbeddedGraphStore embeddedGraphStore = (EmbeddedGraphStore) store;
EmbeddedNeo4jServer server = embeddedGraphStore.getServer();
server.start();
EmbeddedNeo4jServer server = embeddedGraphStore.getEmbeddedNeo4jServer();
LOGGER.info("Running server");
server.start();
if (configuration.server().daemon()) {
// let the neo4j daemon do the job
LOGGER.info("Running server. Use <Ctrl-C> to stop server.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.buschmais.jqassistant.commandline.CliExecutionException;
import com.buschmais.jqassistant.commandline.configuration.CliConfiguration;
import com.buschmais.jqassistant.core.runtime.api.configuration.ConfigurationBuilder;
import com.buschmais.jqassistant.core.runtime.api.configuration.Server;
import com.buschmais.jqassistant.core.runtime.api.plugin.PluginRepository;
import com.buschmais.jqassistant.core.shared.artifact.ArtifactProvider;
import com.buschmais.jqassistant.core.store.impl.EmbeddedGraphStore;
import com.buschmais.jqassistant.neo4j.embedded.EmbeddedNeo4jServer;
import com.buschmais.jqassistant.neo4j.embedded.api.configuration.Server;

import org.apache.commons.cli.*;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -47,8 +47,9 @@ class ServerTaskTest {
@BeforeEach
final void setUp() {
doReturn(embeddedNeo4jServer).when(store)
.getServer();
doReturn(server).when(configuration).server();
.getEmbeddedNeo4jServer();
doReturn(server).when(configuration)
.server();
serverTask = new ServerTask() {
@Override
void withStore(CliConfiguration configuration, StoreOperation storeOperation) throws CliExecutionException {
Expand All @@ -59,7 +60,8 @@ void withStore(CliConfiguration configuration, StoreOperation storeOperation) th

@Test
void daemon() throws CliExecutionException, ParseException {
doReturn(true).when(server).daemon();
doReturn(true).when(server)
.daemon();

startServer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static class DistributionParameterResolver implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.getIndex() == 0 && parameterContext.getParameter()
.getType() == DISTRIBUTION.class;
.getType() == DISTRIBUTION.class;
}

@Override
Expand All @@ -92,7 +92,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
}

public static final String RULES_DIRECTORY = AbstractCLIIT.class.getResource("/rules")
.getFile();
.getFile();

public static final String TEST_CONCEPT = "default:TestConcept";
public static final String TEST_CONCEPT_WITH_PARAMETER = "default:TestConceptWithParameter";
Expand Down Expand Up @@ -138,12 +138,12 @@ public int getExitCode() {
@BeforeAll
public static void beforeAll() {
userHome = AbstractCLIIT.class.getResource("/userhome/")
.getFile();
.getFile();
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder("CLI IT", 110);
configurationBuilder.with(com.buschmais.jqassistant.core.store.api.configuration.Store.class,
com.buschmais.jqassistant.core.store.api.configuration.Store.URI, "bolt://localhost:7687");
com.buschmais.jqassistant.core.store.api.configuration.Store.URI, "bolt://localhost:7687");
configuration = ConfigurationMappingLoader.builder(CliConfiguration.class)
.load(configurationBuilder.build());
.load(configurationBuilder.build());
}

/**
Expand All @@ -156,7 +156,7 @@ public void before(DISTRIBUTION distribution) throws IOException {
.isGreaterThanOrEqualTo(distribution.minRuntimeVersion.feature())
.isLessThanOrEqualTo(distribution.maxRuntimeVersion.feature());
this.neo4jVersion = distribution.name()
.toLowerCase(Locale.getDefault());
.toLowerCase(Locale.getDefault());
this.jqaHome = getjQAHomeDirectory(neo4jVersion);
File workingDirectory = getWorkingDirectory();
FileUtils.cleanDirectory(workingDirectory);
Expand Down Expand Up @@ -191,11 +191,11 @@ protected String getNeo4jVersion() {
* Execute the shell script.
*
* @param args
* The arguments.
* The arguments.
* @throws IOException
* If an error occurs.
* If an error occurs.
* @throws InterruptedException
* If an error occurs.
* If an error occurs.
*/
protected ExecutionResult execute(String... args) {
assumeTrue("Test cannot be executed on this operating system.", SystemUtils.IS_OS_WINDOWS || SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC_OSX);
Expand All @@ -219,7 +219,7 @@ protected ExecutionResult execute(String... args) {
builder.directory(workingDirectory);

log.info("Executing '{}'.", command.stream()
.collect(joining(" ")));
.collect(joining(" ")));
Process process;
try {
process = builder.start();
Expand All @@ -232,10 +232,10 @@ protected ExecutionResult execute(String... args) {
executorService.submit(standardConsole);
executorService.submit(errorConsole);
return ExecutionResult.builder()
.process(process)
.standardConsole(standardConsole.getOutput())
.errorConsole(errorConsole.getOutput())
.build();
.process(process)
.standardConsole(standardConsole.getOutput())
.errorConsole(errorConsole.getOutput())
.build();
}

/**
Expand All @@ -245,7 +245,7 @@ protected ExecutionResult execute(String... args) {
*/
protected File getWorkingDirectory() {
File workingDirectory = new File("target" + "/" + this.getClass()
.getSimpleName());
.getSimpleName());
workingDirectory.mkdirs();
return workingDirectory;
}
Expand All @@ -262,7 +262,7 @@ protected File getDefaultReportDirectory() {
protected void withStore(File directory, StoreOperation storeOperation) {
ExecutionResult serverExecutionResult = execute("server", "-Djqassistant.store.uri=" + directory.toURI());
try {
Store remoteStore = storeFactory.getStore(configuration.store(), () -> directory);
Store remoteStore = storeFactory.getStore(configuration.store(), configuration.server(), () -> directory);
waitAtMost(30, SECONDS).untilAsserted(() -> assertThatNoException().isThrownBy(() -> remoteStore.start()));
try {
storeOperation.run(remoteStore);
Expand All @@ -271,7 +271,7 @@ protected void withStore(File directory, StoreOperation storeOperation) {
}
} finally {
PrintWriter printWriter = new PrintWriter(serverExecutionResult.getProcess()
.getOutputStream());
.getOutputStream());
printWriter.println();
printWriter.flush();
}
Expand Down Expand Up @@ -302,9 +302,9 @@ private static class ConsoleReader implements Runnable {
* Constructor.
*
* @param stream
* The stream to redirect.
* The stream to redirect.
* @param printStream
* The target stream.
* The target stream.
*/
private ConsoleReader(InputStream stream, PrintStream printStream) {
this.stream = stream;
Expand Down
5 changes: 5 additions & 0 deletions core/neo4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.buschmais.jqassistant.core</groupId>
<artifactId>shared</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
public interface EmbeddedNeo4jServer {

// TODO This method should take com.buschmais.jqassistant.core.store.api.configuration.Embedded as parameter instead of the single values. Requires moving the embedded Neo4j server to the core store module.
void initialize(EmbeddedDatastore embeddedDatastore, String listenAddress, Integer httpPort, Integer boltPort, ClassLoader classLoader,
Collection<Class<?>> procedureTypes, Collection<Class<?>> functionTypes);
void initialize(EmbeddedDatastore embeddedDatastore, com.buschmais.jqassistant.neo4j.embedded.api.configuration.Server server, String listenAddress,
Integer httpPort, Integer boltPort, ClassLoader classLoader, Collection<Class<?>> procedureTypes, Collection<Class<?>> functionTypes);

void start();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.buschmais.jqassistant.core.runtime.api.configuration;
package com.buschmais.jqassistant.neo4j.embedded.api.configuration;

import com.buschmais.jqassistant.core.shared.annotation.Description;

Expand All @@ -14,4 +14,10 @@ public interface Server {
@WithDefault("false")
boolean daemon();

String OPEN_BROWSER = "open-browser";

@Description("Open server URL in browser.")
@WithDefault("false")
boolean openBrowser();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.buschmais.jqassistant.neo4j.embedded.impl;

import java.awt.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;

import com.buschmais.jqassistant.neo4j.embedded.EmbeddedNeo4jServer;
Expand Down Expand Up @@ -33,14 +37,17 @@ class Neo4jCommunityNeoServer implements EmbeddedNeo4jServer {
private Integer httpPort;
private Integer boltPort;

private com.buschmais.jqassistant.neo4j.embedded.api.configuration.Server serverConfiguration;

@Override
public final void initialize(EmbeddedDatastore embeddedDatastore, String listenAddress, Integer httpPort, Integer boltPort, ClassLoader classLoader,
Collection<Class<?>> procedureTypes, Collection<Class<?>> functionTypes) {
public void initialize(EmbeddedDatastore embeddedDatastore, com.buschmais.jqassistant.neo4j.embedded.api.configuration.Server server, String listenAddress,
Integer httpPort, Integer boltPort, ClassLoader classLoader, Collection<Class<?>> procedureTypes, Collection<Class<?>> functionTypes) {
this.embeddedDatastore = embeddedDatastore;
this.classLoader = classLoader;
this.listenAddress = listenAddress;
this.httpPort = httpPort;
this.boltPort = boltPort;
this.serverConfiguration = server;
registerProceduresAndFunctions(procedureTypes, functionTypes);
}

Expand All @@ -56,7 +63,24 @@ public void start() {
} catch (Exception e) {
throw new IllegalStateException("Cannot start embedded server.", e);
}
LOGGER.info("Neo4j browser available at http://{}:{}?dbms=bolt://{}:{}&preselectAuthMethod=NO_AUTH.", listenAddress, httpPort, listenAddress,boltPort);
String url = String.format("http://%s:%d?dbms=bolt://%s:%d&preselectAuthMethod=NO_AUTH", listenAddress, httpPort, listenAddress, boltPort);
if (serverConfiguration.openBrowser()) {
openBrowser(url);
} else {
LOGGER.info("Neo4j browser available at {}.", url);
}
}

@Override
public void stop() {
if (server != null) {
LOGGER.info("Stopping HTTP server.");
try {
server.stop();
} catch (Exception e) {
throw new IllegalStateException("Cannot stop embedded server.", e);
}
}
}

private WebAppContext getWebAppContext(String contextPath, String resourceRoot) {
Expand All @@ -67,14 +91,15 @@ private WebAppContext getWebAppContext(String contextPath, String resourceRoot)
return webAppContext;
}

@Override
public void stop() {
if (server != null) {
LOGGER.info("Stopping HTTP server.");
private static void openBrowser(String url) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop()
.isSupported(Desktop.Action.BROWSE)) {
log.info("Opening browser using URL {}.", url);
try {
server.stop();
} catch (Exception e) {
throw new IllegalStateException("Cannot stop embedded server.", e);
Desktop.getDesktop()
.browse(new URI(url));
} catch (IOException | URISyntaxException e) {
log.warn("Cannot open browser for URL {}", url, e);
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion core/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,11 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.21.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.buschmais.jqassistant.core.shared.annotation.Description;
import com.buschmais.jqassistant.core.shared.configuration.Plugin;
import com.buschmais.jqassistant.core.store.api.configuration.Store;
import com.buschmais.jqassistant.neo4j.embedded.api.configuration.Server;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.buschmais.jqassistant.core.store.impl.EmbeddedGraphStore;
import com.buschmais.jqassistant.core.store.impl.RemoteGraphStore;
import com.buschmais.jqassistant.core.store.spi.StorePluginRepository;
import com.buschmais.jqassistant.neo4j.embedded.api.configuration.Server;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
Expand All @@ -25,7 +26,7 @@ public class StoreFactory {

private final ArtifactProvider artifactProvider;

public Store getStore(com.buschmais.jqassistant.core.store.api.configuration.Store configuration, Supplier<File> storeDirectorySupplier) {
public Store getStore(com.buschmais.jqassistant.core.store.api.configuration.Store configuration, Server server, Supplier<File> storeDirectorySupplier) {
URI uri = configuration.uri()
.orElse(storeDirectorySupplier.get()
.toURI())
Expand All @@ -38,7 +39,7 @@ public Store getStore(com.buschmais.jqassistant.core.store.api.configuration.Sto
switch (scheme.toLowerCase()) {
case "file":
case "memory":
return new EmbeddedGraphStore(uri, configuration, storePluginRepository, artifactProvider);
return new EmbeddedGraphStore(uri, configuration, server, storePluginRepository, artifactProvider);
default:
configuration.remote()
.username()
Expand Down
Loading

0 comments on commit 4a76fdb

Please sign in to comment.