Changeset 13331 in josm


Ignore:
Timestamp:
2018-01-16T00:28:24+01:00 (6 years ago)
Author:
Don-vip
Message:

see #14097, see #15783 - robustness to situations where JavaScript engine cannot be retrieved, for whatever reason

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

    r13298 r13331  
    1212import javax.script.Invocable;
    1313import javax.script.ScriptEngine;
    14 import javax.script.ScriptEngineManager;
    1514import javax.script.ScriptException;
    1615
     
    2322import org.openstreetmap.josm.tools.LanguageInfo;
    2423import org.openstreetmap.josm.tools.Logging;
     24import org.openstreetmap.josm.tools.Utils;
    2525
    2626/**
     
    3636     * Javascript engine
    3737     */
    38     public static final ScriptEngine ENGINE = new ScriptEngineManager(null).getEngineByName("JavaScript");
     38    public static final ScriptEngine ENGINE = Utils.getJavaScriptEngine();
    3939
    4040    /**
  • trunk/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java

    r12977 r13331  
    144144    private Optional<String> tryParseSearchTerm(String searchTerm) {
    145145        try {
    146             String query = this.overpassQueryBuilder.constructQuery(searchTerm);
    147 
    148             return Optional.of(query);
    149         } catch (UncheckedParseException ex) {
     146            return Optional.of(overpassQueryBuilder.constructQuery(searchTerm));
     147        } catch (UncheckedParseException | IllegalStateException ex) {
    150148            Logging.error(ex);
    151149            JOptionPane.showMessageDialog(
     
    158156                    JOptionPane.ERROR_MESSAGE
    159157            );
    160 
    161158            return Optional.empty();
    162159        }
  • trunk/src/org/openstreetmap/josm/gui/io/CustomConfigurator.java

    r13268 r13331  
    2626
    2727import javax.script.ScriptEngine;
    28 import javax.script.ScriptEngineManager;
    2928import javax.script.ScriptException;
    3029import javax.swing.JOptionPane;
     
    421420                this.mainPrefs = mainPrefs;
    422421                PreferencesUtils.resetLog();
    423                 engine = new ScriptEngineManager(null).getEngineByName("JavaScript");
     422                engine = Utils.getJavaScriptEngine();
     423                if (engine == null) {
     424                    throw new ScriptException("Failed to retrieve JavaScript engine");
     425                }
    424426                engine.eval("API={}; API.pref={}; API.fragments={};");
    425427
  • trunk/src/org/openstreetmap/josm/tools/OverpassTurboQueryWizard.java

    r13268 r13331  
    77import javax.script.Invocable;
    88import javax.script.ScriptEngine;
    9 import javax.script.ScriptEngineManager;
    109import javax.script.ScriptException;
    1110
     
    2221
    2322    private static OverpassTurboQueryWizard instance;
    24     private final ScriptEngine engine = new ScriptEngineManager(null).getEngineByName("JavaScript");
     23    private final ScriptEngine engine = Utils.getJavaScriptEngine();
    2524
    2625    /**
     
    3736
    3837    private OverpassTurboQueryWizard() {
    39         if (engine == null) {
    40             throw new IllegalStateException("Failed to retrieve JavaScript engine");
    41         }
    4238        try (CachedFile file = new CachedFile("resource://data/overpass-wizard.js");
    4339             Reader reader = file.getContentReader()) {
    44             engine.eval("var console = {error: " + Logging.class.getCanonicalName() + ".warn};");
    45             engine.eval("var global = {};");
    46             engine.eval(reader);
    47             engine.eval("var overpassWizard = function(query) {" +
    48                     "  return global.overpassWizard(query, {" +
    49                     "    comment: false," +
    50                     "    outputFormat: 'xml'," +
    51                     "    outputMode: 'recursive_meta'" +
    52                     "  });" +
    53                     "}");
     40            if (engine != null) {
     41                engine.eval("var console = {error: " + Logging.class.getCanonicalName() + ".warn};");
     42                engine.eval("var global = {};");
     43                engine.eval(reader);
     44                engine.eval("var overpassWizard = function(query) {" +
     45                        "  return global.overpassWizard(query, {" +
     46                        "    comment: false," +
     47                        "    outputFormat: 'xml'," +
     48                        "    outputMode: 'recursive_meta'" +
     49                        "  });" +
     50                        "}");
     51            }
    5452        } catch (ScriptException | IOException ex) {
    5553            throw new IllegalStateException("Failed to initialize OverpassTurboQueryWizard", ex);
     
    6462     */
    6563    public String constructQuery(String search) {
     64        if (engine == null) {
     65            throw new IllegalStateException("Failed to retrieve JavaScript engine");
     66        }
    6667        try {
    6768            final Object result = ((Invocable) engine).invokeFunction("overpassWizard", search);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r13273 r13331  
    6161import java.util.zip.ZipFile;
    6262
     63import javax.script.ScriptEngine;
     64import javax.script.ScriptEngineManager;
    6365import javax.xml.XMLConstants;
    6466import javax.xml.parsers.DocumentBuilder;
     
    17201722    }
    17211723
     1724    /**
     1725     * Returns JRE JavaScript Engine (Nashorn by default), if any.
     1726     * Catches and logs SecurityException and return null in case of error.
     1727     * @return JavaScript Engine, or null.
     1728     * @since 13301
     1729     */
     1730    public static ScriptEngine getJavaScriptEngine() {
     1731        try {
     1732            return new ScriptEngineManager(null).getEngineByName("JavaScript");
     1733        } catch (SecurityException e) {
     1734            Logging.error(e);
     1735            return null;
     1736        }
     1737    }
    17221738}
Note: See TracChangeset for help on using the changeset viewer.