src/java.naming/share/classes/javax/naming/ldap/ControlFactory.java
author stefank
Mon, 25 Nov 2019 15:00:32 +0100
changeset 59255 a74627659f96
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234602: ZGC: Windows compile error in ZHeuristic Reviewed-by: pliden, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18580
6c4badf4ffba 8019407: Fix doclint issues in javax.naming.*
darcy
parents: 7668
diff changeset
     2
 * Copyright (c) 1999, 2013, 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 javax.naming.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.naming.Context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.naming.internal.FactoryEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.naming.internal.ResourceManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * This abstract class represents a factory for creating LDAPv3 controls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * LDAPv3 controls are defined in
5604
d5b97d8361d0 6952701: Use http://www.ietf.org/rfc for rfc references in jdk public APIs
sherman
parents: 2
diff changeset
    40
  * <A HREF="http://www.ietf.org/rfc/rfc2251.txt">RFC 2251</A>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * When a service provider receives a response control, it uses control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  * factories to return the specific/appropriate control class implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  * @see Control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public abstract class ControlFactory {
18580
6c4badf4ffba 8019407: Fix doclint issues in javax.naming.*
darcy
parents: 7668
diff changeset
    54
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Creates a new instance of a control factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected ControlFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      * Creates a control using this control factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      * The factory is used by the service provider to return controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      * that it reads from the LDAP protocol as specialized control classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      * Without this mechanism, the provider would be returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      * controls that only contained data in BER encoded format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      *<p>
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
    68
      * Typically, {@code ctl} is a "basic" control containing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      * BER encoded data. The factory is used to create a specialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      * control implementation, usually by decoding the BER encoded data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
      * that provides methods to access that data in a type-safe and friendly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
      * manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      * For example, a factory might use the BER encoded data in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      * basic control and return an instance of a VirtualListReplyControl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
      *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
      * If this factory cannot create a control using the argument supplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
      * it should return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
      * A factory should only throw an exception if it is sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
      * it is the only intended factory and that no other control factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
      * should be tried. This might happen, for example, if the BER data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
      * in the control does not match what is expected of a control with
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
    83
      * the given OID. Since this method throws {@code NamingException},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
      * any other internally generated exception that should be propagated
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
    85
      * must be wrapped inside a {@code NamingException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      * @param ctl A non-null control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      * @return A possibly null Control.
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
    90
      * @exception NamingException If {@code ctl} contains invalid data that prevents it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      * from being used to create a control. A factory should only throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      * an exception if it knows how to produce the control (identified by the OID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      * but is unable to because of, for example invalid BER data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public abstract Control getControlInstance(Control ctl) throws NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      * Creates a control using known control factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      * The following rule is used to create the control:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
      *<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
      * <li> Use the control factories specified in
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   103
      *    the {@code LdapContext.CONTROL_FACTORIES} property of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      *    environment, and of the provider resource file associated with
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   105
      *    {@code ctx}, in that order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      *    The value of this property is a colon-separated list of factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      *    class names that are tried in order, and the first one that succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      *    in creating the control is the one used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      *    If none of the factories can be loaded,
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   110
      *    return {@code ctl}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      *    If an exception is encountered while creating the control, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
      *    exception is passed up to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
      *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      * <p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32029
diff changeset
   115
      * Note that a control factory must be public and must have a public
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32029
diff changeset
   116
      * constructor that accepts no arguments.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32029
diff changeset
   117
      * In cases where the factory is in a named module then it must be in a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32029
diff changeset
   118
      * package which is exported by that module to the {@code java.naming}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32029
diff changeset
   119
      * module.
23712
d46a902c1aed 8039041: Tidy warnings cleanup for javax.naming
yan
parents: 18580
diff changeset
   120
      *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      * @param ctl The non-null control object containing the OID and BER data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      * @param ctx The possibly null context in which the control is being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      * If null, no such information is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
      * @param env The possibly null environment of the context. This is used
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   125
      * to find the value of the {@code LdapContext.CONTROL_FACTORIES} property.
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   126
      * @return A control object created using {@code ctl}; or
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   127
      *         {@code ctl} if a control object cannot be created using
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      *         the algorithm described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
      * @exception NamingException if a naming exception was encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
      *         while attempting to create the control object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
      *         If one of the factories accessed throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      *         exception, it is propagated up to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
      * If an error was encountered while loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
      * and instantiating the factory and object classes, the exception
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   135
      * is wrapped inside a {@code NamingException} and then rethrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public static Control getControlInstance(Control ctl, Context ctx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                             Hashtable<?,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // Get object factories list from environment properties or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // provider resource file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        FactoryEnumeration factories = ResourceManager.getFactories(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            LdapContext.CONTROL_FACTORIES, env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (factories == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return ctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Try each factory until one succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        Control answer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        ControlFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        while (answer == null && factories.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            factory = (ControlFactory)factories.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            answer = factory.getControlInstance(ctl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return (answer != null)? answer : ctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}