directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r368358 - in /directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi: ServerContextFactory.java ssl/support/LdapsInitializer.java
Date Thu, 12 Jan 2006 12:51:46 GMT
Author: trustin
Date: Thu Jan 12 04:50:40 2006
New Revision: 368358

URL: http://svn.apache.org/viewcvs?rev=368358&view=rev
Log:
Extracted LDAPS-specific code to LdapsInitializer

Added:
    directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ssl/support/LdapsInitializer.java
  (with props)
Modified:
    directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java

Modified: directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java?rev=368358&r1=368357&r2=368358&view=diff
==============================================================================
--- directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java
(original)
+++ directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java
Thu Jan 12 04:50:40 2006
@@ -50,6 +50,7 @@
 import org.apache.ldap.common.exception.LdapNamingException;
 import org.apache.ldap.server.DirectoryService;
 import org.apache.ldap.server.configuration.ServerStartupConfiguration;
+import org.apache.ldap.server.jndi.ssl.support.LdapsInitializer;
 import org.apache.ldap.server.jndi.ssl.support.ServerX509TrustManager;
 import org.apache.ldap.server.protocol.ExtendedOperationHandler;
 import org.apache.ldap.server.protocol.LdapProtocolProvider;
@@ -368,80 +369,14 @@
     private void startLDAPS( ServerStartupConfiguration cfg, Hashtable env ) throws NamingException
     {
         // Skip if disabled
-        int port = cfg.getLdapsPort();
-        if( port < 0 )
+        if( !cfg.isEnableLdaps() )
         {
             return;
         }
         
-        // Load the certificate
-        char[] certPasswdChars = cfg.getLdapsCertificatePassword().toCharArray();
-        String storePath = cfg.getLdapsCertificateFile().getPath();
-        
-        KeyStore ks = null;
-        try
-        {
-            ks = loadKeyStore( storePath, "PKCS12" );
-        }
-        catch( Exception e )
-        {
-            try
-            {
-                ks = loadKeyStore( storePath, "JKS" );
-            }
-            catch( Exception e2 )
-            {
-                throw ( NamingException ) new NamingException( "Failed to load a certificate:
" + storePath ).initCause( e );
-            }
-        }
-
-        SSLContext sslCtx;
-        try
-        {
-            // Set up key manager factory to use our key store
-            KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
-            kmf.init( ks, certPasswdChars );
-    
-            // Initialize the SSLContext to work with our key managers.
-            sslCtx = SSLContext.getInstance( "TLS" );
-            sslCtx.init( kmf.getKeyManagers(),
-                    new TrustManager[] { new ServerX509TrustManager() }, new SecureRandom()
);
-        }
-        catch( Exception e )
-        {
-            throw ( NamingException ) new NamingException( "Failed to create a SSL context."
).initCause( e );
-        }
-        
-        Service service = new Service( "LDAPS", TransportType.SOCKET, new InetSocketAddress(
port ) );
-        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();
-        chain.addLast( "SSL", new SSLFilter( sslCtx ) );
+        Service service = new Service( "LDAPS", TransportType.SOCKET, new InetSocketAddress(
cfg.getLdapsPort() ) );
+        IoFilterChainBuilder chain = LdapsInitializer.init( cfg );
         startLDAP0( cfg, env, service, chain );
-    }
-
-    private KeyStore loadKeyStore( String storePath, String storeType ) throws KeyStoreException,
IOException, CertificateException, NoSuchAlgorithmException
-    {
-        FileInputStream in = null;
-        // Create keystore
-        KeyStore ks = KeyStore.getInstance( storeType );
-        try
-        {
-            in = new FileInputStream( storePath );
-            ks.load( in, null );
-            return ks;
-        }
-        finally
-        {
-            if( in != null )
-            {
-                try
-                {
-                    in.close();
-                }
-                catch( IOException ignored )
-                {
-                }
-            }
-        }
     }
 
     private void startLDAP0( ServerStartupConfiguration cfg, Hashtable env, Service service,
IoFilterChainBuilder chainBuilder )

Added: directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ssl/support/LdapsInitializer.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ssl/support/LdapsInitializer.java?rev=368358&view=auto
==============================================================================
--- directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ssl/support/LdapsInitializer.java
(added)
+++ directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ssl/support/LdapsInitializer.java
Thu Jan 12 04:50:40 2006
@@ -0,0 +1,117 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.server.jndi.ssl.support;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.cert.CertificateException;
+
+import javax.naming.NamingException;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+
+import org.apache.ldap.server.configuration.ServerStartupConfiguration;
+import org.apache.mina.common.DefaultIoFilterChainBuilder;
+import org.apache.mina.common.IoFilterChainBuilder;
+import org.apache.mina.filter.SSLFilter;
+
+/**
+ * Loads the certificate file for LDAPS support and creates the appropriate
+ * MINA filter chain.
+ *
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ *
+ */
+public class LdapsInitializer
+{
+    public static IoFilterChainBuilder init( ServerStartupConfiguration cfg ) throws NamingException
+    {
+        // Load the certificate
+        char[] certPasswdChars = cfg.getLdapsCertificatePassword().toCharArray();
+        String storePath = cfg.getLdapsCertificateFile().getPath();
+        
+        KeyStore ks = null;
+        try
+        {
+            ks = loadKeyStore( storePath, "PKCS12" );
+        }
+        catch( Exception e )
+        {
+            try
+            {
+                ks = loadKeyStore( storePath, "JKS" );
+            }
+            catch( Exception e2 )
+            {
+                throw ( NamingException ) new NamingException( "Failed to load a certificate:
" + storePath ).initCause( e );
+            }
+        }
+
+        SSLContext sslCtx;
+        try
+        {
+            // Set up key manager factory to use our key store
+            KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
+            kmf.init( ks, certPasswdChars );
+    
+            // Initialize the SSLContext to work with our key managers.
+            sslCtx = SSLContext.getInstance( "TLS" );
+            sslCtx.init( kmf.getKeyManagers(),
+                    new TrustManager[] { new ServerX509TrustManager() }, new SecureRandom()
);
+        }
+        catch( Exception e )
+        {
+            throw ( NamingException ) new NamingException( "Failed to create a SSL context."
).initCause( e );
+        }
+        
+        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();
+        chain.addLast( "SSL", new SSLFilter( sslCtx ) );
+        return chain;
+    }
+    
+    private static KeyStore loadKeyStore( String storePath, String storeType ) throws KeyStoreException,
IOException, CertificateException, NoSuchAlgorithmException
+    {
+        FileInputStream in = null;
+        // Create keystore
+        KeyStore ks = KeyStore.getInstance( storeType );
+        try
+        {
+            in = new FileInputStream( storePath );
+            ks.load( in, null );
+            return ks;
+        }
+        finally
+        {
+            if( in != null )
+            {
+                try
+                {
+                    in.close();
+                }
+                catch( IOException ignored )
+                {
+                }
+            }
+        }
+    }
+}

Propchange: directory/sandbox/trustin/ldaps/apacheds-server/src/main/java/org/apache/ldap/server/jndi/ssl/support/LdapsInitializer.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message