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; 018 019import org.springframework.util.ClassUtils; 020 021/** 022 * An enumeration of possible types of web application. 023 * 024 * @author Andy Wilkinson 025 * @author Brian Clozel 026 * @since 2.0.0 027 */ 028public enum WebApplicationType { 029 030 /** 031 * The application should not run as a web application and should not start an 032 * embedded web server. 033 */ 034 NONE, 035 036 /** 037 * The application should run as a servlet-based web application and should start an 038 * embedded servlet web server. 039 */ 040 SERVLET, 041 042 /** 043 * The application should run as a reactive web application and should start an 044 * embedded reactive web server. 045 */ 046 REACTIVE; 047 048 private static final String[] SERVLET_INDICATOR_CLASSES = { "javax.servlet.Servlet", 049 "org.springframework.web.context.ConfigurableWebApplicationContext" }; 050 051 private static final String WEBMVC_INDICATOR_CLASS = "org.springframework." 052 + "web.servlet.DispatcherServlet"; 053 054 private static final String WEBFLUX_INDICATOR_CLASS = "org." 055 + "springframework.web.reactive.DispatcherHandler"; 056 057 private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer"; 058 059 private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext"; 060 061 private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext"; 062 063 static WebApplicationType deduceFromClasspath() { 064 if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) 065 && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null) 066 && !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) { 067 return WebApplicationType.REACTIVE; 068 } 069 for (String className : SERVLET_INDICATOR_CLASSES) { 070 if (!ClassUtils.isPresent(className, null)) { 071 return WebApplicationType.NONE; 072 } 073 } 074 return WebApplicationType.SERVLET; 075 } 076 077 static WebApplicationType deduceFromApplicationContext( 078 Class<?> applicationContextClass) { 079 if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, applicationContextClass)) { 080 return WebApplicationType.SERVLET; 081 } 082 if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, applicationContextClass)) { 083 return WebApplicationType.REACTIVE; 084 } 085 return WebApplicationType.NONE; 086 } 087 088 private static boolean isAssignable(String target, Class<?> type) { 089 try { 090 return ClassUtils.resolveClassName(target, null).isAssignableFrom(type); 091 } 092 catch (Throwable ex) { 093 return false; 094 } 095 } 096 097}