01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.jndi.provider.ldap;
19:
20: import java.util.Hashtable;
21:
22: import javax.naming.ConfigurationException;
23: import javax.naming.Context;
24: import javax.naming.InvalidNameException;
25: import javax.naming.NamingException;
26: import javax.naming.spi.InitialContextFactory;
27:
28: import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
29:
30: public class LdapContextFactory implements InitialContextFactory {
31:
32: public Context getInitialContext(Hashtable<?, ?> envmt)
33: throws NamingException {
34: Hashtable<Object, Object> myEnv = null;
35: if (envmt == null) {
36: myEnv = new Hashtable<Object, Object>();
37: } else {
38: myEnv = (Hashtable<Object, Object>) envmt.clone();
39: }
40: String url = (String) myEnv.get(Context.PROVIDER_URL);
41: LdapUrlParser parser = null;
42: try {
43: parser = LdapUtils.parserURL(url, false);
44: } catch (InvalidNameException e) {
45: throw new ConfigurationException(e.getMessage());
46: }
47:
48: String host = parser.getHost();
49: int port = parser.getPort();
50: String dn = parser.getBaseObject();
51:
52: LdapClient client = LdapClient.newInstance(host, port, myEnv);
53:
54: LdapContextImpl context = new LdapContextImpl(client, myEnv, dn);
55:
56: return context;
57:
58: }
59: }
|