/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2004 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer: Florent BENOIT
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.security.realm.factory;

//import java
import java.util.Hashtable;

//import javax
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;

//import objectweb.util
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * This class provides an implementation of a JResource JNDI factory for
 * managing users
 * @author Florent Benoit
 */
public class JResourceLDAPFactory extends JResourceFactory {

    /**
     * The Java type for which this factory knows how to create objects.
     */
    private static final String FACTORY_TYPE = "org.objectweb.jonas.security.realm.factory.JResourceLDAP";

    /**
     * Creates a javax.mail.Session object using the location or reference
     * information specified.
     * @param obj the possibly null object containing location or reference
     *        information that can be used in creating an object.
     * @param name the name of this object relative to nameCtx, or null if no
     *        name is specified.
     * @param nameCtx the context relative to which the name parameter is
     *        specified, or null if name is relative to the default initial
     *        context.
     * @param environment the possibly null environment that is used in creating
     *        the object.
     * @return a newly created javax.mail.Session object with the specific
     *         configuration; null if an object cannot be created.
     * @throws Exception if this object factory encountered an exception while
     *         attempting to create an object, and no other object factories are
     *         to be tried.
     */
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {

        // Get the reference
        Reference ref = (Reference) obj;

        // Get the class name
        String clname = ref.getClassName();

        // Check the class name
        if (!ref.getClassName().equals(FACTORY_TYPE)) {
            getLogger().log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE
                    + "', but found type is '" + clname + "'.");
            return (null);
        }

        String jResName = (String) ref.get("name").getContent();
        String initialContextFactory = (String) ref.get("initialContextFactory").getContent();
        String providerUrl = (String) ref.get("providerUrl").getContent();
        String securityAuthentication = (String) ref.get("securityAuthentication").getContent();
        String securityPrincipal = (String) ref.get("securityPrincipal").getContent();
        String securityCredentials = (String) ref.get("securityCredentials").getContent();
        String securityProtocol = (String) ref.get("securityProtocol").getContent();
        String language = (String) ref.get("language").getContent();
        String referral = (String) ref.get("referral").getContent();
        String stateFactories = (String) ref.get("stateFactories").getContent();
        String authenticationMode = (String) ref.get("authenticationMode").getContent();
        String userPasswordAttribute = (String) ref.get("userPasswordAttribute").getContent();
        String userRolesAttribute = (String) ref.get("userRolesAttribute").getContent();
        String roleNameAttribute = (String) ref.get("roleNameAttribute").getContent();
        String baseDN = (String) ref.get("baseDN").getContent();
        String userDN = (String) ref.get("userDN").getContent();
        String userSearchFilter = (String) ref.get("userSearchFilter").getContent();
        String roleDN = (String) ref.get("roleDN").getContent();
        String roleSearchFilter = (String) ref.get("roleSearchFilter").getContent();
        String algorithm = (String) ref.get("algorithm").getContent();

        // Create and return a new object
        JResourceLDAP jResourceLDAP = new JResourceLDAP();
        jResourceLDAP.setName(jResName);
        jResourceLDAP.setInitialContextFactory(initialContextFactory);
        jResourceLDAP.setProviderUrl(providerUrl);
        jResourceLDAP.setSecurityAuthentication(securityAuthentication);
        jResourceLDAP.setSecurityPrincipal(securityPrincipal);
        jResourceLDAP.setSecurityCredentials(securityCredentials);
        jResourceLDAP.setSecurityProtocol(securityProtocol);
        jResourceLDAP.setLanguage(language);
        jResourceLDAP.setReferral(referral);
        jResourceLDAP.setStateFactories(stateFactories);
        jResourceLDAP.setAuthenticationMode(authenticationMode);
        jResourceLDAP.setUserPasswordAttribute(userPasswordAttribute);
        jResourceLDAP.setUserRolesAttribute(userRolesAttribute);
        jResourceLDAP.setRoleNameAttribute(roleNameAttribute);
        jResourceLDAP.setBaseDN(baseDN);
        jResourceLDAP.setUserDN(userDN);
        jResourceLDAP.setUserSearchFilter(userSearchFilter);
        jResourceLDAP.setRoleDN(roleDN);
        jResourceLDAP.setRoleSearchFilter(roleSearchFilter);
        jResourceLDAP.setAlgorithm(algorithm);

        return jResourceLDAP;
    }

}