KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jaas > ldap > ServerContextFactory


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.jaas.ldap;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.InetSocketAddress JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Properties JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.naming.directory.DirContext JavaDoc;
29 import javax.naming.ldap.Control JavaDoc;
30 import javax.naming.ldap.InitialLdapContext JavaDoc;
31 import javax.naming.ldap.LdapContext JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.kerberos.protocol.KerberosProtocolProvider;
36 import org.apache.kerberos.sam.SamSubsystem;
37 import org.apache.kerberos.service.KdcConfiguration;
38 import org.apache.kerberos.store.JndiPrincipalStoreImpl;
39 import org.apache.kerberos.store.PrincipalStore;
40 import org.apache.ldap.common.exception.LdapConfigurationException;
41 import org.apache.ldap.common.name.LdapName;
42 import org.apache.ldap.common.util.NamespaceTools;
43 import org.apache.ldap.common.util.PropertiesUtils;
44 import org.apache.ldap.server.jndi.ContextFactoryService;
45 import org.apache.ldap.server.jndi.CoreContextFactory;
46 import org.apache.ldap.server.protocol.LdapProtocolProvider;
47 import org.apache.mina.common.TransportType;
48 import org.apache.mina.registry.Service;
49 import org.apache.mina.registry.ServiceRegistry;
50
51
52 /**
53  * Adds additional bootstrapping for server socket listeners when firing
54  * up the server.
55  *
56  * @version $Rev: 233391 $ $Date: 2005-08-18 16:38:47 -0600 (Thu, 18 Aug 2005) $
57  * @see javax.naming.spi.InitialContextFactory
58  */

59 public class ServerContextFactory extends CoreContextFactory {
60     private static final Log log = LogFactory.getLog(ServerContextFactory.class);
61     private static Service ldapService;
62     private static Service kerberosService;
63     private static ServiceRegistry minaRegistry;
64
65     protected ServiceRegistry getMinaRegistry() {
66         return minaRegistry;
67     }
68
69     public void afterShutdown(ContextFactoryService service) {
70         if (minaRegistry != null) {
71             if (ldapService != null) {
72                 minaRegistry.unbind(ldapService);
73                 if (log.isInfoEnabled()) {
74                     log.info("Unbind of LDAP Service complete: " + ldapService);
75                 }
76                 ldapService = null;
77             }
78
79             if (kerberosService != null) {
80                 minaRegistry.unbind(kerberosService);
81                 if (log.isInfoEnabled()) {
82                     log.info("Unbind of KRB5 Service complete: " + kerberosService);
83                 }
84                 kerberosService = null;
85             }
86         }
87     }
88
89     public void afterStartup(ContextFactoryService service) throws NamingException JavaDoc {
90         ServerStartupConfiguration cfg =
91                 (ServerStartupConfiguration) service.getConfiguration().getStartupConfiguration();
92         Hashtable JavaDoc env = service.getConfiguration().getEnvironment();
93
94         if (cfg.isEnableNetworking()) {
95             setupRegistry(cfg);
96             startLdapProtocol(cfg, env);
97
98             if (cfg.isEnableKerberos()) {
99                 startKerberosProtocol(env);
100             }
101         }
102     }
103
104     /**
105      * Starts up the MINA registry so various protocol providers can be started.
106      */

107     private void setupRegistry(ServerStartupConfiguration cfg) {
108         minaRegistry = cfg.getMinaServiceRegistry();
109     }
110
111
112     /**
113      * Starts the Kerberos protocol provider which is backed by the LDAP store.
114      *
115      * @throws NamingException if there are problems starting up the Kerberos provider
116      */

117     private void startKerberosProtocol(Hashtable JavaDoc env) throws NamingException JavaDoc {
118         /*
119          * Looks like KdcConfiguration takes properties and we use Hashtable for JNDI
120          * so I'm copying over the String based properties into a new Properties obj.
121          */

122         Properties JavaDoc props = new Properties JavaDoc();
123         Iterator JavaDoc list = env.keySet().iterator();
124         while (list.hasNext()) {
125             String JavaDoc key = (String JavaDoc) list.next();
126
127             if (env.get(key) instanceof String JavaDoc) {
128                 props.setProperty(key, (String JavaDoc) env.get(key));
129             }
130         }
131
132         // construct the configuration, get the port, create the service, and prepare kdc objects
133
KdcConfiguration config = new KdcConfiguration(props);
134         int port = PropertiesUtils.get(env, KdcConfiguration.KERBEROS_PORT_KEY, KdcConfiguration.DEFAULT_KERBEROS_PORT);
135         Service service = new Service("kerberos", TransportType.DATAGRAM, new InetSocketAddress JavaDoc(port));
136         LdapContext JavaDoc ctx = getBaseRealmContext(config, env);
137         PrincipalStore store = new JndiPrincipalStoreImpl(ctx, new LdapName("ou=Users"));
138         SamSubsystem.getInstance().setUserContext((DirContext JavaDoc) ctx, "ou=Users");
139
140         try {
141             minaRegistry.bind(service, new KerberosProtocolProvider(config, store));
142             kerberosService = service;
143             if (log.isInfoEnabled()) {
144                 log.info("Successful bind of KRB5 Service completed: " + kerberosService);
145             }
146         }
147         catch (IOException JavaDoc e) {
148             log.error("Could not start the kerberos service on port " +
149                     KdcConfiguration.DEFAULT_KERBEROS_PORT, e);
150         }
151     }
152
153
154     /**
155      * Maps a Kerberos Realm name to a position within the DIT. The primary realm of
156      * the KDC will use this area for configuration and for storing user entries.
157      *
158      * @param config the KDC's configuration
159      * @param env the JNDI environment properties
160      * @return the base context for the primary realm of the KDC
161      * @throws NamingException
162      */

163     private LdapContext JavaDoc getBaseRealmContext(KdcConfiguration config, Hashtable JavaDoc env) throws NamingException JavaDoc {
164         Hashtable JavaDoc cloned = (Hashtable JavaDoc) env.clone();
165         String JavaDoc dn = NamespaceTools.inferLdapName(config.getPrimaryRealm());
166         cloned.put(Context.PROVIDER_URL, dn);
167
168         if (log.isInfoEnabled()) {
169             log.info("Getting initial context for realm base at " + dn + " for " + config.getPrimaryRealm());
170         }
171
172         return new InitialLdapContext JavaDoc(cloned, new Control JavaDoc[]{});
173     }
174
175
176     /**
177      * Starts up the LDAP protocol provider to service LDAP requests
178      *
179      * @throws NamingException if there are problems starting the LDAP provider
180      */

181     private void startLdapProtocol(ServerStartupConfiguration cfg, Hashtable JavaDoc env) throws NamingException JavaDoc {
182         int port = cfg.getLdapPort();
183         InetAddress JavaDoc host = cfg.getHost();
184         Service service = new Service("ldap", TransportType.SOCKET, new InetSocketAddress JavaDoc(host, port));
185
186         try {
187             minaRegistry.bind(service, new LdapProtocolProvider((Hashtable JavaDoc) env.clone()));
188             ldapService = service;
189             if (log.isInfoEnabled()) {
190                 log.info("Successful bind of LDAP Service completed: " + ldapService);
191             }
192         }
193         catch (IOException JavaDoc e) {
194             String JavaDoc msg = "Failed to bind the LDAP protocol service to the service registry: " + service;
195             LdapConfigurationException lce = new LdapConfigurationException(msg);
196             lce.setRootCause(e);
197             log.error(msg, e);
198             throw lce;
199         }
200     }
201 }
202
Popular Tags