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.web.context; 018 019import org.springframework.boot.web.server.WebServer; 020import org.springframework.context.ApplicationEvent; 021 022/** 023 * Event to be published after the application context is refreshed and the 024 * {@link WebServer} is ready. Useful for obtaining the local port of a running server. 025 * 026 * @author Brian Clozel 027 * @author Stephane Nicoll 028 * @since 2.0.0 029 */ 030@SuppressWarnings("serial") 031public abstract class WebServerInitializedEvent extends ApplicationEvent { 032 033 protected WebServerInitializedEvent(WebServer webServer) { 034 super(webServer); 035 } 036 037 /** 038 * Access the {@link WebServer}. 039 * @return the embedded web server 040 */ 041 public WebServer getWebServer() { 042 return getSource(); 043 } 044 045 /** 046 * Access the application context that the server was created in. Sometimes it is 047 * prudent to check that this matches expectations (like being equal to the current 048 * context) before acting on the server itself. 049 * @return the applicationContext that the server was created from 050 */ 051 public abstract WebServerApplicationContext getApplicationContext(); 052 053 /** 054 * Access the source of the event (an {@link WebServer}). 055 * @return the embedded web server 056 */ 057 @Override 058 public WebServer getSource() { 059 return (WebServer) super.getSource(); 060 } 061 062}