Path: blob/master/test/jdk/javax/script/MyContext.java
41144 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* This is pluggable context used by test for 639861425*/2627import javax.script.*;28import java.util.*;29import java.io.*;3031public class MyContext implements ScriptContext {3233public static final int APP_SCOPE = 125;3435protected Writer writer;3637protected Writer errorWriter;3839protected Reader reader;404142protected Bindings appScope;43protected Bindings engineScope;44protected Bindings globalScope;454647public MyContext() {48appScope = new SimpleBindings();49engineScope = new SimpleBindings();50globalScope = null;51reader = new InputStreamReader(System.in);52writer = new PrintWriter(System.out , true);53errorWriter = new PrintWriter(System.err, true);54}5556public void setBindings(Bindings bindings, int scope) {5758switch (scope) {59case APP_SCOPE:60if (bindings == null) {61throw new NullPointerException("App scope cannot be null.");62}63appScope = bindings;64break;6566case ENGINE_SCOPE:67if (bindings == null) {68throw new NullPointerException("Engine scope cannot be null.");69}70engineScope = bindings;71break;72case GLOBAL_SCOPE:73globalScope = bindings;74break;75default:76throw new IllegalArgumentException("Invalid scope value.");77}78}7980public Object getAttribute(String name) {81if (engineScope.containsKey(name)) {82return getAttribute(name, ENGINE_SCOPE);83} else if (appScope.containsKey(name)) {84return getAttribute(name, APP_SCOPE);85} else if (globalScope != null && globalScope.containsKey(name)) {86return getAttribute(name, GLOBAL_SCOPE);87}8889return null;90}9192public Object getAttribute(String name, int scope) {9394switch (scope) {95case APP_SCOPE:96return appScope.get(name);9798case ENGINE_SCOPE:99return engineScope.get(name);100101case GLOBAL_SCOPE:102if (globalScope != null) {103return globalScope.get(name);104}105return null;106107default:108throw new IllegalArgumentException("Illegal scope value.");109}110}111112public Object removeAttribute(String name, int scope) {113114switch (scope) {115case APP_SCOPE:116if (getBindings(APP_SCOPE) != null) {117return getBindings(APP_SCOPE).remove(name);118}119return null;120121122case ENGINE_SCOPE:123if (getBindings(ENGINE_SCOPE) != null) {124return getBindings(ENGINE_SCOPE).remove(name);125}126return null;127128case GLOBAL_SCOPE:129if (getBindings(GLOBAL_SCOPE) != null) {130return getBindings(GLOBAL_SCOPE).remove(name);131}132return null;133134default:135throw new IllegalArgumentException("Illegal scope value.");136}137}138139public void setAttribute(String name, Object value, int scope) {140141switch (scope) {142case APP_SCOPE:143appScope.put(name, value);144return;145146case ENGINE_SCOPE:147engineScope.put(name, value);148return;149150case GLOBAL_SCOPE:151if (globalScope != null) {152globalScope.put(name, value);153}154return;155156default:157throw new IllegalArgumentException("Illegal scope value.");158}159}160161public Writer getWriter() {162return writer;163}164165public Reader getReader() {166return reader;167}168169public void setReader(Reader reader) {170this.reader = reader;171}172173public void setWriter(Writer writer) {174this.writer = writer;175}176177public Writer getErrorWriter() {178return errorWriter;179}180181public void setErrorWriter(Writer writer) {182this.errorWriter = writer;183}184185public int getAttributesScope(String name) {186if (engineScope.containsKey(name)) {187return ENGINE_SCOPE;188} else if (appScope.containsKey(name)) {189return APP_SCOPE;190} else if (globalScope != null && globalScope.containsKey(name)) {191return GLOBAL_SCOPE;192} else {193return -1;194}195}196197public Bindings getBindings(int scope) {198if (scope == ENGINE_SCOPE) {199return engineScope;200} else if (scope == APP_SCOPE) {201return appScope;202} else if (scope == GLOBAL_SCOPE) {203return globalScope;204} else {205throw new IllegalArgumentException("Illegal scope value.");206}207}208209public List<Integer> getScopes() {210return scopes;211}212213private static List<Integer> scopes;214static {215scopes = new ArrayList<Integer>(3);216scopes.add(ENGINE_SCOPE);217scopes.add(APP_SCOPE);218scopes.add(GLOBAL_SCOPE);219scopes = Collections.unmodifiableList(scopes);220}221}222223224