本文整理了Java中org.mozilla.javascript.ContextFactory
类的一些代码示例,展示了ContextFactory
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ContextFactory
类的具体详情如下:
包路径:org.mozilla.javascript.ContextFactory
类名称:ContextFactory
[英]Factory class that Rhino runtime uses to create new Contextinstances. A ContextFactory
can also notify listeners about context creation and release.
When the Rhino runtime needs to create new Context instance during execution of Context#enter() or Context, it will call #makeContext() of the current global ContextFactory. See #getGlobal() and #initGlobal(ContextFactory).
It is also possible to use explicit ContextFactory instances for Context creation. This is useful to have a set of independent Rhino runtime instances under single JVM. See #call(ContextAction).
The following example demonstrates Context customization to terminate scripts running more then 10 seconds and to provide better compatibility with JavaScript code using MSIE-specific features.
import org.mozilla.javascript.*;
class MyFactory extends ContextFactory
{
// Custom
Context to store execution time.
private static class MyContext extends Context
{
long startTime;
}
static {
// Initialize GlobalFactory with custom factory
ContextFactory.initGlobal(new MyFactory());
}
// Override
#makeContext()protected Context makeContext()
{
MyContext cx = new MyContext();
// Make Rhino runtime to call observeInstructionCount
// each 10000 bytecode instructions
cx.setInstructionObserverThreshold(10000);
return cx;
}
// Override
#hasFeature(Context,int)public boolean hasFeature(Context cx, int featureIndex)
{
// Turn on maximum compatibility with MSIE scripts
switch (featureIndex) {
case
Context#FEATURE_NON_ECMA_GET_YEAR:
return true;
case
Context#FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME:
return true;
case
Context#FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
return true;
case
Context#FEATURE_PARENT_PROTO_PROPERTIES:
return false;
}
return super.hasFeature(cx, featureIndex);
}
// Override
#observeInstructionCount(Context,int)protected void observeInstructionCount(Context cx, int instructionCount)
{
MyContext mcx = (MyContext)cx;
long currentTime = System.currentTimeMillis();
if (currentTime - mcx.startTime > 10*1000) {
// More then 10 seconds from Context creation time:
// it is time to stop the script.
// Throw Error instance to ensure that script will never
// get control back through catch or finally.
throw new Error();
}
}
// Override
#doTopCall(Callable,Context,Scriptable,Scriptable,Object[])protected Object doTopCall(Callable callable,
Context cx, Scriptable scope,
Scriptable thisObj, Object[] args)
{
MyContext mcx = (MyContext)cx;
mcx.startTime = System.currentTimeMillis();
return super.doTopCall(callable, cx, scope, thisObj, args);
}
}
[中]Rhino运行时用于创建新ContextInstance的工厂类。ContextFactory
还可以通知侦听器上下文的创建和释放。
当Rhino运行时需要在执行Context#enter()或Context时创建新的Context实例时,它将调用当前全局ContextFactory的#makeContext()。请参阅#getGlobal()和#initGlobal(ContextFactory)。
还可以使用显式ContextFactory实例创建上下文。在单个JVM下拥有一组独立的Rhino运行时实例非常有用。参见#调用(上下文操作)。
下面的示例演示了上下文自定义,以终止运行超过10秒的脚本,并使用MSIE特定功能提供与JavaScript代码更好的兼容性。
import org.mozilla.javascript.*;
class MyFactory extends ContextFactory
{
// Custom
Context to store execution time.
private static class MyContext extends Context
{
long startTime;
}
static {
// Initialize GlobalFactory with custom factory
ContextFactory.initGlobal(new MyFactory());
}
// Override
#makeContext()protected Context makeContext()
{
MyContext cx = new MyContext();
// Make Rhino runtime to call observeInstructionCount
// each 10000 bytecode instructions
cx.setInstructionObserverThreshold(10000);
return cx;
}
// Override
#hasFeature(Context,int)public boolean hasFeature(Context cx, int featureIndex)
{
// Turn on maximum compatibility with MSIE scripts
switch (featureIndex) {
case
Context#FEATURE_NON_ECMA_GET_YEAR:
return true;
case
Context#FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME:
return true;
case
Context#FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
return true;
case
Context#FEATURE_PARENT_PROTO_PROPERTIES:
return false;
}
return super.hasFeature(cx, featureIndex);
}
// Override
#observeInstructionCount(Context,int)protected void observeInstructionCount(Context cx, int instructionCount)
{
MyContext mcx = (MyContext)cx;
long currentTime = System.currentTimeMillis();
if (currentTime - mcx.startTime > 10*1000) {
// More then 10 seconds from Context creation time:
// it is time to stop the script.
// Throw Error instance to ensure that script will never
// get control back through catch or finally.
throw new Error();
}
}
// Override
#doTopCall(Callable,Context,Scriptable,Scriptable,Object[])protected Object doTopCall(Callable callable,
Context cx, Scriptable scope,
Scriptable thisObj, Object[] args)
{
MyContext mcx = (MyContext)cx;
mcx.startTime = System.currentTimeMillis();
return super.doTopCall(callable, cx, scope, thisObj, args);
}
}
代码示例来源:origin: pentaho/pentaho-kettle
Script jsscript;
jscx = ContextFactory.getGlobal().enterContext();
jsscope = jscx.initStandardObjects( null, false );
try {
jscx.setOptimizationLevel( Integer.valueOf( transMeta.environmentSubstitute( optimizationLevel ) ) );
} catch ( NumberFormatException nfe ) {
error_message =
Scriptable jsR = Context.toObject( jsScripts[i].getScript(), jsscope );
jsscope.put( jsScripts[i].getScriptName(), jsscope, jsR );
代码示例来源:origin: EngineHub/WorldEdit
@Override
protected Object doTopCall(Callable callable, Context cx, Scriptable scope,
Scriptable thisObj, Object[] args) {
RhinoContext mcx = (RhinoContext) cx;
mcx.startTime = System.currentTimeMillis();
return super.doTopCall(callable, cx, scope, thisObj, args);
}
代码示例来源:origin: galenframework/galen
public static String getVersion() {
return ContextFactory.getGlobal().enterContext().getImplementationVersion();
}
代码示例来源:origin: apache/incubator-druid
private static Function compile(String function)
{
final ContextFactory contextFactory = ContextFactory.getGlobal();
final Context context = contextFactory.enterContext();
context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
final ScriptableObject scope = context.initStandardObjects();
final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
Context.exit();
return new Function()
{
@Override
public double apply(Object[] args)
{
// ideally we need a close() function to discard the context once it is not used anymore
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
}
return Context.toNumber(fn.call(cx, scope, scope, args));
}
};
}
代码示例来源:origin: spring-projects/spring-framework
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
jsContext = ContextFactory.getGlobal().enterContext();
jsScope = jsContext.initStandardObjects();
view = new MappingJackson2JsonView();
}
代码示例来源:origin: pentaho/pentaho-kettle
Scriptable scope;
cx = ContextFactory.getGlobal().enterContext();
scope = cx.initStandardObjects( null );
Object res = cx.evaluateString( scope, this.script, "<cmd>", 1, null );
boolean retval = Context.toBoolean( res );
代码示例来源:origin: apache/incubator-druid
@Override
public Object apply(Object input)
{
// ideally we need a close() function to discard the context once it is not used anymore
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
}
final Object res = fn.call(cx, scope, scope, new Object[]{input});
return res != null ? Context.toObject(res, scope) : null;
}
};
代码示例来源:origin: org.apache.xmlgraphics/batik-bridge
/**
* Creates a Context object for use with the interpreter.
*/
protected Context makeContext() {
Context cx = super.makeContext();
cx.setWrapFactory(wrapFactory);
cx.setSecurityController(securityController);
cx.setClassShutter(classShutter);
if (rhinoClassLoader == null) {
cx.setOptimizationLevel(-1);
}
return cx;
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public String apply(Object input)
{
// ideally we need a close() function to discard the context once it is not used anymore
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
}
final Object res = fn.call(cx, scope, scope, new Object[]{input});
return res != null ? Context.toString(res) : null;
}
};
代码示例来源:origin: rhino/js
/**
* @deprecated
* @see ContextFactory#removeListener(ContextFactory.Listener)
* @see ContextFactory#getGlobal()
*/
public static void removeContextListener(ContextListener listener)
{
ContextFactory.getGlobal().addListener(listener);
}
代码示例来源:origin: datacleaner/DataCleaner
@Close
public void close() {
final Context context = _contextFactory.enterContext();
try {
_closeFunction.call(context, _sharedScope, _sharedScope, new Object[0]);
} finally {
Context.exit();
}
}
代码示例来源:origin: cat.inspiracio/rhino-js-engine
protected Context makeContext() {
Context cx = super.makeContext();
cx.setOptimizationLevel(-1);
return cx;
}
});
代码示例来源:origin: apache/incubator-druid
@Override
public double apply(Object[] args)
{
// ideally we need a close() function to discard the context once it is not used anymore
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
}
return Context.toNumber(fn.call(cx, scope, scope, args));
}
};
代码示例来源:origin: io.apisense/rhino-android
/**
* Create new Context instance to be associated with the current thread.
*/
@Override
protected Context makeContext() {
Context cx = super.makeContext();
cx.setLanguageVersion(languageVersion);
cx.setOptimizationLevel(optimizationLevel);
cx.setClassShutter(RhinoClassShutter.getInstance());
cx.setWrapFactory(RhinoWrapFactory.getInstance());
return cx;
}
代码示例来源:origin: rhino/js
public static Scriptable runScript(final Script script)
{
return (Scriptable)ContextFactory.getGlobal().call(
new ContextAction() {
public Object run(Context cx)
{
ScriptableObject global = ScriptRuntime.getGlobal(cx);
script.exec(cx, global);
return global;
}
});
}
代码示例来源:origin: cat.inspiracio/rhino-js-engine
public void initialize() {
if (!initialized) {
if ("true".equals(getProperty(USE_INTERPRETER_SYSTEM_PROPERTY))) {
if (!ContextFactory.hasExplicitGlobal()) {
ContextFactory.initGlobal(new ContextFactory() {
protected Context makeContext() {
Context cx = super.makeContext();
cx.setOptimizationLevel(-1);
return cx;
}
});
}
}
if (listener != null) {
ContextFactory.getGlobal().addListener(listener);
}
initialized = true;
}
}
代码示例来源:origin: rhq-project/rhq
@Override
protected Context makeContext() {
Context cx = super.makeContext();
cx.setOptimizationLevel(-1);
cx.setWrapFactory(new CustomWrapFactory());
return cx;
}
代码示例来源:origin: com.sun.phobos/phobos-rhino
private static Context prepareNewContext(ContextFactory factory,
Object contextHelper)
{
Context cx = factory.makeContext();
if (cx.factory != null || cx.enterCount != 0) {
throw new IllegalStateException("factory.makeContext() returned Context instance already associated with some thread");
}
cx.factory = factory;
factory.onContextCreated(cx);
if (factory.isSealed() && !cx.isSealed()) {
cx.seal(null);
}
VMBridge.instance.setContext(contextHelper, cx);
return cx;
}
代码示例来源:origin: rhino/js
public static Object doTopCall(Callable callable,
Context cx, Scriptable scope,
Scriptable thisObj, Object[] args)
{
if (scope == null)
throw new IllegalArgumentException();
if (cx.topCallScope != null) throw new IllegalStateException();
Object result;
cx.topCallScope = ScriptableObject.getTopLevelScope(scope);
cx.useDynamicScope = cx.hasFeature(Context.FEATURE_DYNAMIC_SCOPE);
ContextFactory f = cx.getFactory();
try {
result = f.doTopCall(callable, cx, scope, thisObj, args);
} finally {
cx.topCallScope = null;
// Cleanup cached references
cx.cachedXMLLib = null;
if (cx.currentActivationCall != null) {
// Function should always call exitActivationFunction
// if it creates activation record
throw new IllegalStateException();
}
}
return result;
}
代码示例来源:origin: org.apache.xmlgraphics/batik-bridge
/**
* To be used by <code>WindowWrapper</code>.
*/
void callHandler(final Function handler, final Object[] args) {
contextFactory.call(new ContextAction() {
public Object run(Context cx) {
handler.call(cx, globalObject, globalObject, args);
return null;
}
});
}
内容来源于网络,如有侵权,请联系作者删除!