blob: c0014460b69e8507249aaedd50a976c545f52d5e [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohair2283b9d2010-05-25 15:58:33 -07002 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
ohair2283b9d2010-05-25 15:58:33 -070019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/*
25 * This is pluggable context used by test for 6398614
26 */
27
28import javax.script.*;
29import java.util.*;
30import java.io.*;
31
32public class MyContext implements ScriptContext {
33
34 public static final int APP_SCOPE = 125;
35
36 protected Writer writer;
37
38 protected Writer errorWriter;
39
40 protected Reader reader;
41
42
43 protected Bindings appScope;
44 protected Bindings engineScope;
45 protected Bindings globalScope;
46
47
48 public MyContext() {
49 appScope = new SimpleBindings();
50 engineScope = new SimpleBindings();
51 globalScope = null;
52 reader = new InputStreamReader(System.in);
53 writer = new PrintWriter(System.out , true);
54 errorWriter = new PrintWriter(System.err, true);
55 }
56
57 public void setBindings(Bindings bindings, int scope) {
58
59 switch (scope) {
60 case APP_SCOPE:
61 if (bindings == null) {
62 throw new NullPointerException("App scope cannot be null.");
63 }
64 appScope = bindings;
65 break;
66
67 case ENGINE_SCOPE:
68 if (bindings == null) {
69 throw new NullPointerException("Engine scope cannot be null.");
70 }
71 engineScope = bindings;
72 break;
73 case GLOBAL_SCOPE:
74 globalScope = bindings;
75 break;
76 default:
77 throw new IllegalArgumentException("Invalid scope value.");
78 }
79 }
80
81 public Object getAttribute(String name) {
82 if (engineScope.containsKey(name)) {
83 return getAttribute(name, ENGINE_SCOPE);
84 } else if (appScope.containsKey(name)) {
85 return getAttribute(name, APP_SCOPE);
86 } else if (globalScope != null && globalScope.containsKey(name)) {
87 return getAttribute(name, GLOBAL_SCOPE);
88 }
89
90 return null;
91 }
92
93 public Object getAttribute(String name, int scope) {
94
95 switch (scope) {
96 case APP_SCOPE:
97 return appScope.get(name);
98
99 case ENGINE_SCOPE:
100 return engineScope.get(name);
101
102 case GLOBAL_SCOPE:
103 if (globalScope != null) {
104 return globalScope.get(name);
105 }
106 return null;
107
108 default:
109 throw new IllegalArgumentException("Illegal scope value.");
110 }
111 }
112
113 public Object removeAttribute(String name, int scope) {
114
115 switch (scope) {
116 case APP_SCOPE:
117 if (getBindings(APP_SCOPE) != null) {
118 return getBindings(APP_SCOPE).remove(name);
119 }
120 return null;
121
122
123 case ENGINE_SCOPE:
124 if (getBindings(ENGINE_SCOPE) != null) {
125 return getBindings(ENGINE_SCOPE).remove(name);
126 }
127 return null;
128
129 case GLOBAL_SCOPE:
130 if (getBindings(GLOBAL_SCOPE) != null) {
131 return getBindings(GLOBAL_SCOPE).remove(name);
132 }
133 return null;
134
135 default:
136 throw new IllegalArgumentException("Illegal scope value.");
137 }
138 }
139
140 public void setAttribute(String name, Object value, int scope) {
141
142 switch (scope) {
143 case APP_SCOPE:
144 appScope.put(name, value);
145 return;
146
147 case ENGINE_SCOPE:
148 engineScope.put(name, value);
149 return;
150
151 case GLOBAL_SCOPE:
152 if (globalScope != null) {
153 globalScope.put(name, value);
154 }
155 return;
156
157 default:
158 throw new IllegalArgumentException("Illegal scope value.");
159 }
160 }
161
162 public Writer getWriter() {
163 return writer;
164 }
165
166 public Reader getReader() {
167 return reader;
168 }
169
170 public void setReader(Reader reader) {
171 this.reader = reader;
172 }
173
174 public void setWriter(Writer writer) {
175 this.writer = writer;
176 }
177
178 public Writer getErrorWriter() {
179 return errorWriter;
180 }
181
182 public void setErrorWriter(Writer writer) {
183 this.errorWriter = writer;
184 }
185
186 public int getAttributesScope(String name) {
187 if (engineScope.containsKey(name)) {
188 return ENGINE_SCOPE;
189 } else if (appScope.containsKey(name)) {
190 return APP_SCOPE;
191 } else if (globalScope != null && globalScope.containsKey(name)) {
192 return GLOBAL_SCOPE;
193 } else {
194 return -1;
195 }
196 }
197
198 public Bindings getBindings(int scope) {
199 if (scope == ENGINE_SCOPE) {
200 return engineScope;
201 } else if (scope == APP_SCOPE) {
202 return appScope;
203 } else if (scope == GLOBAL_SCOPE) {
204 return globalScope;
205 } else {
206 throw new IllegalArgumentException("Illegal scope value.");
207 }
208 }
209
210 public List<Integer> getScopes() {
211 return scopes;
212 }
213
214 private static List<Integer> scopes;
215 static {
216 scopes = new ArrayList<Integer>(3);
217 scopes.add(ENGINE_SCOPE);
218 scopes.add(APP_SCOPE);
219 scopes.add(GLOBAL_SCOPE);
220 scopes = Collections.unmodifiableList(scopes);
221 }
222}