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

com.alibaba.dubbo.monitor.simple.RegistryContainer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.dubbo.monitor.simple;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.utils.ConcurrentHashSet;
import com.alibaba.dubbo.common.utils.ConfigUtils;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.container.Container;
import com.alibaba.dubbo.container.spring.SpringContainer;
import com.alibaba.dubbo.registry.NotifyListener;
import com.alibaba.dubbo.registry.RegistryService;

/**
 * RegistryContainer
 * 
 * @author william.liangf
 */
public class RegistryContainer implements Container {

    public static final String REGISTRY_ADDRESS = "dubbo.registry.address";

    private final Set applications = new ConcurrentHashSet();

    private final Map> providerServiceApplications = new ConcurrentHashMap>();

    private final Map> providerApplicationServices = new ConcurrentHashMap>();

    private final Map> consumerServiceApplications = new ConcurrentHashMap>();

    private final Map> consumerApplicationServices = new ConcurrentHashMap>();

    private final Set services = new ConcurrentHashSet();

    private final Map> serviceProviders = new ConcurrentHashMap>();

    private final Map> serviceConsumers = new ConcurrentHashMap>();

    private RegistryService registry;
    
    private static RegistryContainer INSTANCE = null;
    
    public RegistryContainer() {
        INSTANCE = this;
    }
    
    public static RegistryContainer getInstance() {
        if (INSTANCE == null) {
            ExtensionLoader.getExtensionLoader(Container.class).getExtension("registry");
        }
        return INSTANCE;
    }

    public RegistryService getRegistry() {
        return registry;
    }

    public Set getApplications() {
        return Collections.unmodifiableSet(applications);
    }
    
    public Set getDependencies(String application, boolean reverse) {
        if (reverse) {
            Set dependencies = new HashSet();
            Set services = providerApplicationServices.get(application);
            if (services != null && services.size() > 0) {
                for (String service : services) {
                    Set applications = consumerServiceApplications.get(service);
                    if (applications != null && applications.size() > 0) {
                        dependencies.addAll(applications);
                    }
                }
            }
            return dependencies;
        } else {
            Set dependencies = new HashSet();
            Set services = consumerApplicationServices.get(application);
            if (services != null && services.size() > 0) {
                for (String service : services) {
                    Set applications = providerServiceApplications.get(service);
                    if (applications != null && applications.size() > 0) {
                        dependencies.addAll(applications);
                    }
                }
            }
            return dependencies;
        }
    }

    public Set getServices() {
        return Collections.unmodifiableSet(services);
    }

    public Map> getServiceProviders() {
        return Collections.unmodifiableMap(serviceProviders);
    }

    public List getProvidersByService(String service) {
        List urls = serviceProviders.get(service);
        return urls == null ? null : Collections.unmodifiableList(urls);
    }

    public List getProvidersByHost(String host) {
        List urls = new ArrayList();
        if (host != null && host.length() > 0) {
            for (List providers : serviceProviders.values()) {
                for (URL url : providers) {
                    if (host.equals(url.getHost())) {
                        urls.add(url);
                    }
                }
            }
        }
        return urls;
    }

    public List getProvidersByApplication(String application) {
        List urls = new ArrayList();
        if (application != null && application.length() > 0) {
            for (List providers : serviceProviders.values()) {
                for (URL url : providers) {
                    if (application.equals(url.getParameter(Constants.APPLICATION_KEY))) {
                        urls.add(url);
                    }
                }
            }
        }
        return urls;
    }

    public Set getHosts() {
        Set addresses = new HashSet();
        for (List providers : serviceProviders.values()) {
            for (URL url : providers) {
                addresses.add(url.getHost());
            }
        }
        for (List providers : serviceConsumers.values()) {
            for (URL url : providers) {
                addresses.add(url.getHost());
            }
        }
        return addresses;
    }

    public Map> getServiceConsumers() {
        return Collections.unmodifiableMap(serviceConsumers);
    }

    public List getConsumersByService(String service) {
        List urls = serviceConsumers.get(service);
        return urls == null ? null : Collections.unmodifiableList(urls);
    }

    public List getConsumersByHost(String host) {
        List urls = new ArrayList();
        if (host != null && host.length() > 0) {
            for (List consumers : serviceConsumers.values()) {
                for (URL url : consumers) {
                    if (host.equals(url.getHost())) {
                        urls.add(url);
                    }
                }
            }
        }
        return Collections.unmodifiableList(urls);
    }

    public List getConsumersByApplication(String application) {
        List urls = new ArrayList();
        if (application != null && application.length() > 0) {
            for (List consumers : serviceConsumers.values()) {
                for (URL url : consumers) {
                    if (application.equals(url.getParameter(Constants.APPLICATION_KEY))) {
                        urls.add(url);
                    }
                }
            }
        }
        return urls;
    }

    public void start() {
        String url = ConfigUtils.getProperty(REGISTRY_ADDRESS);
        if (url == null || url.length() == 0) {
            throw new IllegalArgumentException("Please set java start argument: -D" + REGISTRY_ADDRESS + "=zookeeper://127.0.0.1:2181");
        }
        registry = (RegistryService) SpringContainer.getContext().getBean("registryService");
        URL subscribeUrl = new URL(Constants.ADMIN_PROTOCOL, NetUtils.getLocalHost(), 0, "",
                                    Constants.INTERFACE_KEY, Constants.ANY_VALUE, 
                                    Constants.GROUP_KEY, Constants.ANY_VALUE, 
                                    Constants.VERSION_KEY, Constants.ANY_VALUE,
                                    Constants.CLASSIFIER_KEY, Constants.ANY_VALUE,
                                    Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY + "," 
                                            + Constants.CONSUMERS_CATEGORY,
                                    Constants.CHECK_KEY, String.valueOf(false));
        registry.subscribe(subscribeUrl, new NotifyListener() {
            public void notify(List urls) {
                if (urls == null || urls.size() == 0) {
                    return;
                }
                Map> proivderMap = new HashMap>();
                Map> consumerMap = new HashMap>();
                for (URL url : urls) {
                    String application = url.getParameter(Constants.APPLICATION_KEY);
                    if (application != null && application.length() > 0) {
                        applications.add(application);
                    }
                    String service = url.getServiceInterface();
                    services.add(service);
                    String category = url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
                    if (Constants.PROVIDERS_CATEGORY.equals(category)) {
                        if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                            serviceProviders.remove(service);
                        } else {
                            List list = proivderMap.get(service);
                            if (list == null) {
                                list = new ArrayList();
                                proivderMap.put(service, list);
                            }
                            list.add(url);
                            if (application != null && application.length() > 0) {
                                Set serviceApplications = providerServiceApplications.get(service);
                                if (serviceApplications == null) {
                                    providerServiceApplications.put(service, new ConcurrentHashSet());
                                    serviceApplications = providerServiceApplications.get(service);
                                }
                                serviceApplications.add(application);
        
                                Set applicationServices = providerApplicationServices.get(application);
                                if (applicationServices == null) {
                                    providerApplicationServices.put(application, new ConcurrentHashSet());
                                    applicationServices = providerApplicationServices.get(application);
                                }
                                applicationServices.add(service);
                            }
                        }
                    } else if (Constants.CONSUMERS_CATEGORY.equals(category)) {
                        if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                            serviceConsumers.remove(service);
                        } else {
                            List list = consumerMap.get(service);
                            if (list == null) {
                                list = new ArrayList();
                                consumerMap.put(service, list);
                            }
                            list.add(url);
                            if (application != null && application.length() > 0) {
                                Set serviceApplications = consumerServiceApplications.get(service);
                                if (serviceApplications == null) {
                                    consumerServiceApplications.put(service, new ConcurrentHashSet());
                                    serviceApplications = consumerServiceApplications.get(service);
                                }
                                serviceApplications.add(application);
        
                                Set applicationServices = consumerApplicationServices.get(application);
                                if (applicationServices == null) {
                                    consumerApplicationServices.put(application, new ConcurrentHashSet());
                                    applicationServices = consumerApplicationServices.get(application);
                                }
                                applicationServices.add(service);
                            }
                            
                        }
                    }
                }
                if (proivderMap != null && proivderMap.size() > 0) {
                    serviceProviders.putAll(proivderMap);
                }
                if (consumerMap != null && consumerMap.size() > 0) {
                    serviceConsumers.putAll(consumerMap);
                }
            }
        });
    }

    public void stop() {
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy