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

org.ow2.petals.admin.jmx.registry.JMXRegistryView Maven / Gradle / Ivy

/****************************************************************************
 *
 * Copyright (c) 2010-2012, EBM WebSourcing
 * 
 * This 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 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 *
 *****************************************************************************/

package org.ow2.petals.admin.jmx.registry;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.ow2.petals.admin.api.exception.RegistryRegexpPatternException;
import org.ow2.petals.admin.registry.Endpoint;
import org.ow2.petals.admin.registry.Endpoint.EndpointType;
import org.ow2.petals.admin.registry.RegistryView;
import org.ow2.petals.jmx.commons.PetalsEndpointRegistryServiceConstants;

/**
 * 
 * @author Nicolas Oddoux - EBM WebSourcing
 */
public class JMXRegistryView implements RegistryView {

    private final List endpoints;
    
    private final Map> endpointsByServiceName;
    
    private final Map> endpointsByInterfaceName;
    
    public JMXRegistryView(List> endpointMaps, String endpointNameRegex,
            String serviceNameRegex, String interfaceNameRegex)
            throws RegistryRegexpPatternException {
        this.endpoints = new ArrayList();
        this.endpointsByServiceName = new HashMap>();
        this.endpointsByInterfaceName = new HashMap>();
        
        initialize(endpointMaps, endpointNameRegex, serviceNameRegex, interfaceNameRegex);
    }

    private void initialize(List> endpointMaps, String endpointNameRegex,
            String serviceNameRegex, String interfaceNameRegex)
            throws RegistryRegexpPatternException {
        try {
            Pattern endpointNamePattern = Pattern.compile(endpointNameRegex);
            Pattern serviceNamePattern = Pattern.compile(serviceNameRegex);
            Pattern interfaceNamePattern = Pattern.compile(interfaceNameRegex);
            
            for (Map endpointMap : endpointMaps) {
                String endpointName = (String) endpointMap
                        .get(PetalsEndpointRegistryServiceConstants.Endpoint.ENDPOINT_NAME);
                String endpointTypeStr = (String) endpointMap
                        .get(PetalsEndpointRegistryServiceConstants.Endpoint.TYPE);
                EndpointType endpointType;
                if (endpointTypeStr
                        .equals(PetalsEndpointRegistryServiceConstants.EndpointType.INTERNAL)) {
                    endpointType = EndpointType.INTERNAL;
                } else {
                    endpointType = EndpointType.EXTERNAL;
                }
                String containerName = (String) endpointMap
                        .get(PetalsEndpointRegistryServiceConstants.Endpoint.CONTAINER_NAME);
                String componentName = (String) endpointMap
                        .get(PetalsEndpointRegistryServiceConstants.Endpoint.COMPONENT_NAME);
                String serviceName = (String) endpointMap
                        .get(PetalsEndpointRegistryServiceConstants.Endpoint.SERVICE_NAME);
                List interfaceNames = Arrays.asList((String[]) endpointMap
                        .get(PetalsEndpointRegistryServiceConstants.Endpoint.INTERFACE_NAMES));

                if (isSelectedEndpoint(endpointName, serviceName, interfaceNames,
                        endpointNamePattern, serviceNamePattern, interfaceNamePattern)) {
                    Endpoint endpoint = new Endpoint(endpointName, endpointType, containerName,
                            componentName, serviceName, interfaceNames);
                    this.endpoints.add(endpoint);

                    List endpointsForThisServiceName;
                    if (this.endpointsByServiceName.containsKey(serviceName)) {
                        endpointsForThisServiceName = this.endpointsByServiceName.get(serviceName);
                    } else {
                        endpointsForThisServiceName = new ArrayList();
                        this.endpointsByServiceName.put(serviceName, endpointsForThisServiceName);
                    }
                    endpointsForThisServiceName.add(endpoint);

                    for (String interfaceName : interfaceNames) {
                        List endpointsForThisInterfaceName;
                        if (this.endpointsByInterfaceName.containsKey(interfaceName)) {
                            endpointsForThisInterfaceName = this.endpointsByInterfaceName
                                    .get(interfaceName);
                        } else {
                            endpointsForThisInterfaceName = new ArrayList();
                            this.endpointsByInterfaceName.put(interfaceName,
                                    endpointsForThisInterfaceName);
                        }
                        endpointsForThisInterfaceName.add(endpoint);
                    }
                }
            }
        } catch (final PatternSyntaxException e) {
            throw new RegistryRegexpPatternException(e.getPattern(), e.getDescription());
        }
    }
    
    private static final boolean isSelectedEndpoint(String endpointName, String serviceName,
            List interfaceNames, Pattern endpointNamePattern, Pattern serviceNamePattern,
            Pattern interfaceNamePattern) {
        
        Matcher endpointNameMatcher = endpointNamePattern.matcher(endpointName);
        boolean isSelectedEndpoint = endpointNameMatcher.matches();
        
        if(isSelectedEndpoint) {
            Matcher serviceNameMatcher = serviceNamePattern.matcher(serviceName);
            isSelectedEndpoint = serviceNameMatcher.matches();
            
            if(isSelectedEndpoint) {
                for (String interfaceName : interfaceNames) {
                    Matcher interfaceNameMatcher = interfaceNamePattern.matcher(interfaceName);
                    isSelectedEndpoint = interfaceNameMatcher.matches();
                    if(isSelectedEndpoint) {
                        break;
                    }
                }
            }
        }
        
        return isSelectedEndpoint;
    }

    @Override
    public List getAllEndpoints() {
        return this.endpoints;
    }

    @Override
    public Map> getListOfEndpointsByServiceName() {
        return this.endpointsByServiceName;
    }

    @Override
    public Map> getListOfEndpointsByInterfaceName() {
        return this.endpointsByInterfaceName;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy