Skip to content

Commit

Permalink
URl handler issue on Mac for all users nroduit#583
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Sep 6, 2024
1 parent 179bd05 commit b54f23b
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 140 deletions.
4 changes: 0 additions & 4 deletions weasis-distributions/script/resources/macosx/run.sh

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import ch.qos.logback.core.util.FileSize;
import com.formdev.flatlaf.util.SystemInfo;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.io.File;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -75,17 +78,74 @@ public class AppLauncher extends WeasisLauncher implements Singleton.SingletonAp
logger.addAppender(rollingFileAppender);
}

private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AppLauncher.class);

public AppLauncher(ConfigData configData) {
super(configData);
}

public static void main(String[] argv) throws Exception {
final Type launchType = Type.NATIVE;
System.setProperty("weasis.launch.type", launchType.name());

ConfigData configData = new ConfigData(argv);
if (!Singleton.invoke(configData)) {
AppLauncher instance = new AppLauncher(configData);

// Init handler for open URI and open file events
Desktop app = Desktop.getDesktop();
if (app.isSupported(Action.APP_OPEN_URI)) {
long time = System.currentTimeMillis();
boolean noArgs = argv.length == 0;
app.setOpenURIHandler(
e -> {
String uri = e.getURI().toString();
LOGGER.info("Get URI event from OS. URI: {}", uri);
int index = Utils.getWeasisProtocolIndex(uri);
if (index < 0) {
uri = "dicom:get -r \"" + uri + "\""; // NON-NLS
instance.executeCommands(List.of(uri), null);
} else {
boolean sameInstance = System.currentTimeMillis() - time < 3000;
String[] args;
if (SystemInfo.isMacOS) {
args = new String[] {"open", "-n", "-b", "org.weasis.launcher", "--args", uri};
} else if (SystemInfo.isWindows) {
args = new String[] {"cmd", "/c", "start", uri};
} else {
args = new String[] {"xdg-open", uri};
}
Thread launcherThread =
Thread.ofVirtual()
.start(
() -> {
try {
ProcessBuilder pb = new ProcessBuilder(args);
pb.start().waitFor(15, TimeUnit.SECONDS);
if (sameInstance && noArgs) {
LOGGER.info(
"Configuration with URI is different, restart Weasis with new configuration");
instance.shutdownHook();
}
} catch (Exception ex) {
LOGGER.error("Cannot start Weasis from URI", ex);
}
});
launcherThread.start();
}
});
}
if (app.isSupported(Desktop.Action.APP_OPEN_FILE)) {
app.setOpenFileHandler(
e -> {
List<String> files =
e.getFiles().stream()
.map(f -> "dicom:get -l \"" + f.getPath() + "\"") // NON-NLS
.toList();
LOGGER.info("Get oOpen file event from OS. Files: {}", files);
instance.executeCommands(files, null);
});
}

Singleton.start(instance, configData.getSourceID());
instance.launch(launchType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private void handleMagicWord(BufferedReader in, Socket s, InputStreamReader isr)

if (siApp.canStartNewActivation(props)) {
siApp.newActivation(args);
LOGGER.debug("Sending ACK");
LOGGER.debug("Sending ACK for loading args");
sendResponse(s, isr, SI_ACK);
} else {
LOGGER.debug("Sending EXIT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import com.formdev.flatlaf.FlatSystemProperties;
import com.formdev.flatlaf.util.SystemInfo;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileDescriptor;
Expand Down Expand Up @@ -123,6 +121,7 @@ public static String valueOf(int state) {
protected String look = null;
protected RemotePrefService remotePrefs;
protected String localPrefsDir;
protected volatile boolean closing = false;

protected final Properties modulesi18n;
protected final ConfigData configData;
Expand Down Expand Up @@ -179,6 +178,9 @@ public final void launch(Type type) throws Exception {
try {

String goshArgs = getGoshArgs(serverProp);
if (closing) {
return;
}
// Now create an instance of the framework with our configuration properties.
mFelix = new Felix(serverProp);
// Initialize the framework, but don't start it yet.
Expand All @@ -195,7 +197,9 @@ public final void launch(Type type) throws Exception {
new ServiceTracker(
mFelix.getBundleContext(), "org.apache.felix.service.command.CommandProcessor", null);
mTracker.open();

if (closing) {
return;
}
// Start the framework.
mFelix.start();

Expand All @@ -208,44 +212,16 @@ public final void launch(Type type) throws Exception {
LOGGER.info(
"Logs has been delegated to the OSGI service and can be read in {}", logActivation);
}

// Init after default properties for UI
Desktop app = Desktop.getDesktop();
if (app.isSupported(Action.APP_OPEN_URI)) {
app.setOpenURIHandler(
e -> {
String uri = e.getURI().toString();
LOGGER.info("Get URI event from OS. URI: {}", uri);
int index = Utils.getWeasisProtocolIndex(uri);
if (index < 0) {
uri = "dicom:get -r \"" + uri + "\""; // NON-NLS
executeCommands(List.of(uri), null);
} else {
configData.extractArgFromUri(uri);
executeCommands(configData.getArguments(), null);
}
});
}
if (app.isSupported(Desktop.Action.APP_OPEN_FILE)) {

app.setOpenFileHandler(
e -> {
List<String> files =
e.getFiles().stream()
.map(f -> "dicom:get -l \"" + f.getPath() + "\"") // NON-NLS
.toList();
LOGGER.info("Get oOpen file event from OS. Files: {}", files);
executeCommands(files, null);
});
}

executeCommands(configData.getArguments(), goshArgs);

checkBundleUI(serverProp);
frameworkLoaded = true;

showMessage(mainFrame, serverProp);

if (closing) {
return;
}
// Wait for framework to stop to exit the VM.
mFelix.waitForStop(0);
System.exit(0);
Expand Down Expand Up @@ -1130,7 +1106,8 @@ public static Locale textToLocale(String value) {
return Locale.getDefault();
}

private void shutdownHook() {
protected void shutdownHook() {
closing = true;
if (mFelix == null || (mFelix.getState() & 6) != 0) {
return;
}
Expand Down

0 comments on commit b54f23b

Please sign in to comment.