001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.naming.directory;
019:
020: import java.util.Hashtable;
021:
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.Name;
025: import javax.naming.NamingEnumeration;
026: import javax.naming.NamingException;
027: import javax.naming.NoInitialContextException;
028: import javax.naming.NotContextException;
029: import javax.naming.directory.SearchResult;
030:
031: import org.apache.harmony.jndi.internal.nls.Messages;
032:
033: /**
034: * This is the root context for directory service operations.
035: *
036: * <p>
037: * The <code>InitialDirContext</code> behavior is defined by the specification
038: * for <code>javax.naming.InitialContext</code>.
039: * </p>
040: */
041: public class InitialDirContext extends InitialContext implements
042: DirContext {
043:
044: /**
045: * Constructs a new <code>InitialDirContext</code> with no environment
046: * properties.
047: *
048: * @throws NamingException
049: * If failed to a construct new instance.
050: */
051: public InitialDirContext() throws NamingException {
052: super ();
053: }
054:
055: /**
056: * Constructs a new <code>InitialDirContext</code> instance with no
057: * environment properties. A mechanism for subclass constructors to
058: * construct a new <code>InitialDirContext</code> instance before all
059: * environment parameters are known.
060: *
061: * @param flag
062: * If flag is true, the new instance is created but not
063: * initialized. In this case the subclass constructor is expected
064: * to call <code>init</code> after the environment parameters
065: * are known. If flag is false, a new instance is created and
066: * initialized with no environment parameters.
067: * @throws NamingException
068: * If failed to construct new instance.
069: */
070: protected InitialDirContext(boolean flag) throws NamingException {
071: super (flag);
072: }
073:
074: /**
075: * Constructs a new <code>InitialDirContext</code> instance with
076: * environment properties.
077: *
078: * @param hashtable
079: * Contains the environment parameters. This constructor will not
080: * change the hashtable or keep a reference to it. The hashtable
081: * parameter may be null.
082: * @throws NamingException
083: * If failed to construct a new instance.
084: * @see InitialContext
085: */
086: public InitialDirContext(Hashtable<?, ?> hashtable)
087: throws NamingException {
088: super (hashtable);
089: }
090:
091: private DirContext getURLOrDefaultInitDirCtx(Name name)
092: throws NamingException {
093: return castToDirContext(super .getURLOrDefaultInitCtx(name));
094: }
095:
096: /*
097: * Try to cast the default context to DirContext.
098: */
099: private DirContext castToDirContext(Context ctx)
100: throws NoInitialContextException, NotContextException {
101: if (ctx instanceof DirContext) {
102: return (DirContext) ctx;
103: } else if (null == ctx) {
104: // jndi.1A=Cannot create initial context.
105: throw new NoInitialContextException(Messages
106: .getString("jndi.1A")); //$NON-NLS-1$
107: } else {
108: // jndi.1B=DirContext object is required.
109: throw new NotContextException(Messages.getString("jndi.1B")); //$NON-NLS-1$
110: }
111: }
112:
113: private DirContext getURLOrDefaultInitDirCtx(String name)
114: throws NamingException {
115: return castToDirContext(super .getURLOrDefaultInitCtx(name));
116: }
117:
118: public void bind(Name name, Object obj, Attributes attributes)
119: throws NamingException {
120: getURLOrDefaultInitDirCtx(name).bind(name, obj, attributes);
121: }
122:
123: public void bind(String s, Object obj, Attributes attributes)
124: throws NamingException {
125: getURLOrDefaultInitDirCtx(s).bind(s, obj, attributes);
126: }
127:
128: public DirContext createSubcontext(Name name, Attributes attributes)
129: throws NamingException {
130: return getURLOrDefaultInitDirCtx(name).createSubcontext(name,
131: attributes);
132: }
133:
134: public DirContext createSubcontext(String s, Attributes attributes)
135: throws NamingException {
136: return getURLOrDefaultInitDirCtx(s).createSubcontext(s,
137: attributes);
138: }
139:
140: public Attributes getAttributes(Name name) throws NamingException {
141: return getURLOrDefaultInitDirCtx(name).getAttributes(name);
142: }
143:
144: public Attributes getAttributes(Name name, String[] as)
145: throws NamingException {
146: return getURLOrDefaultInitDirCtx(name).getAttributes(name, as);
147: }
148:
149: public Attributes getAttributes(String s) throws NamingException {
150: return getURLOrDefaultInitDirCtx(s).getAttributes(s);
151: }
152:
153: public Attributes getAttributes(String s, String[] as)
154: throws NamingException {
155: return getURLOrDefaultInitDirCtx(s).getAttributes(s, as);
156: }
157:
158: public DirContext getSchema(Name name) throws NamingException {
159: return getURLOrDefaultInitDirCtx(name).getSchema(name);
160: }
161:
162: public DirContext getSchema(String s) throws NamingException {
163: return getURLOrDefaultInitDirCtx(s).getSchema(s);
164: }
165:
166: public DirContext getSchemaClassDefinition(Name name)
167: throws NamingException {
168: return getURLOrDefaultInitDirCtx(name)
169: .getSchemaClassDefinition(name);
170: }
171:
172: public DirContext getSchemaClassDefinition(String s)
173: throws NamingException {
174: return getURLOrDefaultInitDirCtx(s).getSchemaClassDefinition(s);
175: }
176:
177: public void modifyAttributes(Name name, int i, Attributes attributes)
178: throws NamingException {
179: getURLOrDefaultInitDirCtx(name).modifyAttributes(name, i,
180: attributes);
181: }
182:
183: public void modifyAttributes(Name name,
184: ModificationItem[] modificationItems)
185: throws NamingException {
186: getURLOrDefaultInitDirCtx(name).modifyAttributes(name,
187: modificationItems);
188: }
189:
190: public void modifyAttributes(String s, int i, Attributes attributes)
191: throws NamingException {
192: getURLOrDefaultInitDirCtx(s).modifyAttributes(s, i, attributes);
193: }
194:
195: public void modifyAttributes(String s,
196: ModificationItem[] modificationItems)
197: throws NamingException {
198: getURLOrDefaultInitDirCtx(s).modifyAttributes(s,
199: modificationItems);
200:
201: }
202:
203: public void rebind(Name name, Object obj, Attributes attributes)
204: throws NamingException {
205: getURLOrDefaultInitDirCtx(name).rebind(name, obj, attributes);
206: }
207:
208: public void rebind(String s, Object obj, Attributes attributes)
209: throws NamingException {
210: getURLOrDefaultInitDirCtx(s).rebind(s, obj, attributes);
211: }
212:
213: public NamingEnumeration<SearchResult> search(Name name,
214: Attributes attributes) throws NamingException {
215: return getURLOrDefaultInitDirCtx(name).search(name, attributes);
216: }
217:
218: public NamingEnumeration<SearchResult> search(Name name,
219: Attributes attributes, String[] as) throws NamingException {
220: return getURLOrDefaultInitDirCtx(name).search(name, attributes,
221: as);
222: }
223:
224: public NamingEnumeration<SearchResult> search(Name name,
225: String filter, Object[] objs, SearchControls searchControls)
226: throws NamingException {
227: return getURLOrDefaultInitDirCtx(name).search(name, filter,
228: objs, searchControls);
229: }
230:
231: public NamingEnumeration<SearchResult> search(Name name,
232: String filter, SearchControls searchcontrols)
233: throws NamingException {
234: return getURLOrDefaultInitDirCtx(name).search(name, filter,
235: searchcontrols);
236: }
237:
238: public NamingEnumeration<SearchResult> search(String name,
239: Attributes attributes) throws NamingException {
240: return getURLOrDefaultInitDirCtx(name).search(name, attributes);
241: }
242:
243: public NamingEnumeration<SearchResult> search(String name,
244: Attributes attributes, String[] as) throws NamingException {
245: return getURLOrDefaultInitDirCtx(name).search(name, attributes,
246: as);
247: }
248:
249: public NamingEnumeration<SearchResult> search(String name,
250: String filter, Object[] objs, SearchControls searchControls)
251: throws NamingException {
252: return getURLOrDefaultInitDirCtx(name).search(name, filter,
253: objs, searchControls);
254: }
255:
256: public NamingEnumeration<SearchResult> search(String name,
257: String filter, SearchControls searchControls)
258: throws NamingException {
259: return getURLOrDefaultInitDirCtx(name).search(name, filter,
260: searchControls);
261: }
262:
263: }
|