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

Print this page




   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
  23  * questions.
  24  */
  25 
  26 package javax.script;
  27 import java.util.*;
  28 import java.net.URL;
  29 import java.io.*;
  30 import java.security.*;
  31 import sun.misc.Service;
  32 import sun.misc.ServiceConfigurationError;
  33 import sun.reflect.Reflection;
  34 import sun.security.util.SecurityConstants;
  35 
  36 /**
  37  * The <code>ScriptEngineManager</code> implements a discovery and instantiation
  38  * mechanism for <code>ScriptEngine</code> classes and also maintains a
  39  * collection of key/value pairs storing state shared by all engines created
  40  * by the Manager. This class uses the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism to enumerate all the
  41  * implementations of <code>ScriptEngineFactory</code>. <br><br>
  42  * The <code>ScriptEngineManager</code> provides a method to return an array of all these factories
  43  * as well as utility methods which look up factories on the basis of language name, file extension
  44  * and mime type.
  45  * <p>
  46  * The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope"  maintained
  47  * by the manager is available to all instances of <code>ScriptEngine</code> created
  48  * by the <code>ScriptEngineManager</code>.  The values in the <code>Bindings</code> are
  49  * generally exposed in all scripts.
  50  *
  51  * @author Mike Grogan
  52  * @author A. Sundararajan


  87         init(loader);
  88     }
  89 
  90     private void init(final ClassLoader loader) {
  91         globalScope = new SimpleBindings();
  92         engineSpis = new HashSet<ScriptEngineFactory>();
  93         nameAssociations = new HashMap<String, ScriptEngineFactory>();
  94         extensionAssociations = new HashMap<String, ScriptEngineFactory>();
  95         mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
  96         AccessController.doPrivileged(new PrivilegedAction<Object>() {
  97             public Object run() {
  98                 initEngines(loader);
  99                 return null;
 100             }
 101         });
 102     }
 103 
 104     private void initEngines(final ClassLoader loader) {
 105         Iterator<ScriptEngineFactory> itr = null;
 106         try {

 107             if (loader != null) {
 108                 itr = Service.providers(ScriptEngineFactory.class, loader);
 109             } else {
 110                 itr = Service.installedProviders(ScriptEngineFactory.class);
 111             }

 112         } catch (ServiceConfigurationError err) {
 113             System.err.println("Can't find ScriptEngineFactory providers: " +
 114                           err.getMessage());
 115             if (DEBUG) {
 116                 err.printStackTrace();
 117             }
 118             // do not throw any exception here. user may want to
 119             // manage his/her own factories using this manager
 120             // by explicit registratation (by registerXXX) methods.
 121             return;
 122         }
 123 
 124         try {
 125             while (itr.hasNext()) {
 126                 try {
 127                     ScriptEngineFactory fact = itr.next();
 128                     engineSpis.add(fact);
 129                 } catch (ServiceConfigurationError err) {
 130                     System.err.println("ScriptEngineManager providers.next(): "
 131                                  + err.getMessage());




   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
  23  * questions.
  24  */
  25 
  26 package javax.script;
  27 import java.util.*;


  28 import java.security.*;
  29 import java.util.ServiceLoader;
  30 import java.util.ServiceConfigurationError;
  31 import sun.reflect.Reflection;
  32 import sun.security.util.SecurityConstants;
  33 
  34 /**
  35  * The <code>ScriptEngineManager</code> implements a discovery and instantiation
  36  * mechanism for <code>ScriptEngine</code> classes and also maintains a
  37  * collection of key/value pairs storing state shared by all engines created
  38  * by the Manager. This class uses the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism to enumerate all the
  39  * implementations of <code>ScriptEngineFactory</code>. <br><br>
  40  * The <code>ScriptEngineManager</code> provides a method to return an array of all these factories
  41  * as well as utility methods which look up factories on the basis of language name, file extension
  42  * and mime type.
  43  * <p>
  44  * The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope"  maintained
  45  * by the manager is available to all instances of <code>ScriptEngine</code> created
  46  * by the <code>ScriptEngineManager</code>.  The values in the <code>Bindings</code> are
  47  * generally exposed in all scripts.
  48  *
  49  * @author Mike Grogan
  50  * @author A. Sundararajan


  85         init(loader);
  86     }
  87 
  88     private void init(final ClassLoader loader) {
  89         globalScope = new SimpleBindings();
  90         engineSpis = new HashSet<ScriptEngineFactory>();
  91         nameAssociations = new HashMap<String, ScriptEngineFactory>();
  92         extensionAssociations = new HashMap<String, ScriptEngineFactory>();
  93         mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
  94         AccessController.doPrivileged(new PrivilegedAction<Object>() {
  95             public Object run() {
  96                 initEngines(loader);
  97                 return null;
  98             }
  99         });
 100     }
 101 
 102     private void initEngines(final ClassLoader loader) {
 103         Iterator<ScriptEngineFactory> itr = null;
 104         try {
 105             ServiceLoader<ScriptEngineFactory> sl;
 106             if (loader != null) {
 107                 sl = ServiceLoader.load(ScriptEngineFactory.class, loader);
 108             } else {
 109                 sl = ServiceLoader.loadInstalled(ScriptEngineFactory.class);
 110             }
 111             itr = sl.iterator();
 112         } catch (ServiceConfigurationError err) {
 113             System.err.println("Can't find ScriptEngineFactory providers: " +
 114                           err.getMessage());
 115             if (DEBUG) {
 116                 err.printStackTrace();
 117             }
 118             // do not throw any exception here. user may want to
 119             // manage his/her own factories using this manager
 120             // by explicit registratation (by registerXXX) methods.
 121             return;
 122         }
 123 
 124         try {
 125             while (itr.hasNext()) {
 126                 try {
 127                     ScriptEngineFactory fact = itr.next();
 128                     engineSpis.add(fact);
 129                 } catch (ServiceConfigurationError err) {
 130                     System.err.println("ScriptEngineManager providers.next(): "
 131                                  + err.getMessage());