SharedContextEvaluator.java revision 85:38c44687e4bd
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 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 26package jdk.nashorn.internal.test.framework; 27 28import static jdk.nashorn.tools.Shell.COMPILATION_ERROR; 29import static jdk.nashorn.tools.Shell.RUNTIME_ERROR; 30import static jdk.nashorn.tools.Shell.SUCCESS; 31 32import java.io.File; 33import java.io.IOException; 34import java.io.OutputStream; 35import java.io.PrintWriter; 36import jdk.nashorn.api.scripting.NashornException; 37import jdk.nashorn.internal.runtime.Context; 38import jdk.nashorn.internal.runtime.ErrorManager; 39import jdk.nashorn.internal.runtime.ScriptFunction; 40import jdk.nashorn.internal.runtime.ScriptObject; 41import jdk.nashorn.internal.runtime.ScriptRuntime; 42import jdk.nashorn.internal.runtime.Source; 43import jdk.nashorn.internal.runtime.options.Options; 44 45/** 46 * A script evaluator that shares a single Nashorn Context instance to run 47 * scripts many times on it. 48 */ 49public final class SharedContextEvaluator implements ScriptEvaluator { 50 // The shared Nashorn Context 51 private final Context context; 52 53 // We can't replace output and error streams after Context is created 54 // So, we create these delegating streams - so that we can replace underlying 55 // delegate streams for each script run call 56 private final DelegatingOutputStream ctxOut; 57 private final DelegatingOutputStream ctxErr; 58 59 private static class DelegatingOutputStream extends OutputStream { 60 private OutputStream underlying; 61 62 public DelegatingOutputStream(final OutputStream out) { 63 this.underlying = out; 64 } 65 66 @Override 67 public void close() throws IOException { 68 underlying.close(); 69 } 70 71 @Override 72 public void flush() throws IOException { 73 underlying.flush(); 74 } 75 76 @Override 77 public void write(byte[] b) throws IOException { 78 underlying.write(b); 79 } 80 81 @Override 82 public void write(byte[] b, int off, int len) throws IOException { 83 underlying.write(b, off, len); 84 } 85 86 @Override 87 public void write(int b) throws IOException { 88 underlying.write(b); 89 } 90 91 void setDelegatee(OutputStream stream) { 92 this.underlying = stream; 93 } 94 } 95 96 /** 97 * SharedContextEvaluator constructor 98 * @param args initial script arguments to create shared context 99 */ 100 public SharedContextEvaluator(final String[] args) { 101 this.ctxOut = new DelegatingOutputStream(System.out); 102 this.ctxErr = new DelegatingOutputStream(System.err); 103 PrintWriter wout = new PrintWriter(ctxOut, true); 104 PrintWriter werr = new PrintWriter(ctxErr, true); 105 Options options = new Options("nashorn", werr); 106 options.process(args); 107 ErrorManager errors = new ErrorManager(werr); 108 this.context = new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader()); 109 } 110 111 @Override 112 public int run(final OutputStream out, final OutputStream err, final String[] args) throws IOException { 113 final ScriptObject oldGlobal = Context.getGlobal(); 114 try { 115 ctxOut.setDelegatee(out); 116 ctxErr.setDelegatee(err); 117 final ErrorManager errors = context.getErrorManager(); 118 final ScriptObject global = context.createGlobal(); 119 Context.setGlobal(global); 120 121 // For each file on the command line. 122 for (final String fileName : args) { 123 if (fileName.startsWith("-")) { 124 // ignore options in shared context mode (which was initialized upfront!) 125 continue; 126 } 127 final File file = new File(fileName); 128 ScriptFunction script = context.compileScript(new Source(fileName, file.toURI().toURL()), global); 129 if (script == null || errors.getNumberOfErrors() != 0) { 130 return COMPILATION_ERROR; 131 } 132 133 try { 134 ScriptRuntime.apply(script, global); 135 } catch (final NashornException e) { 136 errors.error(e.toString()); 137 if (context._dump_on_error) { 138 e.printStackTrace(context.getErr()); 139 } 140 141 return RUNTIME_ERROR; 142 } 143 } 144 } finally { 145 context.getOut().flush(); 146 context.getErr().flush(); 147 Context.setGlobal(oldGlobal); 148 } 149 150 return SUCCESS; 151 } 152} 153