src/share/classes/javax/script/ScriptEngineManager.java

Print this page
rev 8061 : 8011697: ScriptEngine "js" randomly means either "rhino" or "nashorn", but should instead select one
Summary: changed HashSet to ArrayList.
   1 /*
   2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  60         ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
  61         init(ctxtLoader);
  62     }
  63 
  64     /**
  65      * This constructor loads the implementations of
  66      * <code>ScriptEngineFactory</code> visible to the given
  67      * <code>ClassLoader</code> using the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism.<br><br>
  68      * If loader is <code>null</code>, the script engine factories that are
  69      * bundled with the platform and that are in the usual extension
  70      * directories (installed extensions) are loaded. <br><br>
  71      *
  72      * @param loader ClassLoader used to discover script engine factories.
  73      */
  74     public ScriptEngineManager(ClassLoader loader) {
  75         init(loader);
  76     }
  77 
  78     private void init(final ClassLoader loader) {
  79         globalScope = new SimpleBindings();
  80         engineSpis = new HashSet<ScriptEngineFactory>();
  81         nameAssociations = new HashMap<String, ScriptEngineFactory>();
  82         extensionAssociations = new HashMap<String, ScriptEngineFactory>();
  83         mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
  84         AccessController.doPrivileged(new PrivilegedAction<Object>() {
  85             public Object run() {
  86                 initEngines(loader);
  87                 return null;
  88             }
  89         });
  90     }
  91 
  92     private void initEngines(final ClassLoader loader) {
  93         Iterator<ScriptEngineFactory> itr = null;
  94         try {
  95             ServiceLoader<ScriptEngineFactory> sl;
  96             if (loader != null) {
  97                 sl = ServiceLoader.load(ScriptEngineFactory.class, loader);
  98             } else {
  99                 sl = ServiceLoader.loadInstalled(ScriptEngineFactory.class);
 100             }


 329                 if (mimeType.equals(type)) {
 330                     try {
 331                         ScriptEngine engine = spi.getScriptEngine();
 332                         engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
 333                         return engine;
 334                     } catch (Exception exp) {
 335                         if (DEBUG) exp.printStackTrace();
 336                     }
 337                 }
 338             }
 339         }
 340         return null;
 341     }
 342 
 343     /**
 344      * Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
 345      * found by the discovery mechanism.
 346      * @return List of all discovered <code>ScriptEngineFactory</code>s.
 347      */
 348     public List<ScriptEngineFactory> getEngineFactories() {
 349         List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
 350         for (ScriptEngineFactory spi : engineSpis) {
 351             res.add(spi);
 352         }
 353         return Collections.unmodifiableList(res);
 354     }
 355 
 356     /**
 357      * Registers a <code>ScriptEngineFactory</code> to handle a language
 358      * name.  Overrides any such association found using the Discovery mechanism.
 359      * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
 360      * @param factory The class to associate with the given name.
 361      * @throws NullPointerException if any of the parameters is null.
 362      */
 363     public void registerEngineName(String name, ScriptEngineFactory factory) {
 364         if (name == null || factory == null) throw new NullPointerException();
 365         nameAssociations.put(name, factory);
 366     }
 367 
 368     /**
 369      * Registers a <code>ScriptEngineFactory</code> to handle a mime type.
 370      * Overrides any such association found using the Discovery mechanism.
 371      *
 372      * @param type The mime type  to be associated with the
 373      * <code>ScriptEngineFactory</code>.


 377      */
 378     public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
 379         if (type == null || factory == null) throw new NullPointerException();
 380         mimeTypeAssociations.put(type, factory);
 381     }
 382 
 383     /**
 384      * Registers a <code>ScriptEngineFactory</code> to handle an extension.
 385      * Overrides any such association found using the Discovery mechanism.
 386      *
 387      * @param extension The extension type  to be associated with the
 388      * <code>ScriptEngineFactory</code>.
 389      * @param factory The class to associate with the given extension.
 390      * @throws NullPointerException if any of the parameters is null.
 391      */
 392     public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
 393         if (extension == null || factory == null) throw new NullPointerException();
 394         extensionAssociations.put(extension, factory);
 395     }
 396 
 397     /** Set of script engine factories discovered. */
 398     private HashSet<ScriptEngineFactory> engineSpis;
 399 
 400     /** Map of engine name to script engine factory. */
 401     private HashMap<String, ScriptEngineFactory> nameAssociations;
 402 
 403     /** Map of script file extension to script engine factory. */
 404     private HashMap<String, ScriptEngineFactory> extensionAssociations;
 405 
 406     /** Map of script script MIME type to script engine factory. */
 407     private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
 408 
 409     /** Global bindings associated with script engines created by this manager. */
 410     private Bindings globalScope;
 411 }
   1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  60         ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
  61         init(ctxtLoader);
  62     }
  63 
  64     /**
  65      * This constructor loads the implementations of
  66      * <code>ScriptEngineFactory</code> visible to the given
  67      * <code>ClassLoader</code> using the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism.<br><br>
  68      * If loader is <code>null</code>, the script engine factories that are
  69      * bundled with the platform and that are in the usual extension
  70      * directories (installed extensions) are loaded. <br><br>
  71      *
  72      * @param loader ClassLoader used to discover script engine factories.
  73      */
  74     public ScriptEngineManager(ClassLoader loader) {
  75         init(loader);
  76     }
  77 
  78     private void init(final ClassLoader loader) {
  79         globalScope = new SimpleBindings();
  80         engineSpis = new ArrayList<ScriptEngineFactory>();
  81         nameAssociations = new HashMap<String, ScriptEngineFactory>();
  82         extensionAssociations = new HashMap<String, ScriptEngineFactory>();
  83         mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
  84         AccessController.doPrivileged(new PrivilegedAction<Object>() {
  85             public Object run() {
  86                 initEngines(loader);
  87                 return null;
  88             }
  89         });
  90     }
  91 
  92     private void initEngines(final ClassLoader loader) {
  93         Iterator<ScriptEngineFactory> itr = null;
  94         try {
  95             ServiceLoader<ScriptEngineFactory> sl;
  96             if (loader != null) {
  97                 sl = ServiceLoader.load(ScriptEngineFactory.class, loader);
  98             } else {
  99                 sl = ServiceLoader.loadInstalled(ScriptEngineFactory.class);
 100             }


 329                 if (mimeType.equals(type)) {
 330                     try {
 331                         ScriptEngine engine = spi.getScriptEngine();
 332                         engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
 333                         return engine;
 334                     } catch (Exception exp) {
 335                         if (DEBUG) exp.printStackTrace();
 336                     }
 337                 }
 338             }
 339         }
 340         return null;
 341     }
 342 
 343     /**
 344      * Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
 345      * found by the discovery mechanism.
 346      * @return List of all discovered <code>ScriptEngineFactory</code>s.
 347      */
 348     public List<ScriptEngineFactory> getEngineFactories() {
 349         return Collections.unmodifiableList(engineSpis);




 350     }
 351 
 352     /**
 353      * Registers a <code>ScriptEngineFactory</code> to handle a language
 354      * name.  Overrides any such association found using the Discovery mechanism.
 355      * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
 356      * @param factory The class to associate with the given name.
 357      * @throws NullPointerException if any of the parameters is null.
 358      */
 359     public void registerEngineName(String name, ScriptEngineFactory factory) {
 360         if (name == null || factory == null) throw new NullPointerException();
 361         nameAssociations.put(name, factory);
 362     }
 363 
 364     /**
 365      * Registers a <code>ScriptEngineFactory</code> to handle a mime type.
 366      * Overrides any such association found using the Discovery mechanism.
 367      *
 368      * @param type The mime type  to be associated with the
 369      * <code>ScriptEngineFactory</code>.


 373      */
 374     public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
 375         if (type == null || factory == null) throw new NullPointerException();
 376         mimeTypeAssociations.put(type, factory);
 377     }
 378 
 379     /**
 380      * Registers a <code>ScriptEngineFactory</code> to handle an extension.
 381      * Overrides any such association found using the Discovery mechanism.
 382      *
 383      * @param extension The extension type  to be associated with the
 384      * <code>ScriptEngineFactory</code>.
 385      * @param factory The class to associate with the given extension.
 386      * @throws NullPointerException if any of the parameters is null.
 387      */
 388     public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
 389         if (extension == null || factory == null) throw new NullPointerException();
 390         extensionAssociations.put(extension, factory);
 391     }
 392 
 393     /** List of script engine factories discovered. */
 394     private ArrayList<ScriptEngineFactory> engineSpis;
 395 
 396     /** Map of engine name to script engine factory. */
 397     private HashMap<String, ScriptEngineFactory> nameAssociations;
 398 
 399     /** Map of script file extension to script engine factory. */
 400     private HashMap<String, ScriptEngineFactory> extensionAssociations;
 401 
 402     /** Map of script script MIME type to script engine factory. */
 403     private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
 404 
 405     /** Global bindings associated with script engines created by this manager. */
 406     private Bindings globalScope;
 407 }