8064467: type info persistence failed to calculate directory name
Reviewed-by: hannesw, lagergren
--- a/nashorn/make/build.xml Wed Jul 05 20:06:58 2017 +0200
+++ b/nashorn/make/build.xml Tue Nov 11 16:17:37 2014 +0100
@@ -156,6 +156,7 @@
<fileset dir="${src.dir}/jdk/nashorn/tools/resources/"/>
</copy>
<copy file="${src.dir}/jdk/internal/dynalink/support/messages.properties" todir="${build.classes.dir}/jdk/internal/dynalink/support"/>
+ <copy file="${src.dir}/jdk/nashorn/internal/codegen/anchor.properties" todir="${build.classes.dir}/jdk/nashorn/internal/codegen"/>
<echo message="full=${nashorn.fullversion}" file="${build.classes.dir}/jdk/nashorn/internal/runtime/resources/version.properties"/>
<echo file="${build.classes.dir}/jdk/nashorn/internal/runtime/resources/version.properties" append="true">${line.separator}</echo>
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java Wed Jul 05 20:06:58 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java Tue Nov 11 16:17:37 2014 +0100
@@ -33,6 +33,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -221,11 +223,37 @@
private static void reportError(final String msg, final File file, final Exception e) {
final long now = System.currentTimeMillis();
if(now - lastReportedError > ERROR_REPORT_THRESHOLD) {
- getLogger().warning(String.format("Failed to %s %s", msg, file), e);
+ reportError(String.format("Failed to %s %s", msg, file), e);
lastReportedError = now;
}
}
+ /**
+ * Logs an error message with warning severity (reasoning being that we're reporting an error that'll disable the
+ * type info cache, but it's only logged as a warning because that doesn't prevent Nashorn from running, it just
+ * disables a performance-enhancing cache).
+ * @param msg the message to log
+ * @param e the exception that represents the error.
+ */
+ private static void reportError(final String msg, final Exception e) {
+ getLogger().warning(msg, "\n", exceptionToString(e));
+ }
+
+ /**
+ * A helper that prints an exception stack trace into a string. We have to do this as if we just pass the exception
+ * to {@link DebugLogger#warning(Object...)}, it will only log the exception message and not the stack, making
+ * problems harder to diagnose.
+ * @param e the exception
+ * @return the string representation of {@link Exception#printStackTrace()} output.
+ */
+ private static String exceptionToString(final Exception e) {
+ final StringWriter sw = new StringWriter();
+ final PrintWriter pw = new PrintWriter(sw, false);
+ e.printStackTrace(pw);
+ pw.flush();
+ return sw.toString();
+ }
+
private static File createBaseCacheDir() {
if(MAX_FILES == 0 || Options.getBooleanProperty("nashorn.typeInfo.disabled")) {
return null;
@@ -233,7 +261,7 @@
try {
return createBaseCacheDirPrivileged();
} catch(final Exception e) {
- getLogger().warning("Failed to create cache dir", e);
+ reportError("Failed to create cache dir", e);
return null;
}
}
@@ -267,7 +295,7 @@
try {
return createCacheDirPrivileged(baseDir);
} catch(final Exception e) {
- getLogger().warning("Failed to create cache dir", e);
+ reportError("Failed to create cache dir", e);
return null;
}
}
@@ -280,7 +308,7 @@
try {
versionDirName = getVersionDirName();
} catch(final Exception e) {
- getLogger().warning("Failed to calculate version dir name", e);
+ reportError("Failed to calculate version dir name", e);
return null;
}
final File versionDir = new File(baseDir, versionDirName);
@@ -328,7 +356,12 @@
* @throws Exception if digest could not be created
*/
public static String getVersionDirName() throws Exception {
- final URL url = OptimisticTypesPersistence.class.getResource("");
+ // NOTE: getResource("") won't work if the JAR file doesn't have directory entries (and JAR files in JDK distro
+ // don't, or at least it's a bad idea counting on it). Alternatively, we could've tried
+ // getResource("OptimisticTypesPersistence.class") but behavior of getResource with regard to its willingness
+ // to hand out URLs to .class files is also unspecified. Therefore, the most robust way to obtain an URL to our
+ // package is to have a small non-class anchor file and start out from its URL.
+ final URL url = OptimisticTypesPersistence.class.getResource("anchor.properties");
final String protocol = url.getProtocol();
if (protocol.equals("jar")) {
// Normal deployment: nashorn.jar
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/anchor.properties Tue Nov 11 16:17:37 2014 +0100
@@ -0,0 +1,27 @@
+#
+# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+#
+# This code is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 2 only, as
+# published by the Free Software Foundation. Oracle designates this
+# particular file as subject to the "Classpath" exception as provided
+# by Oracle in the LICENSE file that accompanied this code.
+#
+# This code is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# version 2 for more details (a copy is included in the LICENSE file that
+# accompanied this code).
+#
+# You should have received a copy of the GNU General Public License version
+# 2 along with this work; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+# or visit www.oracle.com if you need additional information or have any
+# questions.
+#
+
+
+# This file exists only so OptimisticTypesPersistence.getVersionDirName() can take its URL.