jdk/src/solaris/classes/java/lang/ProcessEnvironment.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7803 56bc97d69d93
child 11275 7cb0861d512f
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 2003, 2010, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/* We use APIs that access the standard Unix environ array, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * is defined by UNIX98 to look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *    char **environ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * These are unsorted, case-sensitive, null-terminated arrays of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of the form FOO=BAR\000 which are usually encoded in the user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * default encoding (file.encoding is an excellent choice for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * encoding/decoding these).  However, even though the user cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * directly access the underlying byte representation, we take pains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to pass on the child the exact byte representation we inherit from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the parent process for any environment name or value not created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Javaland.  So we keep track of all the byte representations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Internally, we define the types Variable and Value that exhibit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * String/byteArray duality.  The internal representation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * environment then looks like a Map<Variable,Value>.  But we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * expose this to the user -- we only provide a Map<String,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * view, although we could also provide a Map<byte[],byte[]> view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The non-private methods in this class are not for general use even
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * within this package.  Instead, they are the system-dependent parts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * of the system-independent method of the same name.  Don't even
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * think of using this class unless your method's name appears below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
final class ProcessEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final HashMap<Variable,Value> theEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final Map<String,String> theUnmodifiableEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final int MIN_NAME_LENGTH = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // We cache the C environment.  This means that subsequent calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // to putenv/setenv from C will not be visible from Java code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        byte[][] environ = environ();
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    71
        theEnvironment = new HashMap<>(environ.length/2 + 3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // Read environment variables back to front,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // so that earlier variables override later ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        for (int i = environ.length-1; i > 0; i-=2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            theEnvironment.put(Variable.valueOf(environ[i-1]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                               Value.valueOf(environ[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        theUnmodifiableEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            = Collections.unmodifiableMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            (new StringEnvironment(theEnvironment));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /* Only for use by System.getenv(String) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static String getenv(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return theUnmodifiableEnvironment.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* Only for use by System.getenv() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static Map<String,String> getenv() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return theUnmodifiableEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* Only for use by ProcessBuilder.environment() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static Map<String,String> environment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return new StringEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            ((Map<Variable,Value>)(theEnvironment.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /* Only for use by Runtime.exec(...String[]envp...) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static Map<String,String> emptyEnvironment(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return new StringEnvironment(new HashMap<Variable,Value>(capacity));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static native byte[][] environ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // This class is not instantiable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private ProcessEnvironment() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // Check that name is suitable for insertion into Environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static void validateVariable(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (name.indexOf('=')      != -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            name.indexOf('\u0000') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                ("Invalid environment variable name: \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // Check that value is suitable for insertion into Environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static void validateValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (value.indexOf('\u0000') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                ("Invalid environment variable value: \"" + value + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // A class hiding the byteArray-String duality of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // text data on Unixoid operating systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static abstract class ExternalData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        protected final String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        protected final byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        protected ExternalData(String str, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            this.str = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            this.bytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return o instanceof ExternalData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                && arrayEquals(getBytes(), ((ExternalData) o).getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return arrayHash(getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static class Variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        extends ExternalData implements Comparable<Variable>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        protected Variable(String str, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            super(str, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public static Variable valueOfQueryOnly(Object str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return valueOfQueryOnly((String) str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public static Variable valueOfQueryOnly(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return new Variable(str, str.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public static Variable valueOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            validateVariable(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return valueOfQueryOnly(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public static Variable valueOf(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return new Variable(new String(bytes), bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public int compareTo(Variable variable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return arrayCompare(getBytes(), variable.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return o instanceof Variable && super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static class Value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        extends ExternalData implements Comparable<Value>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        protected Value(String str, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            super(str, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        public static Value valueOfQueryOnly(Object str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return valueOfQueryOnly((String) str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public static Value valueOfQueryOnly(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return new Value(str, str.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        public static Value valueOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            validateValue(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return valueOfQueryOnly(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        public static Value valueOf(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return new Value(new String(bytes), bytes);
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 int compareTo(Value value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return arrayCompare(getBytes(), value.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return o instanceof Value && super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // This implements the String map view the user sees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static class StringEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        extends AbstractMap<String,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        private Map<Variable,Value> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        private static String toString(Value v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return v == null ? null : v.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        public StringEnvironment(Map<Variable,Value> m) {this.m = m;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public int size()        {return m.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        public boolean isEmpty() {return m.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        public void clear()      {       m.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return m.containsKey(Variable.valueOfQueryOnly(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return m.containsValue(Value.valueOfQueryOnly(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        public String get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return toString(m.get(Variable.valueOfQueryOnly(key)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        public String put(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return toString(m.put(Variable.valueOf(key),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                  Value.valueOf(value)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public String remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return toString(m.remove(Variable.valueOfQueryOnly(key)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public Set<String> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return new StringKeySet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public Set<Map.Entry<String,String>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return new StringEntrySet(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        public Collection<String> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return new StringValues(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // It is technically feasible to provide a byte-oriented view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        //      public Map<byte[],byte[]> asByteArrayMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        //          return new ByteArrayEnvironment(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        //      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // Convert to Unix style environ as a monolithic byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // inspired by the Windows Environment Block, except we work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // exclusively with bytes instead of chars, and we need only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // one trailing NUL on Unix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // This keeps the JNI as simple and efficient as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        public byte[] toEnvironmentBlock(int[]envc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            int count = m.size() * 2; // For added '=' and NUL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            for (Map.Entry<Variable,Value> entry : m.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                count += entry.getKey().getBytes().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                count += entry.getValue().getBytes().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            byte[] block = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            for (Map.Entry<Variable,Value> entry : m.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                byte[] key   = entry.getKey  ().getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                byte[] value = entry.getValue().getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                System.arraycopy(key, 0, block, i, key.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                i+=key.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                block[i++] = (byte) '=';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                System.arraycopy(value, 0, block, i, value.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                i+=value.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                // No need to write NUL byte explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                //block[i++] = (byte) '\u0000';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            envc[0] = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    static byte[] toEnvironmentBlock(Map<String,String> map, int[]envc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return map == null ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            ((StringEnvironment)map).toEnvironmentBlock(envc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private static class StringEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        implements Map.Entry<String,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        private final Map.Entry<Variable,Value> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        public StringEntry(Map.Entry<Variable,Value> e) {this.e = e;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        public String getKey()   {return e.getKey().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public String getValue() {return e.getValue().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public String setValue(String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return e.setValue(Value.valueOf(newValue)).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        public String toString() {return getKey() + "=" + getValue();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return o instanceof StringEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                && e.equals(((StringEntry)o).e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public int hashCode()    {return e.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static class StringEntrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        extends AbstractSet<Map.Entry<String,String>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        private final Set<Map.Entry<Variable,Value>> s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        public StringEntrySet(Set<Map.Entry<Variable,Value>> s) {this.s = s;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        public int size()        {return s.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        public boolean isEmpty() {return s.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public void clear()      {       s.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        public Iterator<Map.Entry<String,String>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return new Iterator<Map.Entry<String,String>>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                Iterator<Map.Entry<Variable,Value>> i = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                public Map.Entry<String,String> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    return new StringEntry(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                public void remove() {i.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        private static Map.Entry<Variable,Value> vvEntry(final Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (o instanceof StringEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return ((StringEntry)o).e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return new Map.Entry<Variable,Value>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                public Variable getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    return Variable.valueOfQueryOnly(((Map.Entry)o).getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                public Value getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    return Value.valueOfQueryOnly(((Map.Entry)o).getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                public Value setValue(Value value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        public boolean contains(Object o) { return s.contains(vvEntry(o)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        public boolean remove(Object o)   { return s.remove(vvEntry(o)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return o instanceof StringEntrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                && s.equals(((StringEntrySet) o).s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        public int hashCode() {return s.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private static class StringValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
          extends AbstractCollection<String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        private final Collection<Value> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        public StringValues(Collection<Value> c) {this.c = c;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        public int size()        {return c.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public boolean isEmpty() {return c.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        public void clear()      {       c.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        public Iterator<String> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return new Iterator<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                Iterator<Value> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                public String next()     {return i.next().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                public void remove()     {i.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return c.contains(Value.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return c.remove(Value.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return o instanceof StringValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                && c.equals(((StringValues)o).c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        public int hashCode() {return c.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private static class StringKeySet extends AbstractSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        private final Set<Variable> s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        public StringKeySet(Set<Variable> s) {this.s = s;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public int size()        {return s.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        public boolean isEmpty() {return s.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        public void clear()      {       s.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        public Iterator<String> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return new Iterator<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                Iterator<Variable> i = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                public String next()     {return i.next().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                public void remove()     {       i.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return s.contains(Variable.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return s.remove(Variable.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    // Replace with general purpose method someday
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private static int arrayCompare(byte[]x, byte[] y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        int min = x.length < y.length ? x.length : y.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        for (int i = 0; i < min; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (x[i] != y[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return x[i] - y[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return x.length - y.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    // Replace with general purpose method someday
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private static boolean arrayEquals(byte[] x, byte[] y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (x.length != y.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        for (int i = 0; i < x.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (x[i] != y[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    // Replace with general purpose method someday
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private static int arrayHash(byte[] x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        for (int i = 0; i < x.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            hash = 31 * hash + x[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}