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.ldap;
019:
020: import java.util.Hashtable;
021: import javax.naming.directory.InitialDirContext;
022: import javax.naming.NamingException;
023: import javax.naming.NotContextException;
024:
025: import org.apache.harmony.jndi.internal.nls.Messages;
026:
027: /**
028: * This is used as the starting context when the LDAPv3 extended functionality
029: * provided by the <code>javax.naming.ldap</code> package is required.
030: *
031: * See <code>LdapContext</code> for a description of the Request and Response
032: * controls.
033: *
034: * @see LdapContext
035: */
036: public class InitialLdapContext extends InitialDirContext implements
037: LdapContext {
038:
039: /*
040: * This is set to the environment property java.naming.ldap.version.
041: */
042: private static final String LDAP_VERSION = "java.naming.ldap.version"; //$NON-NLS-1$
043:
044: /*
045: * This is set to the environment property java.naming.ldap.control.connect.
046: */
047: private static final String CONNECT_CONTROL = "java.naming.ldap.control.connect"; //$NON-NLS-1$
048:
049: /*
050: * The version of this LDAP context implementation.
051: */
052: private static final String THIS_LDAP_VERSION = "3"; //$NON-NLS-1$
053:
054: /**
055: * Constructs an <code>InitialLdapContext</code> instance without using
056: * any environment properties or connection controls.
057: *
058: * @throws NamingException
059: * If an error is encountered.
060: */
061: public InitialLdapContext() throws NamingException {
062: this (null, null);
063: }
064:
065: /**
066: * Constructs an <code>InitialLdapContext</code> instance using the
067: * supplied environment properties and connection controls.
068: *
069: * @param h
070: * the environment properties which may be null
071: * @param cs
072: * the connection controls which may be null
073: * @throws NamingException
074: * If an error is encountered.
075: */
076: @SuppressWarnings("unchecked")
077: public InitialLdapContext(Hashtable<?, ?> h, Control[] cs)
078: throws NamingException {
079: super (true);
080:
081: /*
082: * Prepare the environment properties to be inherited by the service
083: * provider.
084: */
085: Hashtable<Object, Object> newEnvironment = null;
086: if (null == h) {
087: newEnvironment = new Hashtable<Object, Object>();
088: } else {
089: newEnvironment = (Hashtable<Object, Object>) h.clone();
090: }
091:
092: // Set the environment property java.naming.ldap.control.connect
093: if (null != cs) {
094: Control[] cloneOfCs = new Control[cs.length];
095: System.arraycopy(cs, 0, cloneOfCs, 0, cs.length);
096: newEnvironment.put(CONNECT_CONTROL, cloneOfCs);
097: }
098:
099: // Set the environment property java.naming.ldap.version to be 3
100: newEnvironment.put(LDAP_VERSION, THIS_LDAP_VERSION);
101:
102: // Initialize the initial context
103: super .init(newEnvironment);
104: }
105:
106: /*
107: * Gets the default initial context and verify that it's an instance of
108: * LdapContext.
109: */
110: private LdapContext getDefaultInitLdapContext()
111: throws NamingException {
112: if (!(super .defaultInitCtx instanceof LdapContext)) {
113: // jndi.1D=Expected an LdapContext object.
114: throw new NotContextException(Messages.getString("jndi.1D")); //$NON-NLS-1$
115: }
116: return (LdapContext) super .defaultInitCtx;
117: }
118:
119: public ExtendedResponse extendedOperation(ExtendedRequest e)
120: throws NamingException {
121: return getDefaultInitLdapContext().extendedOperation(e);
122: }
123:
124: public LdapContext newInstance(Control[] ac) throws NamingException {
125: return getDefaultInitLdapContext().newInstance(ac);
126: }
127:
128: public void reconnect(Control[] ac) throws NamingException {
129: getDefaultInitLdapContext().reconnect(ac);
130: }
131:
132: public Control[] getConnectControls() throws NamingException {
133: return getDefaultInitLdapContext().getConnectControls();
134: }
135:
136: public void setRequestControls(Control[] ac) throws NamingException {
137: getDefaultInitLdapContext().setRequestControls(ac);
138: }
139:
140: public Control[] getRequestControls() throws NamingException {
141: return getDefaultInitLdapContext().getRequestControls();
142: }
143:
144: public Control[] getResponseControls() throws NamingException {
145: return getDefaultInitLdapContext().getResponseControls();
146: }
147:
148: }
|