1/*
2 * Copyright (c) 1999, 2009, 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
27package javax.naming.directory;
28
29import java.util.Hashtable;
30import javax.naming.spi.NamingManager;
31import javax.naming.*;
32
33/**
34 * This class is the starting context for performing
35 * directory operations. The documentation in the class description
36 * of InitialContext (including those for synchronization) apply here.
37 *
38 *
39 * @author Rosanna Lee
40 * @author Scott Seligman
41 *
42 * @see javax.naming.InitialContext
43 * @since 1.3
44 */
45
46public class InitialDirContext extends InitialContext implements DirContext {
47
48    /**
49     * Constructs an initial DirContext with the option of not
50     * initializing it.  This may be used by a constructor in
51     * a subclass when the value of the environment parameter
52     * is not yet known at the time the {@code InitialDirContext}
53     * constructor is called.  The subclass's constructor will
54     * call this constructor, compute the value of the environment,
55     * and then call {@code init()} before returning.
56     *
57     * @param lazy
58     *          true means do not initialize the initial DirContext; false
59     *          is equivalent to calling {@code new InitialDirContext()}
60     * @throws  NamingException if a naming exception is encountered
61     *
62     * @see InitialContext#init(Hashtable)
63     * @since 1.3
64     */
65    protected InitialDirContext(boolean lazy) throws NamingException {
66        super(lazy);
67    }
68
69    /**
70     * Constructs an initial DirContext.
71     * No environment properties are supplied.
72     * Equivalent to {@code new InitialDirContext(null)}.
73     *
74     * @throws  NamingException if a naming exception is encountered
75     *
76     * @see #InitialDirContext(Hashtable)
77     */
78    public InitialDirContext() throws NamingException {
79        super();
80    }
81
82    /**
83     * Constructs an initial DirContext using the supplied environment.
84     * Environment properties are discussed in the
85     * {@code javax.naming.InitialContext} class description.
86     *
87     * <p> This constructor will not modify {@code environment}
88     * or save a reference to it, but may save a clone.
89     * Caller should not modify mutable keys and values in
90     * {@code environment} after it has been passed to the constructor.
91     *
92     * @param environment
93     *          environment used to create the initial DirContext.
94     *          Null indicates an empty environment.
95     *
96     * @throws  NamingException if a naming exception is encountered
97     */
98    public InitialDirContext(Hashtable<?,?> environment)
99        throws NamingException
100    {
101        super(environment);
102    }
103
104    private DirContext getURLOrDefaultInitDirCtx(String name)
105            throws NamingException {
106        Context answer = getURLOrDefaultInitCtx(name);
107        if (!(answer instanceof DirContext)) {
108            if (answer == null) {
109                throw new NoInitialContextException();
110            } else {
111                throw new NotContextException(
112                    "Not an instance of DirContext");
113            }
114        }
115        return (DirContext)answer;
116    }
117
118    private DirContext getURLOrDefaultInitDirCtx(Name name)
119            throws NamingException {
120        Context answer = getURLOrDefaultInitCtx(name);
121        if (!(answer instanceof DirContext)) {
122            if (answer == null) {
123                throw new NoInitialContextException();
124            } else {
125                throw new NotContextException(
126                    "Not an instance of DirContext");
127            }
128        }
129        return (DirContext)answer;
130    }
131
132// DirContext methods
133// Most Javadoc is deferred to the DirContext interface.
134
135    public Attributes getAttributes(String name)
136            throws NamingException {
137        return getAttributes(name, null);
138    }
139
140    public Attributes getAttributes(String name, String[] attrIds)
141            throws NamingException {
142        return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);
143    }
144
145    public Attributes getAttributes(Name name)
146            throws NamingException {
147        return getAttributes(name, null);
148    }
149
150    public Attributes getAttributes(Name name, String[] attrIds)
151            throws NamingException {
152        return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);
153    }
154
155    public void modifyAttributes(String name, int mod_op, Attributes attrs)
156            throws NamingException {
157        getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);
158    }
159
160    public void modifyAttributes(Name name, int mod_op, Attributes attrs)
161            throws NamingException  {
162        getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);
163    }
164
165    public void modifyAttributes(String name, ModificationItem[] mods)
166            throws NamingException  {
167        getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);
168    }
169
170    public void modifyAttributes(Name name, ModificationItem[] mods)
171            throws NamingException  {
172        getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);
173    }
174
175    public void bind(String name, Object obj, Attributes attrs)
176            throws NamingException  {
177        getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);
178    }
179
180    public void bind(Name name, Object obj, Attributes attrs)
181            throws NamingException  {
182        getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);
183    }
184
185    public void rebind(String name, Object obj, Attributes attrs)
186            throws NamingException  {
187        getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);
188    }
189
190    public void rebind(Name name, Object obj, Attributes attrs)
191            throws NamingException  {
192        getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);
193    }
194
195    public DirContext createSubcontext(String name, Attributes attrs)
196            throws NamingException  {
197        return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);
198    }
199
200    public DirContext createSubcontext(Name name, Attributes attrs)
201            throws NamingException  {
202        return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);
203    }
204
205    public DirContext getSchema(String name) throws NamingException {
206        return getURLOrDefaultInitDirCtx(name).getSchema(name);
207    }
208
209    public DirContext getSchema(Name name) throws NamingException {
210        return getURLOrDefaultInitDirCtx(name).getSchema(name);
211    }
212
213    public DirContext getSchemaClassDefinition(String name)
214            throws NamingException {
215        return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);
216    }
217
218    public DirContext getSchemaClassDefinition(Name name)
219            throws NamingException {
220        return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);
221    }
222
223// -------------------- search operations
224
225    public NamingEnumeration<SearchResult>
226        search(String name, Attributes matchingAttributes)
227        throws NamingException
228    {
229        return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);
230    }
231
232    public NamingEnumeration<SearchResult>
233        search(Name name, Attributes matchingAttributes)
234        throws NamingException
235    {
236        return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);
237    }
238
239    public NamingEnumeration<SearchResult>
240        search(String name,
241               Attributes matchingAttributes,
242               String[] attributesToReturn)
243        throws NamingException
244    {
245        return getURLOrDefaultInitDirCtx(name).search(name,
246                                                      matchingAttributes,
247                                                      attributesToReturn);
248    }
249
250    public NamingEnumeration<SearchResult>
251        search(Name name,
252               Attributes matchingAttributes,
253               String[] attributesToReturn)
254        throws NamingException
255    {
256        return getURLOrDefaultInitDirCtx(name).search(name,
257                                            matchingAttributes,
258                                            attributesToReturn);
259    }
260
261    public NamingEnumeration<SearchResult>
262        search(String name,
263               String filter,
264               SearchControls cons)
265        throws NamingException
266    {
267        return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);
268    }
269
270    public NamingEnumeration<SearchResult>
271        search(Name name,
272               String filter,
273               SearchControls cons)
274        throws NamingException
275    {
276        return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);
277    }
278
279    public NamingEnumeration<SearchResult>
280        search(String name,
281               String filterExpr,
282               Object[] filterArgs,
283               SearchControls cons)
284        throws NamingException
285    {
286        return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
287                                                      filterArgs, cons);
288    }
289
290    public NamingEnumeration<SearchResult>
291        search(Name name,
292               String filterExpr,
293               Object[] filterArgs,
294               SearchControls cons)
295        throws NamingException
296    {
297        return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
298                                                      filterArgs, cons);
299    }
300}
301