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

org.ow2.petals.admin.jmx.JMXArtifactAdministration 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.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.management.ObjectName;

import org.ow2.petals.admin.api.ArtifactAdministration;
import org.ow2.petals.admin.api.artifact.Artifact;
import org.ow2.petals.admin.api.artifact.ArtifactState.State;
import org.ow2.petals.admin.api.artifact.Component;
import org.ow2.petals.admin.api.artifact.Component.ComponentType;
import org.ow2.petals.admin.api.artifact.ServiceAssembly;
import org.ow2.petals.admin.api.artifact.ServiceUnit;
import org.ow2.petals.admin.api.artifact.SharedLibrary;
import org.ow2.petals.admin.api.artifact.lifecycle.ArtifactLifecycleFactory;
import org.ow2.petals.admin.api.artifact.lifecycle.ComponentLifecycle;
import org.ow2.petals.admin.api.artifact.lifecycle.ServiceAssemblyLifecycle;
import org.ow2.petals.admin.api.conf.PetalsAdminConfiguration;
import org.ow2.petals.admin.api.exception.ArtifactAdministrationException;
import org.ow2.petals.admin.api.exception.ArtifactNotFoundException;
import org.ow2.petals.admin.api.exception.ArtifactTypeIsNeededException;
import org.ow2.petals.admin.api.exception.ArtifactVersionIsNeededException;
import org.ow2.petals.admin.api.exception.UnsupportedArtifactTypeException;
import org.ow2.petals.admin.jmx.artifact.JMXArtifactLifecycleFactory;
import org.ow2.petals.jbi.descriptor.original.JBIDescriptorBuilder;
import org.ow2.petals.jbi.descriptor.original.generated.Jbi;
import org.ow2.petals.jmx.api.api.DeploymentServiceClient;
import org.ow2.petals.jmx.api.api.InstallationServiceClient;
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.DeploymentServiceDoesNotExistException;
import org.ow2.petals.jmx.api.api.exception.DeploymentServiceErrorException;
import org.ow2.petals.jmx.api.api.exception.InstallationServiceDoesNotExistException;
import org.ow2.petals.jmx.api.api.exception.InstallationServiceErrorException;

/**
 * 
 * @author Nicolas Oddoux - EBM WebSourcing
 */
public class JMXArtifactAdministration extends ArtifactAdministration {

    /**
     * The current configuration
     */
    private final PetalsAdminConfiguration configuration;

    protected JMXArtifactAdministration(final ArtifactLifecycleFactory artifactLifecycleFactory,
            final PetalsAdminConfiguration configuration) {
        super(artifactLifecycleFactory);
        this.configuration = configuration;
    }

    @Override
    public void stopAndUndeployAllArtifacts() throws ArtifactAdministrationException {
        try {
            JMXClient client = JMXClientConnection.getJMXClient();
            DeploymentServiceClient deploymentServiceClient = client.getDeploymentServiceClient();
            deploymentServiceClient.stopAllServiceAssemblies();
            deploymentServiceClient.shutdownAllServiceAssemblies();
            deploymentServiceClient.undeployAllServiceAssemblies(true);
            InstallationServiceClient installationServiceClient = client
                    .getInstallationServiceClient();
            installationServiceClient.stopAllComponents();
            installationServiceClient.shutdownAllComponents();
            installationServiceClient.uninstallAllComponents();
            installationServiceClient.unloadAllInstallers(true);
            installationServiceClient.uninstallAllSharedLibrary();
        } catch (ConnectionErrorException cee) {
            throw new ArtifactAdministrationException(cee);
        } catch (DeploymentServiceErrorException dsee) {
            throw new ArtifactAdministrationException(dsee);
        } catch (DeploymentServiceDoesNotExistException dsdnee) {
            throw new ArtifactAdministrationException(dsdnee);
        } catch (InstallationServiceDoesNotExistException isdnee) {
            throw new ArtifactAdministrationException(isdnee);
        } catch (InstallationServiceErrorException isee) {
            throw new ArtifactAdministrationException(isee);
        }
    }

    @Override
    public List listArtifacts() throws ArtifactAdministrationException {
        try {
            final JMXArtifactLifecycleFactory factory = new JMXArtifactLifecycleFactory(this.configuration);
            final List artifacts = new ArrayList();

            final JMXClient client = JMXClientConnection.getJMXClient();

            // Retrieve shared libraries
            final String[][] sls = client.getInstallationServiceClient().getInstalledSharedLibraries();
            for (final String[] sl : sls) {
                artifacts.add(new SharedLibrary(sl[0], sl[1]));
            }

            // Retrieve components BC and SE
            final ObjectName[] bindingComponents = client.getAdminServiceClient().getBindingComponents();
            for (final ObjectName bindingComponent : bindingComponents) {
                final String name = bindingComponent.getKeyProperty("name");
                final Component component = new Component(name, ComponentType.BC);
                final ComponentLifecycle componentLifecycle = factory.createComponentLifecycle(component);
                componentLifecycle.updateState();
                artifacts.add(component);
            }

            final ObjectName[] serviceEngines = client.getAdminServiceClient().getEngineComponents();
            for (final ObjectName serviceEngine : serviceEngines) {
                final String name = serviceEngine.getKeyProperty("name");
                final Component component = new Component(name, ComponentType.SE);
                final ComponentLifecycle componentLifecycle = factory.createComponentLifecycle(component);
                componentLifecycle.updateState();
                artifacts.add(component);
            }

            // Retrieve components that have only their installer loaded
            final String[] installers = client.getInstallationServiceClient().getInstallers();
            for (final String installer : installers) {
                boolean alreadyAdded = false;
                for (final Artifact alreadyAddedArtifact : artifacts) {
                    // We check the name first because it is more differential
                    // than the type of artifact
                    if (alreadyAddedArtifact.getName().equals(installer)
                            && alreadyAddedArtifact instanceof Component) {
                        alreadyAdded = true;
                        break;
                    }
                }
                if (!alreadyAdded) {
                    final Component component;
                    if (client.getAdminServiceClient().isBinding(installer)) {
                        component = new Component(installer, ComponentType.BC);
                    } else if (client.getAdminServiceClient().isEngine(installer)) {
                        component = new Component(installer, ComponentType.SE);
                    } else {
                        // This exception should not occurs
                        throw new ArtifactAdministrationException(
                                "Unable to determine the type of the component for installer '"
                                        + installer + "'.");
                    }
                    component.setState(State.LOADED);
                    artifacts.add(component);
                }
            }

            // Retrieve service assemblies
            final String[] saNames = client.getDeploymentServiceClient().getDeployedServiceAssemblies();
            for (final String saName : saNames) {
                final ServiceAssembly serviceAssembly = createServiceAssembly(client, saName);
                final ServiceAssemblyLifecycle saLifecycle = factory
                        .createServiceAssemblyLifecycle(serviceAssembly);
                saLifecycle.updateState();
                artifacts.add(serviceAssembly);
            }

            return artifacts;
        } catch (final ConnectionErrorException cee) {
            throw new ArtifactAdministrationException(cee);
        } catch (final AdminServiceErrorException asee) {
            throw new ArtifactAdministrationException(asee);
        } catch (final AdminDoesNotExistException adnee) {
            throw new ArtifactAdministrationException(adnee);
        } catch (final DeploymentServiceErrorException dsee) {
            throw new ArtifactAdministrationException(dsee);
        } catch (final DeploymentServiceDoesNotExistException dsdnee) {
            throw new ArtifactAdministrationException(dsdnee);
        } catch (final InstallationServiceErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (final InstallationServiceDoesNotExistException e) {
            throw new ArtifactAdministrationException(e);
        }
    }

    @Override
    public Artifact getArtifact(final String type, final String name, final String version)
            throws UnsupportedArtifactTypeException, ArtifactNotFoundException, ArtifactTypeIsNeededException,
            ArtifactVersionIsNeededException, ArtifactAdministrationException {

        final Artifact artifact;
        if (type != null) {
            if (ComponentType.BC.toString().equals(type)) {
                artifact = this.getArtifactBC(name);
            } else if (ComponentType.SE.toString().equals(type)) {
                artifact = this.getArtifactSE(name);
            } else if (SharedLibrary.TYPE.equals(type)) {
                artifact = this.getArtifactSL(name, version);
            } else if (ServiceAssembly.TYPE.equals(type)) {
                artifact = this.getArtifactSA(name);
            } else {
                throw new UnsupportedArtifactTypeException(type);
            }

            if (artifact == null) {
                throw new ArtifactNotFoundException(type, name, version);
            }
        }
        else {
            final Artifact artifactBC = this.getArtifactBC(name);
            final Artifact artifactSE = this.getArtifactSE(name);
            final Artifact artifactSL = this.getArtifactSL(name, version);
            final Artifact artifactSA = this.getArtifactSA(name);
            if (artifactBC != null && artifactSE == null && artifactSL == null && artifactSA == null) {
                artifact = artifactBC;
            }
            else if (artifactBC == null && artifactSE != null && artifactSL == null && artifactSA == null) {
                artifact = artifactSE;
            }
            else if (artifactBC == null && artifactSE == null && artifactSL != null && artifactSA == null) {
                artifact = artifactSL;
            }
            else if (artifactBC == null && artifactSE == null && artifactSL == null && artifactSA != null) {
                artifact = artifactSA;
            }
            else if (artifactBC == null && artifactSE == null && artifactSL == null && artifactSA == null) {
                throw new ArtifactNotFoundException(type, name, version);
            }
            else {
                // More than one artifact match the name. 
                throw new ArtifactTypeIsNeededException(name);
            }
        }

        return artifact;
    }

    private Artifact getArtifactBC(final String name) throws ArtifactAdministrationException {

        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            final ObjectName[] bindingComponents = client.getAdminServiceClient()
                    .getBindingComponents();
            for (final ObjectName bindingComponent : bindingComponents) {
                final String bcName = bindingComponent.getKeyProperty("name");
                if (bcName.equals(name)) {
                    return new Component(name, ComponentType.BC);
                }
            }

            // Here, no BC with the given name is installed. We check if a
            // component installer was only loaded.
            if (client.getAdminServiceClient().isBinding(name)) {
                // A component installer exists for this binding component
                return new Component(name, ComponentType.BC);
            } else {
                return null;
            }
        } catch (final ConnectionErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (final AdminServiceErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (final AdminDoesNotExistException e) {
            throw new ArtifactAdministrationException(e);
        }
    }

    private Artifact getArtifactSE(final String name) throws ArtifactAdministrationException {

        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            final ObjectName[] bindingComponents = client.getAdminServiceClient()
                    .getEngineComponents();
            for (final ObjectName bindingComponent : bindingComponents) {
                final String seName = bindingComponent.getKeyProperty("name");
                if (seName.equals(name)) {
                    return new Component(name, ComponentType.SE);
                }
            }

            // Here, no SE with the given name is installed. We check if a
            // component installer was only loaded.
            if (client.getAdminServiceClient().isEngine(name)) {
                // A component installer exists for this service engine
                return new Component(name, ComponentType.SE);
            } else {
                return null;
            }
        } catch (final ConnectionErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (AdminServiceErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (AdminDoesNotExistException e) {
            throw new ArtifactAdministrationException(e);
        }
    }

    private Artifact getArtifactSL(final String askedSlName, final String askedSlVersion)
            throws ArtifactAdministrationException {

        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            final String[][] sls = client.getInstallationServiceClient().getInstalledSharedLibraries();
            if (askedSlVersion == null) {
                boolean slNameFound = false;
                String slVersion = null;
                for (final String[] sl : sls) {
                    if (sl[0].equals(askedSlName)) {
                        if (!slNameFound) {
                            slNameFound = true;
                            slVersion = sl[1];
                        } else {
                            // More than one shared library matches the given name (several versions exist)
                            throw new ArtifactVersionIsNeededException(askedSlName);
                        }
                    }
                }
                if (slNameFound) {
                    return new SharedLibrary(askedSlName, slVersion);
                } else {
                    return null;
                }
            } else {
                SharedLibrary result = null;
                for (final String[] sl : sls) {
                    if (sl[0].equals(askedSlName) && sl[1].equals(askedSlVersion)) {
                        result = new SharedLibrary(askedSlName, askedSlVersion);
                        break;
                    }
                }
                return result;
            }
        } catch (final ConnectionErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (final InstallationServiceErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (final InstallationServiceDoesNotExistException e) {
            throw new ArtifactAdministrationException(e);
        }
    }

    private Artifact getArtifactSA(final String name) throws ArtifactAdministrationException {

        try {
            final JMXClient client = JMXClientConnection.getJMXClient();
            final String[] saNames = client.getDeploymentServiceClient()
                    .getDeployedServiceAssemblies();
            for (final String saName : saNames) {
                if (saName.equals(name)) {
                    return createServiceAssembly(client, saName);
                }
            }
            return null;
        } catch (final ConnectionErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (final DeploymentServiceErrorException e) {
            throw new ArtifactAdministrationException(e);
        } catch (final DeploymentServiceDoesNotExistException e) {
            throw new ArtifactAdministrationException(e);
        }
    }

    private ServiceAssembly createServiceAssembly(JMXClient client, String saName)
            throws ArtifactAdministrationException {
        try {
            String descriptor;
            List serviceUnits;
            descriptor = client.getDeploymentServiceClient().getServiceAssemblyDescriptor(saName);

            ByteArrayInputStream bais = new ByteArrayInputStream(descriptor.getBytes());
            Jbi saJbiDescriptor = JBIDescriptorBuilder.getInstance().buildJavaJBIDescriptor(bais);
            bais.close();

            serviceUnits = new ArrayList();
            List suDescriptors = saJbiDescriptor
                    .getServiceAssembly().getServiceUnit();
            for (org.ow2.petals.jbi.descriptor.original.generated.ServiceUnit suDescriptor : suDescriptors) {
                String suName = suDescriptor.getIdentification().getName();
                String suTargetComponentName = suDescriptor.getTarget().getComponentName();
                ServiceUnit serviceUnit = new ServiceUnit(suName, "NOVERSION",
                        suTargetComponentName);
                serviceUnits.add(serviceUnit);
            }
            return new ServiceAssembly(saName, serviceUnits);
        } catch (Exception e) {
            throw new ArtifactAdministrationException(e);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy