/**
 * Copyright (c) 2014-2015 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.se.rmi;

import java.util.ArrayList;
import java.util.List;

import org.ow2.petals.component.framework.DefaultBootstrap;
import org.ow2.petals.se.rmi.registry.RmiProperties;
import org.w3c.dom.Element;

/**
 * The bootstrap class.
 * 
 * @author Christophe DENEUX - Linagora
 */
public class RmiBootstrap extends DefaultBootstrap {

    @Override
    public List<String> getAttributeList() {
        final List<String> attributes = new ArrayList<String>();

        attributes.add("rmiPort");
        attributes.add("rmiHost");
        attributes.add("embeddedRegistry");
        attributes.add("componentContextName");

        return attributes;
    }

    public int getRmiPort() {

        final String rmiPortString = this.getParam(RmiProperties.RMI_PORT);
        if (rmiPortString != null) {
            return Integer.parseInt(rmiPortString);
        } else {
            return RmiProperties.DEFAULT_RMI_PORT;
        }
    }

    public String getRmiHost() {
        return this.getParam(RmiProperties.RMI_HOST);
    }

    public boolean isEmbeddedRegistry() {
        return Boolean.parseBoolean(this.getParam(RmiProperties.RMI_EMBEDDED_REGISTRY));
    }

    public String getComponentContextName() {
        return this.getParam(RmiProperties.RMI_COMPONENT_CONTEXT_NAME);
    }

    public void setRmiPort(final int rmiPort) {
        this.setParam(RmiProperties.RMI_PORT, Integer.toString(rmiPort));
    }

    public void setRmiHost(final String rmiHost) {
        this.setParam(RmiProperties.RMI_HOST, rmiHost);
    }

    public void setEmbeddedRegistry(final boolean embeddedRegistry) {
        this.setParam(RmiProperties.RMI_EMBEDDED_REGISTRY, Boolean.toString(embeddedRegistry));
    }

    public void setComponentContextName(final String componentContextName) {
        this.setParam(RmiProperties.RMI_COMPONENT_CONTEXT_NAME, componentContextName);
    }

    /**
     * Get a parameter.
     * 
     * @param name
     * @param value
     * @deprecated This method as been moved in {@link DefaultBootstrap} in next version of CDK core
     */
    @Deprecated
    public String getParam(final String name) {
        for (Element element : this.getJbiComponentConfiguration().getComponent().getAny()) {
            if (element.getLocalName().equals(name)) {
                return element.getTextContent();
            }
        }

        return null;
    }

    /**
     * Set a parameter.
     * 
     * @param name
     * @param value
     * @deprecated This method as been moved in {@link DefaultBootstrap} in next version of CDK core
     */
    @Deprecated
    public void setParam(final String name, String value) {
        for (Element element : this.getJbiComponentConfiguration().getComponent().getAny()) {
            if (element.getLocalName().equals(name)) {
                element.setTextContent(value);
            }
        }
    }
}
