1 /*
   2  * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.naming.spi;
  26 
  27 import javax.naming.*;
  28 import javax.naming.directory.Attributes;
  29 import java.util.Hashtable;
  30 
  31 /**
  32   * This interface represents a factory for obtaining the state of an
  33   * object and corresponding attributes for binding.
  34   *<p>
  35   * The JNDI framework allows for object implementations to
  36   * be loaded in dynamically via {@code object factories}.
  37   * <p>
  38   * A {@code DirStateFactory} extends {@code StateFactory}
  39   * by allowing an {@code Attributes} instance
  40   * to be supplied to and be returned by the {@code getStateToBind()} method.
  41   * {@code DirStateFactory} implementations are intended to be used by
  42   * {@code DirContext} service providers.
  43   * When a caller binds an object using {@code DirContext.bind()},
  44   * he might also specify a set of attributes to be bound with the object.
  45   * The object and attributes to be bound are passed to
  46   * the {@code getStateToBind()} method of a factory.
  47   * If the factory processes the object and attributes, it returns
  48   * a corresponding pair of object and attributes to be bound.
  49   * If the factory does not process the object, it must return null.
  50   *<p>
  51   * For example, a caller might bind a printer object with some printer-related
  52   * attributes.
  53   *<blockquote><pre>
  54   * ctx.rebind("inky", printer, printerAttrs);
  55   *</pre></blockquote>
  56   * An LDAP service provider for {@code ctx} uses a {@code DirStateFactory}
  57   * (indirectly via {@code DirectoryManager.getStateToBind()})
  58   * and gives it {@code printer} and {@code printerAttrs}. A factory for
  59   * an LDAP directory might turn {@code printer} into a set of attributes
  60   * and merge that with {@code printerAttrs}. The service provider then
  61   * uses the resulting attributes to create an LDAP entry and updates
  62   * the directory.
  63   *
  64   * <p> Since {@code DirStateFactory} extends {@code StateFactory}, it
  65   * has two {@code getStateToBind()} methods, where one
  66   * differs from the other by the attributes
  67   * argument. {@code DirectoryManager.getStateToBind()} will only use
  68   * the form that accepts the attributes argument, while
  69   * {@code NamingManager.getStateToBind()} will only use the form that
  70   * does not accept the attributes argument.
  71   *
  72   * <p> Either form of the {@code getStateToBind()} method of a
  73   * DirStateFactory may be invoked multiple times, possibly using different
  74   * parameters.  The implementation is thread-safe.
  75   *
  76   * @author Rosanna Lee
  77   * @author Scott Seligman
  78   *
  79   * @see DirectoryManager#getStateToBind
  80   * @see DirObjectFactory
  81   * @since 1.3
  82   */
  83 public interface DirStateFactory extends StateFactory {
  84 /**
  85  * Retrieves the state of an object for binding given the object and attributes
  86  * to be transformed.
  87  *<p>
  88  * {@code DirectoryManager.getStateToBind()}
  89  * successively loads in state factories. If a factory implements
  90  * {@code DirStateFactory}, {@code DirectoryManager} invokes this method;
  91  * otherwise, it invokes {@code StateFactory.getStateToBind()}.
  92  * It does this until a factory produces a non-null answer.
  93  *<p>
  94  * When an exception is thrown by a factory,
  95  * the exception is passed on to the caller
  96  * of {@code DirectoryManager.getStateToBind()}. The search for other factories
  97  * that may produce a non-null answer is halted.
  98  * A factory should only throw an exception if it is sure that
  99  * it is the only intended factory and that no other factories
 100  * should be tried.
 101  * If this factory cannot create an object using the arguments supplied,
 102  * it should return null.
 103  * <p>
 104  * The {@code name} and {@code nameCtx} parameters may
 105  * optionally be used to specify the name of the object being created.
 106  * See the description of "Name and Context Parameters" in
 107  * {@link ObjectFactory#getObjectInstance ObjectFactory.getObjectInstance()}
 108  * for details.
 109  * If a factory uses {@code nameCtx} it should synchronize its use
 110  * against concurrent access, since context implementations are not
 111  * guaranteed to be thread-safe.
 112  *<p>
 113  * The {@code name}, {@code inAttrs}, and {@code environment} parameters
 114  * are owned by the caller.
 115  * The implementation will not modify these objects or keep references
 116  * to them, although it may keep references to clones or copies.
 117  * The object returned by this method is owned by the caller.
 118  * The implementation will not subsequently modify it.
 119  * It will contain either a new {@code Attributes} object that is
 120  * likewise owned by the caller, or a reference to the original
 121  * {@code inAttrs} parameter.
 122  *
 123  * @param obj A possibly null object whose state is to be retrieved.
 124  * @param name The name of this object relative to {@code nameCtx},
 125  *              or null if no name is specified.
 126  * @param nameCtx The context relative to which the {@code name}
 127  *              parameter is specified, or null if {@code name} is
 128  *              relative to the default initial context.
 129  * @param environment The possibly null environment to
 130  *              be used in the creation of the object's state.
 131  * @param inAttrs The possibly null attributes to be bound with the object.
 132  *      The factory must not modify {@code inAttrs}.
 133  * @return A {@code Result} containing the object's state for binding
 134  * and the corresponding
 135  * attributes to be bound; null if the object don't use this factory.
 136  * @exception NamingException If this factory encountered an exception
 137  * while attempting to get the object's state, and no other factories are
 138  * to be tried.
 139  *
 140  * @see DirectoryManager#getStateToBind
 141  */
 142     public Result getStateToBind(Object obj, Name name, Context nameCtx,
 143                                  Hashtable<?,?> environment,
 144                                  Attributes inAttrs)
 145         throws NamingException;
 146 
 147 
 148         /**
 149          * An object/attributes pair for returning the result of
 150          * DirStateFactory.getStateToBind().
 151          */
 152     public static class Result {
 153         /**
 154          * The possibly null object to be bound.
 155          */
 156         private Object obj;
 157 
 158 
 159         /**
 160          * The possibly null attributes to be bound.
 161          */
 162         private Attributes attrs;
 163 
 164         /**
 165           * Constructs an instance of Result.
 166           *
 167           * @param obj The possibly null object to be bound.
 168           * @param outAttrs The possibly null attributes to be bound.
 169           */
 170         public Result(Object obj, Attributes outAttrs) {
 171             this.obj = obj;
 172             this.attrs = outAttrs;
 173         }
 174 
 175         /**
 176          * Retrieves the object to be bound.
 177          * @return The possibly null object to be bound.
 178          */
 179         public Object getObject() { return obj; };
 180 
 181         /**
 182          * Retrieves the attributes to be bound.
 183          * @return The possibly null attributes to be bound.
 184          */
 185         public Attributes getAttributes() { return attrs; };
 186 
 187     }
 188 }