diff --git a/base-third-project/base-third-step1/pom.xml b/base-third-project/base-third-step1/pom.xml
index cc9979a75..c0e2c1c99 100644
--- a/base-third-project/base-third-step1/pom.xml
+++ b/base-third-project/base-third-step1/pom.xml
@@ -27,7 +27,6 @@
../../fine-iconloader../../fine-icu4j../../fine-imageJ
- ../../fine-j2v8../../fine-jboss-transaction-api../../fine-jgit
diff --git a/build.third_step1-jdk11.gradle b/build.third_step1-jdk11.gradle
index 3b9eff091..449ddbe81 100644
--- a/build.third_step1-jdk11.gradle
+++ b/build.third_step1-jdk11.gradle
@@ -43,7 +43,6 @@ sourceSets{
"${srcDir}/fine-hsqldb/src/main/java",
"${srcDir}/fine-iconloader/src/main/java",
"${srcDir}/fine-icu4j/src/main/java",
- "${srcDir}/fine-j2v8/src/main/java",
// "${srcDir}/fine-jai/src/main/java",
"${srcDir}/fine-jboss-transaction-api/src/main/java",
"${srcDir}/fine-jgit/src/main/java",
@@ -95,8 +94,6 @@ def resourceDirs = [
"${srcDir}/fine-iconloader/src/main/resources",
"${srcDir}/fine-icu4j/src/main/java",
"${srcDir}/fine-icu4j/src/main/resources",
- "${srcDir}/fine-j2v8/src/main/java",
- "${srcDir}/fine-j2v8/src/main/resources",
// "${srcDir}/fine-jai/src/main/java",
"${srcDir}/fine-jboss-transaction-api/src/main/java",
"${srcDir}/fine-jboss-transaction-api/src/main/resources",
diff --git a/build.third_step1.gradle b/build.third_step1.gradle
index e164d7c91..40ca6ddcb 100644
--- a/build.third_step1.gradle
+++ b/build.third_step1.gradle
@@ -44,7 +44,6 @@ sourceSets{
"${srcDir}/fine-hsqldb/src/main/java",
"${srcDir}/fine-iconloader/src/main/java",
"${srcDir}/fine-icu4j/src/main/java",
- "${srcDir}/fine-j2v8/src/main/java",
// "${srcDir}/fine-jai/src/main/java",
"${srcDir}/fine-jboss-transaction-api/src/main/java",
"${srcDir}/fine-jgit/src/main/java",
@@ -142,8 +141,6 @@ task copyFiles(type:Copy,dependsOn:'compileJava'){
with dataContent.call("${srcDir}/fine-hsqldb/src/main/resources")
with dataContent.call("${srcDir}/fine-icu4j/src/main/java")
with dataContent.call("${srcDir}/fine-icu4j/src/main/resources")
- with dataContent.call("${srcDir}/fine-j2v8/src/main/java")
- with dataContent.call("${srcDir}/fine-j2v8/src/main/resources")
// with dataContent.call("${srcDir}/fine-jai/src/main/java")
with dataContent.call("${srcDir}/fine-jboss-transaction-api/src/main/java")
with dataContent.call("${srcDir}/fine-jboss-transaction-api/src/main/resources")
diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java
index ebaec03e6..c9a70763c 100644
--- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java
+++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java
@@ -613,12 +613,15 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
ExecutionOptions options,
GenerationTarget... targets) {
if ( !StringHelper.isEmpty( sqlString ) ) {
+ String formattedSql = null;
for ( GenerationTarget target : targets ) {
try {
- target.accept( formatter.format( sqlString ) );
+ formattedSql = formatter.format( sqlString );
+ target.accept( formattedSql );
}
catch (CommandAcceptanceException e) {
if ( !quiet ) {
+ log.warnf( formattedSql );
options.getExceptionHandler().handleException( e );
}
return false;
diff --git a/fine-j2v8/.gitignore b/fine-j2v8/.gitignore
deleted file mode 100644
index 5d453cd8b..000000000
--- a/fine-j2v8/.gitignore
+++ /dev/null
@@ -1,6 +0,0 @@
-*.iml
-.idea/
-.DS_Store
-.project
-.classpath
-*.gradle
\ No newline at end of file
diff --git a/fine-j2v8/pom.xml b/fine-j2v8/pom.xml
deleted file mode 100644
index 72cbb4efa..000000000
--- a/fine-j2v8/pom.xml
+++ /dev/null
@@ -1,18 +0,0 @@
-
-
- 4.0.0
-
-
- com.fr.third
- step1
- ${revision}
- ../base-third-project/base-third-step1
-
-
- fine-j2v8
- ${revision}
-
-
-
\ No newline at end of file
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/JavaCallback.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/JavaCallback.java
deleted file mode 100644
index 2bf5a19b3..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/JavaCallback.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2014 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- ******************************************************************************/
-package com.eclipsesource.v8;
-
-/**
- * Classes that implement this interface provide a method
- * which can be invoked from JavaScript. The method can return
- * a result.
- *
- * After creating an instance of a class that implements this
- * interface it can be registered as a Callback on a V8Object.
- */
-public interface JavaCallback {
-
- /**
- * Called when a JS Function invokes a the registered Java
- * method.
- *
- * @param receiver The V8Object that the function was called on.
- * @param parameters The parameters passed to the JS Function. The
- * parameter array does not need to be released, by any objects accessed
- * from the array must be.
- *
- * @return A result that should be passed back to JavaScript. The
- * result must be either an Integer, Double, Boolean, String or V8Value.
- */
- public Object invoke(V8Object receiver, V8Array parameters);
-
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/JavaVoidCallback.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/JavaVoidCallback.java
deleted file mode 100644
index 7298d5a40..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/JavaVoidCallback.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2014 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- ******************************************************************************/
-package com.eclipsesource.v8;
-
-/**
- * Classes that implement this interface provide a method
- * which can be invoked from JavaScript.
- *
- * After creating an instance of a class that implements this
- * interface it can be registered as a Callback on a V8Object.
- */
-public interface JavaVoidCallback {
-
- /**
- * Called when a JS Function invokes a the registered Java
- * method.
- *
- * @param receiver The V8Object that the function was called on.
- * @param parameters The parameters passed to the JS Function. The
- * parameter array does not need to be released, by any objects accessed
- * from the array must be.
- */
- public void invoke(V8Object receiver, V8Array parameters);
-
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/LibraryLoader.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/LibraryLoader.java
deleted file mode 100644
index e996a1df0..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/LibraryLoader.java
+++ /dev/null
@@ -1,243 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2015 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- * Wolfgang Steiner - code separation PlatformDetector/LibraryLoader
- ******************************************************************************/
-package com.eclipsesource.v8;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.management.ManagementFactory;
-
-class LibraryLoader {
-
- static final String SEPARATOR;
- static final String DELIMITER;
-
- static final String SWT_LIB_DIR = ".j2v8";
-
- static final String MINIMUM_VERSION = "6.2";
-
- static {
- DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
- SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$
- }
-
- /**
- * Returns the base-name for the native J2V8 library file.
- * @param withLinuxVendor include/exclude the {vendor} part from the returned filename
- *
NOTE: Vendors are only included for linux systems
- * @return The filename string has the following structure:
- *
{arch}-[vendor]-{operating_system}
- */
- public static String computeLibraryShortName(boolean withLinuxVendor) {
- String prefix = "j2v8";
- String vendor = withLinuxVendor && PlatformDetector.OS.isLinux() ? PlatformDetector.Vendor.getName() : null;
- String os = PlatformDetector.OS.getName();
- String arch = PlatformDetector.Arch.getName();
-
- final String separator = "-";
-
- return
- prefix +
- (vendor != null ? separator + vendor : "") +
- separator + os +
- separator + arch;
- }
-
- public static String computeLibraryFullName(boolean withLinuxVendor) {
- return "lib" + computeLibraryShortName(withLinuxVendor) + "." + PlatformDetector.OS.getLibFileExtension();
- }
-
- static boolean tryLoad(boolean withLinuxVendor, StringBuffer message) {
- String libShortName = computeLibraryShortName(withLinuxVendor);
- String libFullName = computeLibraryFullName(withLinuxVendor);
- String ideLocation = System.getProperty("user.dir") + SEPARATOR + "jni" + SEPARATOR + libFullName;
-
- /* Try loading library from java library path */
- if (load(libFullName, message)) {
- return true;
- }
- if (load(libShortName, message)) {
- return true;
- }
-
- /* Try loading library from the IDE location */
- if (new File(ideLocation).exists()) {
- if (load(ideLocation, message)) {
- return true;
- }
- }
-
- return false;
- }
-
- static void loadLibrary(final String tempDirectory) {
- if (PlatformDetector.OS.isAndroid()) {
- System.loadLibrary("j2v8");
- return;
- }
-
- StringBuffer message = new StringBuffer();
-
- // try loading a vendor-specific library first
- if (tryLoad(true, message))
- return;
-
- // if there is no vendor-specific library, just try to load the default OS library
- if (tryLoad(false, message))
- return;
-
- String path = null;
-
- if (tempDirectory != null) {
- path = tempDirectory;
- } else {
- path = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
- }
-
- // try extracting a vendor-specific library first
- if (extract(path, true, message))
- return;
-
- // if there is no vendor-specific library, just try to extract the default OS library
- if (extract(path, false, message))
- return;
-
- /* Failed to find the library */
- throw new UnsatisfiedLinkError("Could not load J2V8 library. Reasons: " + message.toString()); //$NON-NLS-1$
- }
-
- static boolean load(final String libName, final StringBuffer message) {
- try {
- if (libName.indexOf(SEPARATOR) != -1) {
- System.load(libName);
- } else {
- System.loadLibrary(libName);
- }
- return true;
- } catch (UnsatisfiedLinkError e) {
- if (message.length() == 0) {
- message.append(DELIMITER);
- }
- message.append('\t');
- message.append(e.getMessage());
- message.append(DELIMITER);
- }
- return false;
- }
-
- static boolean extract(String libPath, boolean withLinuxVendor, StringBuffer message) {
- String libFullName = computeLibraryFullName(withLinuxVendor);
- return extract(libPath + SEPARATOR + libFullName, libFullName, message);
- }
-
- static boolean extract(final String fileName, final String mappedName, final StringBuffer message) {
- FileOutputStream os = null;
- InputStream is = null;
- File file = new File(fileName);
- //这部分自己修改过,主要是以为linux通过System.getProperty("java.io.tmpdir")获取到的是相对路径,但是System.load方法加载需要文件的绝对路径。
- String absoluteName = file.getAbsolutePath();
- boolean extracted = false;
- try {
- if (file.exists()) {
- file.delete();
- }
- is = LibraryLoader.class.getResourceAsStream("/" + mappedName); //$NON-NLS-1$
- if (is != null) {
- extracted = true;
- int read;
- byte[] buffer = new byte[4096];
- os = new FileOutputStream(fileName);
- while ((read = is.read(buffer)) != -1) {
- os.write(buffer, 0, read);
- }
- os.close();
- is.close();
- chmod("755", fileName);
- if (load(absoluteName, message)) {
- return true;
- }
- }
- } catch (Throwable e) {
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e1) {
- }
- try {
- if (is != null) {
- is.close();
- }
- } catch (IOException e1) {
- }
- if (extracted && file.exists()) {
- file.delete();
- }
- }
- return false;
- }
-
- static void chmod(final String permision, final String path) {
- if (PlatformDetector.OS.isWindows()) {
- return;
- }
- try {
- Runtime.getRuntime().exec(new String[] { "chmod", permision, path }).waitFor(); //$NON-NLS-1$
- } catch (Throwable e) {
- }
- }
-
- //内核版本windows NT6.2以下存在加载dll之后无法正常退出的问题,注册钩子函数退出时杀掉进程
- static void checkExceptionVersion() {
- String version = System.getProperty("os.version");
- if (PlatformDetector.OS.isWindows() && belowMinimumVersion(version)) {
- registerExit();
- }
- }
-
- //低于指定版本,6.2以下
- private static boolean belowMinimumVersion(String version) {
- return compareVersion(version, MINIMUM_VERSION) < 0;
- }
-
- private static int compareVersion(String version1, String version2) {
- String[] versionArray1 = version1.split("\\.");//注意此处为正则匹配,不能用".";
- String[] versionArray2 = version2.split("\\.");
- int idx = 0;
- int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小长度值
- int diff = 0;
- while (idx < minLength
- && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度
- && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符
- ++idx;
- }
- //如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大;
- diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
- return diff;
- }
-
- //低于指定版本,6.2以下
- private static void registerExit() {
- Runtime.getRuntime().addShutdownHook(new Thread() {
- @Override
- public void run() {
- String name = ManagementFactory.getRuntimeMXBean().getName();
- String pid = name.split("@")[0];
- try {
- Runtime.getRuntime().exec("taskkill /F /PID " + pid);
- } catch (Exception ignore) {
- }
- }
- });
- }
-}
\ No newline at end of file
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/NodeJS.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/NodeJS.java
deleted file mode 100644
index 30d148364..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/NodeJS.java
+++ /dev/null
@@ -1,239 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2016 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- ******************************************************************************/
-package com.eclipsesource.v8;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
-
-/**
- * An isolate NodeJS runtime.
- *
- * This class is only available on some platforms. In particular any methods
- * on this class, on an Android device, will lead to an UnsupportedOperationException.
- */
-public class NodeJS {
-
- private static final String TMP_JS_EXT = ".js.tmp";
- private static final String NEXT_TICK = "nextTick";
- private static final String PROCESS = "process";
- private static final String GLOBAL = "global";
- private static final String STARTUP_CALLBACK = "__run";
- private static final String STARTUP_SCRIPT = "global." + STARTUP_CALLBACK + "(require, exports, module, __filename, __dirname);";
- private static final String STARTUP_SCRIPT_NAME = "startup";
- private static final String VERSIONS = "versions";
- private static final String NODE = "node";
- private String nodeVersion = null;
-
- private V8 v8;
- private V8Function require;
-
- /**
- * Creates a NodeJS Runtime
- *
- * @return The NodeJS runtime.
- *
- * May throw an UnsupportedOperationException if node.js integration has not
- * been compiled for your platform.
- */
- public static NodeJS createNodeJS() {
- return createNodeJS(null);
- }
-
- /**
- * Returns the version of Node.js that is runtime is built against.
- * This uses process.versions.node to get the version.
- *
- * @return The version of Node.js.
- */
- public String getNodeVersion() {
- if (nodeVersion != null) {
- return nodeVersion;
- }
- V8Object process = null;
- V8Object versions = null;
- try {
- process = v8.getObject(PROCESS);
- versions = process.getObject(VERSIONS);
- nodeVersion = versions.getString(NODE);
- } finally {
- safeRelease(process);
- safeRelease(versions);
- }
- return nodeVersion;
- }
-
- /**
- * Creates a NodeJS runtime and executes a JS Script
- *
- * @param file The JavaScript to execute or null for no script.
- * @return The NodeJS runtime.
- *
- * May throw an UnsupportedOperationException if node.js integration has not
- * been compiled for your platform.
- */
- public static NodeJS createNodeJS(final File file) {
- V8 v8 = V8.createV8Runtime(GLOBAL);
- final NodeJS node = new NodeJS(v8);
- v8.registerJavaMethod(new JavaVoidCallback() {
-
- @Override
- public void invoke(final V8Object receiver, final V8Array parameters) {
- V8Function require = (V8Function) parameters.get(0);
- try {
- node.init(require.twin());
- } finally {
- require.close();
- }
- }
- }, STARTUP_CALLBACK);
- try {
- File startupScript = createTemporaryScriptFile(STARTUP_SCRIPT, STARTUP_SCRIPT_NAME);
- try {
- v8.createNodeRuntime(startupScript.getAbsolutePath());
- } finally {
- startupScript.delete();
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- if (file != null) {
- node.exec(file);
- }
- return node;
- }
-
- /**
- * Returns the V8 runtime being used for this NodeJS instance.
- *
- * @return The V8 Runtime.
- */
- public V8 getRuntime() {
- return v8;
- }
-
- /**
- * Handles the next message in the message loop. Returns True
- * if there are more messages to handle, false otherwise.
- *
- * @return True if there are more messages to handle, false otherwise.
- */
- public boolean handleMessage() {
- v8.checkThread();
- return v8.pumpMessageLoop();
- }
-
- /**
- * Releases the NodeJS runtime.
- */
- public void release() {
- v8.checkThread();
- if (!require.isReleased()) {
- require.close();
- }
- if (!v8.isReleased()) {
- v8.close();
- }
- }
-
- /**
- * Returns true if there are more messages to process, false otherwise.
- *
- * @return True if there are more messages to process, false otherwise.
- */
- public boolean isRunning() {
- v8.checkThread();
- return v8.isRunning();
- }
-
- /**
- * Invokes NodeJS require() on the specified file. This will load the module, execute
- * it and return the exports object to the caller. The exports object must be released.
- *
- * @param file The module to load.
- * @return The exports object.
- */
- public V8Object require(final File file) {
- v8.checkThread();
- V8Array requireParams = new V8Array(v8);
- try {
- requireParams.push(file.getAbsolutePath());
- return (V8Object) require.call(null, requireParams);
- } finally {
- requireParams.close();
- }
- }
-
- /**
- * Execute a NodeJS script. This will load the script and execute it on the
- * next tick. This is the same as how NodeJS executes scripts at startup. Since
- * the script won't actually run until the next tick, this method does not return
- * a result.
- *
- * @param file The script to execute.
- */
- public void exec(final File file) {
- V8Function scriptExecution = createScriptExecutionCallback(file);
- V8Object process = null;
- V8Array parameters = null;
- try {
- process = v8.getObject(PROCESS);
- parameters = new V8Array(v8);
- parameters.push(scriptExecution);
- process.executeObjectFunction(NEXT_TICK, parameters);
- } finally {
- safeRelease(process);
- safeRelease(parameters);
- safeRelease(scriptExecution);
- }
- }
-
- private V8Function createScriptExecutionCallback(final File file) {
- V8Function v8Function = new V8Function(v8, new JavaCallback() {
- @Override
- public Object invoke(final V8Object receiver, final V8Array parameters) {
- V8Array requireParams = new V8Array(v8);
- try {
- requireParams.push(file.getAbsolutePath());
- return require.call(null, requireParams);
- } finally {
- requireParams.close();
- }
- }
- });
- return v8Function;
- }
-
- private void safeRelease(final Releasable releasable) {
- if (releasable != null) {
- releasable.release();
- }
- }
-
- private NodeJS(final V8 v8) {
- this.v8 = v8;
- }
-
- private void init(final V8Function require) {
- this.require = require;
- }
-
- private static File createTemporaryScriptFile(final String script, final String name) throws IOException {
- File tempFile = File.createTempFile(name, TMP_JS_EXT);
- PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
- try {
- writer.print(script);
- } finally {
- writer.close();
- }
- return tempFile;
- }
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/Platform.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/Platform.java
deleted file mode 100644
index 8168935bc..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/Platform.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.eclipsesource.v8;
-
-public class Platform {
- public static final String ANDROID = "android";
- public static final String LINUX = "linux";
- public static final String MACOSX = "macosx";
- public static final String WINDOWS = "windows";
-
- public static final String NATIVE_CLIENT = "nacl";
-
- public static final String UNKNOWN = "unknown";
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/PlatformDetector.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/PlatformDetector.java
deleted file mode 100644
index 49cd427d8..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/PlatformDetector.java
+++ /dev/null
@@ -1,319 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2017 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Trustin Lee - original OS/Arch/Vendor detection code (see: https://github.com/trustin/os-maven-plugin)
- * Wolfgang Steiner - initial API and implementation
- *
- * Copyright 2014 Trustin Heuiseung Lee.
- *
- * Licensed 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 com.eclipsesource.v8;
-
-import java.io.BufferedReader;
-import java.io.Closeable;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.Locale;
-
-public class PlatformDetector {
- public static class Arch {
- public static String getName() {
- final String archProperty = System.getProperty("os.arch");
- final String archName = normalizeArch(archProperty);
-
- if (archName.equals(Platform.UNKNOWN)) {
- throw new UnsatisfiedLinkError("Unsupported arch: " + archProperty);
- }
-
- return archName;
- }
- }
-
- public static class OS {
- public static String getName() {
- final String osProperty = System.getProperty("os.name");
- final String osName = normalizeOs(osProperty);
-
- final String vendorProperty = System.getProperty("java.specification.vendor");
- final String vendorName = normalize(vendorProperty);
-
- // special handling for android
- if (vendorName.contains("android") || osName.contains("android")) {
- return Platform.ANDROID;
- }
-
- if (osName.equals(Platform.UNKNOWN)) {
- throw new UnsatisfiedLinkError("Unsupported platform/vendor: " + osProperty + " / " + vendorProperty);
- }
-
- return osName;
- }
-
- public static boolean isWindows() {
- return getName().equals(Platform.WINDOWS);
- }
-
- public static boolean isMac() {
- return getName().equals(Platform.MACOSX);
- }
-
- public static boolean isLinux() {
- return getName().equals(Platform.LINUX);
- }
-
- public static boolean isNativeClient() {
- return getName().equals(Platform.NATIVE_CLIENT);
- }
-
- public static boolean isAndroid() {
- return getName().equals(Platform.ANDROID);
- }
-
- public static String getLibFileExtension() {
- if (isWindows()) {
- return "dll";
- }
-
- if (isMac()) {
- return "dylib";
- }
-
- if (isLinux()
- || isAndroid()
- || isNativeClient()) {
- return "so";
- }
-
- throw new UnsatisfiedLinkError("Unsupported platform library-extension for: " + getName());
- }
- }
-
- public static class Vendor {
- private static final String[] LINUX_OS_RELEASE_FILES = {"/etc/os-release", "/usr/lib/os-release"};
- private static final String REDHAT_RELEASE_FILE = "/etc/redhat-release";
- private static final String LINUX_ID_PREFIX = "ID=";
-
- public static String getName() {
- if (OS.isWindows()) {
- return "microsoft";
- }
- if (OS.isMac()) {
- return "apple";
- }
- if (OS.isLinux()) {
- return getLinuxOsReleaseId();
- }
- if (OS.isAndroid()) {
- return "google";
- }
- //如果if条件全部不符合,就会陷入死循环,代码存在风险
- //throw new UnsatisfiedLinkError("Unsupported vendor: " + getName());
- return null;
- }
-
- private static String getLinuxOsReleaseId() {
- // First, look for the os-release file.
- for (String osReleaseFileName : LINUX_OS_RELEASE_FILES) {
- File file = new File(osReleaseFileName);
- if (file.exists()) {
- return parseLinuxOsReleaseFile(file);
- }
- }
-
- // Older versions of redhat don't have /etc/os-release. In this case, try
- // parsing this file.
- File file = new File(REDHAT_RELEASE_FILE);
- if (file.exists()) {
- return parseLinuxRedhatReleaseFile(file);
- }
-
- //linux系统下如果缺失/etc/os-release,/usr/lib/os-release,/etc/redhat-release三个文件,就会和getName方法就会一直互相调用
- //throw new UnsatisfiedLinkError("Unsupported linux vendor: " + getName());
- return null;
- }
-
- private static String parseLinuxOsReleaseFile(final File file) {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
-
- String id = null;
- String line;
- while((line = reader.readLine()) != null) {
- // Parse the ID line.
- if (line.startsWith(LINUX_ID_PREFIX)) {
- // Set the ID for this version.
- id = normalizeOsReleaseValue(line.substring(LINUX_ID_PREFIX.length()));
- break;
- }
- }
-
- return id;
- } catch (IOException ignored) {
- // Just absorb. Don't treat failure to read /etc/os-release as an error.
- } finally {
- closeQuietly(reader);
- }
- return null;
- }
-
- private static String parseLinuxRedhatReleaseFile(final File file) {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
-
- // There is only a single line in this file.
- String line = reader.readLine();
- if (line != null) {
- line = line.toLowerCase(Locale.US);
-
- String id;
- if (line.contains("centos")) {
- id = "centos";
- } else if (line.contains("fedora")) {
- id = "fedora";
- } else if (line.contains("red hat enterprise linux")) {
- id = "rhel";
- } else {
- // Other variants are not currently supported.
- return null;
- }
-
- return id;
- }
- } catch (IOException ignored) {
- // Just absorb. Don't treat failure to read /etc/os-release as an error.
- } finally {
- closeQuietly(reader);
- }
- return null;
- }
-
- private static void closeQuietly(final Closeable obj) {
- try {
- if (obj != null) {
- obj.close();
- }
- } catch (IOException ignored) {
- // Ignore.
- }
- }
- }
-
- private static String normalizeOsReleaseValue(final String value) {
- // Remove any quotes from the string.
- return value.trim().replace("\"", "");
- }
-
- private static String normalizeOs(String value) {
- value = normalize(value);
- if (value.startsWith("aix")) {
- return "aix";
- }
- if (value.startsWith("hpux")) {
- return "hpux";
- }
- if (value.startsWith("os400")) {
- // Avoid the names such as os4000
- if ((value.length() <= 5) || !Character.isDigit(value.charAt(5))) {
- return "os400";
- }
- }
- if (value.startsWith("android")) {
- return Platform.ANDROID;
- }
- if (value.startsWith("linux")) {
- return Platform.LINUX;
- }
- if (value.startsWith("nacl")) {
- return Platform.NATIVE_CLIENT;
- }
- if (value.startsWith("macosx") || value.startsWith("osx")) {
- return Platform.MACOSX;
- }
- if (value.startsWith("freebsd")) {
- return "freebsd";
- }
- if (value.startsWith("openbsd")) {
- return "openbsd";
- }
- if (value.startsWith("netbsd")) {
- return "netbsd";
- }
- if (value.startsWith("solaris") || value.startsWith("sunos")) {
- return "sunos";
- }
- if (value.startsWith("windows")) {
- return Platform.WINDOWS;
- }
-
- return Platform.UNKNOWN;
- }
-
- private static String normalizeArch(String value) {
- value = normalize(value);
- if (value.matches("^(x8664|amd64|ia32e|em64t|x64)$")) {
- return "x86_64";
- }
- if (value.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) {
- return "x86_32";
- }
- if (value.matches("^(ia64|itanium64)$")) {
- return "itanium_64";
- }
- if (value.matches("^(sparc|sparc32)$")) {
- return "sparc_32";
- }
- if (value.matches("^(sparcv9|sparc64)$")) {
- return "sparc_64";
- }
- if (value.matches("^(arm|arm32)$") || value.startsWith("armv7")) {
- return "arm_32";
- }
- if ("aarch64".equals(value) || value.startsWith("armv8")) {
- return "aarch_64";
- }
- if (value.matches("^(ppc|ppc32)$")) {
- return "ppc_32";
- }
- if ("ppc64".equals(value)) {
- return "ppc_64";
- }
- if ("ppc64le".equals(value)) {
- return "ppcle_64";
- }
- if ("s390".equals(value)) {
- return "s390_32";
- }
- if ("s390x".equals(value)) {
- return "s390_64";
- }
-
- return Platform.UNKNOWN;
- }
-
- private static String normalize(final String value) {
- if (value == null) {
- return "";
- }
- return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
- }
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/ReferenceHandler.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/ReferenceHandler.java
deleted file mode 100644
index 740dc18ee..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/ReferenceHandler.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2016 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- ******************************************************************************/
-package com.eclipsesource.v8;
-
-/**
- * Callback used to track when native handles are created and released.
- */
-public interface ReferenceHandler {
-
- /**
- * Called when a native handle is first created. The V8Value
- * referenced by that handle is passed as a parameter.
- *
- * @param object The V8Value referenced by the handle
- */
- public void v8HandleCreated(V8Value object);
-
- /**
- * Called when a native handle is released. The V8Value
- * referenced by that handle is passed as a parameter.
- *
- * @param object The V8Value referenced by the handle
- */
- public void v8HandleDisposed(V8Value object);
-
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/Releasable.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/Releasable.java
deleted file mode 100644
index 3edee1e61..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/Releasable.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2015 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- ******************************************************************************/
-package com.eclipsesource.v8;
-
-import java.io.Closeable;
-
-/**
- * An interface used to denote all V8 Classes which can be released.
- */
-public interface Releasable extends Closeable {
-
- /**
- * Release the underlying resources. Once an object is released
- * it typically cannot be used again.
- */
- void close();
-
- /**
- * Synonym for {@link #close()}.
- */
- void release();
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/SignatureProvider.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/SignatureProvider.java
deleted file mode 100644
index 035c5b06b..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/SignatureProvider.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.eclipsesource.v8;
-
-public interface SignatureProvider {
- public byte[] getSignature(String uri);
-}
diff --git a/fine-j2v8/src/main/java/com/eclipsesource/v8/V8.java b/fine-j2v8/src/main/java/com/eclipsesource/v8/V8.java
deleted file mode 100644
index bac35971a..000000000
--- a/fine-j2v8/src/main/java/com/eclipsesource/v8/V8.java
+++ /dev/null
@@ -1,1687 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2014 EclipseSource and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * EclipseSource - initial API and implementation
- ******************************************************************************/
-package com.eclipsesource.v8;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import com.eclipsesource.v8.inspector.V8InspectorDelegate;
-import com.eclipsesource.v8.utils.V8Executor;
-import com.eclipsesource.v8.utils.V8Map;
-import com.eclipsesource.v8.utils.V8Runnable;
-
-/**
- * An isolated V8Runtime. All JavaScript execution must exist
- * on a single runtime, and data is not shared between runtimes.
- * A runtime must be created and released when finished.
- *
- * All access to a runtime must come from the same thread, unless
- * the thread explicitly gives up control using the V8Locker.
- *
- * A public static factory method can be used to create the runtime.
- *
- * V8 runtime = V8.createV8Runtime();
- *
- */
-public class V8 extends V8Object {
-
- private static Object lock = new Object();
- private volatile static int runtimeCounter = 0;
- private static String v8Flags = null;
- private static boolean initialized = false;
- protected Map v8WeakReferences = new HashMap();
-
- private Map data = null;
- private final V8Locker locker;
- private SignatureProvider signatureProvider = null;
- private long objectReferences = 0;
- private long v8RuntimePtr = 0;
- private List resources = null;
- private V8Map executors = null;
- private boolean forceTerminateExecutors = false;
- private Map functionRegistry = new HashMap();
- private LinkedList referenceHandlers = new LinkedList();
- private LinkedList releaseHandlers = new LinkedList();
-
- private static boolean nativeLibraryLoaded = false;
- private static Error nativeLoadError = null;
- private static Exception nativeLoadException = null;
- private static V8Value undefined = new V8Object.Undefined();
- private static Object invalid = new Object();
-
- private class MethodDescriptor {
- Object object;
- Method method;
- JavaCallback callback;
- JavaVoidCallback voidCallback;
- boolean includeReceiver;
- }
-
- private synchronized static void load(final String tmpDirectory) {
- try {
- LibraryLoader.loadLibrary(tmpDirectory);
- nativeLibraryLoaded = true;
- LibraryLoader.checkExceptionVersion();
- } catch (Error e) {
- nativeLoadError = e;
- } catch (Exception e) {
- nativeLoadException = e;
- }
- }
-
- /**
- * Determines if the native libraries are loaded.
- *
- * @return Returns true if the native libraries are loaded,
- * false otherwise.
- */
- public static boolean isLoaded() {
- return nativeLibraryLoaded;
- }
-
- /**
- * Sets the V8 flags on the platform. All runtimes will be created
- * with the same flags. Flags must be set before the runtime is
- * created.
- *
- * @param flags The flags to set on V8
- */
- public static void setFlags(final String flags) {
- v8Flags = flags;
- initialized = false;
- }
-
- /**
- * Creates a new V8Runtime and loads the required
- * native libraries if they are not already loaded.
- * The current thread is given the lock to this runtime.
- *
- * @return A new isolated V8 Runtime.
- */
- public static V8 createV8Runtime() {
- return createV8Runtime(null, null);
- }
-
- /**
- * Creates a new V8Runtime and loads the required native libraries if they
- * are not already loaded. An alias is also set for the global scope. For example,
- * 'window' can be set as the global scope name.
- *
- * The current thread is given the lock to this runtime.
- *
- * @param globalAlias The name to associate with the global scope.
- *
- * @return A new isolated V8 Runtime.
- */
- public static V8 createV8Runtime(final String globalAlias) {
- return createV8Runtime(globalAlias, null);
- }
-
- /**
- * Creates a new V8Runtime and loads the required native libraries if they
- * are not already loaded. An alias is also set for the global scope. For example,
- * 'window' can be set as the global scope name.
- *
- * The current thread is given the lock to this runtime.
- *
- * @param globalAlias The name to associate with the global scope.
- * @param tempDirectory The name of the directory to extract the native
- * libraries too.
- *
- * @return A new isolated V8 Runtime.
- */
- public static V8 createV8Runtime(final String globalAlias, final String tempDirectory) {
- if (!nativeLibraryLoaded) {
- synchronized (lock) {
- if (!nativeLibraryLoaded) {
- load(tempDirectory);
- }
- }
- }
- checkNativeLibraryLoaded();
- if (!initialized) {
- _setFlags(v8Flags);
- initialized = true;
- }
- V8 runtime = new V8(globalAlias);
- synchronized (lock) {
- runtimeCounter++;
- }
- return runtime;
- }
-
- public void setSignatureProvider(final SignatureProvider signatureProvider) {
- this.signatureProvider = signatureProvider;
- }
-
- /**
- * Adds a ReferenceHandler to track when new V8Objects are created.
- *
- * @param handler The ReferenceHandler to add
- */
- public void addReferenceHandler(final ReferenceHandler handler) {
- referenceHandlers.add(0, handler);
- }
-
- /**
- * Adds a handler that will be called when the runtime is being released.
- * The runtime will still be available when the handler is executed.
- *
- * @param handler The handler to invoke when the runtime, is being released
- */
- public void addReleaseHandler(final V8Runnable handler) {
- releaseHandlers.add(handler);
- }
-
- /**
- * Removes an existing ReferenceHandler from the collection of reference handlers.
- * If the ReferenceHandler does not exist in the collection, it is ignored.
- *
- * @param handler The reference handler to remove
- */
- public void removeReferenceHandler(final ReferenceHandler handler) {
- referenceHandlers.remove(handler);
- }
-
- /**
- * Removes an existing release handler from the collection of release handlers.
- * If the release handler does not exist in the collection, it is ignored.
- *
- * @param handler The handler to remove
- */
- public void removeReleaseHandler(final V8Runnable handler) {
- releaseHandlers.remove(handler);
- }
-
- /**
- * Associates an arbitrary object with this runtime.
- *
- * @param key The key used to reference this object
- * @param value The object to associate with this runtime
- */
- public synchronized void setData(final String key, final Object value) {
- if (data == null) {
- data = new HashMap();
- }
- data.put(key, value);
- }
-
- /**
- * Returns the data object associated with this runtime, null if no object
- * has been associated.
- *
- * @param key The key used to reference this object
- *
- * @return The data object associated with this runtime, or null.
- */
- public Object getData(final String key) {
- if (data == null) {
- return null;
- }
- return data.get(key);
- }
-
- private void notifyReleaseHandlers(final V8 runtime) {
- for (V8Runnable handler : releaseHandlers) {
- handler.run(runtime);
- }
- }
-
- private void notifyReferenceCreated(final V8Value object) {
- for (ReferenceHandler referenceHandler : referenceHandlers) {
- referenceHandler.v8HandleCreated(object);
- }
- }
-
- private void notifyReferenceDisposed(final V8Value object) {
- for (ReferenceHandler referenceHandler : referenceHandlers) {
- referenceHandler.v8HandleDisposed(object);
- }
- }
-
- private static void checkNativeLibraryLoaded() {
- if (!nativeLibraryLoaded) {
- String vendorName = LibraryLoader.computeLibraryShortName(true);
- String baseName = LibraryLoader.computeLibraryShortName(false);
- String message = "J2V8 native library not loaded (" + baseName + "/" + vendorName + ")";
-
- if (nativeLoadError != null) {
- throw new IllegalStateException(message, nativeLoadError);
- } else if (nativeLoadException != null) {
- throw new IllegalStateException(message, nativeLoadException);
- } else {
- throw new IllegalStateException(message);
- }
- }
- }
-
- protected V8() {
- this(null);
- }
-
- protected V8(final String globalAlias) {
- super(null);
- released = false;
- v8RuntimePtr = _createIsolate(globalAlias);
- locker = new V8Locker(this);
- checkThread();
- objectHandle = _getGlobalObject(v8RuntimePtr);
- }
-
- public long createInspector(final V8InspectorDelegate inspectorDelegate, final String contextName) {
- return _createInspector(v8RuntimePtr, inspectorDelegate, contextName);
- }
-
- public void dispatchProtocolMessage(final long V8InspectorPtr, final String protocolMessage) {
- checkThread();
- _dispatchProtocolMessage(v8RuntimePtr, V8InspectorPtr, protocolMessage);
- }
-
- public void schedulePauseOnNextStatement(final long V8InspectorPtr, final String reason) {
- checkThread();
- _schedulePauseOnNextStatement(v8RuntimePtr, V8InspectorPtr, reason);
- }
-
- /**
- * Returns an UNDEFINED constant.
- *
- * @return The UNDEFINED constant value.
- */
- public static V8Value getUndefined() {
- return undefined;
- }
-
- /**
- * Returns the number of active runtimes.
- *
- * @return The number of active runtimes.
- */
- public static int getActiveRuntimes() {
- return runtimeCounter;
- }
-
- /**
- * Returns the number of Object References for this runtime.
- *
- * @return The number of Object References on this runtime.
- */
- public long getObjectReferenceCount() {
- return objectReferences - v8WeakReferences.size();
- }
-
- protected long getV8RuntimePtr() {
- return v8RuntimePtr;
- }
-
- /**
- * Gets the version of the V8 engine
- *
- * @return The version of the V8 Engine.
- */
- public static String getV8Version() {
- return _getVersion();
- }
-
- /**
- * Returns the revision ID of this version as specified
- * by the source code management system. Currently we use
- * Git, so this will return the commit ID for this revision.
- *
- * @return The revision ID of this version of J2V8
- */
- public static String getSCMRevision() {
- return "Unknown revision ID";
- }
-
- /*
- * (non-Javadoc)
- * @see com.eclipsesource.v8.V8Value#close()
- */
- @Override
- public void close() {
- release(true);
- }
-
- /*
- * (non-Javadoc)
- * @see com.eclipsesource.v8.V8Value#release()
- */
- @Override
- @Deprecated
- public void release() {
- release(true);
- }
-
- /**
- * Terminates any JavaScript executing on this runtime. Once
- * the runtime is released, any executors that were spawned
- * will also be force terminated.
- */
- public void terminateExecution() {
- forceTerminateExecutors = true;
- terminateExecution(v8RuntimePtr);
- }
-
- /**
- * Release native resources associated with this runtime. Once
- * released, a runtime cannot be reused.
- *
- * @param reportMemoryLeaks True if memory leaks should be
- * reported by throwing an IllegalStateException if any
- * objects were not released.
- */
- public void release(final boolean reportMemoryLeaks) {
- if (isReleased()) {
- return;
- }
- checkThread();
- try {
- notifyReleaseHandlers(this);
- } finally {
- releaseResources();
- shutdownExecutors(forceTerminateExecutors);
- if (executors != null) {
- executors.clear();
- }
- releaseNativeMethodDescriptors();
- synchronized (lock) {
- runtimeCounter--;
- }
- _releaseRuntime(v8RuntimePtr);
- v8RuntimePtr = 0L;
- released = true;
- if (reportMemoryLeaks && (getObjectReferenceCount() > 0)) {
- throw new IllegalStateException(getObjectReferenceCount() + " Object(s) still exist in runtime");
- }
- }
- }
-
- private void releaseNativeMethodDescriptors() {
- Set nativeMethodDescriptors = functionRegistry.keySet();
- for (Long nativeMethodDescriptor : nativeMethodDescriptors) {
- releaseMethodDescriptor(v8RuntimePtr, nativeMethodDescriptor);
- }
- }
-
- private void releaseResources() {
- if (resources != null) {
- for (Releasable releasable : resources) {
- releasable.release();
- }
- resources.clear();
- resources = null;
- }
- }
-
- /**
- * Registers an executor with this runtime. An executor is another
- * runtime with its own thread. By registering an executor, it can be
- * terminated when this runtime is released.
- *
- * @param key The key to associate the executor with.
- * @param executor The executor itself.
- */
- public void registerV8Executor(final V8Object key, final V8Executor executor) {
- checkThread();
- if (executors == null) {
- executors = new V8Map();
- }
- executors.put(key, executor);
- }
-
- /**
- * Removes the executor from this runtime. The executor is
- * *NOT* shutdown, simply removed from the list of known
- * executors.
- *
- * @param key The key the executor was associated with.
- * @return The executor or null if it does not exist.
- */
- public V8Executor removeExecutor(final V8Object key) {
- checkThread();
- if (executors == null) {
- return null;
- }
- return executors.remove(key);
- }
-
- /**
- * Returns the executor associated with the given key.
- *
- * @param key The key the executor was associated with.
- * @return The executor or null if it does not exist.
- */
- public V8Executor getExecutor(final V8Object key) {
- checkThread();
- if (executors == null) {
- return null;
- }
- return executors.get(key);
- }
-
- /**
- * Shutdown all executors associated with this runtime.
- * If force terminate is specified, it will forcefully terminate
- * the executors, otherwise it will simply signal that they
- * should terminate.
- *
- * @param forceTerminate Specify if the executors should be
- * forcefully terminated, or simply notified to shutdown when ready.
- */
- public void shutdownExecutors(final boolean forceTerminate) {
- checkThread();
- if (executors == null) {
- return;
- }
- for (V8Executor executor : executors.values()) {
- if (forceTerminate) {
- executor.forceTermination();
- } else {
- executor.shutdown();
- }
- }
- }
-
- /**
- * Registers a resource with this runtime. All registered
- * resources will be released before the runtime is released.
- *
- * @param resource The resource to register.
- */
- public void registerResource(final Releasable resource) {
- checkThread();
- if (resources == null) {
- resources = new ArrayList();
- }
- resources.add(resource);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as an integer.
- * If the result is not an integer, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- *
- * @return The result of the script as an integer, or V8ResultUndefinedException if
- * the result is not an integer.
- */
- public int executeIntegerScript(final String script) {
- return executeIntegerScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as an integer.
- * If the result is not an integer, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- * @param scriptName The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for excepton purposes.
- *
- * @return The result of the script as an integer, or V8ResultUndefinedException if
- * the result is not an integer.
- */
- public int executeIntegerScript(final String script, final String scriptName, final int lineNumber) {
- checkThread();
- checkScript(script);
- return executeIntegerScript(v8RuntimePtr, script, scriptName, lineNumber);
- }
-
- protected void createTwin(final V8Value value, final V8Value twin) {
- checkThread();
- createTwin(v8RuntimePtr, value.getHandle(), twin.getHandle());
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a double.
- * If the result is not a double, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- *
- * @return The result of the script as a double, or V8ResultUndefinedException if
- * the result is not a double.
- */
- public double executeDoubleScript(final String script) {
- return executeDoubleScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a double.
- * If the result is not a double, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- * @param scriptName The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for exception stack trace purposes.
- *
- * @return The result of the script as a double, or V8ResultUndefinedException if
- * the result is not a double.
- */
- public double executeDoubleScript(final String script, final String scriptName, final int lineNumber) {
- checkThread();
- checkScript(script);
- return executeDoubleScript(v8RuntimePtr, script, scriptName, lineNumber);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a String.
- * If the result is not a String, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- *
- * @return The result of the script as a String, or V8ResultUndefinedException if
- * the result is not a String.
- */
- public String executeStringScript(final String script) {
- return executeStringScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a String.
- * If the result is not a String, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- * @param scriptName The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for exception stack trace purposes.
- *
- * @return The result of the script as a String, or V8ResultUndefinedException if
- * the result is not a String.
- */
- public String executeStringScript(final String script, final String scriptName, final int lineNumber) {
- checkThread();
- checkScript(script);
- return executeStringScript(v8RuntimePtr, script, scriptName, lineNumber);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a boolean.
- * If the result is not a boolean, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- *
- * @return The result of the script as a boolean, or V8ResultUndefinedException if
- * the result is not a boolean.
- */
- public boolean executeBooleanScript(final String script) {
- return executeBooleanScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a boolean.
- * If the result is not a boolean, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- * @param scriptName The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for exception stack trace purposes.
- *
- * @return The result of the script as a boolean, or V8ResultUndefinedException if
- * the result is not a boolean.
- */
- public boolean executeBooleanScript(final String script, final String scriptName, final int lineNumber) {
- checkThread();
- checkScript(script);
- return executeBooleanScript(v8RuntimePtr, script, scriptName, lineNumber);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a V8Array.
- * If the result is not a V8Array, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- *
- * @return The result of the script as a V8Array, or V8ResultUndefinedException if
- * the result is not a V8Array.
- */
- public V8Array executeArrayScript(final String script) {
- return executeArrayScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a V8Array.
- * If the result is not a V8Array, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- * @param scriptName The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for exception stack trace purposes.
- *
- * @return The result of the script as a V8Array, or V8ResultUndefinedException if
- * the result is not a V8Array.
- */
- public V8Array executeArrayScript(final String script, final String scriptName, final int lineNumber) {
- checkThread();
- Object result = this.executeScript(script, scriptName, lineNumber);
- if (result instanceof V8Array) {
- return (V8Array) result;
- }
- throw new V8ResultUndefined();
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a Java Object.
- * Primitives will be boxed.
- *
- * @param script The script to execute.
- *
- * @return The result of the script as a Java Object.
- */
- public Object executeScript(final String script) {
- return executeScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a Java Object.
- * Primitives will be boxed.
- *
- * @param script The script to execute.
- * @param uri The name of the script
- *
- * @return The result of the script as a Java Object.
- */
- public Object executeScript(final String script, final String uri) {
- checkThread();
- checkScript(script);
- return executeScript(getV8RuntimePtr(), UNKNOWN, script, uri, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a Java Object.
- * Primitives will be boxed.
- *
- * @param script The script to execute.
- * @param uri The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for exception stack trace purposes.
- *
- * @return The result of the script as a Java Object.
- */
- public Object executeScript(final String script, final String uri, final int lineNumber) {
- checkThread();
- checkScript(script);
- return executeScript(getV8RuntimePtr(), UNKNOWN, script, uri, lineNumber);
- }
-
- /**
- * Executes a JS Script module on this runtime and returns the result as a Java Object.
- * Primitives will be boxed.
- *
- * If the script does not match the signature (as verified with the public key) then a
- * V8SecurityException will be thrown.
- *
- * @param script The signed script to execute
- * @param modulePrefix The module prefix
- * @param modulePostfix The module postfix
- * @param uri The name of the script
- *
- * @return The result of the script as a Java Object.
- */
- public Object executeModule(final String script, final String modulePrefix, final String modulePostfix, final String uri) {
- checkThread();
- checkScript(script);
- return executeScript(getV8RuntimePtr(), UNKNOWN, modulePrefix + script + modulePostfix, uri, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a V8Object.
- * If the result is not a V8Object, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- *
- * @return The result of the script as a V8Object, or V8ResultUndefinedException if
- * the result is not a V8Object.
- */
- public V8Object executeObjectScript(final String script) {
- return this.executeObjectScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime and returns the result as a V8Object.
- * If the result is not a V8Object, then a V8ResultUndefinedException is thrown.
- *
- * @param script The script to execute.
- * @param scriptName The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for exception stack trace purposes.
- *
- * @return The result of the script as a V8Object, or V8ResultUndefinedException if
- * the result is not a V8Object.
- */
- public V8Object executeObjectScript(final String script, final String scriptName, final int lineNumber) {
- checkThread();
- Object result = this.executeScript(script, scriptName, lineNumber);
- if (result instanceof V8Object) {
- return (V8Object) result;
- }
- throw new V8ResultUndefined();
- }
-
- /**
- * Executes a JS Script on this runtime.
- *
- * @param script The script to execute.
- */
- public void executeVoidScript(final String script) {
- executeVoidScript(script, null, 0);
- }
-
- /**
- * Executes a JS Script on this runtime.
- *
- * @param script The script to execute.
- * @param scriptName The name of the script
- * @param lineNumber The line number that is considered to be the first line of
- * the script. Typically 0, but could be set to another value for exception stack trace purposes.
- */
- public void executeVoidScript(final String script, final String scriptName, final int lineNumber) {
- checkThread();
- checkScript(script);
- executeVoidScript(v8RuntimePtr, script, scriptName, lineNumber);
- }
-
- /**
- * Returns the locker associated with this runtime. The locker allows
- * threads to give up control of the runtime and other threads to acquire
- * control.
- *
- * @return The locker associated with this runtime.
- */
- public V8Locker getLocker() {
- return locker;
- }
-
- /**
- * Returns the unique build ID of the native library.
- *
- * @return The unique build ID of the Native library.
- */
- public static long getBuildID() {
- return _getBuildID();
- }
-
- /**
- * Indicates to V8 that the system is low on memory.
- * V8 may use this to attempt to recover space by running
- * the garbage collector.
- */
- public void lowMemoryNotification() {
- checkThread();
- lowMemoryNotification(getV8RuntimePtr());
- }
-
- void checkRuntime(final V8Value value) {
- if ((value == null) || value.isUndefined()) {
- return;
- }
- V8 runtime = value.getRuntime();
- if ((runtime == null) ||
- runtime.isReleased() ||
- (runtime != this)) {
- throw new Error("Invalid target runtime");
- }
- }
-
- void checkThread() {
- locker.checkThread();
- if (isReleased()) {
- throw new Error("Runtime disposed error");
- }
- }
-
- static void checkScript(final String script) {
- if (script == null) {
- throw new NullPointerException("Script is null");
- }
- }
-
- void registerCallback(final Object object, final Method method, final long objectHandle, final String jsFunctionName, final boolean includeReceiver) {
- MethodDescriptor methodDescriptor = new MethodDescriptor();
- methodDescriptor.object = object;
- methodDescriptor.method = method;
- methodDescriptor.includeReceiver = includeReceiver;
- long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, isVoidMethod(method));
- functionRegistry.put(methodID, methodDescriptor);
- }
-
- void registerVoidCallback(final JavaVoidCallback callback, final long objectHandle, final String jsFunctionName) {
- MethodDescriptor methodDescriptor = new MethodDescriptor();
- methodDescriptor.voidCallback = callback;
- long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, true);
- functionRegistry.put(methodID, methodDescriptor);
- }
-
- void registerCallback(final JavaCallback callback, final long objectHandle, final String jsFunctionName) {
- long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, false);
- createAndRegisterMethodDescriptor(callback, methodID);
- }
-
- void createAndRegisterMethodDescriptor(final JavaCallback callback, final long methodID) {
- MethodDescriptor methodDescriptor = new MethodDescriptor();
- methodDescriptor.callback = callback;
- functionRegistry.put(methodID, methodDescriptor);
- }
-
- private boolean isVoidMethod(final Method method) {
- Class> returnType = method.getReturnType();
- if (returnType.equals(Void.TYPE)) {
- return true;
- }
- return false;
- }
-
- private Object getDefaultValue(final Class> type) {
- if (type.equals(V8Object.class)) {
- return new V8Object.Undefined();
- } else if (type.equals(V8Array.class)) {
- return new V8Array.Undefined();
- }
- return invalid;
- }
-
- protected void disposeMethodID(final long methodID) {
- functionRegistry.remove(methodID);
- }
-
- protected void weakReferenceReleased(final long objectID) {
- V8Value v8Value = v8WeakReferences.get(objectID);
- if (v8Value != null) {
- v8WeakReferences.remove(objectID);
- try {
- v8Value.close();
- } catch (Exception e) {
- // Swallow these exceptions. The V8 GC is running, and
- // if we return to V8 with Java exception on our stack,
- // we will be in a world of hurt.
- }
- }
- }
-
- protected Object callObjectJavaMethod(final long methodID, final V8Object receiver, final V8Array parameters) throws Throwable {
- MethodDescriptor methodDescriptor = functionRegistry.get(methodID);
- if (methodDescriptor.callback != null) {
- return checkResult(methodDescriptor.callback.invoke(receiver, parameters));
- }
- boolean hasVarArgs = methodDescriptor.method.isVarArgs();
- Object[] args = getArgs(receiver, methodDescriptor, parameters, hasVarArgs);
- checkArgs(args);
- try {
- Object result = methodDescriptor.method.invoke(methodDescriptor.object, args);
- return checkResult(result);
- } catch (InvocationTargetException e) {
- throw e.getTargetException();
- } catch (IllegalAccessException e) {
- throw e;
- } catch (IllegalArgumentException e) {
- throw e;
- } finally {
- releaseArguments(args, hasVarArgs);
- }
- }
-
- private Object checkResult(final Object result) {
- if (result == null) {
- return result;
- }
- if (result instanceof Float) {
- return ((Float) result).doubleValue();
- }
- if ((result instanceof Integer) || (result instanceof Double) || (result instanceof Boolean)
- || (result instanceof String)) {
- return result;
- }
- if (result instanceof V8Value) {
- if (((V8Value) result).isReleased()) {
- throw new V8RuntimeException("V8Value already released");
- }
- return result;
- }
- throw new V8RuntimeException("Unknown return type: " + result.getClass());
- }
-
- protected void callVoidJavaMethod(final long methodID, final V8Object receiver, final V8Array parameters) throws Throwable {
- MethodDescriptor methodDescriptor = functionRegistry.get(methodID);
- if (methodDescriptor.voidCallback != null) {
- methodDescriptor.voidCallback.invoke(receiver, parameters);
- return;
- }
- boolean hasVarArgs = methodDescriptor.method.isVarArgs();
- Object[] args = getArgs(receiver, methodDescriptor, parameters, hasVarArgs);
- checkArgs(args);
- try {
- methodDescriptor.method.invoke(methodDescriptor.object, args);
- } catch (InvocationTargetException e) {
- throw e.getTargetException();
- } catch (IllegalAccessException e) {
- throw e;
- } catch (IllegalArgumentException e) {
- throw e;
- } finally {
- releaseArguments(args, hasVarArgs);
- }
- }
-
- private void checkArgs(final Object[] args) {
- for (Object argument : args) {
- if (argument == invalid) {
- throw new IllegalArgumentException("argument type mismatch");
- }
- }
- }
-
- private void releaseArguments(final Object[] args, final boolean hasVarArgs) {
- if (hasVarArgs && ((args.length > 0) && (args[args.length - 1] instanceof Object[]))) {
- Object[] varArgs = (Object[]) args[args.length - 1];
- for (Object object : varArgs) {
- if (object instanceof V8Value) {
- ((V8Value) object).close();
- }
- }
- }
- for (Object arg : args) {
- if (arg instanceof V8Value) {
- ((V8Value) arg).close();
- }
- }
- }
-
- private Object[] getArgs(final V8Object receiver, final MethodDescriptor methodDescriptor, final V8Array parameters, final boolean hasVarArgs) {
- int numberOfParameters = methodDescriptor.method.getParameterTypes().length;
- int varArgIndex = hasVarArgs ? numberOfParameters - 1 : numberOfParameters;
- Object[] args = setDefaultValues(new Object[numberOfParameters], methodDescriptor.method.getParameterTypes(), receiver, methodDescriptor.includeReceiver);
- List