Search in sources :

Example 1 with MetadataParamsFilter

use of org.apache.dubbo.metadata.MetadataParamsFilter in project dubbo by alibaba.

the class ServiceInstanceMetadataCustomizer method customize.

@Override
public void customize(ServiceInstance serviceInstance) {
    ExtensionLoader<MetadataParamsFilter> loader = ExtensionLoader.getExtensionLoader(MetadataParamsFilter.class);
    Set<MetadataParamsFilter> paramsFilters = loader.getSupportedExtensionInstances();
    WritableMetadataService localMetadataService = WritableMetadataService.getDefaultExtension();
    // pick the first interface metadata available.
    // FIXME, check the same key in different urls has the same value
    MetadataInfo metadataInfo = localMetadataService.getMetadataInfos().values().iterator().next();
    MetadataInfo.ServiceInfo serviceInfo = metadataInfo.getServices().values().iterator().next();
    Map<String, String> allParams = new HashMap<>(serviceInfo.getUrl().getParameters());
    // load instance params users want to load.
    // TODO, duplicate logic with that in ApplicationConfig
    Set<InfraAdapter> adapters = ExtensionLoader.getExtensionLoader(InfraAdapter.class).getSupportedExtensionInstances();
    if (CollectionUtils.isNotEmpty(adapters)) {
        Map<String, String> inputParameters = new HashMap<>();
        inputParameters.put(APPLICATION_KEY, ApplicationModel.getName());
        for (InfraAdapter adapter : adapters) {
            Map<String, String> extraParameters = adapter.getExtraAttributes(inputParameters);
            if (CollectionUtils.isNotEmptyMap(extraParameters)) {
                extraParameters.forEach(allParams::putIfAbsent);
            }
        }
    }
    if (CollectionUtils.isEmpty(paramsFilters)) {
        serviceInstance.getMetadata().putAll(allParams);
        return;
    }
    paramsFilters.forEach(filter -> {
        String[] included = filter.instanceParamsIncluded();
        if (included == null) {
            serviceInstance.getMetadata().putAll(allParams);
        } else {
            for (String p : included) {
                if (allParams.get(p) != null) {
                    serviceInstance.getMetadata().put(p, allParams.get(p));
                }
            }
        }
    });
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) MetadataParamsFilter(org.apache.dubbo.metadata.MetadataParamsFilter) HashMap(java.util.HashMap) WritableMetadataService(org.apache.dubbo.metadata.WritableMetadataService) InfraAdapter(org.apache.dubbo.common.infra.InfraAdapter)

Aggregations

HashMap (java.util.HashMap)1 InfraAdapter (org.apache.dubbo.common.infra.InfraAdapter)1 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)1 MetadataParamsFilter (org.apache.dubbo.metadata.MetadataParamsFilter)1 WritableMetadataService (org.apache.dubbo.metadata.WritableMetadataService)1