Package org.wso2.carbon.ldap.server

Source Code of org.wso2.carbon.ldap.server.DirectoryActivator

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you 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.wso2.carbon.ldap.server;

import org.apache.log4j.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.wso2.carbon.apacheds.DirectoryServiceFactory;
import org.wso2.carbon.apacheds.KDCServer;
import org.wso2.carbon.apacheds.KdcConfiguration;
import org.wso2.carbon.apacheds.LDAPConfiguration;
import org.wso2.carbon.apacheds.LDAPServer;
import org.wso2.carbon.apacheds.PartitionInfo;
import org.wso2.carbon.apacheds.PartitionManager;
import org.wso2.carbon.apacheds.exception.DirectoryServerException;
import org.wso2.carbon.ldap.server.util.EmbeddingLDAPException;
import org.wso2.carbon.ldap.server.configuration.LDAPConfigurationBuilder;
import org.wso2.carbon.ldap.server.tenant.LDAPTenantManagerService;
import org.wso2.carbon.user.core.tenant.LDAPTenantManager;

import java.io.File;
import java.io.FileNotFoundException;

public class DirectoryActivator implements BundleActivator {

    private final Logger logger = Logger.getLogger(DirectoryActivator.class);

    private LDAPServer ldapServer;
    private KDCServer kdcServer;

    /**
     * This is called at the bundle start, EmbeddedLDAP-server is started and an implementation
     * of LDAPTenantManager is registered in OSGI.
     * @param bundleContext
     *
     */
    public void start(BundleContext bundleContext) {

        try {
            /*Read the embedded-ldap configuration file.*/
            LDAPConfigurationBuilder configurationBuilder = new LDAPConfigurationBuilder(
                    getLdapConfigurationFile());
            /*Make relevant objects that encapsulate different parts of config file.*/
            configurationBuilder.buildConfigurations();

            boolean embeddedLDAPEnabled = configurationBuilder.isEmbeddedLDAPEnabled();
            //start LDAPServer only if embedded-ldap is enabled.
            if (embeddedLDAPEnabled) {

                LDAPConfiguration ldapConfiguration = configurationBuilder.getLdapConfiguration();
                /*set the embedded-apacheds's schema location which is: carbon-home/repository/data/
                is-default-schema.zip
                */
                setSchemaLocation();

                /* Set working directory where schema directory and ldap partitions are created*/
                setWorkingDirectory(ldapConfiguration);

                startLdapServer(ldapConfiguration);

                /* replace default password with that is provided in the configuration file.*/
                this.ldapServer.changeConnectionUserPassword(
                        configurationBuilder.getConnectionPassword());

                // Add admin (default)partition if it is not already created.
                PartitionManager partitionManager = this.ldapServer.getPartitionManager();
                PartitionInfo defaultPartitionInfo=
                        configurationBuilder.getPartitionConfigurations();
                boolean defaultPartitionAlreadyExisted=partitionManager.partitionDirectoryExists(
                        defaultPartitionInfo.getPartitionId());

                if (!defaultPartitionAlreadyExisted) {
                    partitionManager.addPartition(defaultPartitionInfo);
                } else {
                    partitionManager.initializeExistingPartition(defaultPartitionInfo);
                }

                // Start KDC if enabled
                if (configurationBuilder.isKdcEnabled()) {

                    startKDC(configurationBuilder.getKdcConfigurations());

                    // Add kerberos attributes only if the default partition is newly created.
                    if (!defaultPartitionAlreadyExisted) {

                        kdcServer.kerberizePartition(configurationBuilder.
                                getPartitionConfigurations(), this.ldapServer);
                    }
                }

                //create and register LDAPTenantManager implementation in OSGI.
                LDAPTenantManager ldapTenantManager = new LDAPTenantManagerService(this.ldapServer.
                        getPartitionManager());
                bundleContext.registerService(LDAPTenantManager.class.getName(), ldapTenantManager,
                                              null);
                if(logger.isDebugEnabled()){
                    logger.debug("apacheds-server component started.");
                }

            } else if (!embeddedLDAPEnabled) {
                //if needed, create a dummy tenant manager service and register it.
                logger.info("Embedded LDAP is disabled.");
            }
        } catch (FileNotFoundException e) {
            String errorMessage = "Could not start the embedded-ldap. ";
            logger.error(errorMessage, e);

        } catch (DirectoryServerException e){
            String errorMessage = "Could not start the embedded-ldap. ";
            logger.error(errorMessage, e);

        } catch (EmbeddingLDAPException e) {
            String errorMessage = "Could not start the embedded-ldap. ";
            logger.error(errorMessage, e);
        }

    }

    private void setSchemaLocation() throws EmbeddingLDAPException {
        String schemaLocation = "repository" + File.separator + "data" + File.separator +
                "is-default-schema.zip";
        File dataDir = new File(getCarbonHome(), schemaLocation);

        // Set schema location
        System.setProperty("schema.zip.store.location", dataDir.getAbsolutePath());
    }

    private String getCarbonHome() throws EmbeddingLDAPException {
        String carbonHome = System.getProperty("carbon.home");
        if (carbonHome == null) {
            String msg = "carbon.home property not set. Cannot find carbon home directory.";
            logger.error(msg);
            throw new EmbeddingLDAPException(msg);
        }

        return carbonHome;
    }

    private File getLdapConfigurationFile() throws EmbeddingLDAPException {

        String configurationFilePath = "repository" + File.separator + "conf" + File.separator +
                "embedded-ldap.xml";
        return new File(getCarbonHome(), configurationFilePath);
    }

    private void setWorkingDirectory(LDAPConfiguration ldapConfiguration)
            throws EmbeddingLDAPException {

        if (ldapConfiguration.getWorkingDirectory().equals(".")) {
            File dataDir = new File(getCarbonHome(), "repository/data");
            if (!dataDir.exists()) {
                if (!dataDir.mkdir()) {
                    String msg = "Unable to create data directory at " + dataDir.getAbsolutePath();
                    logger.error(msg);
                    throw new EmbeddingLDAPException(msg);
                }
            }

            File bundleDataDir = new File(dataDir, "org.wso2.carbon.directory");
            if (!bundleDataDir.exists()) {
                if (!bundleDataDir.mkdirs()) {
                    String msg = "Unable to create schema data directory at " + bundleDataDir.
                            getAbsolutePath();
                    logger.error(msg);
                    throw new EmbeddingLDAPException(msg);

                }
            }

            ldapConfiguration.setWorkingDirectory(bundleDataDir.getAbsolutePath());
        }
    }

    private void startLdapServer(LDAPConfiguration ldapConfiguration)
            throws DirectoryServerException {

        this.ldapServer =DirectoryServiceFactory.createLDAPServer(DirectoryServiceFactory.
                LDAPServerType.APACHE_DIRECTORY_SERVICE);

        logger.info("Initializing Directory Server with working directory " + ldapConfiguration.
                getWorkingDirectory() +" and port " + ldapConfiguration.getLdapPort());

        this.ldapServer.init(ldapConfiguration);

        this.ldapServer.start();
    }
   
    private void startKDC(KdcConfiguration kdcConfiguration)
            throws DirectoryServerException {

        kdcServer =DirectoryServiceFactory.createKDCServer(DirectoryServiceFactory.LDAPServerType.
                APACHE_DIRECTORY_SERVICE);
        kdcServer.init(kdcConfiguration, this.ldapServer);

        kdcServer.start();

    }

    public void stop(BundleContext bundleContext)
            throws Exception {

        if (this.kdcServer != null) {
            this.kdcServer.stop();
        }

        if (this.ldapServer != null) {

            this.ldapServer.stop();
        }
    }

}

TOP

Related Classes of org.wso2.carbon.ldap.server.DirectoryActivator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.