001/*
002 * Copyright 2012-2017 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.config;
018
019import org.springframework.boot.ansi.AnsiOutput;
020import org.springframework.boot.ansi.AnsiOutput.Enabled;
021import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
022import org.springframework.boot.context.properties.bind.Binder;
023import org.springframework.context.ApplicationListener;
024import org.springframework.core.Ordered;
025import org.springframework.core.env.ConfigurableEnvironment;
026
027/**
028 * An {@link ApplicationListener} that configures {@link AnsiOutput} depending on the
029 * value of the property {@code spring.output.ansi.enabled}. See {@link Enabled} for valid
030 * values.
031 *
032 * @author Raphael von der GrĂ¼n
033 * @author Madhura Bhave
034 * @since 1.2.0
035 */
036public class AnsiOutputApplicationListener
037                implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
038
039        @Override
040        public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
041                ConfigurableEnvironment environment = event.getEnvironment();
042                Binder.get(environment)
043                                .bind("spring.output.ansi.enabled", AnsiOutput.Enabled.class)
044                                .ifBound(AnsiOutput::setEnabled);
045                AnsiOutput.setConsoleAvailable(environment
046                                .getProperty("spring.output.ansi.console-available", Boolean.class));
047        }
048
049        @Override
050        public int getOrder() {
051                // Apply after ConfigFileApplicationListener has called EnvironmentPostProcessors
052                return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
053        }
054
055}