Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #4421 - Refactor initializeXXX methods to be protected to DefaultWebApplication implementation #4430

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 46 additions & 25 deletions core/api/src/main/java/cloud/piranha/core/api/WebApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@
*/
public interface WebApplication extends ServletContext {

/**
* Status enum.
*/
enum Status {

/**
* Web application is in setup mode.
*/
SETUP,

/**
* Web application has processed system declared constructs (e.g.
* web.xml / web-fragment.xml and Servlet annotations).
*/
DECLARED,

/**
* Web application is initialized.
*/
INITIALIZED,

/**
* Web application is servicing.
*/
SERVICING,

/**
* Web application is in error state.
*/
ERROR
}

/**
* Add a mapping for the given filter.
*
Expand Down Expand Up @@ -196,6 +228,13 @@ default Set<String> addFilterMapping(String filterName, boolean isMatchAfter, St
default String getServletContextId() {
return getVirtualServerName() + " " + getContextPath();
}

/**
* Get the status.
*
* @return the status.
*/
Status getStatus();

/**
* Initialize the web application.
Expand All @@ -204,31 +243,6 @@ default String getServletContextId() {
*/
WebApplication initialize();

/**
* Marks the end of initializing declared (web.xml, annotations) artifacts
*/
void initializeDeclaredFinish();

/**
* Initialize the filters.
*/
void initializeFilters();

/**
* Finish the initialization.
*/
void initializeFinish();

/**
* Initialize the servlet container initializers.
*/
void initializeInitializers();

/**
* Initialize the servlets.
*/
void initializeServlets();

/**
* Is the application distributable.
*
Expand Down Expand Up @@ -348,6 +362,13 @@ void service(ServletRequest request, ServletResponse response)
*/
void setServletContextName(String servletContextName);

/**
* Set the status.
*
* @param status the status.
*/
void setStatus(Status status);

/**
* Set the virtual server name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import cloud.piranha.core.api.ServletEnvironment;
import static cloud.piranha.core.api.ServletEnvironment.UNAVAILABLE;
import cloud.piranha.core.api.WebApplication;
import static cloud.piranha.core.api.WebApplication.Status.DECLARED;
import static cloud.piranha.core.api.WebApplication.Status.ERROR;
import static cloud.piranha.core.api.WebApplication.Status.INITIALIZED;
import static cloud.piranha.core.api.WebApplication.Status.SERVICING;
import static cloud.piranha.core.api.WebApplication.Status.SETUP;
import cloud.piranha.core.api.WebApplicationRequestMapper;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
Expand Down Expand Up @@ -114,32 +119,6 @@
*/
public class DefaultWebApplication implements WebApplication {

/**
* Stores the SETUP constant.
*/
protected static final int SETUP = 0;

/**
* Stores the INITIALIZED_DECLARED constant. This signals that web.xml,
* web-fragment.xml and annotations have been processed.
*/
protected static final int INITIALIZED_DECLARED = 4;

/**
* Stores the INITIALIZED constant.
*/
protected static final int INITIALIZED = 1;

/**
* Stores the SERVICING constant.
*/
protected static final int SERVICING = 2;

/**
* Stores the ERROR constant.
*/
protected static final int ERROR = 3;

/**
* Stores the 'Illegal to add listener because state is not SETUP' message.
*/
Expand Down Expand Up @@ -191,6 +170,11 @@ public class DefaultWebApplication implements WebApplication {
*/
protected String servletContextName;

/**
* Stores the status.
*/
protected Status status;

/**
* Stores the virtual server name.
*/
Expand All @@ -201,11 +185,6 @@ public class DefaultWebApplication implements WebApplication {
*/
protected String responseCharacterEncoding;

/**
* Stores the status.
*/
protected int status;

/**
* Stores the active requests and the associated responses.
*/
Expand Down Expand Up @@ -334,7 +313,7 @@ public DefaultWebApplication() {
mimeTypes.put("htm", "text/html");
mimeTypes.put("text", "text/plain");
mimeTypes.put("txt", "text/plain");

status = SETUP;
}

@Override
Expand Down Expand Up @@ -409,7 +388,7 @@ public void addInitializer(ServletContainerInitializer servletContainerInitializ

@Override
public ServletRegistration.Dynamic addJspFile(String servletName, String jspFile) {
if (status != SETUP && status != INITIALIZED_DECLARED) {
if (status != SETUP && status != DECLARED) {
throw new IllegalStateException("Illegal to add JSP file because state is not SETUP");
}
if (isEmpty(servletName)) {
Expand All @@ -423,7 +402,7 @@ public ServletRegistration.Dynamic addJspFile(String servletName, String jspFile
public void addListener(String className) {
checkTainted();

if (status != SETUP && status != INITIALIZED_DECLARED) {
if (status != SETUP && status != DECLARED) {
throw new IllegalStateException(ILLEGAL_TO_ADD_LISTENER);
}

Expand All @@ -437,7 +416,7 @@ public void addListener(String className) {
@Override
public void addListener(Class<? extends EventListener> type) {
checkTainted();
if (status != SETUP && status != INITIALIZED_DECLARED) {
if (status != SETUP && status != DECLARED) {
throw new IllegalStateException(ILLEGAL_TO_ADD_LISTENER);
}
try {
Expand All @@ -450,15 +429,15 @@ public void addListener(Class<? extends EventListener> type) {
@Override
public <T extends EventListener> void addListener(T listener) {
checkTainted();
if (status != SETUP && status != INITIALIZED_DECLARED) {
if (status != SETUP && status != DECLARED) {
throw new IllegalStateException(ILLEGAL_TO_ADD_LISTENER);
}
if (listener instanceof ServletContextListener servletContextListener) {
if (source != null && !(source instanceof ServletContainerInitializer)) {
throw new IllegalArgumentException("Illegal to add ServletContextListener because this context was not passed to a ServletContainerInitializer");
}

if (status == INITIALIZED_DECLARED) {
if (status == DECLARED) {
contextListeners.add(servletContextListener);
} else {
declaredContextListeners.add(servletContextListener);
Expand Down Expand Up @@ -948,6 +927,11 @@ public int getSessionTimeout() {
return getManager().getHttpSessionManager().getSessionTimeout();
}

@Override
public Status getStatus() {
return status;
}

@Override
public String getVirtualServerName() {
return virtualServerName;
Expand All @@ -970,20 +954,11 @@ public WebApplication initialize() {
return this;
}

@Override
public void initializeDeclaredFinish() {
if (status == SETUP) {
status = INITIALIZED_DECLARED;
LOGGER.log(DEBUG, "Initialized declared items for web application at {0}", contextPath);
}
if (status == ERROR) {
LOGGER.log(WARNING, "An error occurred initializing webapplication at {0}", contextPath);
}
}

@Override
public void initializeFilters() {
if (status == SETUP || status == INITIALIZED_DECLARED) {
/**
* Initialize the filters.
*/
protected void initializeFilters() {
if (status == SETUP || status == DECLARED) {
List<String> filterNames = new ArrayList<>(filters.keySet());
filterNames.stream().map(filters::get).forEach(environment -> {
try {
Expand All @@ -997,9 +972,11 @@ public void initializeFilters() {
}
}

@Override
public void initializeFinish() {
if (status == SETUP || status == INITIALIZED_DECLARED) {
/**
* Finish the initialization.
*/
protected void initializeFinish() {
if (status == SETUP || status == DECLARED) {
status = INITIALIZED;
LOGGER.log(DEBUG, "Initialized web application at {0}", contextPath);
}
Expand All @@ -1008,8 +985,10 @@ public void initializeFinish() {
}
}

@Override
public void initializeInitializers() {
/**
* Initialize the servlet container initializers.
*/
protected void initializeInitializers() {
boolean error = false;
for (ServletContainerInitializer initializer : initializers) {
try {
Expand Down Expand Up @@ -1091,7 +1070,7 @@ public void initializeInitializers() {
}

@SuppressWarnings("unchecked")
private void initializeServlet(DefaultServletEnvironment environment) {
protected void initializeServlet(DefaultServletEnvironment environment) {
try {
LOGGER.log(DEBUG, "Initializing servlet: {0}", environment.servletName);
if (environment.getServlet() == null) {
Expand Down Expand Up @@ -1129,9 +1108,11 @@ private void initializeServlet(DefaultServletEnvironment environment) {
}
}

@Override
public void initializeServlets() {
if (status == SETUP || status == INITIALIZED_DECLARED) {
/**
* Initialize the servlets.
*/
protected void initializeServlets() {
if (status == SETUP || status == DECLARED) {
List<String> servletsToBeRemoved = new ArrayList<>();
List<String> servletNames = new ArrayList<>(servletEnvironments.keySet());

Expand Down Expand Up @@ -1168,7 +1149,8 @@ private boolean isEmpty(String string) {

@Override
public boolean isInitialized() {
return status >= INITIALIZED && status < ERROR;
return status.ordinal() >= INITIALIZED.ordinal()
&& status.ordinal() < ERROR.ordinal();
}

@Override
Expand Down Expand Up @@ -1307,7 +1289,7 @@ public void setEffectiveMinorVersion(int effectiveMinorVersion) {
public boolean setInitParameter(String name, String value) {
requireNonNull(name);
checkTainted();
if (status != SETUP && status != INITIALIZED_DECLARED) {
if (status != SETUP && status != DECLARED) {
throw new IllegalStateException("Cannot set init parameter once web application is initialized");
}
boolean result = true;
Expand Down Expand Up @@ -1342,7 +1324,7 @@ public void setResponseCharacterEncoding(String responseCharacterEncoding) {
@Override
public void setSessionTimeout(int sessionTimeout) {
checkTainted();
if (status != SETUP && status != INITIALIZED_DECLARED) {
if (status != SETUP && status != DECLARED) {
throw new IllegalStateException("Illegal to set session timeout because state is not SETUP");
}
getManager().getHttpSessionManager().setSessionTimeout(sessionTimeout);
Expand All @@ -1359,6 +1341,11 @@ public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingMode
checkServicing();
getManager().getHttpSessionManager().setSessionTrackingModes(sessionTrackingModes);
}

@Override
public void setStatus(Status status) {
this.status = status;
}

@Override
public void setVirtualServerName(String virtualServerName) {
Expand Down Expand Up @@ -1413,7 +1400,7 @@ protected void verifyRequestResponseTypes(ServletRequest request, ServletRespons
* @param desiredStatus the desired status.
* @param message the message.
*/
protected void verifyState(int desiredStatus, String message) {
protected void verifyState(Status desiredStatus, String message) {
if (status != desiredStatus) {
throw new RuntimeException(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1431,16 +1431,6 @@ void testInitialize() {
assertTrue(webApplication.isInitialized());
}

/**
* Test initializeDeclaredFinish method.
*/
@Test
void testInitializeDeclaredFinish() {
DefaultWebApplication webApplication = new DefaultWebApplication();
webApplication.initializeDeclaredFinish();
assertFalse(webApplication.isInitialized());
}

/**
* Test initializeFilters method.
*/
Expand Down
Loading
Loading