-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write out a file if scijava.app.config-file is set
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
Showing
4 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |