@Bean public ContainerAttributes containerAttributes() { final ContainerAttributes containerAttributes = new ContainerAttributes(); setConfiguredContainerAttributes(containerAttributes); final String containerIp = environment.getProperty(CONTAINER_ATTRIBUTES_PREFIX + ContainerAttributes.IP_ADDRESS_KEY); final String containerHostname = environment.getProperty(CONTAINER_ATTRIBUTES_PREFIX + ContainerAttributes.HOST_KEY); containerAttributes.setIp( StringUtils.hasText(containerIp) ? containerIp : RuntimeUtils.getIpAddress()); containerAttributes.setHost( StringUtils.hasText(containerHostname) ? containerHostname : RuntimeUtils.getHost()); containerAttributes.setPid(RuntimeUtils.getPid()); return containerAttributes; }
/** @param containerAttributes */ private void setConfiguredContainerAttributes(ContainerAttributes containerAttributes) { Map<String, String> attributes = new HashMap<String, String>(); for (PropertySource<?> propertySource : environment.getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) propertySource; for (String key : ps.getPropertyNames()) { if (key.startsWith(CONTAINER_ATTRIBUTES_PREFIX)) { String attributeKey = key.replaceAll(CONTAINER_ATTRIBUTES_PREFIX, ""); attributes.put(attributeKey, environment.getProperty(key)); } } } } containerAttributes.putAll(attributes); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ContainerAttributes containerAttributes = applicationContext.getParent().getBean(ContainerAttributes.class); applicationContext.setId(containerAttributes.getId()); }