Path: blob/master/src/java.naming/share/classes/javax/naming/spi/ObjectFactory.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming.spi;2627import java.util.Hashtable;2829import javax.naming.*;3031/**32* This interface represents a factory for creating an object.33*<p>34* The JNDI framework allows for object implementations to35* be loaded in dynamically via <em>object factories</em>.36* For example, when looking up a printer bound in the name space,37* if the print service binds printer names to References, the printer38* Reference could be used to create a printer object, so that39* the caller of lookup can directly operate on the printer object40* after the lookup.41* <p>An {@code ObjectFactory} is responsible42* for creating objects of a specific type. In the above example,43* you may have a PrinterObjectFactory for creating Printer objects.44*<p>45* An object factory must implement the {@code ObjectFactory} interface.46* In addition, the factory class must be public and must have a47* public constructor that accepts no parameters.48* Note that in cases where the factory is in a named module then it must be49* in a package which is exported by that module to the {@code java.naming}50* module.51*<p>52* The {@code getObjectInstance()} method of an object factory may53* be invoked multiple times, possibly using different parameters.54* The implementation is thread-safe.55*<p>56* The mention of URL in the documentation for this class refers to57* a URL string as defined by RFC 1738 and its related RFCs. It is58* any string that conforms to the syntax described therein, and59* may not always have corresponding support in the java.net.URL60* class or Web browsers.61*62* @author Rosanna Lee63* @author Scott Seligman64*65* @see NamingManager#getObjectInstance66* @see NamingManager#getURLContext67* @see ObjectFactoryBuilder68* @see StateFactory69* @since 1.370*/7172public interface ObjectFactory {73/**74* Creates an object using the location or reference information75* specified.76* <p>77* Special requirements of this object are supplied78* using {@code environment}.79* An example of such an environment property is user identity80* information.81*<p>82* {@code NamingManager.getObjectInstance()}83* successively loads in object factories and invokes this method84* on them until one produces a non-null answer. When an exception85* is thrown by an object factory, the exception is passed on to the caller86* of {@code NamingManager.getObjectInstance()}87* (and no search is made for other factories88* that may produce a non-null answer).89* An object factory should only throw an exception if it is sure that90* it is the only intended factory and that no other object factories91* should be tried.92* If this factory cannot create an object using the arguments supplied,93* it should return null.94*<p>95* A <em>URL context factory</em> is a special ObjectFactory that96* creates contexts for resolving URLs or objects whose locations97* are specified by URLs. The {@code getObjectInstance()} method98* of a URL context factory will obey the following rules.99* <ol>100* <li>If {@code obj} is null, create a context for resolving URLs of the101* scheme associated with this factory. The resulting context is not tied102* to a specific URL: it is able to handle arbitrary URLs with this factory's103* scheme id. For example, invoking {@code getObjectInstance()} with104* {@code obj} set to null on an LDAP URL context factory would return a105* context that can resolve LDAP URLs106* such as "ldap://ldap.wiz.com/o=wiz,c=us" and107* "ldap://ldap.umich.edu/o=umich,c=us".108* <li>109* If {@code obj} is a URL string, create an object (typically a context)110* identified by the URL. For example, suppose this is an LDAP URL context111* factory. If {@code obj} is "ldap://ldap.wiz.com/o=wiz,c=us",112* getObjectInstance() would return the context named by the distinguished113* name "o=wiz, c=us" at the LDAP server ldap.wiz.com. This context can114* then be used to resolve LDAP names (such as "cn=George")115* relative to that context.116* <li>117* If {@code obj} is an array of URL strings, the assumption is that the118* URLs are equivalent in terms of the context to which they refer.119* Verification of whether the URLs are, or need to be, equivalent is up120* to the context factory. The order of the URLs in the array is121* not significant.122* The object returned by getObjectInstance() is like that of the single123* URL case. It is the object named by the URLs.124* <li>125* If {@code obj} is of any other type, the behavior of126* {@code getObjectInstance()} is determined by the context factory127* implementation.128* </ol>129*130* <p>131* The {@code name} and {@code environment} parameters132* are owned by the caller.133* The implementation will not modify these objects or keep references134* to them, although it may keep references to clones or copies.135*136* <p>137* <b>Name and Context Parameters.</b> 138* <a id=NAMECTX></a>139*140* The {@code name} and {@code nameCtx} parameters may141* optionally be used to specify the name of the object being created.142* {@code name} is the name of the object, relative to context143* {@code nameCtx}.144* If there are several possible contexts from which the object145* could be named -- as will often be the case -- it is up to146* the caller to select one. A good rule of thumb is to select the147* "deepest" context available.148* If {@code nameCtx} is null, {@code name} is relative149* to the default initial context. If no name is being specified, the150* {@code name} parameter should be null.151* If a factory uses {@code nameCtx} it should synchronize its use152* against concurrent access, since context implementations are not153* guaranteed to be thread-safe.154*155* @param obj The possibly null object containing location or reference156* information that can be used in creating an object.157* @param name The name of this object relative to {@code nameCtx},158* or null if no name is specified.159* @param nameCtx The context relative to which the {@code name}160* parameter is specified, or null if {@code name} is161* relative to the default initial context.162* @param environment The possibly null environment that is used in163* creating the object.164* @return The object created; null if an object cannot be created.165* @throws Exception if this object factory encountered an exception166* while attempting to create an object, and no other object factories are167* to be tried.168*169* @see NamingManager#getObjectInstance170* @see NamingManager#getURLContext171*/172public Object getObjectInstance(Object obj, Name name, Context nameCtx,173Hashtable<?,?> environment)174throws Exception;175}176177178