/**
 * Copyright (c) 2015-2017 Linagora
 * 
 * This program/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 (at your
 * option) any later version.
 * 
 * This program/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 program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.bc.gateway;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.Nullable;
import org.ow2.petals.basisapi.exception.PetalsException;
import org.ow2.petals.bc.gateway.jbidescriptor.generated.JbiTransportListener;
import org.ow2.petals.bc.gateway.utils.BcGatewayJbiHelper;
import org.ow2.petals.binding.gateway.clientserver.api.AdminService;
import org.ow2.petals.component.framework.DefaultBootstrap;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.ow2.petals.component.framework.mbean.MBeanHelper;

/**
 * There is one instance of this class for the whole component. The class is declared in the jbi.xml.
 * 
 * @author vnoel
 *
 */
public class BcGatewayBootstrap extends DefaultBootstrap implements AdminService {

    @Override
    public Collection<String> getMBeanOperationsNames() {
        final Collection<String> methods = super.getMBeanOperationsNames();

        methods.addAll(MBeanHelper.getMethodsNames(AdminService.class));

        return methods;
    }

    /**
     * This will automatically be saved in the jbi.xml by the bootstrap before install of the component!
     */
    @Override
    public void addTransportListener(final @Nullable String id, final int port) throws PetalsException {
        assert id != null;
        try {
            BcGatewayJbiHelper.addTransportListener(id, port, this.getJbiComponentConfiguration().getComponent());
        } catch (final PEtALSCDKException e) {
            final PetalsException ex = new PetalsException(e.getMessage());
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    }

    @Override
    public void setTransportListenerPort(final @Nullable String id, final int port) throws PetalsException {
        assert id != null;
        try {
            BcGatewayJbiHelper.setTransportListenerPort(id, port, this.getJbiComponentConfiguration().getComponent());
        } catch (final PEtALSCDKException e) {
            final PetalsException ex = new PetalsException(e.getMessage());
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    }

    @Override
    public Boolean removeTransportListener(final @Nullable String id) throws PetalsException {
        assert id != null;
        try {
            return BcGatewayJbiHelper.removeTransportListener(id,
                    this.getJbiComponentConfiguration().getComponent()) != null;
        } catch (final PEtALSCDKException e) {
            final PetalsException ex = new PetalsException(e.getMessage());
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    }

    @Override
    public Map<String, Object[]> getTransportListeners() throws PetalsException {
        try {
            final Collection<JbiTransportListener> transportListeners = BcGatewayJbiHelper
                    .getTransportListeners(this.getJbiComponentConfiguration().getComponent());
            final Map<String, Object[]> results = new HashMap<>();
            for (final JbiTransportListener transportListener : transportListeners) {
                results.put(transportListener.getId(),
                        new Object[] { Integer.valueOf(transportListener.getPort()), false, "" });
            }
            return results;
        } catch (final PEtALSCDKException e) {
            final PetalsException ex = new PetalsException(e.getMessage());
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    }

}

@@ -1080,24 +1080,86 @@
 rtListener;%0A
+import org.ow2.petals.bc.gateway.utils.BcGatewayJbiConstants;%0A
 import org.o
@@ -1201,24 +1201,24 @@
 yJbiHelper;%0A
-
 import org.o
@@ -1686,16 +1686,39 @@
 nService
+, BcGatewayJbiConstants
  %7B%0A%0A    
@@ -1932,24 +1932,24 @@
 e.class));%0A%0A
-
         retu
@@ -1963,24 +1963,1113 @@
 ods;%0A    %7D%0A%0A
+    @Override%0A    public Collection%3CString%3E getMBeanAttributesNames() %7B%0A        final Collection%3CString%3E attributes = super.getMBeanAttributesNames();%0A%0A        attributes.add(%22consumerDomainsMaxPoolSize%22);%0A        attributes.add(%22providerDomainsMaxPoolSize%22);%0A%0A        return attributes;%0A    %7D%0A%0A    /**%0A     * TODO merge that with the code in %7B@link BcGatewayJbiHelper%7D? Because for it, the default value is in the xsd,%0A     * while here it is hardcoded...%0A     */%0A    public int getConsumerDomainsMaxPoolSize() %7B%0A        return this.getParamAsInteger(EL_CONSUMER_DOMAINS_MAX_POOL_SIZE.getLocalPart(), 6);%0A    %7D%0A%0A    public void setConsumerDomainsMaxPoolSize(int value) %7B%0A        this.setParam(EL_CONSUMER_DOMAINS_MAX_POOL_SIZE.getLocalPart(), Integer.toString(value));%0A    %7D%0A%0A    public int getProviderDomainsMaxPoolSize() %7B%0A        return this.getParamAsInteger(EL_PROVIDER_DOMAINS_MAX_POOL_SIZE.getLocalPart(), 6);%0A    %7D%0A%0A    public void setProviderDomainsMaxPoolSize(int value) %7B%0A        this.setParam(EL_PROVIDER_DOMAINS_MAX_POOL_SIZE.getLocalPart(), Integer.toString(value));%0A    %7D%0A%0A
     /**%0A    

make attributes settable at install