KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > directory > ServerContextFactory


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 package org.apache.geronimo.directory;
18
19 import java.io.IOException JavaDoc;
20 import java.net.InetAddress JavaDoc;
21 import java.net.InetSocketAddress JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import javax.naming.NamingException JavaDoc;
27 import javax.naming.Context 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.service.KdcConfiguration;
37 import org.apache.kerberos.store.JndiPrincipalStoreImpl;
38 import org.apache.kerberos.store.PrincipalStore;
39 import org.apache.kerberos.sam.SamSubsystem;
40 import org.apache.ldap.common.exception.LdapConfigurationException;
41 import org.apache.ldap.common.name.LdapName;
42 import org.apache.ldap.common.util.PropertiesUtils;
43 import org.apache.ldap.common.util.NamespaceTools;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
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                 log.debug("Unbind of LDAP Service complete: " + ldapService);
74                 ldapService = null;
75             }
76
77             if (kerberosService != null) {
78                 minaRegistry.unbind(kerberosService);
79                 log.debug("Unbind of KRB5 Service complete: " + kerberosService);
80                 kerberosService = null;
81             }
82         }
83     }
84
85     public void afterStartup(ContextFactoryService service) throws NamingException JavaDoc {
86         ServerStartupConfiguration cfg =
87                 (ServerStartupConfiguration) service.getConfiguration().getStartupConfiguration();
88         Hashtable JavaDoc env = service.getConfiguration().getEnvironment();
89
90         if (cfg.isEnableNetworking()) {
91             setupRegistry(cfg);
92             startLdapProtocol(cfg, env);
93
94             if (cfg.isEnableKerberos()) {
95                 startKerberosProtocol(env);
96             }
97         }
98     }
99
100     /**
101      * Starts up the MINA registry so various protocol providers can be started.
102      */

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

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

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

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

173     private void startLdapProtocol(ServerStartupConfiguration cfg, Hashtable JavaDoc env) throws NamingException JavaDoc {
174         int port = cfg.getLdapPort();
175         InetAddress JavaDoc host = cfg.getHost();
176         Service service = new Service("ldap", TransportType.SOCKET, new InetSocketAddress JavaDoc(host, port));
177 // Service service = new Service( "ldap", TransportType.SOCKET, new InetSocketAddress( port ) );
178

179         try {
180             minaRegistry.bind(service, new LdapProtocolProvider((Hashtable JavaDoc) env.clone()));
181             ldapService = service;
182             log.debug("Successful bind of LDAP Service completed: " + ldapService);
183         }
184         catch (IOException JavaDoc e) {
185             String JavaDoc msg = "Failed to bind the LDAP protocol service to the service registry: " + service;
186             LdapConfigurationException lce = new LdapConfigurationException(msg);
187             lce.setRootCause(e);
188             log.error(msg, e);
189             throw lce;
190         }
191     }
192 }
Popular Tags