001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.context;
018
019import java.util.concurrent.atomic.AtomicLong;
020
021import org.springframework.context.ApplicationContext;
022import org.springframework.context.ApplicationContextInitializer;
023import org.springframework.context.ConfigurableApplicationContext;
024import org.springframework.core.Ordered;
025import org.springframework.core.env.ConfigurableEnvironment;
026import org.springframework.util.StringUtils;
027
028/**
029 * {@link ApplicationContextInitializer} that sets the Spring
030 * {@link ApplicationContext#getId() ApplicationContext ID}. The
031 * {@code spring.application.name} property is used to create the ID. If the property is
032 * not set {@code application} is used.
033 *
034 * @author Dave Syer
035 * @author Andy Wilkinson
036 */
037public class ContextIdApplicationContextInitializer implements
038                ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
039
040        private int order = Ordered.LOWEST_PRECEDENCE - 10;
041
042        public void setOrder(int order) {
043                this.order = order;
044        }
045
046        @Override
047        public int getOrder() {
048                return this.order;
049        }
050
051        @Override
052        public void initialize(ConfigurableApplicationContext applicationContext) {
053                ContextId contextId = getContextId(applicationContext);
054                applicationContext.setId(contextId.getId());
055                applicationContext.getBeanFactory().registerSingleton(ContextId.class.getName(),
056                                contextId);
057        }
058
059        private ContextId getContextId(ConfigurableApplicationContext applicationContext) {
060                ApplicationContext parent = applicationContext.getParent();
061                if (parent != null && parent.containsBean(ContextId.class.getName())) {
062                        return parent.getBean(ContextId.class).createChildId();
063                }
064                return new ContextId(getApplicationId(applicationContext.getEnvironment()));
065        }
066
067        private String getApplicationId(ConfigurableEnvironment environment) {
068                String name = environment.getProperty("spring.application.name");
069                return StringUtils.hasText(name) ? name : "application";
070        }
071
072        /**
073         * The ID of a context.
074         */
075        class ContextId {
076
077                private final AtomicLong children = new AtomicLong(0);
078
079                private final String id;
080
081                ContextId(String id) {
082                        this.id = id;
083                }
084
085                ContextId createChildId() {
086                        return new ContextId(this.id + "-" + this.children.incrementAndGet());
087                }
088
089                String getId() {
090                        return this.id;
091                }
092
093        }
094
095}