/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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 (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.ejb3.remote;

import org.jboss.as.ee.metadata.EJBClientDescriptorMetaData;
import org.jboss.ejb.client.EJBClientConfiguration;
import org.jboss.logging.Logger;
import org.jboss.msc.service.ServiceRegistry;
import org.xnio.OptionMap;

import java.util.Properties;

/**
 * @author Jaikiran Pai
 */
public class EJBClientClusterNodeConfig extends EJBClientCommonConnectionConfig implements EJBClientConfiguration.ClusterNodeConfiguration {

    private static final Logger logger = Logger.getLogger(EJBClientClusterNodeConfig.class);

    private final EJBClientDescriptorMetaData.ClusterNodeConfig delegate;

    public EJBClientClusterNodeConfig(final EJBClientDescriptorMetaData.ClusterNodeConfig clusterNodeConfig, final ClassLoader deploymentClassLoader, final ServiceRegistry serviceRegistry) {
        this.delegate = clusterNodeConfig;

        this.setConnectionTimeout(clusterNodeConfig.getConnectTimeout());

        final Properties channelProps = clusterNodeConfig.getChannelCreationOptions();
        if (channelProps != null) {
            // we don't use the deployment CL here since the XNIO project isn't necessarily added as a dep on the deployment's
            // module CL
            final OptionMap channelOpts = getOptionMapFromProperties(channelProps, this.getClass().getClassLoader());
            logger.debugf("Channel creation options for node %s are %s", clusterNodeConfig.getNodeName(), channelOpts);
            this.setChannelCreationOptions(channelOpts);
        }

        final Properties connectionProps = clusterNodeConfig.getConnectionOptions();
        if (connectionProps != null) {
            // we don't use the deployment CL here since the XNIO project isn't necessarily added as a dep on the deployment's
            // module CL
            final OptionMap connectOpts = getOptionMapFromProperties(connectionProps, this.getClass().getClassLoader());
            logger.debugf("Connection creation options for node %s are %s", clusterNodeConfig.getNodeName(), connectOpts);
            this.setConnectionCreationOptions(connectOpts);
        }

        this.setCallbackHandler(serviceRegistry, clusterNodeConfig.getUserName(), clusterNodeConfig.getSecurityRealm());

    }

    @Override
    public String getNodeName() {
        return this.delegate.getNodeName();
    }
}
