corba/src/share/classes/sun/rmi/rmic/iiop/NameContext.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
 * Licensed Materials - Property of IBM
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
 * RMI-IIOP v1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
 * Copyright IBM Corp. 1998 1999  All Rights Reserved
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
package sun.rmi.rmic.iiop;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
 * A NameContext enables detection of strings which differ only
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
 * in case.
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
 * @author      Bryan Atsatt
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
class NameContext {
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
    private Hashtable table;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
    private boolean allowCollisions;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
     * Get a context for the given name. Name may be null, in
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
     * which case this method will return the default context.
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
    public static synchronized NameContext forName (String name,
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
                                                    boolean allowCollisions,
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
                                                    BatchEnvironment env) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
        NameContext result = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
        // Do we need to use the default context?
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
        if (name == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
            // Yes.
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
            name = "null";
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
        // Have we initialized our hashtable?
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
        if (env.nameContexts == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
            // Nope, so do it...
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
            env.nameContexts = new Hashtable();
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
            // Yes, see if we already have the requested
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
            // context...
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
            result = (NameContext) env.nameContexts.get(name);
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
        // Do we have the requested context?
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
        if (result == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
            // Nope, so create and add it...
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
            result = new NameContext(allowCollisions);
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
            env.nameContexts.put(name,result);
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
        return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
     * Construct a context.
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
     * @param allowCollisions true if case-sensitive name collisions
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
     * are allowed, false if not.
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    public NameContext (boolean allowCollisions) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
        this.allowCollisions = allowCollisions;
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
        table = new Hashtable();
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
     * Add a name to this context. If constructed with allowCollisions
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
     * false and a collision occurs, this method will throw an exception
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
     * in which the message contains the string: "name" and "collision".
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
    public void assertPut (String name) throws Exception {
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
        String message = add(name);
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
        if (message != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            throw new Exception(message);
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
     * Add a name to this context..
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
    public void put (String name) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
        if (allowCollisions == false) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
            throw new Error("Must use assertPut(name)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
        add(name);
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
     * Add a name to this context. If constructed with allowCollisions
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
     * false and a collision occurs, this method will return a message
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
     * string, otherwise returns null.
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
    private String add (String name) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
        // First, create a key by converting name to lowercase...
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
        String key = name.toLowerCase();
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
        // Does this key exist in the context?
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
        Name value = (Name) table.get(key);
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
        if (value != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
            // Yes, so they match if we ignore case. Do they match if
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
            // we don't ignore case?
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
            if (!name.equals(value.name)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
                // No, so this is a case-sensitive match. Are we
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
                // supposed to allow this?
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
                if (allowCollisions) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
                    // Yes, make sure it knows that it collides...
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
                    value.collisions = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
                    // No, so return a message string...
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
                    return new String("\"" + name + "\" and \"" + value.name + "\"");
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
            // No, so add it...
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
            table.put(key,new Name(name,false));
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
        return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
     * Get a name from the context. If it has collisions, the name
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
     * will be converted as specified in section 5.2.7.
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
    public String get (String name) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
        Name it = (Name) table.get(name.toLowerCase());
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
        String result = name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        // Do we need to mangle it?
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
        if (it.collisions) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
            // Yep, so do it...
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
            int length = name.length();
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
            boolean allLower = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
            for (int i = 0; i < length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
                if (Character.isUpperCase(name.charAt(i))) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
                    result += "_";
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
                    result += i;
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
                    allLower = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
            if (allLower) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
                result += "_";
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
        return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
     * Remove all entries.
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
    public void clear () {
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
        table.clear();
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
    public class Name {
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
        public String name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        public boolean collisions;
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
        public Name (String name, boolean collisions) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
            this.name = name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
            this.collisions = collisions;
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
}