001/*
002 * Copyright 2012-2016 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.devtools.restart;
018
019import org.springframework.boot.context.event.ApplicationFailedEvent;
020import org.springframework.boot.context.event.ApplicationPreparedEvent;
021import org.springframework.boot.context.event.ApplicationReadyEvent;
022import org.springframework.boot.context.event.ApplicationStartingEvent;
023import org.springframework.context.ApplicationEvent;
024import org.springframework.context.ApplicationListener;
025import org.springframework.core.Ordered;
026
027/**
028 * {@link ApplicationListener} to initialize the {@link Restarter}.
029 *
030 * @author Phillip Webb
031 * @author Andy Wilkinson
032 * @since 1.3.0
033 * @see Restarter
034 */
035public class RestartApplicationListener
036                implements ApplicationListener<ApplicationEvent>, Ordered {
037
038        private int order = HIGHEST_PRECEDENCE;
039
040        private static final String ENABLED_PROPERTY = "spring.devtools.restart.enabled";
041
042        @Override
043        public void onApplicationEvent(ApplicationEvent event) {
044                if (event instanceof ApplicationStartingEvent) {
045                        onApplicationStartingEvent((ApplicationStartingEvent) event);
046                }
047                if (event instanceof ApplicationPreparedEvent) {
048                        onApplicationPreparedEvent((ApplicationPreparedEvent) event);
049                }
050                if (event instanceof ApplicationReadyEvent
051                                || event instanceof ApplicationFailedEvent) {
052                        Restarter.getInstance().finish();
053                }
054                if (event instanceof ApplicationFailedEvent) {
055                        onApplicationFailedEvent((ApplicationFailedEvent) event);
056                }
057        }
058
059        private void onApplicationStartingEvent(ApplicationStartingEvent event) {
060                // It's too early to use the Spring environment but we should still allow
061                // users to disable restart using a System property.
062                String enabled = System.getProperty(ENABLED_PROPERTY);
063                if (enabled == null || Boolean.parseBoolean(enabled)) {
064                        String[] args = event.getArgs();
065                        DefaultRestartInitializer initializer = new DefaultRestartInitializer();
066                        boolean restartOnInitialize = !AgentReloader.isActive();
067                        Restarter.initialize(args, false, initializer, restartOnInitialize);
068                }
069                else {
070                        Restarter.disable();
071                }
072        }
073
074        private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
075                Restarter.getInstance().prepare(event.getApplicationContext());
076        }
077
078        private void onApplicationFailedEvent(ApplicationFailedEvent event) {
079                Restarter.getInstance().remove(event.getApplicationContext());
080        }
081
082        @Override
083        public int getOrder() {
084                return this.order;
085        }
086
087        /**
088         * Set the order of the listener.
089         * @param order the order of the listener
090         */
091        public void setOrder(int order) {
092                this.order = order;
093        }
094
095}