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