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

org.ow2.petals.admin.jmx.JMXContainerAdministration Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/**
 * Copyright (c) 2012 EBM WebSourcing, 2012-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.admin.jmx;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.ow2.petals.admin.api.ArtifactAdministration;
import org.ow2.petals.admin.api.ContainerAdministration;
import org.ow2.petals.admin.api.artifact.Artifact;
import org.ow2.petals.admin.api.artifact.Component;
import org.ow2.petals.admin.api.artifact.Logger;
import org.ow2.petals.admin.api.artifact.ServiceAssembly;
import org.ow2.petals.admin.api.artifact.SharedLibrary;
import org.ow2.petals.admin.api.exception.ArtifactAdministrationException;
import org.ow2.petals.admin.api.exception.ContainerAdministrationException;
import org.ow2.petals.admin.api.exception.InvalidLogLevelException;
import org.ow2.petals.admin.api.exception.LoggerNotFoundException;
import org.ow2.petals.admin.api.exception.NoConnectionException;
import org.ow2.petals.admin.jmx.topology.TopologyUtils;
import org.ow2.petals.admin.topology.Container;
import org.ow2.petals.admin.topology.Container.PortType;
import org.ow2.petals.admin.topology.Domain;
import org.ow2.petals.basisapi.exception.PetalsException;
import org.ow2.petals.clientserverapi.system.logging.exception.LoggingServiceException;
import org.ow2.petals.clientserverapi.topology.RemoteContainer;
import org.ow2.petals.clientserverapi.topology.exception.TopologyServiceException;
import org.ow2.petals.jmx.api.api.JMXClient;
import org.ow2.petals.jmx.api.api.exception.AdminDoesNotExistException;
import org.ow2.petals.jmx.api.api.exception.AdminServiceErrorException;
import org.ow2.petals.jmx.api.api.exception.ConnectionErrorException;
import org.ow2.petals.jmx.api.api.exception.LoggerServiceDoesNotExistException;
import org.ow2.petals.jmx.api.api.exception.LoggerServiceErrorException;
import org.ow2.petals.jmx.api.api.exception.NotRemoteJMXClientException;
import org.ow2.petals.jmx.api.api.exception.PetalsAdminDoesNotExistException;
import org.ow2.petals.jmx.api.api.exception.PetalsAdminServiceErrorException;
import org.ow2.petals.jmx.api.api.exception.TopologyServiceDoesNotExistException;
import org.ow2.petals.jmx.api.api.exception.TopologyServiceErrorException;

/**
 * 
 * @author Nicolas Oddoux - EBM WebSourcing
 */
public class JMXContainerAdministration implements ContainerAdministration {

    private final ArtifactAdministration artifactAdmin;

    protected JMXContainerAdministration(final ArtifactAdministration artifactAdmin) {
        this.artifactAdmin = artifactAdmin;
    }

    @Override
    public void connect(String host, int port, String user, String password)
            throws ContainerAdministrationException {
        try {
            JMXClientConnection.createJMXClient(host, port, user, password);
        } catch (ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        }
    }

    @Override
    public boolean isConnected() throws ContainerAdministrationException {
        return JMXClientConnection.isConnected();
    }

    @Override
    public void disconnect() throws NoConnectionException, ContainerAdministrationException {
        try {
            JMXClientConnection.disconnectJMXClient();
        } catch (ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        }
    }

    @Override
    public String getSystemInfo() throws ContainerAdministrationException {
        try {
            JMXClient client = JMXClientConnection.getJMXClient();
            String systemInfo = client.getAdminServiceClient().getSystemInfo();
            return systemInfo;
        } catch (ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (AdminServiceErrorException asee) {
            throw new ContainerAdministrationException(asee);
        } catch (AdminDoesNotExistException adnee) {
            throw new ContainerAdministrationException(adnee);
        }
    }

    @Override
    public void stopContainer() throws ContainerAdministrationException {
        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            client.getPetalsAdminServiceClient().stopContainer();
            JMXClientConnection.disconnectJMXClient();
        } catch (final ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (final PetalsAdminServiceErrorException pasee) {
            throw new ContainerAdministrationException(pasee);
        } catch (final PetalsAdminDoesNotExistException padnee) {
            throw new ContainerAdministrationException(padnee);
        } catch (final PetalsException e) {
            throw new ContainerAdministrationException(e);
        }
    }

    @Override
    public void shutdownContainer() throws ContainerAdministrationException {
        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            client.getPetalsAdminServiceClient().shutdownContainer();
            JMXClientConnection.disconnectJMXClient();
        } catch (final ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (final PetalsAdminServiceErrorException pasee) {
            throw new ContainerAdministrationException(pasee);
        } catch (final PetalsAdminDoesNotExistException padnee) {
            throw new ContainerAdministrationException(padnee);
        } catch (final PetalsException e) {
            throw new ContainerAdministrationException(e);
        }
    }

    @Override
    public Domain getTopology(final String containerFilterRegexp, final String securityPassPhrase,
            final boolean retrieveJbiArtifacts)
            throws ContainerAdministrationException {
        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            final Set> topology = client.getTopologyServiceClient().retrieveTopology(
                    securityPassPhrase, false);
            final Domain domain = TopologyUtils.createListOfDomains(topology, containerFilterRegexp);
            if (retrieveJbiArtifacts) {
                for (final Container container : domain.getContainers()) {
                    this.retrieveJbiArtifacts(container);
                }
            }
            return domain;
        } catch (final ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (final TopologyServiceErrorException pasee) {
            throw new ContainerAdministrationException(pasee);
        } catch (final TopologyServiceDoesNotExistException padnee) {
            throw new ContainerAdministrationException(padnee);
        } catch (final PetalsException e) {
            throw new ContainerAdministrationException(e);
        }
    }

    private void retrieveJbiArtifacts(final Container container) throws ContainerAdministrationException {

        // Caution: to get artifacts deployed on other containers we must change the JMX connection, and restore the
        // initial one at end.
        // TODO: Rework JMX connection management to avoid to have a static JMX client.
        try {
            final boolean isAlreadyConnected;
            final JMXClient initialJmxClient = JMXClientConnection.getJMXClient();
            if (InetAddress.getByName(container.getHost()).equals(initialJmxClient.getHost())
                    && container.getPorts().get(PortType.JMX).intValue() == initialJmxClient.getPort()) {
                // NOP: I'm already connected on this container, I reuse the connection
                isAlreadyConnected = true;
            } else {
                // It's another container
                isAlreadyConnected = false;
            }

            try {
                if (!isAlreadyConnected) {
                    JMXClientConnection.disconnectJMXClient();
                    JMXClientConnection.createJMXClient(container.getHost(),
                            container.getPorts().get(PortType.JMX).intValue(), container.getJmxUsername(),
                            container.getJmxPassword());
                }
                final List artifacts = this.artifactAdmin.listArtifacts();
                for (final Artifact artifact : artifacts) {
                    if (artifact instanceof ServiceAssembly) {
                        container.addServiceAssembly((ServiceAssembly) artifact);
                    } else if (artifact instanceof Component) {
                        container.addComponent((Component) artifact);
                    } else if (artifact instanceof SharedLibrary) {
                        container.addSharedLibrary((SharedLibrary) artifact);
                    } else {
                        throw new ArtifactAdministrationException(
                                String.format("Unknown artifact: %s(%s)", artifact.getName(), artifact.getType()));
                    }
                }
            } catch (final ConnectionErrorException e) {
                // If we are not able to connect to container different from the initial one, we skip to retrieve JBI
                // artifacts.
                // In a distributed topology, it is needed to provide the pass-phrase to get artifacts deployed on
                // remote
                // containers
            } catch (final ArtifactAdministrationException e) {
                throw new ContainerAdministrationException(e);
            } finally {
                if (!isAlreadyConnected) {
                    try {
                        JMXClientConnection.disconnectJMXClient();
                    } catch (final NoConnectionException e) {
                        // NOP, because no connection previously exists
                    }
                    JMXClientConnection.createJMXClient(initialJmxClient.getHost().getHostName(),
                            initialJmxClient.getPort(), initialJmxClient.getUsername(), initialJmxClient.getPassword());
                }
            }
        } catch (final ConnectionErrorException e) {
            throw new ContainerAdministrationException(e);
        } catch (final NotRemoteJMXClientException e) {
            throw new ContainerAdministrationException(e);
        } catch (final UnknownHostException e) {
            throw new ContainerAdministrationException(e);
        }
    }

    @Override
    public Properties getServerProperties() throws ContainerAdministrationException {
        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            return client.getPetalsAdminServiceClient().retrieveServerProperties();
        } catch (final ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (final PetalsAdminServiceErrorException pasee) {
            throw new ContainerAdministrationException(pasee);
        } catch (final PetalsAdminDoesNotExistException padnee) {
            throw new ContainerAdministrationException(padnee);
        } catch (final PetalsException e) {
            throw new ContainerAdministrationException(e);
        }
    }

    @Override
    public void changeLoggerLevel(String loggerName, String loggerLevel)
            throws LoggerNotFoundException, InvalidLogLevelException,
            ContainerAdministrationException {
        // TODO: LoggerNotFoundException and InvalidLogLevelException must be
        // thrown here according to the result of setLevelForLogger because the
        // container knows its log level and loggers.
        try {
            JMXClient client = JMXClientConnection.getJMXClient();
            client.getLoggerServiceClient().setLoggerLevel(loggerName, loggerLevel);
        } catch (ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (LoggerServiceErrorException lsee) {
            throw new ContainerAdministrationException(lsee);
        } catch (LoggerServiceDoesNotExistException ldnee) {
            throw new ContainerAdministrationException(ldnee);
        }
    }
    
    @Override
    public List getLoggers()
            throws ContainerAdministrationException {
        try {
            JMXClient client = JMXClientConnection.getJMXClient();
            final String[][] loggersArray = client.getLoggerServiceClient().getLoggers();
            final List loggers = new LinkedList<>();
            for (int i = 0; i < loggersArray.length; i++) {
                loggers.add(new Logger(loggersArray[i][0], loggersArray[i][1]));
            }
            return loggers;
        } catch (final ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (final LoggerServiceErrorException lsee) {
            throw new ContainerAdministrationException(lsee);
        } catch (final LoggerServiceDoesNotExistException lsdnee) {
            throw new ContainerAdministrationException(lsdnee);
        } catch (final LoggingServiceException e) {
            throw new ContainerAdministrationException(e);
        }
    }

    @Override
    public void attachContainer(final String targetDomainName, final String targetContainerHost,
            final int targetContainerPort, final String targetContainerUser, final String targetContainerPwd,
            final String securityPassPhrase) throws ContainerAdministrationException {
        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            final RemoteContainer targetContainerConfiguration = new RemoteContainer();
            targetContainerConfiguration.setHost(targetContainerHost);
            targetContainerConfiguration.setPort(targetContainerPort);
            targetContainerConfiguration.setUser(targetContainerUser);
            targetContainerConfiguration.setPassword(targetContainerPwd);
            targetContainerConfiguration.setDomainName(targetDomainName);

            client.getTopologyServiceClient().attachContainerTo(targetContainerConfiguration, securityPassPhrase);

        } catch (final ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (final TopologyServiceErrorException lsee) {
            throw new ContainerAdministrationException(lsee);
        } catch (final TopologyServiceDoesNotExistException ldnee) {
            throw new ContainerAdministrationException(ldnee);
        } catch (final TopologyServiceException e) {
            throw new ContainerAdministrationException(e);
        }
    }

    @Override
    public void detachContainer() throws ContainerAdministrationException {
        try {
            JMXClientConnection.getJMXClient().getTopologyServiceClient().detachContainer();

        } catch (final ConnectionErrorException cee) {
            throw new ContainerAdministrationException(cee);
        } catch (final TopologyServiceErrorException lsee) {
            throw new ContainerAdministrationException(lsee);
        } catch (final TopologyServiceDoesNotExistException ldnee) {
            throw new ContainerAdministrationException(ldnee);
        } catch (final TopologyServiceException e) {
            throw new ContainerAdministrationException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy