📄 contextfactory.java
字号:
private boolean isDom3Present() { Class nodeClass = Kit.classOrNull("org.w3c.dom.Node"); if (nodeClass == null) return false; // Check to see whether DOM3 is present; use a new method defined in // DOM3 that is vital to our implementation try { nodeClass.getMethod("getUserData", new Class[] { String.class }); return true; } catch (NoSuchMethodException e) { return false; } } /** * Provides a default * {@link org.mozilla.javascript.xml.XMLLib.Factory XMLLib.Factory} * to be used by the <code>Context</code> instances produced by this * factory. See {@link Context#getE4xImplementationFactory} for details. * * May return null, in which case E4X functionality is not supported in * Rhino. * * The default implementation now prefers the DOM3 E4X implementation. */ protected org.mozilla.javascript.xml.XMLLib.Factory getE4xImplementationFactory() { // Must provide default implementation, rather than abstract method, // so that past implementors of ContextFactory do not fail at runtime // upon invocation of this method. // Note that the default implementation returns null if we // neither have XMLBeans nor a DOM3 implementation present. if (isDom3Present()) { return org.mozilla.javascript.xml.XMLLib.Factory.create( "org.mozilla.javascript.xmlimpl.XMLLibImpl" ); } else if (Kit.classOrNull("org.apache.xmlbeans.XmlCursor") != null) { return org.mozilla.javascript.xml.XMLLib.Factory.create( "org.mozilla.javascript.xml.impl.xmlbeans.XMLLibImpl" ); } else { return null; } } /** * Create class loader for generated classes. * This method creates an instance of the default implementation * of {@link GeneratedClassLoader}. Rhino uses this interface to load * generated JVM classes when no {@link SecurityController} * is installed. * Application can override the method to provide custom class loading. */ protected GeneratedClassLoader createClassLoader(ClassLoader parent) { return new DefiningClassLoader(parent); } /** * Get ClassLoader to use when searching for Java classes. * Unless it was explicitly initialized with * {@link #initApplicationClassLoader(ClassLoader)} the method returns * null to indicate that Thread.getContextClassLoader() should be used. */ public final ClassLoader getApplicationClassLoader() { return applicationClassLoader; } /** * Set explicit class loader to use when searching for Java classes. * * @see #getApplicationClassLoader() */ public final void initApplicationClassLoader(ClassLoader loader) { if (loader == null) throw new IllegalArgumentException("loader is null"); if (!Kit.testIfCanLoadRhinoClasses(loader)) throw new IllegalArgumentException( "Loader can not resolve Rhino classes"); if (this.applicationClassLoader != null) throw new IllegalStateException( "applicationClassLoader can only be set once"); checkNotSealed(); this.applicationClassLoader = loader; } /** * Execute top call to script or function. * When the runtime is about to execute a script or function that will * create the first stack frame with scriptable code, it calls this method * to perform the real call. In this way execution of any script * happens inside this function. */ protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { return callable.call(cx, scope, thisObj, args); } /** * Implementation of * {@link Context#observeInstructionCount(int instructionCount)}. * This can be used to customize {@link Context} without introducing * additional subclasses. */ protected void observeInstructionCount(Context cx, int instructionCount) { } protected void onContextCreated(Context cx) { Object listeners = this.listeners; for (int i = 0; ; ++i) { Listener l = (Listener)Kit.getListener(listeners, i); if (l == null) break; l.contextCreated(cx); } } protected void onContextReleased(Context cx) { Object listeners = this.listeners; for (int i = 0; ; ++i) { Listener l = (Listener)Kit.getListener(listeners, i); if (l == null) break; l.contextReleased(cx); } } public final void addListener(Listener listener) { checkNotSealed(); synchronized (listenersLock) { if (disabledListening) { throw new IllegalStateException(); } listeners = Kit.addListener(listeners, listener); } } public final void removeListener(Listener listener) { checkNotSealed(); synchronized (listenersLock) { if (disabledListening) { throw new IllegalStateException(); } listeners = Kit.removeListener(listeners, listener); } } /** * The method is used only to implement * Context.disableStaticContextListening() */ final void disableContextListening() { checkNotSealed(); synchronized (listenersLock) { disabledListening = true; listeners = null; } } /** * Checks if this is a sealed ContextFactory. * @see #seal() */ public final boolean isSealed() { return sealed; } /** * Seal this ContextFactory so any attempt to modify it like to add or * remove its listeners will throw an exception. * @see #isSealed() */ public final void seal() { checkNotSealed(); sealed = true; } protected final void checkNotSealed() { if (sealed) throw new IllegalStateException(); } /** * Call {@link ContextAction#run(Context cx)} * using the {@link Context} instance associated with the current thread. * If no Context is associated with the thread, then * {@link #makeContext()} will be called to construct * new Context instance. The instance will be temporary associated * with the thread during call to {@link ContextAction#run(Context)}. * * @see ContextFactory#call(ContextAction) * @see Context#call(ContextFactory factory, Callable callable, * Scriptable scope, Scriptable thisObj, * Object[] args) */ public final Object call(ContextAction action) { return Context.call(this, action); } /** * Get a context associated with the current thread, creating one if need * be. The Context stores the execution state of the JavaScript engine, so * it is required that the context be entered before execution may begin. * Once a thread has entered a Context, then getCurrentContext() may be * called to find the context that is associated with the current thread. * <p> * Calling <code>enterContext()</code> will return either the Context * currently associated with the thread, or will create a new context and * associate it with the current thread. Each call to * <code>enterContext()</code> must have a matching call to * {@link Context#exit()}. * <pre> * Context cx = contextFactory.enterContext(); * try { * ... * cx.evaluateString(...); * } finally { * Context.exit(); * } * </pre> * Instead of using <tt>enterContext()</tt>, <tt>exit()</tt> pair consider * using {@link #call(ContextAction)} which guarantees proper association * of Context instances with the current thread. * With this method the above example becomes: * <pre> * ContextFactory.call(new ContextAction() { * public Object run(Context cx) { * ... * cx.evaluateString(...); * return null; * } * }); * </pre> * @return a Context associated with the current thread * @see Context#getCurrentContext() * @see Context#exit() * @see #call(ContextAction) */ public Context enterContext() { return enterContext(null); } /** * @deprecated use {@link #enterContext()} instead * @return a Context associated with the current thread */ public final Context enter() { return enterContext(null); } /** * @deprecated Use {@link Context#exit()} instead. */ public final void exit() { Context.exit(); } /** * Get a Context associated with the current thread, using the given * Context if need be. * <p> * The same as <code>enterContext()</code> except that <code>cx</code> * is associated with the current thread and returned if the current thread * has no associated context and <code>cx</code> is not associated with any * other thread. * @param cx a Context to associate with the thread if possible * @return a Context associated with the current thread * @see #enterContext() * @see #call(ContextAction) * @throws IllegalStateException if <code>cx</code> is already associated * with a different thread */ public final Context enterContext(Context cx) { return Context.enter(cx, this); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -