All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.ant.condition.HasEndpointDeployed Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/**
 * Copyright (c) 2016 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.ant.condition;

import java.util.List;
import java.util.Map;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.condition.Condition;
import org.ow2.petals.ant.AbstractJBIAntTask;
import org.ow2.petals.clientserverapi.jbi.messaging.endpoint.EndpointDirectoryServiceConstants;
import org.ow2.petals.clientserverapi.jbi.messaging.endpoint.exception.EndpointDirectoryException;
import org.ow2.petals.jmx.api.api.JMXClient;
import org.ow2.petals.jmx.api.api.PetalsJmxApiFactory;
import org.ow2.petals.jmx.api.api.exception.ConnectionErrorException;
import org.ow2.petals.jmx.api.api.exception.DuplicatedServiceException;
import org.ow2.petals.jmx.api.api.exception.EndpointDirectoryServiceDoesNotExistException;
import org.ow2.petals.jmx.api.api.exception.EndpointDirectoryServiceErrorException;
import org.ow2.petals.jmx.api.api.exception.MissingServiceException;

/**
 * A Ant {@link Condition} to check the number of endpoints deployed satisfying criteria
 * 
 * @author Christophe DENEUX - Linagora
 *
 */
public class HasEndpointDeployed implements Condition {

    private String jmxHost = AbstractJBIAntTask.DEFAULT_JMX_HOST;

    private int jmxPort = Integer.parseInt(AbstractJBIAntTask.DEFAULT_JMX_PORT);

    private String jmxUsername;

    private String jmxPassword;

    private String interfaceName;

    private String serviceName;

    private String endpointName;

    private int expectedNumber = 1;

    /**
     * @param host
     *            the host to set
     */
    public void setHost(final String host) {
        this.jmxHost = host;
    }

    /**
     * @param port
     *            the port to set
     */
    public void setPort(final int port) {
        this.jmxPort = port;
    }

    /**
     * @param username
     *            the username to set
     */
    public void setUsername(final String username) {
        this.jmxUsername = username;
    }

    /**
     * @param password
     *            the password to set
     */
    public void setPassword(final String password) {
        this.jmxPassword = password;
    }

    /**
     * @param interfaceName
     *            the interface name of the endpoint to check
     */
    public void setInterfaceName(final String interfaceName) {
        this.interfaceName = interfaceName;
    }

    /**
     * @param serviceName
     *            the service name of the endpoint to check
     */
    public void setServiceName(final String serviceName) {
        this.serviceName = serviceName;
    }

    /**
     * @param endpointName
     *            the endpoint name of the endpoint to check
     */
    public void setEndpointName(final String endpointName) {
        this.endpointName = endpointName;
    }

    /**
     * @param expectedNumber
     *            the expected number of endpoint satisfying the criteria
     */
    public void setExpectedNumber(final int expectedNumber) {
        this.expectedNumber = expectedNumber;
    }

    @Override
    public boolean eval() throws BuildException {

        try {
            final JMXClient jmxClient = PetalsJmxApiFactory.getInstance().createJMXClient(this.jmxHost, this.jmxPort,
                    this.jmxUsername, this.jmxPassword);
            try {
                int endpointMatching = 0;
                final List> endpoints = jmxClient.getEndpointDirectoryClient().getAllEndpoints();
                for (final Map endpoint : endpoints) {
                    final boolean isSameInterfaceName = this.isSameInterfaceName(endpoint);
                    final boolean isSameServiceName = this.serviceName != null ? this.serviceName
                            .equals(endpoint.get(EndpointDirectoryServiceConstants.Endpoint.SERVICE_NAME)) : true;
                    final boolean isSameEndpointName = this.endpointName != null ? this.endpointName
                            .equals(endpoint.get(EndpointDirectoryServiceConstants.Endpoint.ENDPOINT_NAME)) : true;

                    if (isSameInterfaceName && isSameServiceName && isSameEndpointName) {
                        endpointMatching++;
                    }
                }

                return endpointMatching == this.expectedNumber;
            } finally {
                jmxClient.disconnect();
            }
        } catch (final ConnectionErrorException | DuplicatedServiceException | MissingServiceException
                | EndpointDirectoryServiceDoesNotExistException | EndpointDirectoryServiceErrorException
                | EndpointDirectoryException e) {
            throw new BuildException(e);
        }
    }

    private boolean isSameInterfaceName(final Map endpoint) {
        boolean isSameInterfaceName = false;
        if (this.interfaceName != null) {
            for (final String itfName : (String[]) endpoint
                    .get(EndpointDirectoryServiceConstants.Endpoint.INTERFACE_NAMES)) {
                if (itfName.equals(this.interfaceName)) {
                    isSameInterfaceName = true;
                    break;
                }
            }
        } else {
            isSameInterfaceName = true;
        }

        return isSameInterfaceName;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy