jdk/src/java.naming/share/classes/com/sun/jndi/ldap/Obj.java
author stefank
Mon, 25 Aug 2014 09:10:13 +0200
changeset 26314 f8bc1966fb30
parent 25859 3317bb8137f4
child 36511 9d0388c6b336
permissions -rw-r--r--
8055416: Several vm/gc/heap/summary "After GC" events emitted for the same GC ID Reviewed-by: brutisso, ehelin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, 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
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.naming.spi.DirectoryManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.naming.spi.DirStateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.ObjectStreamClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
24043
81e42b05d08b 8035807: Convert use of sun.misc.BASE64Encoder/Decoder with java.util.Base64
mchung
parents: 10326
diff changeset
    41
import java.util.Base64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * Class containing static methods and constants for dealing with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  * encoding/decoding JNDI References and Serialized Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  * in LDAP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
final class Obj {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private Obj () {}; // Make sure no one can create one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // package private; used by Connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static VersionHelper helper = VersionHelper.getVersionHelper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // LDAP attributes used to support Java objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final String[] JAVA_ATTRIBUTES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "objectClass",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "javaSerializedData",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        "javaClassName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        "javaFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        "javaCodeBase",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        "javaReferenceAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        "javaClassNames",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        "javaRemoteLocation"     // Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static final int OBJECT_CLASS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static final int SERIALIZED_DATA = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final int CLASSNAME = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final int FACTORY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static final int CODEBASE = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final int REF_ADDR = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final int TYPENAME = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    85
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final int REMOTE_LOC = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // LDAP object classes to support Java objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static final String[] JAVA_OBJECT_CLASSES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        "javaContainer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        "javaObject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        "javaNamingReference",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        "javaSerializedObject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        "javaMarshalledObject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static final String[] JAVA_OBJECT_CLASSES_LOWER = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        "javacontainer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        "javaobject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        "javanamingreference",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        "javaserializedobject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        "javamarshalledobject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static final int STRUCTURAL = 0;    // structural object class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static final int BASE_OBJECT = 1;   // auxiliary java object class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static final int REF_OBJECT = 2;    // auxiliary reference object class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static final int SER_OBJECT = 3;    // auxiliary serialized object class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static final int MAR_OBJECT = 4;    // auxiliary marshalled object class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Encode an object in LDAP attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Supports binding Referenceable or Reference, Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * and DirContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * If the object supports the Referenceable interface then encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * the reference to the object. See encodeReference() for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * If the object is serializable, it is stored as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * javaClassName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *   value: Object.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * javaSerializedData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *   value: serialized form of Object (in binary form).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * javaTypeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *   value: getTypeNames(Object.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static Attributes encodeObject(char separator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Object obj, Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Attribute objectClass, boolean cloned)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            boolean structural =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                (objectClass.size() == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    (objectClass.size() == 1 && objectClass.contains("top")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (structural) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                objectClass.add(JAVA_OBJECT_CLASSES[STRUCTURAL]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // References
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (obj instanceof Referenceable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                objectClass.add(JAVA_OBJECT_CLASSES[BASE_OBJECT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                objectClass.add(JAVA_OBJECT_CLASSES[REF_OBJECT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (!cloned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    attrs = (Attributes)attrs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                attrs.put(objectClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return (encodeReference(separator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    ((Referenceable)obj).getReference(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    attrs, obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else if (obj instanceof Reference) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                objectClass.add(JAVA_OBJECT_CLASSES[BASE_OBJECT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                objectClass.add(JAVA_OBJECT_CLASSES[REF_OBJECT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (!cloned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    attrs = (Attributes)attrs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                attrs.put(objectClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                return (encodeReference(separator, (Reference)obj, attrs, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // Serializable Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else if (obj instanceof java.io.Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                objectClass.add(JAVA_OBJECT_CLASSES[BASE_OBJECT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (!(objectClass.contains(JAVA_OBJECT_CLASSES[MAR_OBJECT]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    objectClass.contains(JAVA_OBJECT_CLASSES_LOWER[MAR_OBJECT]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    objectClass.add(JAVA_OBJECT_CLASSES[SER_OBJECT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (!cloned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    attrs = (Attributes)attrs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                attrs.put(objectClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                attrs.put(new BasicAttribute(JAVA_ATTRIBUTES[SERIALIZED_DATA],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    serializeObject(obj)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (attrs.get(JAVA_ATTRIBUTES[CLASSNAME]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    attrs.put(JAVA_ATTRIBUTES[CLASSNAME],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (attrs.get(JAVA_ATTRIBUTES[TYPENAME]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    Attribute tAttr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        LdapCtxFactory.createTypeNameAttr(obj.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    if (tAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        attrs.put(tAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // DirContext Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else if (obj instanceof DirContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            "can only bind Referenceable, Serializable, DirContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            //      System.err.println(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Each value in javaCodebase contains a list of space-separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * URLs. Each value is independent; we can pick any of the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * so we just use the first one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return an array of URL strings for the codebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private static String[] getCodebases(Attribute codebaseAttr) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (codebaseAttr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            StringTokenizer parser =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                new StringTokenizer((String)codebaseAttr.get());
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   208
            Vector<String> vec = new Vector<>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            while (parser.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                vec.addElement(parser.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            String[] answer = new String[vec.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            for (int i = 0; i < answer.length; i++) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   214
                answer[i] = vec.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Decode an object from LDAP attribute(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * The object may be a Reference, or a Serialized object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * See encodeObject() and encodeReference() for details on formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static Object decodeObject(Attributes attrs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // Get codebase, which is used in all 3 cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        String[] codebases = getCodebases(attrs.get(JAVA_ATTRIBUTES[CODEBASE]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if ((attr = attrs.get(JAVA_ATTRIBUTES[SERIALIZED_DATA])) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                ClassLoader cl = helper.getURLClassLoader(codebases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                return deserializeObject((byte[])attr.get(), cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else if ((attr = attrs.get(JAVA_ATTRIBUTES[REMOTE_LOC])) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // For backward compatibility only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                return decodeRmiObject(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    (String)attrs.get(JAVA_ATTRIBUTES[CLASSNAME]).get(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    (String)attr.get(), codebases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            attr = attrs.get(JAVA_ATTRIBUTES[OBJECT_CLASS]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (attr != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                (attr.contains(JAVA_OBJECT_CLASSES[REF_OBJECT]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    attr.contains(JAVA_OBJECT_CLASSES_LOWER[REF_OBJECT]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                return decodeReference(attrs, codebases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            NamingException ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Convert a Reference object into several LDAP attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * A Reference is stored as into the following attributes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * javaClassName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *   value: Reference.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * javaFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *   value: Reference.getFactoryClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * javaCodeBase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *   value: Reference.getFactoryClassLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * javaReferenceAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *   value: #0#typeA#valA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *   value: #1#typeB#valB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *   value: #2#typeC##[serialized RefAddr C]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *   value: #3#typeD#valD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * -  the first character denotes the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * -  the number following the first separator denotes the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *    of the RefAddr within the Reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * -  "typeA" is RefAddr.getType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * -  ## denotes that the Base64-encoded form of the non-StringRefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *    is to follow; otherwise the value that follows is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *    StringRefAddr.getContents()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * The default separator is the hash character (#).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * May provide property for this in future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private static Attributes encodeReference(char separator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Reference ref, Attributes attrs, Object orig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (ref == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if ((s = ref.getClassName()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            attrs.put(new BasicAttribute(JAVA_ATTRIBUTES[CLASSNAME], s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ((s = ref.getFactoryClassName()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            attrs.put(new BasicAttribute(JAVA_ATTRIBUTES[FACTORY], s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if ((s = ref.getFactoryClassLocation()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            attrs.put(new BasicAttribute(JAVA_ATTRIBUTES[CODEBASE], s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // Get original object's types if caller has not explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // specified other type names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (orig != null && attrs.get(JAVA_ATTRIBUTES[TYPENAME]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            Attribute tAttr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                LdapCtxFactory.createTypeNameAttr(orig.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (tAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                attrs.put(tAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int count = ref.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            Attribute refAttr = new BasicAttribute(JAVA_ATTRIBUTES[REF_ADDR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            RefAddr refAddr;
24043
81e42b05d08b 8035807: Convert use of sun.misc.BASE64Encoder/Decoder with java.util.Base64
mchung
parents: 10326
diff changeset
   325
            Base64.Encoder encoder = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                refAddr = ref.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (refAddr instanceof StringRefAddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    refAttr.add(""+ separator + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        separator +     refAddr.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        separator + refAddr.getContent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    if (encoder == null)
24043
81e42b05d08b 8035807: Convert use of sun.misc.BASE64Encoder/Decoder with java.util.Base64
mchung
parents: 10326
diff changeset
   336
                        encoder = Base64.getMimeEncoder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    refAttr.add(""+ separator + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        separator + refAddr.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        separator + separator +
24043
81e42b05d08b 8035807: Convert use of sun.misc.BASE64Encoder/Decoder with java.util.Base64
mchung
parents: 10326
diff changeset
   341
                        encoder.encodeToString(serializeObject(refAddr)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            attrs.put(refAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * A RMI object is stored in the directory as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * javaClassName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *   value: Object.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * javaRemoteLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *   value: URL of RMI object (accessed through the RMI Registry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * javaCodebase:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *   value: URL of codebase of where to find classes for object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Return the RMI Location URL itself. This will be turned into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * an RMI object when getObjectInstance() is called on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * %%% Ignore codebase for now. Depend on RMI registry to send code.-RL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @deprecated For backward compatibility only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static Object decodeRmiObject(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        String rmiName, String[] codebases) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return new Reference(className, new StringRefAddr("URL", rmiName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Restore a Reference object from several LDAP attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private static Reference decodeReference(Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        String[] codebases) throws NamingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        String factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if ((attr = attrs.get(JAVA_ATTRIBUTES[CLASSNAME])) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            className = (String)attr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new InvalidAttributesException(JAVA_ATTRIBUTES[CLASSNAME] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        " attribute is required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if ((attr = attrs.get(JAVA_ATTRIBUTES[FACTORY])) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            factory = (String)attr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        Reference ref = new Reference(className, factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            (codebases != null? codebases[0] : null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * string encoding of a RefAddr is either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         *      #posn#<type>#<address>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         *      #posn#<type>##<base64-encoded address>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if ((attr = attrs.get(JAVA_ATTRIBUTES[REF_ADDR])) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            String val, posnStr, type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            char separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            int start, sep, posn;
24043
81e42b05d08b 8035807: Convert use of sun.misc.BASE64Encoder/Decoder with java.util.Base64
mchung
parents: 10326
diff changeset
   404
            Base64.Decoder decoder = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            ClassLoader cl = helper.getURLClassLoader(codebases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
             * Temporary Vector for decoded RefAddr addresses - used to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
             * unordered addresses are correctly re-ordered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
             */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   412
            Vector<RefAddr> refAddrList = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            refAddrList.setSize(attr.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   415
            for (NamingEnumeration<?> vals = attr.getAll(); vals.hasMore(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                val = (String)vals.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (val.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        "malformed " + JAVA_ATTRIBUTES[REF_ADDR] + " attribute - "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        "empty attribute value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                // first character denotes encoding separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                separator = val.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                start = 1;  // skip over separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // extract position within Reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                if ((sep = val.indexOf(separator, start)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        "malformed " + JAVA_ATTRIBUTES[REF_ADDR] + " attribute - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        "separator '" + separator + "'" + "not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                if ((posnStr = val.substring(start, sep)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        "malformed " + JAVA_ATTRIBUTES[REF_ADDR] + " attribute - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        "empty RefAddr position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    posn = Integer.parseInt(posnStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        "malformed " + JAVA_ATTRIBUTES[REF_ADDR] + " attribute - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        "RefAddr position not an integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                start = sep + 1; // skip over position and trailing separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                // extract type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                if ((sep = val.indexOf(separator, start)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        "malformed " + JAVA_ATTRIBUTES[REF_ADDR] + " attribute - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        "RefAddr type not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if ((type = val.substring(start, sep)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        "malformed " + JAVA_ATTRIBUTES[REF_ADDR] + " attribute - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        "empty RefAddr type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                start = sep + 1; // skip over type and trailing separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                // extract content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (start == val.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    // Empty content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    refAddrList.setElementAt(new StringRefAddr(type, null), posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                } else if (val.charAt(start) == separator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    // Double separators indicate a non-StringRefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    // Content is a Base64-encoded serialized RefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    ++start;  // skip over consecutive separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    // %%% RL: exception if empty after double separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    if (decoder == null)
24043
81e42b05d08b 8035807: Convert use of sun.misc.BASE64Encoder/Decoder with java.util.Base64
mchung
parents: 10326
diff changeset
   473
                        decoder = Base64.getMimeDecoder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    RefAddr ra = (RefAddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        deserializeObject(
24043
81e42b05d08b 8035807: Convert use of sun.misc.BASE64Encoder/Decoder with java.util.Base64
mchung
parents: 10326
diff changeset
   477
                            decoder.decode(val.substring(start).getBytes()),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                            cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    refAddrList.setElementAt(ra, posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    // Single separator indicates a StringRefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    refAddrList.setElementAt(new StringRefAddr(type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        val.substring(start)), posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // Copy to real reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            for (int i = 0; i < refAddrList.size(); i++) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   490
                ref.add(refAddrList.elementAt(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return (ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Serialize an object into a byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    private static byte[] serializeObject(Object obj) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   504
            try (ObjectOutputStream serial = new ObjectOutputStream(bytes)) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   505
                serial.writeObject(obj);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   506
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return (bytes.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            NamingException ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Deserializes a byte array into an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static Object deserializeObject(byte[] obj, ClassLoader cl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // Create ObjectInputStream for deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            ByteArrayInputStream bytes = new ByteArrayInputStream(obj);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   526
            try (ObjectInputStream deserial = cl == null ?
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   527
                    new ObjectInputStream(bytes) :
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   528
                    new LoaderInputStream(bytes, cl)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                return deserial.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                NamingException ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            NamingException ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
      * Returns the attributes to bind given an object and its attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    static Attributes determineBindAttrs(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        char separator, Object obj, Attributes attrs, boolean cloned,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   547
        Name name, Context ctx, Hashtable<?,?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // Call state factories to convert object and attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        DirStateFactory.Result res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            DirectoryManager.getStateToBind(obj, name, ctx, env, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        obj = res.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        attrs = res.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // We're only storing attributes; no further processing required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        //if object to be bound is a DirContext extract its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if ((attrs == null) && (obj instanceof DirContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            cloned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            attrs = ((DirContext)obj).getAttributes("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        boolean ocNeedsCloning = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        // Create "objectClass" attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        Attribute objectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (attrs == null || attrs.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            attrs = new BasicAttributes(LdapClient.caseIgnore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            cloned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // No objectclasses supplied, use "top" to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            objectClass = new BasicAttribute("objectClass", "top");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            // Get existing objectclass attribute
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   580
            objectClass = attrs.get("objectClass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (objectClass == null && !attrs.isCaseIgnored()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                // %%% workaround
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   583
                objectClass = attrs.get("objectclass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            // No objectclasses supplied, use "top" to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (objectClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                objectClass =  new BasicAttribute("objectClass", "top");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            } else if (ocNeedsCloning || !cloned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                objectClass = (Attribute)objectClass.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // convert the supplied object into LDAP attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        attrs = encodeObject(separator, obj, attrs, objectClass, cloned);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        // System.err.println("Determined: " + attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * An ObjectInputStream that uses a class loader to find classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private static final class LoaderInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        private ClassLoader classLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        LoaderInputStream(InputStream in, ClassLoader cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            classLoader = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   612
        protected Class<?> resolveClass(ObjectStreamClass desc) throws
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   613
                IOException, ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                // %%% Should use Class.forName(desc.getName(), false, classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                // except we can't because that is only available on JDK1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                return classLoader.loadClass(desc.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                return super.resolveClass(desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   623
         protected Class<?> resolveProxyClass(String[] interfaces) throws
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
             ClassLoader nonPublicLoader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
             boolean hasNonPublicInterface = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
             // define proxy in class loader of non-public interface(s), if any
10326
9feeb910a1e2 7077672: jdk8_tl nightly fail in step-2 build on 8/10/11
jjg
parents: 10324
diff changeset
   629
             Class<?>[] classObjs = new Class<?>[interfaces.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
             for (int i = 0; i < interfaces.length; i++) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   631
                 Class<?> cl = Class.forName(interfaces[i], false, classLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                 if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                     if (hasNonPublicInterface) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                         if (nonPublicLoader != cl.getClassLoader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                             throw new IllegalAccessError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                "conflicting non-public interface class loaders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                     } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                         nonPublicLoader = cl.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                         hasNonPublicInterface = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                 classObjs[i] = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
             try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                 return Proxy.getProxyClass(hasNonPublicInterface ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        nonPublicLoader : classLoader, classObjs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
             } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                 throw new ClassNotFoundException(null, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
}