Search in sources :

Example 1 with InfraAdapter

use of org.apache.dubbo.common.infra.InfraAdapter in project dubbo by alibaba.

the class ApplicationConfig method appendEnvironmentProperties.

private void appendEnvironmentProperties() {
    if (parameters == null) {
        parameters = new HashMap<>();
    }
    Set<InfraAdapter> adapters = ExtensionLoader.getExtensionLoader(InfraAdapter.class).getSupportedExtensionInstances();
    if (CollectionUtils.isNotEmpty(adapters)) {
        Map<String, String> inputParameters = new HashMap<>();
        inputParameters.put(APPLICATION_KEY, getName());
        inputParameters.put(HOST_KEY, getHostname());
        for (InfraAdapter adapter : adapters) {
            Map<String, String> extraParameters = adapter.getExtraAttributes(inputParameters);
            if (CollectionUtils.isNotEmptyMap(extraParameters)) {
                extraParameters.forEach((key, value) -> {
                    String prefix = this.getPrefix() + ".";
                    if (key.startsWith(prefix)) {
                        key = key.substring(prefix.length());
                    }
                    parameters.put(key, value);
                });
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) InfraAdapter(org.apache.dubbo.common.infra.InfraAdapter)

Example 2 with InfraAdapter

use of org.apache.dubbo.common.infra.InfraAdapter 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)2 InfraAdapter (org.apache.dubbo.common.infra.InfraAdapter)2 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)1 MetadataParamsFilter (org.apache.dubbo.metadata.MetadataParamsFilter)1 WritableMetadataService (org.apache.dubbo.metadata.WritableMetadataService)1