001 /*
002 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.naming.directory;
027
028 import java.util.Hashtable;
029 import javax.naming.spi.NamingManager;
030 import javax.naming.*;
031
032 /**
033 * This class is the starting context for performing
034 * directory operations. The documentation in the class description
035 * of InitialContext (including those for synchronization) apply here.
036 *
037 *
038 * @author Rosanna Lee
039 * @author Scott Seligman
040 * @version 1.18 07/05/05
041 *
042 * @see javax.naming.InitialContext
043 * @since 1.3
044 */
045
046 public class InitialDirContext extends InitialContext implements
047 DirContext {
048
049 /**
050 * Constructs an initial DirContext with the option of not
051 * initializing it. This may be used by a constructor in
052 * a subclass when the value of the environment parameter
053 * is not yet known at the time the <tt>InitialDirContext</tt>
054 * constructor is called. The subclass's constructor will
055 * call this constructor, compute the value of the environment,
056 * and then call <tt>init()</tt> before returning.
057 *
058 * @param lazy
059 * true means do not initialize the initial DirContext; false
060 * is equivalent to calling <tt>new InitialDirContext()</tt>
061 * @throws NamingException if a naming exception is encountered
062 *
063 * @see InitialContext#init(Hashtable)
064 * @since 1.3
065 */
066 protected InitialDirContext(boolean lazy) throws NamingException {
067 super (lazy);
068 }
069
070 /**
071 * Constructs an initial DirContext.
072 * No environment properties are supplied.
073 * Equivalent to <tt>new InitialDirContext(null)</tt>.
074 *
075 * @throws NamingException if a naming exception is encountered
076 *
077 * @see #InitialDirContext(Hashtable)
078 */
079 public InitialDirContext() throws NamingException {
080 super ();
081 }
082
083 /**
084 * Constructs an initial DirContext using the supplied environment.
085 * Environment properties are discussed in the
086 * <tt>javax.naming.InitialContext</tt> class description.
087 *
088 * <p> This constructor will not modify <tt>environment</tt>
089 * or save a reference to it, but may save a clone.
090 *
091 * @param environment
092 * environment used to create the initial DirContext.
093 * Null indicates an empty environment.
094 *
095 * @throws NamingException if a naming exception is encountered
096 */
097 public InitialDirContext(Hashtable<?, ?> environment)
098 throws NamingException {
099 super (environment);
100 }
101
102 private DirContext getURLOrDefaultInitDirCtx(String name)
103 throws NamingException {
104 Context answer = getURLOrDefaultInitCtx(name);
105 if (!(answer instanceof DirContext)) {
106 if (answer == null) {
107 throw new NoInitialContextException();
108 } else {
109 throw new NotContextException(
110 "Not an instance of DirContext");
111 }
112 }
113 return (DirContext) answer;
114 }
115
116 private DirContext getURLOrDefaultInitDirCtx(Name name)
117 throws NamingException {
118 Context answer = getURLOrDefaultInitCtx(name);
119 if (!(answer instanceof DirContext)) {
120 if (answer == null) {
121 throw new NoInitialContextException();
122 } else {
123 throw new NotContextException(
124 "Not an instance of DirContext");
125 }
126 }
127 return (DirContext) answer;
128 }
129
130 // DirContext methods
131 // Most Javadoc is deferred to the DirContext interface.
132
133 public Attributes getAttributes(String name) throws NamingException {
134 return getAttributes(name, null);
135 }
136
137 public Attributes getAttributes(String name, String[] attrIds)
138 throws NamingException {
139 return getURLOrDefaultInitDirCtx(name).getAttributes(name,
140 attrIds);
141 }
142
143 public Attributes getAttributes(Name name) throws NamingException {
144 return getAttributes(name, null);
145 }
146
147 public Attributes getAttributes(Name name, String[] attrIds)
148 throws NamingException {
149 return getURLOrDefaultInitDirCtx(name).getAttributes(name,
150 attrIds);
151 }
152
153 public void modifyAttributes(String name, int mod_op,
154 Attributes attrs) throws NamingException {
155 getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op,
156 attrs);
157 }
158
159 public void modifyAttributes(Name name, int mod_op, Attributes attrs)
160 throws NamingException {
161 getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op,
162 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,
198 attrs);
199 }
200
201 public DirContext createSubcontext(Name name, Attributes attrs)
202 throws NamingException {
203 return getURLOrDefaultInitDirCtx(name).createSubcontext(name,
204 attrs);
205 }
206
207 public DirContext getSchema(String name) throws NamingException {
208 return getURLOrDefaultInitDirCtx(name).getSchema(name);
209 }
210
211 public DirContext getSchema(Name name) throws NamingException {
212 return getURLOrDefaultInitDirCtx(name).getSchema(name);
213 }
214
215 public DirContext getSchemaClassDefinition(String name)
216 throws NamingException {
217 return getURLOrDefaultInitDirCtx(name)
218 .getSchemaClassDefinition(name);
219 }
220
221 public DirContext getSchemaClassDefinition(Name name)
222 throws NamingException {
223 return getURLOrDefaultInitDirCtx(name)
224 .getSchemaClassDefinition(name);
225 }
226
227 // -------------------- search operations
228
229 public NamingEnumeration<SearchResult> search(String name,
230 Attributes matchingAttributes) throws NamingException {
231 return getURLOrDefaultInitDirCtx(name).search(name,
232 matchingAttributes);
233 }
234
235 public NamingEnumeration<SearchResult> search(Name name,
236 Attributes matchingAttributes) throws NamingException {
237 return getURLOrDefaultInitDirCtx(name).search(name,
238 matchingAttributes);
239 }
240
241 public NamingEnumeration<SearchResult> search(String name,
242 Attributes matchingAttributes, String[] attributesToReturn)
243 throws NamingException {
244 return getURLOrDefaultInitDirCtx(name).search(name,
245 matchingAttributes, attributesToReturn);
246 }
247
248 public NamingEnumeration<SearchResult> search(Name name,
249 Attributes matchingAttributes, String[] attributesToReturn)
250 throws NamingException {
251 return getURLOrDefaultInitDirCtx(name).search(name,
252 matchingAttributes, attributesToReturn);
253 }
254
255 public NamingEnumeration<SearchResult> search(String name,
256 String filter, SearchControls cons) throws NamingException {
257 return getURLOrDefaultInitDirCtx(name).search(name, filter,
258 cons);
259 }
260
261 public NamingEnumeration<SearchResult> search(Name name,
262 String filter, SearchControls cons) throws NamingException {
263 return getURLOrDefaultInitDirCtx(name).search(name, filter,
264 cons);
265 }
266
267 public NamingEnumeration<SearchResult> search(String name,
268 String filterExpr, Object[] filterArgs, SearchControls cons)
269 throws NamingException {
270 return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
271 filterArgs, cons);
272 }
273
274 public NamingEnumeration<SearchResult> search(Name name,
275 String filterExpr, Object[] filterArgs, SearchControls cons)
276 throws NamingException {
277 return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
278 filterArgs, cons);
279 }
280 }
|