/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, 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.test.clustering.ejb;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;

import org.jboss.ejb.client.Affinity;
import org.jboss.ejb.client.ClusterAffinity;
import org.jboss.ejb.client.EJBClient;

/**
 * @author Paul Ferraro
 */
public class RemoteEJBDirectory extends AbstractEJBDirectory {

    private static final String TX_CONTEXT_NAME = "txn:UserTransaction";

    private static Properties createEnvironment() {
        Properties env = new Properties();
        env.setProperty(Context.INITIAL_CONTEXT_FACTORY, org.wildfly.naming.client.WildFlyInitialContextFactory.class.getName());
        // TODO UserTransaction lookup currently requires environment to be configured with provider URLs.
        // env.setProperty(Context.PROVIDER_URL, String.join(",", EJBClientContext.getCurrent().getConfiguredConnections().stream().map(EJBClientConnection::getDestination).map(URI::toString).collect(Collectors.toList())));
        return env;
    }

    private final String module;

    public RemoteEJBDirectory(String module) throws NamingException {
        super(TX_CONTEXT_NAME, createEnvironment());
        this.module = module;
    }

    @Override
    protected <T> String createJndiName(String beanName, Class<T> beanInterface, Type type) {
        return String.format("ejb:/%s/%s!%s%s", this.module, beanName, beanInterface.getName(), (type == Type.STATEFUL) ? "?stateful" : "");
    }

    @Override
    protected <T> T lookup(String beanName, Class<T> beanInterface, Type type) throws NamingException {
        T bean = super.lookup(beanName, beanInterface, type);
        Affinity affinity = new ClusterAffinity("ejb");
        switch (type) {
            case STATEFUL: {
                EJBClient.setStrongAffinity(bean, affinity);
                break;
            }
            case STATELESS: {
                EJBClient.setWeakAffinity(bean, affinity);
                break;
            }
            default: {
                // No need to set initial affinity
            }
        }
        return bean;
    }
}

@@ -1161,131 +1161,8 @@
 n;%0A%0A
-import org.jboss.ejb.client.Affinity;%0Aimport org.jboss.ejb.client.ClusterAffinity;%0Aimport org.jboss.ejb.client.EJBClient;%0A%0A
 /**%0A
@@ -2362,646 +2362,7 @@
    %7D
-%0A%0A    @Override%0A    protected %3CT%3E T lookup(String beanName, Class%3CT%3E beanInterface, Type type) throws NamingException %7B%0A        T bean = super.lookup(beanName, beanInterface, type);%0A        Affinity affinity = new ClusterAffinity(%22ejb%22);%0A        switch (type) %7B%0A            case STATEFUL: %7B%0A                EJBClient.setStrongAffinity(bean, affinity);%0A                break;%0A            %7D%0A            case STATELESS: %7B%0A                EJBClient.setWeakAffinity(bean, affinity);%0A                break;%0A            %7D%0A            default: %7B%0A                // No need to set initial affinity%0A            %7D%0A        %7D%0A        return bean;%0A    %7D
 %0A%7D%0A

Fix remote EJB failover tests to validate WFLY-9358.