Skip to content

Commit

Permalink
ApplicationService.Config: Use typesafe Path for baseDir
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Aug 20, 2023
1 parent dc4e4da commit 1dda9a9
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 34 deletions.
33 changes: 22 additions & 11 deletions application/src/main/java/bisq/application/ApplicationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Slf4j
Expand All @@ -56,21 +58,25 @@ private static Config from(com.typesafe.config.Config config, String[] args) {
appName = config.getString("appName");
}

String dataDir = null;
Optional<Path> dataDir = Optional.empty();
for (String arg : args) {
if (arg.startsWith("--appName")) {
appName = arg.split("=")[1];
}

if (arg.startsWith("--data-dir")) {
dataDir = arg.split("=")[1];
dataDir = Optional.of(
Paths.get(arg.split("=")[1])
);
}
}

String appDir = dataDir == null ? OsUtils.getUserDataDir() + File.separator + appName : dataDir;
log.info("Use application directory {}", appDir);
Path appDataDir = dataDir.orElse(
OsUtils.getUserDataDir().resolve(appName)
);
log.info("Use application directory {}", appDataDir);

return new Config(appDir,
return new Config(appDataDir,
appName,
config.getString("version"),
config.getBoolean("devMode"),
Expand All @@ -79,15 +85,15 @@ private static Config from(com.typesafe.config.Config config, String[] args) {
config.getBoolean("ignoreSignatureVerification"));
}

private final String baseDir;
private final Path baseDir;
private final String appName;
private final Version version;
private final boolean devMode;
private final List<String> keyIds;
private final boolean ignoreSigningKeyInResourcesCheck;
private final boolean ignoreSignatureVerification;

public Config(String baseDir,
public Config(Path baseDir,
String appName,
String version,
boolean devMode,
Expand Down Expand Up @@ -122,14 +128,15 @@ public ApplicationService(String configFileName, String[] args) {
typesafeAppConfig = typesafeConfig.getConfig("application");
config = Config.from(typesafeAppConfig, args);

Path dataDir = config.getBaseDir();
try {
FileUtils.makeDirs(config.getBaseDir());
FileUtils.makeDirs(dataDir.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
checkInstanceLock();

LogSetup.setup(Paths.get(config.getBaseDir(), "bisq").toString());
LogSetup.setup(dataDir.resolve("bisq").toString());
LogSetup.setLevel(Level.INFO);

DevMode.setDevMode(config.isDevMode());
Expand All @@ -141,13 +148,17 @@ public ApplicationService(String configFileName, String[] args) {
Res.setLanguage(LanguageRepository.getDefaultLanguage());
ResolverConfig.config();

persistenceService = new PersistenceService(config.getBaseDir());
String absoluteDataDirPath = dataDir.toAbsolutePath().toString();
persistenceService = new PersistenceService(absoluteDataDirPath);
}

private void checkInstanceLock() {
// Acquire exclusive lock on file basedir/lock, throw if locks fails
// to avoid running multiple instances using the same basedir
try (FileOutputStream fileOutputStream = new FileOutputStream(Paths.get(config.getBaseDir(), "lock").toString())) {
File lockFilePath = config.getBaseDir()
.resolve("lock")
.toFile();
try (FileOutputStream fileOutputStream = new FileOutputStream(lockFilePath)) {
instanceLock = fileOutputStream.getChannel().tryLock();
} catch (Exception e) {
e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion application/src/main/java/bisq/updater/UpdaterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void onNewReleaseNotificationAdded(ReleaseNotification releaseNotificatio
public CompletableFuture<Void> downloadAndVerify() throws IOException {
String version = releaseNotification.get().getVersionString();
boolean isLauncherUpdate = releaseNotification.get().isLauncherUpdate();
String baseDir = config.getBaseDir();
String baseDir = config.getBaseDir().toAbsolutePath().toString();
List<String> keyIds = config.getKeyIds();
checkArgument(!keyIds.isEmpty());

Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/bisq/common/util/OsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public class OsUtils {
public static final int EXIT_SUCCESS = 0;
public static final int EXIT_FAILURE = 1;

public static File getUserDataDir() {
public static Path getUserDataDir() {
if (isWindows()) {
return new File(System.getenv("APPDATA"));
return Paths.get(System.getenv("APPDATA"));
}

if (isMac()) {
return Paths.get(System.getProperty("user.home"), "Library", "Application Support").toFile();
return Paths.get(System.getProperty("user.home"), "Library", "Application Support");
}

// *nix
return Paths.get(System.getProperty("user.home"), ".local", "share").toFile();
return Paths.get(System.getProperty("user.home"), ".local", "share");
}

public static int availableProcessors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SoundPlayer implements PreventStandbyMode {
private ExecutorService executor;

SoundPlayer(ServiceProvider serviceProvider) {
baseDir = serviceProvider.getConfig().getBaseDir();
baseDir = serviceProvider.getConfig().getBaseDir().toAbsolutePath().toString();
}

public void initialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public abstract class Overlay<T extends Overlay<T>> {

public static void init(ServiceProvider serviceProvider, Region primaryStageOwner) {
Overlay.primaryStageOwner = primaryStageOwner;
Overlay.baseDir = serviceProvider.getConfig().getBaseDir();
Overlay.baseDir = serviceProvider.getConfig().getBaseDir().toAbsolutePath().toString();
Overlay.settingsService = serviceProvider.getSettingsService();
Overlay.shutdownHandler = serviceProvider.getShutDownHandler();
}
Expand Down
3 changes: 2 additions & 1 deletion desktop/src/main/java/bisq/desktop/main/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public void onActivate() {
if (getClass().getClassLoader() instanceof URLClassLoader) {
// We only verify version if we have been loaded as jar into the launcher.
// In that case our class loader is of typ URLClassLoader.
Optional<String> versionFromVersionFile = UpdaterUtils.readVersionFromVersionFile(config.getBaseDir());
String baseDirPath = config.getBaseDir().toAbsolutePath().toString();
Optional<String> versionFromVersionFile = UpdaterUtils.readVersionFromVersionFile(baseDirPath);
if (versionFromVersionFile.isPresent()) {
if (!config.getVersion().toString().equals(versionFromVersionFile.get())) {
String errorMsg = "Version of application (v" + config.getVersion() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class UtilsController implements Controller {
private final PersistenceService persistenceService;

public UtilsController(ServiceProvider serviceProvider) {
baseDir = serviceProvider.getConfig().getBaseDir();
baseDir = serviceProvider.getConfig().getBaseDir().toAbsolutePath().toString();
appName = serviceProvider.getConfig().getAppName();
persistenceService = serviceProvider.getPersistenceService();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void onDeactivate() {
}

void onImportNodeAddress() {
Path path = Path.of(serviceProvider.getConfig().getBaseDir());
Path path = serviceProvider.getConfig().getBaseDir();
File file = FileChooserUtil.openFile(getView().getRoot().getScene(), path.toAbsolutePath().toString());
if (file != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -95,15 +94,16 @@ public DesktopApplicationService(String[] args, ShutDownHandler shutDownHandler)
walletService = Optional.of(new BitcoinWalletService(BitcoinWalletService.Config.from(bitcoinWalletConfig.getConfig("bitcoind")), getPersistenceService()));
break;
case ELECTRUM:
walletService = Optional.of(new ElectrumWalletService(ElectrumWalletService.Config.from(bitcoinWalletConfig.getConfig("electrum")), Path.of(config.getBaseDir())));
walletService = Optional.of(new ElectrumWalletService(ElectrumWalletService.Config.from(bitcoinWalletConfig.getConfig("electrum")), config.getBaseDir()));
break;
case NONE:
default:
walletService = Optional.empty();
break;
}

networkService = new NetworkService(NetworkServiceConfig.from(config.getBaseDir(), getConfig("network")),
networkService = new NetworkService(NetworkServiceConfig.from(config.getBaseDir().toAbsolutePath().toString(),
getConfig("network")),
persistenceService,
securityService.getKeyPairService(),
securityService.getProofOfWorkService());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void main(String[] args) {
private DesktopAppLauncher(String[] args) throws Exception {
options = new Options(args);
String appName = options.getAppName().orElse(DesktopAppLauncher.APP_NAME);
String appDataDir = OsUtils.getUserDataDir().getAbsolutePath() + File.separator + appName;
String appDataDir = OsUtils.getUserDataDir().resolve(appName).toAbsolutePath().toString();
LogSetup.setup(Paths.get(appDataDir, "bisq").toString());
LogSetup.setLevel(Level.INFO);
version = UpdaterUtils.readVersionFromVersionFile(appDataDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int getSocketTimeout() {
}

protected String getBaseDirName() {
return OsUtils.getUserDataDir().getAbsolutePath() + "/Bisq2_" + getClassName();
return OsUtils.getUserDataDir().toAbsolutePath() + "/Bisq2_" + getClassName();
}

protected abstract long getTimeout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void test_peerExchange(Node.Config nodeConfig) throws InterruptedException, Exec
BanList banList = new BanList();
Node tempNode = new Node(banList, nodeConfig, "node-id");
PeerGroupStore peerGroupStore = new PeerGroupStore();
PersistenceService persistenceService = new PersistenceService(getBaseDirName());
PersistenceService persistenceService = new PersistenceService(getBaseDir().toAbsolutePath().toString());
KeepAliveService keepAliveService = new KeepAliveService(tempNode, null, null);
PeerGroupService.Config peerGroupServiceConfig = new PeerGroupService.Config(
null, null, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void main(String[] args) {
.whenComplete((result, throwable) -> {
Map<Transport.Type, Address> addressByNetworkType = applicationService.getNetworkService().getAddressByNetworkType(Node.DEFAULT);
String json = new GsonBuilder().setPrettyPrinting().create().toJson(addressByNetworkType);
Path path = Path.of(applicationService.getConfig().getBaseDir(), "default_node_address.json");
Path path = applicationService.getConfig().getBaseDir().resolve("default_node_address.json");
try {
FileUtils.writeToFile(json, path.toFile());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public OracleNodeApplicationService(String[] args) {

securityService = new SecurityService(persistenceService);

NetworkServiceConfig networkServiceConfig = NetworkServiceConfig.from(config.getBaseDir(), getConfig("network"));
NetworkServiceConfig networkServiceConfig = NetworkServiceConfig.from(config.getBaseDir().toAbsolutePath().toString(),
getConfig("network"));
networkService = new NetworkService(networkServiceConfig,
persistenceService,
securityService.getKeyPairService(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -97,15 +96,16 @@ public RestApiApplicationService(String[] args) {
walletService = Optional.of(new BitcoinWalletService(BitcoinWalletService.Config.from(bitcoinWalletConfig.getConfig("bitcoind")), getPersistenceService()));
break;
case ELECTRUM:
walletService = Optional.of(new ElectrumWalletService(ElectrumWalletService.Config.from(bitcoinWalletConfig.getConfig("electrum")), Path.of(config.getBaseDir())));
walletService = Optional.of(new ElectrumWalletService(ElectrumWalletService.Config.from(bitcoinWalletConfig.getConfig("electrum")), config.getBaseDir()));
break;
case NONE:
default:
walletService = Optional.empty();
break;
}

networkService = new NetworkService(NetworkServiceConfig.from(config.getBaseDir(), getConfig("network")),
networkService = new NetworkService(NetworkServiceConfig.from(config.getBaseDir().toAbsolutePath().toString(),
getConfig("network")),
persistenceService,
securityService.getKeyPairService(),
securityService.getProofOfWorkService());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void main(String[] args) {
.whenComplete((result, throwable) -> {
Map<Transport.Type, Address> addressByNetworkType = applicationService.getNetworkService().getAddressByNetworkType(Node.DEFAULT);
String json = new GsonBuilder().setPrettyPrinting().create().toJson(addressByNetworkType);
Path path = Path.of(applicationService.getConfig().getBaseDir(), "default_node_address.json");
Path path = applicationService.getConfig().getBaseDir().resolve("default_node_address.json");
try {
FileUtils.writeToFile(json, path.toFile());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public SeedNodeApplicationService(String[] args) {

securityService = new SecurityService(persistenceService);

NetworkServiceConfig networkServiceConfig = NetworkServiceConfig.from(config.getBaseDir(), getConfig("network"));
NetworkServiceConfig networkServiceConfig = NetworkServiceConfig.from(config.getBaseDir().toAbsolutePath().toString(),
getConfig("network"));
networkService = new NetworkService(networkServiceConfig,
persistenceService,
securityService.getKeyPairService(),
Expand Down

0 comments on commit 1dda9a9

Please sign in to comment.