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

org.ow2.petals.admin.jmx.topology.TopologyUtils 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.topology;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.ow2.petals.admin.topology.Container;
import org.ow2.petals.admin.topology.Container.PortType;
import org.ow2.petals.admin.topology.Container.State;
import org.ow2.petals.admin.topology.Domain;
import org.ow2.petals.clientserverapi.topology.TopologyService;
import org.ow2.petals.clientserverapi.topology.TopologyService.ContainerPropertyValues;

import com.ebmwebsourcing.easycommons.lang.UncheckedException;

/**
 * 
 * @author Nicolas Oddoux - EBM WebSourcing
 */
public class TopologyUtils {

    private static final BidiMap PORT_TYPE_CONVERTER = new DualHashBidiMap();

    private static final BidiMap STATE_VALUE_CONVERTER = new DualHashBidiMap();

    static {
        PORT_TYPE_CONVERTER.put(TopologyService.ContainerPropertyName.CONF_JMX_RMI_PORT, PortType.JMX);
        PORT_TYPE_CONVERTER.put(TopologyService.ContainerPropertyName.CONF_TRANSPORT_TCP_PORT, PortType.TCP_TRANSPORT);

        STATE_VALUE_CONVERTER.put(ContainerPropertyValues.STATE_REACHABLE, State.REACHABLE);
        STATE_VALUE_CONVERTER.put(ContainerPropertyValues.STATE_UNREACHABLE, State.UNREACHABLE);
    }

    /**
     * 

* Create a {@link Domain} according to: *

    *
  • the provided topology,
  • *
  • a regexp filtering container to add.
  • *
*

* * @param containerFilterRegexp * A container name regexp to filter the containers to return * @return A complete domain definition or limited to the provided filters. */ public static final Domain createListOfDomains(final Set> topology, final String containerFilterRegexp) { final Pattern containerNamePattern = Pattern.compile(containerFilterRegexp); Domain domain = null; final List containersForThisDomain = new ArrayList(); for (final Map map : topology) { final String topologyType = map.get(TopologyService.TopologyItem.TOPOLOGY_TYPE); if (topologyType.equals(TopologyService.TopologyItem.DOMAIN)) { if (domain != null) { throw new UncheckedException("Error in the topology map: more than one domain"); } final String name = map.get(TopologyService.DomainPropertyName.CONF_DOMAIN_NAME); domain = new Domain(name); } else if (topologyType.equals(TopologyService.TopologyItem.CONTAINER)) { final String name = map.get(TopologyService.ContainerPropertyName.CONF_CONTAINER_NAME); final Matcher containerNameMatcher = containerNamePattern.matcher(name); if (containerNameMatcher.matches()) { final String host = map.get(TopologyService.ContainerPropertyName.CONF_HOST); final String jmxUsername = map.get(TopologyService.ContainerPropertyName.CONF_USER); final String jmxPassword = map.get(TopologyService.ContainerPropertyName.CONF_PWD); final String state = map.get(TopologyService.ContainerPropertyName.CONF_STATE); final HashMap ports = new HashMap(); for (final String portType : PORT_TYPE_CONVERTER.keySet()) { if (map.containsKey(portType)) { final int portValue = Integer.valueOf(map.get(portType)); ports.put(PORT_TYPE_CONVERTER.get(portType), portValue); } } final Container container = new Container(name, host, ports, jmxUsername, jmxPassword, STATE_VALUE_CONVERTER.get(state) == null ? State.UNKNOWN : STATE_VALUE_CONVERTER.get(state)); containersForThisDomain.add(container); } } else { throw new UncheckedException(String.format("Unrecognized topology type: '%s'", topologyType)); } } if (domain == null) { throw new UncheckedException("Error in the topology map: no domain"); } domain.addContainers(containersForThisDomain); return domain; } /** * Convert a topology given as {@link Domain} into {@link Set>} * * @param domain * The topology as {@link Domain} * @return The topology as {@link Set>} */ public static final Set> createTopologyMapSet(final Domain domain) { final Set> topology = new HashSet<>(); // domain map final Map domainMap = new HashMap<>(); domainMap.put(TopologyService.TopologyItem.TOPOLOGY_TYPE, TopologyService.TopologyItem.DOMAIN); domainMap.put(TopologyService.DomainPropertyName.CONF_DOMAIN_NAME, domain.getName()); topology.add(domainMap); // container maps for (final Container container : domain.getContainers()) { final Map containerMap = new HashMap<>(); containerMap.put(TopologyService.TopologyItem.TOPOLOGY_TYPE, TopologyService.TopologyItem.CONTAINER); containerMap.put(TopologyService.ContainerPropertyName.CONF_CONTAINER_NAME, container.getContainerName()); containerMap.put(TopologyService.ContainerPropertyName.CONF_HOST, container.getHost()); containerMap.put(TopologyService.ContainerPropertyName.CONF_USER, container.getJmxUsername()); containerMap.put(TopologyService.ContainerPropertyName.CONF_PWD, container.getJmxPassword()); containerMap.put(TopologyService.ContainerPropertyName.CONF_STATE, STATE_VALUE_CONVERTER.getKey(container.getState())); for (final Entry portEntry : container.getPorts().entrySet()) { containerMap.put(PORT_TYPE_CONVERTER.getKey(portEntry.getKey()), portEntry.getValue().toString()); } topology.add(containerMap); } return topology; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy