Skip to content

Commit

Permalink
Manifest configurable parameters (#2). Version 1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvin Syse committed Mar 2, 2016
1 parent a53966c commit 09daf17
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.0.8] - 2016-03-02

### Added

- Support for manifest configurable parameters (https://github.com/edvin/fxlauncher/issues/2)

## [1.0.7] - 2016-02-20

### Added
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>no.tornado</groupId>
<artifactId>fxlauncher</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>
<packaging>jar</packaging>
<name>FX Launcher</name>
<description>Auto updating launcher for JavaFX Applications</description>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/fxlauncher/CreateManifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public static void main(String[] args) throws IOException {
String launchClass = args[1];
Path appPath = Paths.get(args[2]);
FXManifest manifest = create(baseURI, launchClass, appPath);

if (args.length == 4)
manifest.parameters = args[3];

JAXB.marshal(manifest, appPath.resolve("app.xml").toFile());
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/fxlauncher/FXManifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class FXManifest {
String progressBarStyle = "-fx-pref-width: 200;";
@XmlElement
String wrapperStyle = "-fx-spacing: 10; -fx-padding: 25;";
@XmlElement
String parameters;

public String getFilename() {
return String.format("%s.xml", launchClass);
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/fxlauncher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ public void start(Stage primaryStage) throws Exception {
updateManifest();
createUpdateWrapper();
syncFiles();
} catch (Exception ex) {
log.log(Level.WARNING, String.format("Error during %s phase", phase), ex);
}

try {
createApplication();
launchAppFromManifest();
} catch (Exception ex) {
reportError(String.format("Error during %s phase", phase), ex);
}

}).start();
}

Expand Down Expand Up @@ -89,9 +95,9 @@ private void createUpdateWrapper() {

public URLClassLoader createClassLoader() {
List<URL> libs = manifest.files.stream()
.filter(LibraryFile::loadForCurrentPlatform)
.map(LibraryFile::toURL)
.collect(Collectors.toList());
.filter(LibraryFile::loadForCurrentPlatform)
.map(LibraryFile::toURL)
.collect(Collectors.toList());

return new URLClassLoader(libs.toArray(new URL[libs.size()]));
}
Expand All @@ -103,8 +109,8 @@ private void launchAppFromManifest() throws Exception {
Platform.runLater(() -> {
try {
stage.close();
ParametersImpl.registerParameters(app, getParameters());
app.start(primaryStage);
ParametersImpl.registerParameters(app, new LauncherParams(getParameters(), manifest));
app.start(primaryStage);
} catch (Exception ex) {
reportError("Failed to start application", ex);
}
Expand Down Expand Up @@ -152,8 +158,8 @@ private void createApplication() throws Exception {

URLClassLoader classLoader = createClassLoader();
FXMLLoader.setDefaultClassLoader(classLoader);
Thread.currentThread().setContextClassLoader(classLoader);
Platform.runLater(() -> Thread.currentThread().setContextClassLoader(classLoader));
Thread.currentThread().setContextClassLoader(classLoader);
Platform.runLater(() -> Thread.currentThread().setContextClassLoader(classLoader));
Class<? extends Application> appclass = (Class<? extends Application>) classLoader.loadClass(manifest.launchClass);
app = appclass.newInstance();
}
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/fxlauncher/LauncherParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package fxlauncher;

import javafx.application.Application;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Implementation Application.Parameters that wraps the parameters given to the application
* at startup, and adds any manifest configured parameters unless they were overriden
* by the command line.
*/
public class LauncherParams extends Application.Parameters {
private List<String> rawArgs;
private Map<String, String> namedParams;
private List<String> unnamedParams;

public LauncherParams(Application.Parameters delegate, FXManifest manifest) {
rawArgs = new ArrayList<>();
namedParams = new HashMap<>();
unnamedParams = new ArrayList<>();

// Add all raw args from the parent application
rawArgs.addAll(delegate.getRaw());

// Add parameters from the manifest unless they were already specified on the command line
if (manifest.parameters != null) {
for (String arg : manifest.parameters.split("\\s")) {
if (arg != null) {
if (rawArgs.contains(arg))
continue;

if (arg.startsWith("--") && arg.contains("=")) {
String argname = arg.substring(0, arg.indexOf("="));
if (rawArgs.stream().filter(a -> a.startsWith(argname)).findAny().isPresent())
continue;
}

rawArgs.add(arg);
}
}
}

// Compute named and unnamed parameters
computeNamedParams();
computeUnnamedParams();
}

public List<String> getRaw() {
return rawArgs;
}

public List<String> getUnnamed() {
return unnamedParams;
}

public Map<String, String> getNamed() {
return namedParams;
}

/**
* Returns true if the specified string is a named parameter of the
* form: --name=value
*
* @param arg the string to check
* @return true if the string matches the pattern for a named parameter.
*/
private boolean isNamedParam(String arg) {
return arg.startsWith("--") && (arg.indexOf('=') > 2 && validFirstChar(arg.charAt(2)));
}

/**
* This method parses the current array of raw arguments looking for
* name,value pairs. These name,value pairs are then added to the map
* for this parameters object, and are of the form: --name=value.
*/
private void computeNamedParams() {
rawArgs.stream().filter(this::isNamedParam).forEach(arg -> {
final int eqIdx = arg.indexOf('=');
String key = arg.substring(2, eqIdx);
String value = arg.substring(eqIdx + 1);
namedParams.put(key, value);
});
}
/**
* This method computes the list of unnamed parameters, by filtering the
* list of raw arguments, stripping out the named parameters.
*/
private void computeUnnamedParams() {
unnamedParams.addAll(rawArgs.stream().filter(arg -> !isNamedParam(arg)).collect(Collectors.toList()));
}

/**
* Validate the first character of a key. It is valid if it is a letter or
* an "_" character.
*
* @param c the first char of a key string
* @return whether or not it is valid
*/
private boolean validFirstChar(char c) {
return Character.isLetter(c) || c == '_';
}
}

0 comments on commit 09daf17

Please sign in to comment.