1/* 2 * Copyright (c) 1999, 2017, 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 26package javax.naming.spi; 27 28import java.util.Hashtable; 29 30import javax.naming.*; 31 32/** 33 * This interface represents a factory for creating an object. 34 *<p> 35 * The JNDI framework allows for object implementations to 36 * be loaded in dynamically via <em>object factories</em>. 37 * For example, when looking up a printer bound in the name space, 38 * if the print service binds printer names to References, the printer 39 * Reference could be used to create a printer object, so that 40 * the caller of lookup can directly operate on the printer object 41 * after the lookup. 42 * <p>An {@code ObjectFactory} is responsible 43 * for creating objects of a specific type. In the above example, 44 * you may have a PrinterObjectFactory for creating Printer objects. 45 *<p> 46 * An object factory must implement the {@code ObjectFactory} interface. 47 * In addition, the factory class must be public and must have a 48 * public constructor that accepts no parameters. 49 * Note that in cases where the factory is in a named module then it must be 50 * in a package which is exported by that module to the {@code java.naming} 51 * module. 52 *<p> 53 * The {@code getObjectInstance()} method of an object factory may 54 * be invoked multiple times, possibly using different parameters. 55 * The implementation is thread-safe. 56 *<p> 57 * The mention of URL in the documentation for this class refers to 58 * a URL string as defined by RFC 1738 and its related RFCs. It is 59 * any string that conforms to the syntax described therein, and 60 * may not always have corresponding support in the java.net.URL 61 * class or Web browsers. 62 * 63 * @author Rosanna Lee 64 * @author Scott Seligman 65 * 66 * @see NamingManager#getObjectInstance 67 * @see NamingManager#getURLContext 68 * @see ObjectFactoryBuilder 69 * @see StateFactory 70 * @since 1.3 71 */ 72 73public interface ObjectFactory { 74/** 75 * Creates an object using the location or reference information 76 * specified. 77 * <p> 78 * Special requirements of this object are supplied 79 * using {@code environment}. 80 * An example of such an environment property is user identity 81 * information. 82 *<p> 83 * {@code NamingManager.getObjectInstance()} 84 * successively loads in object factories and invokes this method 85 * on them until one produces a non-null answer. When an exception 86 * is thrown by an object factory, the exception is passed on to the caller 87 * of {@code NamingManager.getObjectInstance()} 88 * (and no search is made for other factories 89 * that may produce a non-null answer). 90 * An object factory should only throw an exception if it is sure that 91 * it is the only intended factory and that no other object factories 92 * should be tried. 93 * If this factory cannot create an object using the arguments supplied, 94 * it should return null. 95 *<p> 96 * A <em>URL context factory</em> is a special ObjectFactory that 97 * creates contexts for resolving URLs or objects whose locations 98 * are specified by URLs. The {@code getObjectInstance()} method 99 * of a URL context factory will obey the following rules. 100 * <ol> 101 * <li>If {@code obj} is null, create a context for resolving URLs of the 102 * scheme associated with this factory. The resulting context is not tied 103 * to a specific URL: it is able to handle arbitrary URLs with this factory's 104 * scheme id. For example, invoking {@code getObjectInstance()} with 105 * {@code obj} set to null on an LDAP URL context factory would return a 106 * context that can resolve LDAP URLs 107 * such as "ldap://ldap.wiz.com/o=wiz,c=us" and 108 * "ldap://ldap.umich.edu/o=umich,c=us". 109 * <li> 110 * If {@code obj} is a URL string, create an object (typically a context) 111 * identified by the URL. For example, suppose this is an LDAP URL context 112 * factory. If {@code obj} is "ldap://ldap.wiz.com/o=wiz,c=us", 113 * getObjectInstance() would return the context named by the distinguished 114 * name "o=wiz, c=us" at the LDAP server ldap.wiz.com. This context can 115 * then be used to resolve LDAP names (such as "cn=George") 116 * relative to that context. 117 * <li> 118 * If {@code obj} is an array of URL strings, the assumption is that the 119 * URLs are equivalent in terms of the context to which they refer. 120 * Verification of whether the URLs are, or need to be, equivalent is up 121 * to the context factory. The order of the URLs in the array is 122 * not significant. 123 * The object returned by getObjectInstance() is like that of the single 124 * URL case. It is the object named by the URLs. 125 * <li> 126 * If {@code obj} is of any other type, the behavior of 127 * {@code getObjectInstance()} is determined by the context factory 128 * implementation. 129 * </ol> 130 * 131 * <p> 132 * The {@code name} and {@code environment} parameters 133 * are owned by the caller. 134 * The implementation will not modify these objects or keep references 135 * to them, although it may keep references to clones or copies. 136 * 137 * <p> 138 * <b>Name and Context Parameters.</b> 139 * <a id=NAMECTX></a> 140 * 141 * The {@code name} and {@code nameCtx} parameters may 142 * optionally be used to specify the name of the object being created. 143 * {@code name} is the name of the object, relative to context 144 * {@code nameCtx}. 145 * If there are several possible contexts from which the object 146 * could be named -- as will often be the case -- it is up to 147 * the caller to select one. A good rule of thumb is to select the 148 * "deepest" context available. 149 * If {@code nameCtx} is null, {@code name} is relative 150 * to the default initial context. If no name is being specified, the 151 * {@code name} parameter should be null. 152 * If a factory uses {@code nameCtx} it should synchronize its use 153 * against concurrent access, since context implementations are not 154 * guaranteed to be thread-safe. 155 * 156 * @param obj The possibly null object containing location or reference 157 * information that can be used in creating an object. 158 * @param name The name of this object relative to {@code nameCtx}, 159 * or null if no name is specified. 160 * @param nameCtx The context relative to which the {@code name} 161 * parameter is specified, or null if {@code name} is 162 * relative to the default initial context. 163 * @param environment The possibly null environment that is used in 164 * creating the object. 165 * @return The object created; null if an object cannot be created. 166 * @exception Exception if this object factory encountered an exception 167 * while attempting to create an object, and no other object factories are 168 * to be tried. 169 * 170 * @see NamingManager#getObjectInstance 171 * @see NamingManager#getURLContext 172 */ 173 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 174 Hashtable<?,?> environment) 175 throws Exception; 176} 177