|
Apache CXF example source code file (JavascriptTestUtilities.java)
The Apache CXF JavascriptTestUtilities.java source code/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.cxf.javascript; import java.io.IOException; import java.io.Reader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.cxf.common.classloader.ClassLoaderUtils; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.javascript.service.ServiceJavascriptBuilder; import org.apache.cxf.javascript.types.SchemaJavascriptBuilder; import org.apache.cxf.service.model.SchemaInfo; import org.apache.cxf.service.model.ServiceInfo; import org.apache.cxf.test.TestUtilities; import org.junit.Assert; import org.mozilla.javascript.Context; import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Function; import org.mozilla.javascript.JavaScriptException; import org.mozilla.javascript.RhinoException; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; /** * Test utilities class with some Javascript capability included. */ public class JavascriptTestUtilities extends TestUtilities { private static final Logger LOG = LogUtils.getL7dLogger(JavascriptTestUtilities.class); private static boolean rhinoDebuggerUp; private ContextFactory rhinoContextFactory; private ScriptableObject rhinoScope; private Context rhinoContext; public static class JavaScriptAssertionFailed extends RuntimeException { public JavaScriptAssertionFailed(String what) { super(what); } } public static class JsAssert extends ScriptableObject { public JsAssert() { } public void jsConstructor(String exp) { LOG.severe("Assertion failed: " + exp); throw new JavaScriptAssertionFailed(exp); } @Override public String getClassName() { return "Assert"; } } public static class Trace extends ScriptableObject { public Trace() { } @Override public String getClassName() { return "org_apache_cxf_trace"; } // CHECKSTYLE:OFF public static void jsStaticFunction_trace(String message) { LOG.fine(message); } // CHECKSTYLE:ON } public static class Notifier extends ScriptableObject { private boolean notified; public Notifier() { } @Override public String getClassName() { return "org_apache_cxf_notifier"; } public synchronized boolean waitForJavascript(long timeout) { while (!notified) { try { wait(timeout); return notified; } catch (InterruptedException e) { // do nothing. } } return true; // only here if true on entry. } // CHECKSTYLE:OFF public synchronized void jsFunction_notify() { notified = true; notifyAll(); } // CHECKSTYLE:ON } public static class CountDownNotifier extends ScriptableObject { private CountDownLatch latch; public CountDownNotifier() { } @Override public String getClassName() { return "org_apache_cxf_count_down_notifier"; } public synchronized boolean waitForJavascript(long timeout) { while (true) { try { return latch.await(timeout, TimeUnit.MILLISECONDS); // if it returns at all, we're done. } catch (InterruptedException ie) { // empty on purpose. } } } // CHECKSTYLE:OFF public void jsConstructor(int count) { latch = new CountDownLatch(count); } public void jsFunction_count() { latch.countDown(); } // CHECKSTYLE:ON } public JavascriptTestUtilities(Class<?> classpathReference) { super(classpathReference); } public void initializeRhino() { rhinoContextFactory = new ContextFactory(); if (System.getProperty("cxf.jsdebug") != null && !rhinoDebuggerUp) { try { Class<?> debuggerMain = ClassLoaderUtils.loadClass("org.mozilla.javascript.tools.debugger.Main", getClass()); if (debuggerMain != null) { Method mainMethod = debuggerMain.getMethod("mainEmbedded", ContextFactory.class, Scriptable.class, String.class); mainMethod.invoke(null, rhinoContextFactory, rhinoScope, "Debug embedded JavaScript."); rhinoDebuggerUp = true; } } catch (Exception e) { LOG.log(Level.WARNING, "Failed to launch Rhino debugger", e); } } rhinoContext = rhinoContextFactory.enterContext(); rhinoScope = rhinoContext.initStandardObjects(); try { ScriptableObject.defineClass(rhinoScope, JsAssert.class); ScriptableObject.defineClass(rhinoScope, Trace.class); ScriptableObject.defineClass(rhinoScope, Notifier.class); ScriptableObject.defineClass(rhinoScope, CountDownNotifier.class); // so that the stock test for IE can gracefully fail. rhinoContext.evaluateString(rhinoScope, "var window = new Object();", "<internal>", 0, null); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } finally { Context.exit(); } JsSimpleDomNode.register(rhinoScope); JsSimpleDomParser.register(rhinoScope); JsNamedNodeMap.register(rhinoScope); JsXMLHttpRequest.register(rhinoScope); } public void readResourceIntoRhino(String resourceClasspath) throws IOException { Reader js = getResourceAsReader(resourceClasspath); rhinoContextFactory.enterContext(rhinoContext); try { rhinoContext.evaluateReader(rhinoScope, js, resourceClasspath, 1, null); } finally { Context.exit(); } } public void readStringIntoRhino(String js, String sourceName) { LOG.fine(sourceName + ":\n" + js); rhinoContextFactory.enterContext(rhinoContext); try { rhinoContext.evaluateString(rhinoScope, js, sourceName, 1, null); } finally { Context.exit(); } } public ScriptableObject getRhinoScope() { return rhinoScope; } public ContextFactory getRhinoContextFactory() { return rhinoContextFactory; } public static interface JSRunnable<T> { T run(Context context); } public <T> T runInsideContext(Class Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF JavascriptTestUtilities.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.