< prev index next >

src/jdk/nashorn/internal/runtime/Source.java

Print this page
rev 1336 : 8080090: -d option should dump script source as well
Reviewed-by: hannesw, lagergren


  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.File;
  30 import java.io.FileNotFoundException;

  31 import java.io.IOError;
  32 import java.io.IOException;
  33 import java.io.InputStream;

  34 import java.io.Reader;
  35 import java.lang.ref.WeakReference;
  36 import java.net.MalformedURLException;
  37 import java.net.URISyntaxException;
  38 import java.net.URL;
  39 import java.net.URLConnection;
  40 import java.nio.charset.Charset;
  41 import java.nio.charset.StandardCharsets;
  42 import java.nio.file.Files;
  43 import java.nio.file.Path;
  44 import java.nio.file.Paths;
  45 import java.security.MessageDigest;
  46 import java.security.NoSuchAlgorithmException;

  47 import java.util.Arrays;
  48 import java.util.Base64;
  49 import java.util.Objects;
  50 import java.util.WeakHashMap;
  51 import jdk.nashorn.api.scripting.URLReader;
  52 import jdk.nashorn.internal.parser.Token;
  53 import jdk.nashorn.internal.runtime.logging.DebugLogger;
  54 import jdk.nashorn.internal.runtime.logging.Loggable;
  55 import jdk.nashorn.internal.runtime.logging.Logger;
  56 /**
  57  * Source objects track the origin of JavaScript entities.
  58  */
  59 @Logger(name="source")
  60 public final class Source implements Loggable {
  61     private static final int BUF_SIZE = 8 * 1024;
  62     private static final Cache CACHE = new Cache();
  63 
  64     // Message digest to file name encoder
  65     private final static Base64.Encoder BASE64 = Base64.getUrlEncoder().withoutPadding();
  66 


 947         try {
 948             return file.toURI().toURL();
 949         } catch (final SecurityException | MalformedURLException ignored) {
 950             return null;
 951         }
 952     }
 953 
 954     private static DebugLogger getLoggerStatic() {
 955         final Context context = Context.getContextTrustedOrNull();
 956         return context == null ? null : context.getLogger(Source.class);
 957     }
 958 
 959     @Override
 960     public DebugLogger initLogger(final Context context) {
 961         return context.getLogger(this.getClass());
 962     }
 963 
 964     @Override
 965     public DebugLogger getLogger() {
 966         return initLogger(Context.getContextTrusted());



































 967     }
 968 }


  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.File;
  30 import java.io.FileNotFoundException;
  31 import java.io.FileOutputStream;
  32 import java.io.IOError;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.PrintWriter;
  36 import java.io.Reader;
  37 import java.lang.ref.WeakReference;
  38 import java.net.MalformedURLException;
  39 import java.net.URISyntaxException;
  40 import java.net.URL;
  41 import java.net.URLConnection;
  42 import java.nio.charset.Charset;
  43 import java.nio.charset.StandardCharsets;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.security.MessageDigest;
  48 import java.security.NoSuchAlgorithmException;
  49 import java.time.LocalDateTime;
  50 import java.util.Arrays;
  51 import java.util.Base64;
  52 import java.util.Objects;
  53 import java.util.WeakHashMap;
  54 import jdk.nashorn.api.scripting.URLReader;
  55 import jdk.nashorn.internal.parser.Token;
  56 import jdk.nashorn.internal.runtime.logging.DebugLogger;
  57 import jdk.nashorn.internal.runtime.logging.Loggable;
  58 import jdk.nashorn.internal.runtime.logging.Logger;
  59 /**
  60  * Source objects track the origin of JavaScript entities.
  61  */
  62 @Logger(name="source")
  63 public final class Source implements Loggable {
  64     private static final int BUF_SIZE = 8 * 1024;
  65     private static final Cache CACHE = new Cache();
  66 
  67     // Message digest to file name encoder
  68     private final static Base64.Encoder BASE64 = Base64.getUrlEncoder().withoutPadding();
  69 


 950         try {
 951             return file.toURI().toURL();
 952         } catch (final SecurityException | MalformedURLException ignored) {
 953             return null;
 954         }
 955     }
 956 
 957     private static DebugLogger getLoggerStatic() {
 958         final Context context = Context.getContextTrustedOrNull();
 959         return context == null ? null : context.getLogger(Source.class);
 960     }
 961 
 962     @Override
 963     public DebugLogger initLogger(final Context context) {
 964         return context.getLogger(this.getClass());
 965     }
 966 
 967     @Override
 968     public DebugLogger getLogger() {
 969         return initLogger(Context.getContextTrusted());
 970     }
 971 
 972     private File dumpFile(final String dir) {
 973         final URL u = getURL();
 974         final StringBuilder buf = new StringBuilder();
 975         // make it unique by prefixing current date & time
 976         buf.append(LocalDateTime.now().toString());
 977         buf.append('_');
 978         if (u != null) {
 979             // make it a safe file name
 980             buf.append(u.toString()
 981                     .replace('/', '_')
 982                     .replace('\\', '_'));
 983         } else {
 984             buf.append(getName());
 985         }
 986 
 987         return new File(dir, buf.toString());
 988     }
 989 
 990     void dump(final String dir) {
 991         final File file = dumpFile(dir);
 992         try (final FileOutputStream fos = new FileOutputStream(file)) {
 993             final PrintWriter pw = new PrintWriter(fos);
 994             pw.print(data.toString());
 995             pw.flush();
 996         } catch (final IOException ioExp) {
 997             debug("Skipping source dump for " +
 998                     name +
 999                     ": " +
1000                     ECMAErrors.getMessage(
1001                         "io.error.cant.write",
1002                         dir.toString() +
1003                         " : " + ioExp.toString()));
1004         }
1005     }
1006 }
< prev index next >