diff options
author | Omair Majid <[email protected]> | 2011-03-31 18:19:31 -0400 |
---|---|---|
committer | Omair Majid <[email protected]> | 2011-03-31 18:19:31 -0400 |
commit | bfb6a0a4bd2851b0dc3c4da389614e4bdd22c9c8 (patch) | |
tree | 2143506bfee7a6323390cd61fbebfad49997125b /netx/net/sourceforge | |
parent | d65bc4b61dd9c58d06d14986f48ea525ca5c6231 (diff) |
Let launcher handle parsing exceptions
2011-03-31 Omair Majid <[email protected]>
* netx/net/sourceforge/jnlp/Launcher.java: Add parserSettings and extra.
(setParserSettings): New method.
(setInformationToMerge): New method.
(launch(JNLPFile,Container)): Call mergeExtraInformation.
(launch(URL,boolean)): New method.
(mergeExtraInformation): New method.
(addProperties, addParameters, addArguments): Moved here from Boot.java
(fromUrl): New method.
* netx/net/sourceforge/jnlp/ParserSettings.java: New file.
* netx/net/sourceforge/jnlp/resources/Messages.properties: Remove BArgNA,
BParamNA.
* netx/net/sourceforge/jnlp/runtime/Boot.java
(run): Do not parse JNLP file. Pass ParserSettings and other command line
additions to launcher.
(getFile): Rename to...
(getFileLocation): New method.
(addProperties, addParameters, addArguments): Move to Launcher.java.
Diffstat (limited to 'netx/net/sourceforge')
-rw-r--r-- | netx/net/sourceforge/jnlp/Launcher.java | 152 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/ParserSettings.java | 61 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/resources/Messages.properties | 2 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/runtime/Boot.java | 114 |
4 files changed, 232 insertions, 97 deletions
diff --git a/netx/net/sourceforge/jnlp/Launcher.java b/netx/net/sourceforge/jnlp/Launcher.java index ea0afc4..cb443bd 100644 --- a/netx/net/sourceforge/jnlp/Launcher.java +++ b/netx/net/sourceforge/jnlp/Launcher.java @@ -27,6 +27,7 @@ import java.net.URL; import java.net.UnknownHostException; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.jar.JarFile; import net.sourceforge.jnlp.cache.CacheUtil; @@ -76,6 +77,9 @@ public class Launcher { /** If the application should call System.exit on fatal errors */ private boolean exitOnFailure = true; + private ParserSettings parserSettings = new ParserSettings(); + + private Map<String, String[]> extra = null; /** * Create a launcher with the runtime's default update policy @@ -165,6 +169,26 @@ public class Launcher { } /** + * Set the parser settings to use when the Launcher initiates parsing of + * a JNLP file. + * @param settings + */ + public void setParserSettings(ParserSettings settings) { + parserSettings = settings; + } + + /** + * Set a map to use when trying to extract extra information, including + * arguments, properties and parameters, to be merged into the main JNLP + * @param input a map containing extra information to add to the main JNLP. + * the values for keys "arguments", "parameters", and "properties" are + * used. + */ + public void setInformationToMerge(Map<String, String[]> input) { + this.extra = input; + } + + /** * Launches a JNLP file by calling the launch method for the * appropriate file type. The application will be started in * a new window. @@ -189,6 +213,8 @@ public class Launcher { public ApplicationInstance launch(JNLPFile file, Container cont) throws LaunchException { TgThread tg; + mergeExtraInformation(file, extra); + JNLPRuntime.markNetxRunning(); //First checks whether offline-allowed tag is specified inside the jnlp @@ -246,6 +272,106 @@ public class Launcher { /** * Launches a JNLP file by calling the launch method for the + * appropriate file type. + * + * @param location the URL of the JNLP file to launch + * @param fromSource if true, the JNLP file will be re-read from the source + * location to get the pristine version + * @throws LaunchException if there was an exception + * @return the application instance + */ + public ApplicationInstance launch(URL location, boolean fromSource) throws LaunchException { + return launch(fromUrl(location, fromSource)); + } + + /** + * Merges extra information into the jnlp file + * + * @param file the JNLPFile + * @param extra extra information to merge into the JNLP file + * @throws LaunchException if an exception occurs while extracting + * extra information + */ + private void mergeExtraInformation(JNLPFile file, Map<String, String[]> extra) throws LaunchException { + if (extra == null) { + return; + } + + String[] properties = extra.get("properties"); + if (properties != null) { + addProperties(file, properties); + } + + String[] arguments = extra.get("arguments"); + if (arguments != null && file.isApplication()) { + addArguments(file, arguments); + } + + String[] parameters = extra.get("parameters"); + if (parameters != null && file.isApplet()) { + addParameters(file, parameters); + } + } + + /** + * Add the properties to the JNLP file. + * @throws LaunchException if an exception occurs while extracting + * extra information + */ + private void addProperties(JNLPFile file, String[] props) throws LaunchException { + ResourcesDesc resources = file.getResources(); + + for (int i = 0; i < props.length; i++) { + // allows empty property, not sure about validity of that. + int equals = props[i].indexOf("="); + if (equals == -1) { + throw launchError(new LaunchException(R("BBadProp", props[i]))); + } + + String key = props[i].substring(0, equals); + String value = props[i].substring(equals + 1, props[i].length()); + + resources.addResource(new PropertyDesc(key, value)); + } + } + + /** + * Add the params to the JNLP file; only call if file is + * actually an applet file. + * @throws LaunchException if an exception occurs while extracting + * extra information + */ + private void addParameters(JNLPFile file, String[] params) throws LaunchException { + AppletDesc applet = file.getApplet(); + + for (int i = 0; i < params.length; i++) { + // allows empty param, not sure about validity of that. + int equals = params[i].indexOf("="); + if (equals == -1) { + throw launchError(new LaunchException(R("BBadParam", params[i]))); + } + + String name = params[i].substring(0, equals); + String value = params[i].substring(equals + 1, params[i].length()); + + applet.addParameter(name, value); + } + } + + /** + * Add the arguments to the JNLP file; only call if file is + * actually an application (not installer). + */ + private void addArguments(JNLPFile file, String[] args) { + ApplicationDesc app = file.getApplication(); + + for (int i = 0; i < args.length; i++) { + app.addArgument(args[i]); + } + } + + /** + * Launches a JNLP file by calling the launch method for the * appropriate file type in a different thread. * * @param file the JNLP file to launch @@ -345,6 +471,32 @@ public class Launcher { /** * Returns the JNLPFile for the URL, with error handling. */ + private JNLPFile fromUrl(URL location, boolean fromSource) throws LaunchException { + try { + JNLPFile file = null; + + file = new JNLPFile(location, parserSettings.isStrict()); + + if (fromSource) { + // Launches the jnlp file where this file originated. + if (file.getSourceLocation() != null) { + file = new JNLPFile(file.getSourceLocation(), parserSettings.isStrict()); + } + } + return file; + } catch (Exception ex) { + if (ex instanceof LaunchException) + throw (LaunchException) ex; // already sent to handler when first thrown + else + // IO and Parse + throw launchError(new LaunchException(null, ex, R("LSFatal"), R("LCReadError"), R("LCantRead"), R("LCantReadInfo"))); + } + } + + /** + * Returns the JNLPFile for the URL, with error handling. + */ + @Deprecated private JNLPFile toFile(URL location) throws LaunchException { try { JNLPFile file = null; diff --git a/netx/net/sourceforge/jnlp/ParserSettings.java b/netx/net/sourceforge/jnlp/ParserSettings.java new file mode 100644 index 0000000..b0a69e3 --- /dev/null +++ b/netx/net/sourceforge/jnlp/ParserSettings.java @@ -0,0 +1,61 @@ +/* ParserSettings.java + Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + + +package net.sourceforge.jnlp; + +/** + * Contains settings to be used by the Parser while parsing JNLP files. + * + * Immutable and therefore thread-safe. + */ +public class ParserSettings { + + private final boolean isStrict; + + public ParserSettings() { + isStrict = false; + } + + public ParserSettings(boolean strict) { + isStrict = strict; + } + + public boolean isStrict() { + return isStrict; + } +} diff --git a/netx/net/sourceforge/jnlp/resources/Messages.properties b/netx/net/sourceforge/jnlp/resources/Messages.properties index fb273c7..29593d0 100644 --- a/netx/net/sourceforge/jnlp/resources/Messages.properties +++ b/netx/net/sourceforge/jnlp/resources/Messages.properties @@ -129,8 +129,6 @@ BLaunchAbout=Launching about window... BNeedsFile=Must specify a .jnlp file
RNoAboutJnlp=Unable to find about.jnlp
BFileLoc=JNLP file location
-BArgNA=Arguments not used for this type of JNLP file (ignored).
-BParamNA=Parameters not used for this type of JNLP file (ignored).
BBadProp=Incorrect property format {0} (should be key=value)
BBadParam=Incorrect parameter format {0} (should be name=value)
BNoDir=Directory {0} does not exist.
diff --git a/netx/net/sourceforge/jnlp/runtime/Boot.java b/netx/net/sourceforge/jnlp/runtime/Boot.java index e86644c..cb077fb 100644 --- a/netx/net/sourceforge/jnlp/runtime/Boot.java +++ b/netx/net/sourceforge/jnlp/runtime/Boot.java @@ -19,23 +19,18 @@ package net.sourceforge.jnlp.runtime; import static net.sourceforge.jnlp.runtime.Translator.R; import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; -import net.sourceforge.jnlp.AppletDesc; -import net.sourceforge.jnlp.ApplicationDesc; -import net.sourceforge.jnlp.JNLPFile; import net.sourceforge.jnlp.LaunchException; import net.sourceforge.jnlp.Launcher; -import net.sourceforge.jnlp.ParseException; -import net.sourceforge.jnlp.PropertyDesc; -import net.sourceforge.jnlp.ResourcesDesc; +import net.sourceforge.jnlp.ParserSettings; import net.sourceforge.jnlp.cache.CacheUtil; import net.sourceforge.jnlp.cache.UpdatePolicy; import net.sourceforge.jnlp.security.viewer.CertificateViewer; @@ -189,8 +184,19 @@ public final class Boot implements PrivilegedAction<Void> { return null; } + Map<String, String[]> extra = new HashMap<String, String[]>(); + extra.put("arguments", getOptions("-arg")); + extra.put("parameters", getOptions("-param")); + extra.put("properties", getOptions("-property")); + + boolean strict = (null != getOption("-strict")); + ParserSettings settings = new ParserSettings(strict); + try { - new Launcher(false).launch(getFile()); + Launcher launcher = new Launcher(false); + launcher.setParserSettings(settings); + launcher.setInformationToMerge(extra); + launcher.launch(getFileLocation(), true); } catch (LaunchException ex) { // default handler prints this } catch (Exception ex) { @@ -236,10 +242,10 @@ public final class Boot implements PrivilegedAction<Void> { } /** - * Returns the file to open; does not return if no file was - * specified. + * Returns the url of file to open; does not return if no file was + * specified, or if the file location was invalid. */ - private static JNLPFile getFile() throws ParseException, MalformedURLException, IOException { + private static URL getFileLocation() { String location = getJNLPFile(); @@ -274,89 +280,7 @@ public final class Boot implements PrivilegedAction<Void> { e.printStackTrace(); } - boolean strict = (null != getOption("-strict")); - - JNLPFile file = new JNLPFile(url, strict); - - // Launches the jnlp file where this file originated. - if (file.getSourceLocation() != null) { - file = new JNLPFile(file.getSourceLocation(), strict); - } - - // add in extra params from command line - addProperties(file); - - if (file.isApplet()) - addParameters(file); - - if (file.isApplication()) - addArguments(file); - - if (JNLPRuntime.isDebug()) { - if (getOption("-arg") != null) - if (file.isInstaller() || file.isApplet()) - System.out.println(R("BArgsNA")); - - if (getOption("-param") != null) - if (file.isApplication()) - System.out.println(R("BParamNA")); - } - - return file; - } - - /** - * Add the properties to the JNLP file. - */ - private static void addProperties(JNLPFile file) { - String props[] = getOptions("-property"); - ResourcesDesc resources = file.getResources(); - - for (int i = 0; i < props.length; i++) { - // allows empty property, not sure about validity of that. - int equals = props[i].indexOf("="); - if (equals == -1) - fatalError(R("BBadProp", props[i])); - - String key = props[i].substring(0, equals); - String value = props[i].substring(equals + 1, props[i].length()); - - resources.addResource(new PropertyDesc(key, value)); - } - } - - /** - * Add the params to the JNLP file; only call if file is - * actually an applet file. - */ - private static void addParameters(JNLPFile file) { - String params[] = getOptions("-param"); - AppletDesc applet = file.getApplet(); - - for (int i = 0; i < params.length; i++) { - // allows empty param, not sure about validity of that. - int equals = params[i].indexOf("="); - if (equals == -1) - fatalError(R("BBadParam", params[i])); - - String name = params[i].substring(0, equals); - String value = params[i].substring(equals + 1, params[i].length()); - - applet.addParameter(name, value); - } - } - - /** - * Add the arguments to the JNLP file; only call if file is - * actually an application (not installer). - */ - private static void addArguments(JNLPFile file) { - String args[] = getOptions("-arg"); // FYI args also global variable - ApplicationDesc app = file.getApplication(); - - for (int i = 0; i < args.length; i++) { - app.addArgument(args[i]); - } + return url; } /** |