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.devtools.restart;
018
019import java.util.Collections;
020import java.util.LinkedHashSet;
021import java.util.Set;
022
023import org.springframework.util.ClassUtils;
024
025/**
026 * Utility to determine if an Java agent based reloader (e.g. JRebel) is being used.
027 *
028 * @author Phillip Webb
029 * @since 1.3.0
030 */
031public abstract class AgentReloader {
032
033        private static final Set<String> AGENT_CLASSES;
034
035        static {
036                Set<String> agentClasses = new LinkedHashSet<>();
037                agentClasses.add("org.zeroturnaround.javarebel.Integration");
038                agentClasses.add("org.zeroturnaround.javarebel.ReloaderFactory");
039                agentClasses.add("org.hotswap.agent.HotswapAgent");
040                AGENT_CLASSES = Collections.unmodifiableSet(agentClasses);
041        }
042
043        private AgentReloader() {
044        }
045
046        /**
047         * Determine if any agent reloader is active.
048         * @return true if agent reloading is active
049         */
050        public static boolean isActive() {
051                return isActive(null) || isActive(AgentReloader.class.getClassLoader())
052                                || isActive(ClassLoader.getSystemClassLoader());
053        }
054
055        private static boolean isActive(ClassLoader classLoader) {
056                for (String agentClass : AGENT_CLASSES) {
057                        if (ClassUtils.isPresent(agentClass, classLoader)) {
058                                return true;
059                        }
060                }
061                return false;
062        }
063
064}