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

Load jar dynamically instead of using processbuilder #1039

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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public ApplicationService(String configFileName, String[] args) {
CountryRepository.applyDefaultLocale(locale);
LanguageRepository.setLocale(locale);
FiatCurrencyRepository.setLocale(locale);
Res.setLocale(locale);
Res.setLanguage(LanguageRepository.getDefaultLanguage());
ResolverConfig.config();

persistenceService = new PersistenceService(config.getBaseDir());
Expand Down
8 changes: 4 additions & 4 deletions chat/src/main/java/bisq/chat/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ public ChatService(PersistenceService persistenceService,
bisqEasyPublicChatChannelService,
bisqEasyPrivateTradeChatChannelService,
userIdentityService));
}

@Override
public CompletableFuture<Boolean> initialize() {
log.info("initialize");
// DISCUSSION
addToCommonPublicChatChannelServices(ChatChannelDomain.DISCUSSION,
List.of(new CommonPublicChatChannel(ChatChannelDomain.DISCUSSION, "bisq"),
Expand Down Expand Up @@ -129,11 +133,7 @@ public ChatService(PersistenceService persistenceService,
new CommonPublicChatChannel(ChatChannelDomain.SUPPORT, "reports")));
addToTwoPartyPrivateChatChannelServices(ChatChannelDomain.SUPPORT);
addToChatChannelSelectionServices(ChatChannelDomain.SUPPORT);
}

@Override
public CompletableFuture<Boolean> initialize() {
log.info("initialize");
List<CompletableFuture<Boolean>> list = new ArrayList<>(List.of(bisqEasyPublicChatChannelService.initialize(),
bisqEasyPrivateTradeChatChannelService.initialize()));
list.addAll(commonPublicChatChannelServices.values().stream()
Expand Down
50 changes: 29 additions & 21 deletions common/src/main/java/bisq/common/locale/LanguageRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

@SuppressWarnings("SpellCheckingInspection")
Expand All @@ -31,42 +32,39 @@ public class LanguageRepository {
@Getter
private static String defaultLanguage;

public static void setDefaultLanguage(String defaultLanguage) {
LanguageRepository.defaultLanguage = defaultLanguage;
}

public static void setLocale(Locale defaultLocale) {
LanguageRepository.defaultLanguage = defaultLocale.getLanguage();
}

public static final List<String> RTL_LANGUAGES_CODES = List.of(
"fa", // Persian
"ar", // Arabic
"iw" // Hebrew
);

public static boolean isDefaultLanguageRTL() {
return RTL_LANGUAGES_CODES.contains(defaultLanguage);
}

public static final List<String> CODES = LocaleRepository.LOCALES.stream()
.filter(locale -> !locale.getLanguage().isEmpty() &&
!locale.getDisplayLanguage().isEmpty())
.distinct()
.map(Locale::getLanguage)
.sorted(Comparator.comparing(LanguageRepository::getDisplayName))
.filter(Objects::nonNull)
.distinct()
.sorted(Comparator.comparing(LanguageRepository::getDisplayString))
.collect(Collectors.toList());

public static String getDisplayName(String code) {
public static String getDisplayString(String code) {
Locale locale = Locale.forLanguageTag(code);
return locale.getDisplayName(locale);
return getDisplayLanguage(locale) + " (" + getDisplayLanguageInLocale(locale) + ")";
}

public static String getEnglishCode() {
return new Locale(Locale.ENGLISH.getLanguage()).getLanguage();
public static String getDisplayLanguage(String code) {
return getDisplayLanguage(Locale.forLanguageTag(code));
}

private static final List<String> I18N_CODES = List.of(
// Returns language in defaut locale language (e.g. Spanish if "en" is default)
public static String getDisplayLanguage(Locale locale) {
return locale.getDisplayLanguage();
}

// Returns language in locale's language (e.g. español)
public static String getDisplayLanguageInLocale(Locale locale) {
return locale.getDisplayName(locale);
}

public static final List<String> I18N_CODES = List.of(
"en", // English
"de", // German
"es", // Spanish
Expand Down Expand Up @@ -123,4 +121,14 @@ public static String getEnglishCode() {
"mt" // Maltese
*/
);

public static boolean isDefaultLanguageRTL() {
return RTL_LANGUAGES_CODES.contains(defaultLanguage);
}

public static final List<String> RTL_LANGUAGES_CODES = List.of(
"fa", // Persian
"ar", // Arabic
"iw" // Hebrew
);
}
48 changes: 48 additions & 0 deletions common/src/main/java/bisq/common/logging/AsciiLogo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common.logging;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AsciiLogo {
public static String getAsciiLogo() {
String ls = System.lineSeparator();
return ls + ls +
" ........ ...... " + ls +
" .............. ...... " + ls +
" ................. ...... " + ls +
" ...... .......... .. ...... " + ls +
" ...... ...... ...... ............... ..... ......... .......... " + ls +
" ....... ........ .................. ..... ............. ............... " + ls +
" ...... ........ .......... ....... ..... ...... ... ........ ....... " + ls +
" ...... ..... ....... ..... ..... ..... ..... ...... " + ls +
" ...... ... ... ...... ...... ..... ........... ...... ...... " + ls +
" ...... ..... .... ...... ...... ..... ............ ..... ...... " + ls +
" ...... ..... ...... ..... ........ ...... ...... " + ls +
" ...... .... ... ...... ...... ..... .. ...... ...... ........ " + ls +
" ........ .. ....... ................. ..... .............. ................... " + ls +
" .......... ......... ............. ..... ............ ................. " + ls +
" ...................... ..... .... .... ...... " + ls +
" ................ ...... " + ls +
" .... ...... " + ls +
" ...... " + ls +
ls + ls;

}
}
4 changes: 4 additions & 0 deletions common/src/main/java/bisq/common/logging/LogSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import ch.qos.logback.core.util.FileSize;
import com.google.common.base.Charsets;
import org.slf4j.LoggerFactory;

public class LogSetup {
Expand Down Expand Up @@ -58,6 +59,7 @@ public static void setup(String fileName) {
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n");
encoder.setCharset(Charsets.UTF_8);
encoder.start();

appender.setEncoder(encoder);
Expand All @@ -68,6 +70,8 @@ public static void setup(String fileName) {
logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
logbackLogger.addAppender(appender);
logbackLogger.setLevel(DEFAULT_LOG_LEVEL);

logbackLogger.info(AsciiLogo.getAsciiLogo());
}

public static void setCustomLogLevel(String pattern, Level logLevel) {
Expand Down
57 changes: 57 additions & 0 deletions common/src/main/java/bisq/common/util/JvmUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.management.ManagementFactory;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class JvmUtils { // Make program arguments which starts with -Dapplication via System.setProperty accessible as jvm arguments.
// Useful to pass jvm args as program arguments to a binary (cannot pass jvm options if it's not started by java process).
public static Set<String> getJvmOptionsFromArgs(String[] args, String prefix) {
return Stream.of(args)
.filter(e -> e.startsWith("-D" + prefix))
.collect(Collectors.toSet());
}

public static void addToSystemProperties(Set<String> systemProperties) {
systemProperties.stream()
.filter(e -> e.startsWith("-D"))
.forEach(e -> {
try {
String[] pair = e.split("=");
String key = pair[0].replace("-D", "");
System.setProperty(key, pair[1]);
} catch (Exception exception) {
log.error("error at parsing argument {}", e);
}
});
}


public static Set<String> getJvmOptions(String prefix) {
return ManagementFactory.getRuntimeMXBean().getInputArguments().stream()
.filter(e -> e.startsWith("-D" + prefix))
.collect(Collectors.toSet());

}
}
5 changes: 5 additions & 0 deletions desktop/src/main/java/bisq/desktop/DesktopController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.desktop.common.Transitions;
import bisq.desktop.common.application.JavaFxApplicationData;
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.utils.standby.PreventStandbyModeService;
import bisq.desktop.common.view.Controller;
import bisq.desktop.common.view.Navigation;
import bisq.desktop.common.view.NavigationController;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class DesktopController extends NavigationController {
private final SplashController splashController;
private final UserIdentityService userIdentityService;
private final ServiceProvider serviceProvider;
private final PreventStandbyModeService preventStandbyModeService;

public DesktopController(Observable<State> applicationServiceState,
ServiceProvider serviceProvider,
Expand All @@ -89,6 +91,7 @@ public DesktopController(Observable<State> applicationServiceState,
Browser.setHostServices(applicationJavaFxApplicationData.getHostServices());
Transitions.setSettingsService(settingsService);
AnchorPane viewRoot = view.getRoot();
preventStandbyModeService = new PreventStandbyModeService(serviceProvider);

Navigation.init(settingsService);
Overlay.init(viewRoot,
Expand Down Expand Up @@ -132,6 +135,7 @@ protected Optional<? extends Controller> createController(NavigationTarget navig

@Override
public void onActivate() {
preventStandbyModeService.initialize();
// We show the splash screen as background also if we show the 'unlock' or 'tac' overlay screens
Navigation.navigateTo(NavigationTarget.SPLASH);

Expand All @@ -147,6 +151,7 @@ public void onActivate() {

@Override
public void onDeactivate() {
preventStandbyModeService.shutdown();
}

public void onApplicationServiceInitialized(boolean result, Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.desktop.common.utils.standby;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;
import java.util.Optional;

@Slf4j
class Inhibitor implements PreventStandbyMode {
// On linux, preferred inhibitor is gnome-session-inhibit, then fall back to systemd-inhibit if it is installed.
private static final List<String> INHIBIT_EXECUTABLE_CANDIDATES = List.of(
"/usr/bin/gnome-session-inhibit",
"/bin/gnome-session-inhibit",
"/usr/bin/systemd-inhibit",
"/bin/systemd-inhibit");

static Optional<PreventStandbyMode> findExecutableInhibitor() {
return findInhibitExecutable().map(file -> new Inhibitor(file.getPath()));
}

static Optional<File> findInhibitExecutable() {
return INHIBIT_EXECUTABLE_CANDIDATES.stream()
.map(File::new)
.filter(File::canExecute)
.filter(File::exists)
.findAny();
}

private final String inhibitExecutablePath;
private volatile boolean isPlaying;
private Optional<Process> process = Optional.empty();

Inhibitor(String inhibitExecutablePath) {
this.inhibitExecutablePath = inhibitExecutablePath;
}

@Override
public void initialize() {
if (isPlaying) {
return;
}
isPlaying = true;

new Thread(() -> {
try {
String[] commands = inhibitExecutablePath.contains("gnome-session-inhibit")
? new String[]{inhibitExecutablePath, "--app-id", "Bisq", "--inhibit", "suspend", "--reason", "Avoid Standby", "--inhibit-only"}
: new String[]{inhibitExecutablePath, "--who", "Bisq", "--what", "sleep", "--why", "Avoid Standby", "--mode", "block", "tail", "-f", "/dev/null"};
ProcessBuilder processBuilder = new ProcessBuilder(commands);
process = Optional.of(processBuilder.start());
log.info("Started -- disabled power management via {}", String.join(" ", commands));
} catch (Exception e) {
e.printStackTrace();
}
}, "Linux-InhibitExecutable-thread").start();
}

@Override
public void shutdown() {
isPlaying = false;
process.ifPresent(process -> {
log.info("Stopping process {} isAlive={}", process.toHandle().info(), process.isAlive());
if (process.isAlive()) {
process.destroy();
}
});
process = Optional.empty();
}
}
Loading