Skip to content

Commit

Permalink
Write out a file if scijava.app.config-file is set
Browse files Browse the repository at this point in the history
Specifically, Java.upgrade now writes out a property pair with
key jvm.dir and value equal to the path of thenewly unpacked
Java installation.

This file can be useful for native launchers to help decide
which Java to use when launching.
  • Loading branch information
ctrueden committed Oct 21, 2024
1 parent 90a248c commit 68ba90f
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>app-launcher</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>

<name>SciJava App Launcher</name>
<description>Launcher for SciJava applications.</description>
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/org/scijava/launcher/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*-
* #%L
* Launcher for SciJava applications.
* %%
* Copyright (C) 2007 - 2024 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.launcher;

import java.io.*;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Simple utility for reading and writing a property map to/from plain text.
*
* @author Curtis Rueden
*/
public final class Config {

private Config() { }

public static Map<String, String> load(File file) throws IOException {
Map<String, String> map = new LinkedHashMap<>(); // Keep insertion order.
Files.readAllLines(file.toPath()).forEach(line -> {
int equals = line.indexOf('=');
if (equals >= 0) {
String key = line.substring(0, equals);
String val = line.substring(equals + 1);
map.put(key, val);
}
});
return map;
}

public static void save(File file, Map<String, String> config)
throws IOException
{
try (PrintWriter pw = new PrintWriter(new FileWriter(file))) {
for (Map.Entry<String, String> entry : config.entrySet()) {
pw.println(entry.getKey() + "=" + entry.getValue());
}
}
}

/**
* Updates the given config file by mixing in the specified properties,
* creating the file if it does not already exist.
*/
public static void update(File file, Map<String, String> props)
throws IOException
{
Map<String, String> config;
if (file.isFile()) {
config = load(file);
config.putAll(props);
}
else config = props;
save(file, config);
}

/**
* Updates the given config file with the specified property pair,
* creating the file if it does not already exist.
*/
public static void update(File file, String key, String val)
throws IOException
{
update(file, Collections.singletonMap(key, val));
}
}
19 changes: 17 additions & 2 deletions src/main/java/org/scijava/launcher/Java.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,23 @@ public static void upgrade(BiConsumer<String, Double> subscriber)

// Unpack the downloaded archive.
String[] dir = {null};
waitForTask(Archives.unpack(tmpArchive, javaRootFile,
s -> subscriber.accept("Unpacking " + s, null)));
waitForTask(Archives.unpack(tmpArchive, javaRootFile, s -> {
// Save a reference to the first directory being unpacked.
// This is only a heuristic, but it works for most Java archives.
if (s != null && dir[0] == null && s.endsWith("/")) dir[0] = s;
// Forward the message on to our upgrade subscriber.
subscriber.accept("Unpacking " + s, null);
}));

// Write new installation location into the requested configuration file.
if (dir[0] != null) {
Path newJavaPath = javaRootPath.resolve(dir[0]).normalize().toAbsolutePath();
String configFileValue = System.getProperty("scijava.app.config-file");
if (configFileValue != null && !configFileValue.isEmpty()) {
File configFile = new File(configFileValue);
Config.update(configFile, "jvm.dir", newJavaPath.toString());
}
}

subscriber.accept("Java update complete", null);
}
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/org/scijava/launcher/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*-
* #%L
* Launcher for SciJava applications.
* %%
* Copyright (C) 2007 - 2024 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.launcher;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

/**
* Tests {@link Config}.
*
* @author Curtis Rueden
*/
public class ConfigTest {

@Test
public void test() throws IOException {
Path tmpPath = Files.createTempFile(getClass().getName(), null);
File tmpFile = tmpPath.toFile();
tmpFile.deleteOnExit();
Map<String, String> config = new LinkedHashMap<>();
config.put("meeting", "hello");
config.put("parting", "goodbye");
config.put("laughing", "ha ha");

Config.save(tmpFile, config);
List<String> lines = Files.readAllLines(tmpPath);
List<String> expected =
Arrays.asList("meeting=hello", "parting=goodbye", "laughing=ha ha");
assertEquals(expected, lines);

Map<String, String> config2 = Config.load(tmpFile);
assertEquals(config, config2);
}
}

0 comments on commit 68ba90f

Please sign in to comment.