Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the config dir app.d for application mode if present #3566

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions application-mode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ dependencies {

api project(':proto:proto-backplane-grpc')

implementation project(':Configuration')

Classpaths.inheritJUnitPlatform(project)
Classpaths.inheritAssertJ(project)
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,40 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

public interface ApplicationConfig {

/**
* Returns true if the configuration property {@value ApplicationConfigImpl#APPLICATION_DIR_PROP} is set, or if the
* {@value ApplicationConfigImpl#DEFAULT_APP_DIRNAME} exists in the configuration directory.
*
* @return true if custom application mode is enabled
*/
static boolean isCustomApplicationModeEnabled() {
return ApplicationConfigImpl.APPLICATION_DIR != null;
return ApplicationConfigImpl.applicationDir().isPresent();
}

/**
* The custom application directory. Custom application mode must be enabled.
* The custom application directory. Returns the configuration property
* {@value ApplicationConfigImpl#APPLICATION_DIR_PROP} if it set, otherwise it returns the
* {@value ApplicationConfigImpl#DEFAULT_APP_DIRNAME} from the configuration directory if it exists, otherwise it
* throws an {@link IllegalStateException}.
*
* @return the application dir
* @see #isCustomApplicationModeEnabled()
*/
static Path customApplicationDir() {
if (!isCustomApplicationModeEnabled()) {
final Optional<Path> applicationDir = ApplicationConfigImpl.applicationDir();
if (applicationDir.isEmpty()) {
throw new IllegalStateException(
String.format("Custom application mode is not enabled, please set system property '%s'",
String.format("Custom application mode is not enabled, please set the configuration property '%s'",
ApplicationConfigImpl.APPLICATION_DIR_PROP));
}
final Path applicationDir;
try {
applicationDir = Paths.get(ApplicationConfigImpl.APPLICATION_DIR);
} catch (InvalidPathException e) {
throw new IllegalArgumentException(String.format("Invalid application directory '%s'",
ApplicationConfigImpl.APPLICATION_DIR));
}
return applicationDir;
return applicationDir.get();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/
package io.deephaven.appmode;

import io.deephaven.configuration.ConfigDir;
import io.deephaven.configuration.Configuration;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -11,14 +14,22 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;

public class ApplicationConfigImpl {

public static final String APPLICATION_DIR_PROP = "deephaven.application.dir";
public static final String DEFAULT_APP_DIRNAME = "app.d";

public static final String APPLICATION_DIR = System.getProperty(APPLICATION_DIR_PROP, null);
public static Optional<Path> applicationDir() {
final String explicitDir = Configuration.getInstance().getStringWithDefault(APPLICATION_DIR_PROP, null);
if (explicitDir != null) {
return Optional.of(Path.of(explicitDir));
}
return ConfigDir.get().map(ApplicationConfigImpl::defaultAppDir).filter(Files::isDirectory);
}

public static List<ApplicationConfig> find(Path dir) throws IOException, ClassNotFoundException {
try (Stream<Path> stream =
Expand All @@ -32,6 +43,10 @@ public static List<ApplicationConfig> find(Path dir) throws IOException, ClassNo
}
}

private static Path defaultAppDir(Path configDir) {
return configDir.resolve(DEFAULT_APP_DIRNAME);
}

private static boolean isAppFile(Path path) {
return path.getFileName().toString().endsWith(".app") && Files.isReadable(path)
&& Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS);
Expand Down