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