jdk/src/share/classes/javax/script/ScriptEngineManager.java
author chegar
Thu, 01 Dec 2011 11:09:54 +0000
changeset 11119 6ff03c1202ce
parent 5506 202f599c92aa
child 11124 5fdabdc21259
permissions -rw-r--r--
7116722: Miscellaneous warnings sun.misc ( and related classes ) Reviewed-by: alanb, darcy, forax, hawtin, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.script;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.misc.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.misc.ServiceConfigurationError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.reflect.Reflection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The <code>ScriptEngineManager</code> implements a discovery and instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * mechanism for <code>ScriptEngine</code> classes and also maintains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * collection of key/value pairs storing state shared by all engines created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * implementations of <code>ScriptEngineFactory</code>. <br><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The <code>ScriptEngineManager</code> provides a method to return an array of all these factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * as well as utility methods which look up factories on the basis of language name, file extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * and mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope"  maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * by the manager is available to all instances of <code>ScriptEngine</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * by the <code>ScriptEngineManager</code>.  The values in the <code>Bindings</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * generally exposed in all scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author A. Sundararajan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class ScriptEngineManager  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * If the thread context ClassLoader can be accessed by the caller,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * then the effect of calling this constructor is the same as calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Otherwise, the effect is the same as calling <code>ScriptEngineManager(null)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @see java.lang.Thread#getContextClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public ScriptEngineManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (canCallerAccessLoader(ctxtLoader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (DEBUG) System.out.println("using " + ctxtLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            init(ctxtLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (DEBUG) System.out.println("using bootstrap loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * This constructor loads the implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <code>ScriptEngineFactory</code> visible to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <code>ClassLoader</code> using the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism.<br><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * If loader is <code>null</code>, the script engine factories that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * bundled with the platform and that are in the usual extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * directories (installed extensions) are loaded. <br><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param loader ClassLoader used to discover script engine factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public ScriptEngineManager(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        init(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private void init(final ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        globalScope = new SimpleBindings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        engineSpis = new HashSet<ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        nameAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        extensionAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                initEngines(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private void initEngines(final ClassLoader loader) {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   105
        Iterator<ScriptEngineFactory> itr = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                itr = Service.providers(ScriptEngineFactory.class, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                itr = Service.installedProviders(ScriptEngineFactory.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.err.println("Can't find ScriptEngineFactory providers: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                          err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                try {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   127
                    ScriptEngineFactory fact = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    engineSpis.add(fact);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    System.err.println("ScriptEngineManager providers.next(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                 + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    // one factory failed, but check other factories...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            System.err.println("ScriptEngineManager providers.hasNext(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <code>setBindings</code> stores the specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * in the <code>globalScope</code> field. ScriptEngineManager sets this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <code>Bindings</code> as global bindings for <code>ScriptEngine</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param bindings The specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws IllegalArgumentException if bindings is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void setBindings(Bindings bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (bindings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new IllegalArgumentException("Global scope cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        globalScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <code>getBindings</code> returns the value of the <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * ScriptEngineManager sets this <code>Bindings</code> as global bindings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <code>ScriptEngine</code> objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return The globalScope field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public Bindings getBindings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Sets the specified key/value pair in the Global Scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param key Key to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param value Value to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @throws NullPointerException if key is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @throws IllegalArgumentException if key is empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void put(String key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        globalScope.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Gets the value for the specified key in the Global Scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param key The key whose value is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return The value for the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Object get(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return globalScope.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Looks up and creates a <code>ScriptEngine</code> for a given  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * registered as a handler for the specified name using the <code>registerEngineName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <br><br> If one is not found, it searches the array of <code>ScriptEngineFactory</code> instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * stored by the constructor for one with the specified name.  If a <code>ScriptEngineFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * is found by either method, it is used to create instance of <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param shortName The short name of the <code>ScriptEngine</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return A <code>ScriptEngine</code> created by the factory located in the search.  Returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * if no such factory was found.  The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * created <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @throws NullPointerException if shortName is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public ScriptEngine getEngineByName(String shortName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (shortName == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        //look for registered name first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (null != (obj = nameAssociations.get(shortName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            List<String> names = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                names = spi.getNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                for (String name : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (shortName.equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Look up and create a <code>ScriptEngine</code> for a given extension.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * given extension using <code>registerEngineExtension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param extension The given extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return The engine to handle scripts with this extension.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws NullPointerException if extension is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public ScriptEngine getEngineByExtension(String extension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (extension == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        //look for registered extension first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (null != (obj = extensionAssociations.get(extension))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            List<String> exts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                exts = spi.getExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (exts == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            for (String ext : exts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (extension.equals(ext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Look up and create a <code>ScriptEngine</code> for a given mime type.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * given mime type using <code>registerEngineMimeType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param mimeType The given mime type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return The engine to handle scripts with this mime type.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @throws NullPointerException if mimeType is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public ScriptEngine getEngineByMimeType(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (mimeType == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        //look for registered types first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (null != (obj = mimeTypeAssociations.get(mimeType))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            List<String> types = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                types = spi.getMimeTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (types == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            for (String type : types) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                if (mimeType.equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Returns an array whose elements are instances of all the <code>ScriptEngineFactory</code> classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * found by the discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return List of all discovered <code>ScriptEngineFactory</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public List<ScriptEngineFactory> getEngineFactories() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            res.add(spi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return Collections.unmodifiableList(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Registers a <code>ScriptEngineFactory</code> to handle a language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * name.  Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param factory The class to associate with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void registerEngineName(String name, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (name == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        nameAssociations.put(name, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Registers a <code>ScriptEngineFactory</code> to handle a mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param type The mime type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param factory The class to associate with the given mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (type == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        mimeTypeAssociations.put(type, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Registers a <code>ScriptEngineFactory</code> to handle an extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param extension The extension type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param factory The class to associate with the given extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (extension == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        extensionAssociations.put(extension, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /** Set of script engine factories discovered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private HashSet<ScriptEngineFactory> engineSpis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /** Map of engine name to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private HashMap<String, ScriptEngineFactory> nameAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /** Map of script file extension to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private HashMap<String, ScriptEngineFactory> extensionAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /** Map of script script MIME type to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /** Global bindings associated with script engines created by this manager. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private Bindings globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private boolean canCallerAccessLoader(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            ClassLoader callerLoader = getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (callerLoader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                if (loader != callerLoader || !isAncestor(loader, callerLoader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    } catch (SecurityException exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                } // else fallthru..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } // else fallthru..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } // else fallthru..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    // Note that this code is same as ClassLoader.getCallerClassLoader().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    // But, that method is package private and hence we can't call here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private ClassLoader getCallerClassLoader() {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   444
        Class<?> caller = Reflection.getCallerClass(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (caller == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return caller.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // is cl1 ancestor of cl2?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    private boolean isAncestor(ClassLoader cl1, ClassLoader cl2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            cl2 = cl2.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (cl1 == cl2) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        } while (cl2 != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
}