Path: blob/master/src/java.naming/share/classes/javax/naming/directory/InitialDirContext.java
41159 views
/*1* Copyright (c) 1999, 2009, 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*/242526package javax.naming.directory;2728import java.util.Hashtable;29import javax.naming.*;3031/**32* This class is the starting context for performing33* directory operations. The documentation in the class description34* of InitialContext (including those for synchronization) apply here.35*36*37* @author Rosanna Lee38* @author Scott Seligman39*40* @see javax.naming.InitialContext41* @since 1.342*/4344public class InitialDirContext extends InitialContext implements DirContext {4546/**47* Constructs an initial DirContext with the option of not48* initializing it. This may be used by a constructor in49* a subclass when the value of the environment parameter50* is not yet known at the time the {@code InitialDirContext}51* constructor is called. The subclass's constructor will52* call this constructor, compute the value of the environment,53* and then call {@code init()} before returning.54*55* @param lazy56* true means do not initialize the initial DirContext; false57* is equivalent to calling {@code new InitialDirContext()}58* @throws NamingException if a naming exception is encountered59*60* @see InitialContext#init(Hashtable)61* @since 1.362*/63protected InitialDirContext(boolean lazy) throws NamingException {64super(lazy);65}6667/**68* Constructs an initial DirContext.69* No environment properties are supplied.70* Equivalent to {@code new InitialDirContext(null)}.71*72* @throws NamingException if a naming exception is encountered73*74* @see #InitialDirContext(Hashtable)75*/76public InitialDirContext() throws NamingException {77super();78}7980/**81* Constructs an initial DirContext using the supplied environment.82* Environment properties are discussed in the83* {@code javax.naming.InitialContext} class description.84*85* <p> If the {@code java.naming.provider.url} property of the supplied86* environment consists of a URL (or a list of URLs) using the ldap87* protocol the resulting {@link javax.naming.ldap.LdapContext} will use88* an LDAP server resolved by the configured {@link89* javax.naming.ldap.spi.LdapDnsProvider LdapDnsProviders}:90* <ol>91* <li>If this is the first {@code InitialDirContext} created with a92* {@code java.naming.provider.url} using the ldap protocol then the93* {@linkplain java.util.ServiceLoader ServiceLoader} mechanism is94* used to locate {@linkplain javax.naming.ldap.spi.LdapDnsProvider95* LdapDnsProvider} implementations using the system class loader.96* The order that providers are located is implementation specific97* and an implementation is free to cache the located providers.98* <li>The {@code lookupEndpoints} method of each provider, if instantiated,99* is invoked once with a combination of each of the URLs in the the100* {@code java.naming.provider.url} property and the environment until101* a provider returns non-empty or all providers have been exhausted.102* If none of the103* {@linkplain javax.naming.ldap.spi.LdapDnsProvider LdapDnsProviders}104* return a non-empty105* {@linkplain javax.naming.ldap.spi.LdapDnsProviderResult result} then106* the implementation will make a best-effort attempt to determine an107* endpoint. A108* {@linkplain java.util.ServiceConfigurationError ServiceConfigurationError},109* {@code Error} or {@code RuntimeException} thrown when loading or110* calling an {@linkplain javax.naming.ldap.spi.LdapDnsProvider111* LdapDnsProvider}, if encountered, will be propagated to the calling112* thread.113* </ol>114*115* <p> This constructor will not modify {@code environment}116* or save a reference to it, but may save a clone.117* Caller should not modify mutable keys and values in118* {@code environment} after it has been passed to the constructor.119*120* @param environment121* environment used to create the initial DirContext.122* Null indicates an empty environment.123*124* @throws NamingException if a naming exception is encountered125*/126public InitialDirContext(Hashtable<?,?> environment)127throws NamingException128{129super(environment);130}131132private DirContext getURLOrDefaultInitDirCtx(String name)133throws NamingException {134Context answer = getURLOrDefaultInitCtx(name);135if (!(answer instanceof DirContext)) {136if (answer == null) {137throw new NoInitialContextException();138} else {139throw new NotContextException(140"Not an instance of DirContext");141}142}143return (DirContext)answer;144}145146private DirContext getURLOrDefaultInitDirCtx(Name name)147throws NamingException {148Context answer = getURLOrDefaultInitCtx(name);149if (!(answer instanceof DirContext)) {150if (answer == null) {151throw new NoInitialContextException();152} else {153throw new NotContextException(154"Not an instance of DirContext");155}156}157return (DirContext)answer;158}159160// DirContext methods161// Most Javadoc is deferred to the DirContext interface.162163public Attributes getAttributes(String name)164throws NamingException {165return getAttributes(name, null);166}167168public Attributes getAttributes(String name, String[] attrIds)169throws NamingException {170return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);171}172173public Attributes getAttributes(Name name)174throws NamingException {175return getAttributes(name, null);176}177178public Attributes getAttributes(Name name, String[] attrIds)179throws NamingException {180return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);181}182183public void modifyAttributes(String name, int mod_op, Attributes attrs)184throws NamingException {185getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);186}187188public void modifyAttributes(Name name, int mod_op, Attributes attrs)189throws NamingException {190getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);191}192193public void modifyAttributes(String name, ModificationItem[] mods)194throws NamingException {195getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);196}197198public void modifyAttributes(Name name, ModificationItem[] mods)199throws NamingException {200getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);201}202203public void bind(String name, Object obj, Attributes attrs)204throws NamingException {205getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);206}207208public void bind(Name name, Object obj, Attributes attrs)209throws NamingException {210getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);211}212213public void rebind(String name, Object obj, Attributes attrs)214throws NamingException {215getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);216}217218public void rebind(Name name, Object obj, Attributes attrs)219throws NamingException {220getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);221}222223public DirContext createSubcontext(String name, Attributes attrs)224throws NamingException {225return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);226}227228public DirContext createSubcontext(Name name, Attributes attrs)229throws NamingException {230return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);231}232233public DirContext getSchema(String name) throws NamingException {234return getURLOrDefaultInitDirCtx(name).getSchema(name);235}236237public DirContext getSchema(Name name) throws NamingException {238return getURLOrDefaultInitDirCtx(name).getSchema(name);239}240241public DirContext getSchemaClassDefinition(String name)242throws NamingException {243return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);244}245246public DirContext getSchemaClassDefinition(Name name)247throws NamingException {248return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);249}250251// -------------------- search operations252253public NamingEnumeration<SearchResult>254search(String name, Attributes matchingAttributes)255throws NamingException256{257return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);258}259260public NamingEnumeration<SearchResult>261search(Name name, Attributes matchingAttributes)262throws NamingException263{264return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);265}266267public NamingEnumeration<SearchResult>268search(String name,269Attributes matchingAttributes,270String[] attributesToReturn)271throws NamingException272{273return getURLOrDefaultInitDirCtx(name).search(name,274matchingAttributes,275attributesToReturn);276}277278public NamingEnumeration<SearchResult>279search(Name name,280Attributes matchingAttributes,281String[] attributesToReturn)282throws NamingException283{284return getURLOrDefaultInitDirCtx(name).search(name,285matchingAttributes,286attributesToReturn);287}288289public NamingEnumeration<SearchResult>290search(String name,291String filter,292SearchControls cons)293throws NamingException294{295return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);296}297298public NamingEnumeration<SearchResult>299search(Name name,300String filter,301SearchControls cons)302throws NamingException303{304return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);305}306307public NamingEnumeration<SearchResult>308search(String name,309String filterExpr,310Object[] filterArgs,311SearchControls cons)312throws NamingException313{314return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,315filterArgs, cons);316}317318public NamingEnumeration<SearchResult>319search(Name name,320String filterExpr,321Object[] filterArgs,322SearchControls cons)323throws NamingException324{325return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,326filterArgs, cons);327}328}329330331