0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This is pluggable context used by test for 6398614
0N/A */
0N/A
0N/Aimport javax.script.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class MyContext implements ScriptContext {
0N/A
0N/A public static final int APP_SCOPE = 125;
0N/A
0N/A protected Writer writer;
0N/A
0N/A protected Writer errorWriter;
0N/A
0N/A protected Reader reader;
0N/A
0N/A
0N/A protected Bindings appScope;
0N/A protected Bindings engineScope;
0N/A protected Bindings globalScope;
0N/A
0N/A
0N/A public MyContext() {
0N/A appScope = new SimpleBindings();
0N/A engineScope = new SimpleBindings();
0N/A globalScope = null;
0N/A reader = new InputStreamReader(System.in);
0N/A writer = new PrintWriter(System.out , true);
0N/A errorWriter = new PrintWriter(System.err, true);
0N/A }
0N/A
0N/A public void setBindings(Bindings bindings, int scope) {
0N/A
0N/A switch (scope) {
0N/A case APP_SCOPE:
0N/A if (bindings == null) {
0N/A throw new NullPointerException("App scope cannot be null.");
0N/A }
0N/A appScope = bindings;
0N/A break;
0N/A
0N/A case ENGINE_SCOPE:
0N/A if (bindings == null) {
0N/A throw new NullPointerException("Engine scope cannot be null.");
0N/A }
0N/A engineScope = bindings;
0N/A break;
0N/A case GLOBAL_SCOPE:
0N/A globalScope = bindings;
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException("Invalid scope value.");
0N/A }
0N/A }
0N/A
0N/A public Object getAttribute(String name) {
0N/A if (engineScope.containsKey(name)) {
0N/A return getAttribute(name, ENGINE_SCOPE);
0N/A } else if (appScope.containsKey(name)) {
0N/A return getAttribute(name, APP_SCOPE);
0N/A } else if (globalScope != null && globalScope.containsKey(name)) {
0N/A return getAttribute(name, GLOBAL_SCOPE);
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A
0N/A public Object getAttribute(String name, int scope) {
0N/A
0N/A switch (scope) {
0N/A case APP_SCOPE:
0N/A return appScope.get(name);
0N/A
0N/A case ENGINE_SCOPE:
0N/A return engineScope.get(name);
0N/A
0N/A case GLOBAL_SCOPE:
0N/A if (globalScope != null) {
0N/A return globalScope.get(name);
0N/A }
0N/A return null;
0N/A
0N/A default:
0N/A throw new IllegalArgumentException("Illegal scope value.");
0N/A }
0N/A }
0N/A
0N/A public Object removeAttribute(String name, int scope) {
0N/A
0N/A switch (scope) {
0N/A case APP_SCOPE:
0N/A if (getBindings(APP_SCOPE) != null) {
0N/A return getBindings(APP_SCOPE).remove(name);
0N/A }
0N/A return null;
0N/A
0N/A
0N/A case ENGINE_SCOPE:
0N/A if (getBindings(ENGINE_SCOPE) != null) {
0N/A return getBindings(ENGINE_SCOPE).remove(name);
0N/A }
0N/A return null;
0N/A
0N/A case GLOBAL_SCOPE:
0N/A if (getBindings(GLOBAL_SCOPE) != null) {
0N/A return getBindings(GLOBAL_SCOPE).remove(name);
0N/A }
0N/A return null;
0N/A
0N/A default:
0N/A throw new IllegalArgumentException("Illegal scope value.");
0N/A }
0N/A }
0N/A
0N/A public void setAttribute(String name, Object value, int scope) {
0N/A
0N/A switch (scope) {
0N/A case APP_SCOPE:
0N/A appScope.put(name, value);
0N/A return;
0N/A
0N/A case ENGINE_SCOPE:
0N/A engineScope.put(name, value);
0N/A return;
0N/A
0N/A case GLOBAL_SCOPE:
0N/A if (globalScope != null) {
0N/A globalScope.put(name, value);
0N/A }
0N/A return;
0N/A
0N/A default:
0N/A throw new IllegalArgumentException("Illegal scope value.");
0N/A }
0N/A }
0N/A
0N/A public Writer getWriter() {
0N/A return writer;
0N/A }
0N/A
0N/A public Reader getReader() {
0N/A return reader;
0N/A }
0N/A
0N/A public void setReader(Reader reader) {
0N/A this.reader = reader;
0N/A }
0N/A
0N/A public void setWriter(Writer writer) {
0N/A this.writer = writer;
0N/A }
0N/A
0N/A public Writer getErrorWriter() {
0N/A return errorWriter;
0N/A }
0N/A
0N/A public void setErrorWriter(Writer writer) {
0N/A this.errorWriter = writer;
0N/A }
0N/A
0N/A public int getAttributesScope(String name) {
0N/A if (engineScope.containsKey(name)) {
0N/A return ENGINE_SCOPE;
0N/A } else if (appScope.containsKey(name)) {
0N/A return APP_SCOPE;
0N/A } else if (globalScope != null && globalScope.containsKey(name)) {
0N/A return GLOBAL_SCOPE;
0N/A } else {
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A public Bindings getBindings(int scope) {
0N/A if (scope == ENGINE_SCOPE) {
0N/A return engineScope;
0N/A } else if (scope == APP_SCOPE) {
0N/A return appScope;
0N/A } else if (scope == GLOBAL_SCOPE) {
0N/A return globalScope;
0N/A } else {
0N/A throw new IllegalArgumentException("Illegal scope value.");
0N/A }
0N/A }
0N/A
0N/A public List<Integer> getScopes() {
0N/A return scopes;
0N/A }
0N/A
0N/A private static List<Integer> scopes;
0N/A static {
0N/A scopes = new ArrayList<Integer>(3);
0N/A scopes.add(ENGINE_SCOPE);
0N/A scopes.add(APP_SCOPE);
0N/A scopes.add(GLOBAL_SCOPE);
0N/A scopes = Collections.unmodifiableList(scopes);
0N/A }
0N/A}